vitess.io/vitess@v0.16.2/go/vt/vtadmin/http/request.go (about) 1 /* 2 Copyright 2020 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package http 18 19 import ( 20 "fmt" 21 "net/http" 22 "strconv" 23 24 "github.com/gorilla/mux" 25 26 "vitess.io/vitess/go/vt/topo/topoproto" 27 "vitess.io/vitess/go/vt/vtadmin/errors" 28 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 ) 31 32 // Request wraps an *http.Request to provide some convenience functions for 33 // accessing request data. 34 type Request struct{ *http.Request } 35 36 // Vars returns the route variables in a request, if any, as defined by 37 // gorilla/mux. 38 func (r Request) Vars() Vars { 39 return mux.Vars(r.Request) 40 } 41 42 // ParseQueryParamAsBool attempts to parse the query parameter of the given name 43 // into a boolean value. If the parameter is not set, the provided default value 44 // is returned. 45 func (r Request) ParseQueryParamAsBool(name string, defaultVal bool) (bool, error) { 46 if param := r.URL.Query().Get(name); param != "" { 47 val, err := strconv.ParseBool(param) 48 if err != nil { 49 return defaultVal, &errors.BadRequest{ 50 Err: err, 51 ErrDetails: fmt.Sprintf("could not parse query parameter %s (= %v) into bool value", name, param), 52 } 53 } 54 55 return val, nil 56 } 57 58 return defaultVal, nil 59 } 60 61 // ParseQueryParamAsUint32 attempts to parse the query parameter of the given 62 // name into a uint32 value. If the parameter is not set, the provided default 63 // value is returned. 64 func (r Request) ParseQueryParamAsUint32(name string, defaultVal uint32) (uint32, error) { 65 if param := r.URL.Query().Get(name); param != "" { 66 val, err := strconv.ParseUint(param, 10, 32) 67 if err != nil { 68 return defaultVal, &errors.BadRequest{ 69 Err: err, 70 ErrDetails: fmt.Sprintf("could not parse query parameter %s (= %v) into uint32 value", name, param), 71 } 72 } 73 74 return uint32(val), nil 75 } 76 77 return defaultVal, nil 78 } 79 80 // Vars is a mapping of the route variable values in a given request. 81 // 82 // See (gorilla/mux).Vars for details. We define a type here to add some 83 // additional behavior for extracting non-string values. 84 type Vars map[string]string 85 86 // GetTabletAlias returns the route named `key` as a TabletAlias. 87 // 88 // It returns an error if the route has no variable with that name, or if it 89 // cannot be parsed as a TabletAlias. 90 func (v Vars) GetTabletAlias(key string) (*topodatapb.TabletAlias, error) { 91 aliasStr, ok := v[key] 92 if !ok { 93 return nil, &errors.Internal{ 94 Err: fmt.Errorf("no route variable found with name %s", key), 95 } 96 } 97 98 alias, err := topoproto.ParseTabletAlias(aliasStr) 99 if err != nil { 100 return nil, &errors.BadRequest{ 101 Err: err, 102 ErrDetails: fmt.Sprintf("could not parse route variable %s (= %v) as tablet alias", key, aliasStr), 103 } 104 } 105 106 return alias, nil 107 }