// BindValidRequest binds a params object to a request but only when the request is valid
// if the request is not valid an error will be returned
329925
func (c *Context) BindValidRequest(request *http.Request, route *MatchedRoute, binder RequestBinder) error {
	var res []error

	requestContentType := "*/*"
	// check and validate content type, select consumer
	if runtime.HasBody(request) {
		ct, _, err := runtime.ContentType(request.Header)
		if err != nil {
			res = append(res, err)
		} else {
			if err := validateContentType(route.Consumes, ct); err != nil {
				res = append(res, err)
			}
			if len(res) == 0 {
				cons, ok := route.Consumers[ct]
				if !ok {
					res = append(res, errors.New(500, "no consumer registered for %s", ct))
				} else {
					route.Consumer = cons
					requestContentType = ct
				}
			}
		}
	}

	// check and validate the response format
	if len(res) == 0 && runtime.HasBody(request) {
		if str := NegotiateContentType(request, route.Produces, requestContentType); str == "" {
			res = append(res, errors.InvalidResponseFormat(request.Header.Get(runtime.HeaderAccept), route.Produces))
		}
	}

	// now bind the request with the provided binder
	// it's assumed the binder will also validate the request and return an error if the
	// request is invalid
	if binder != nil && len(res) == 0 {
		if err := binder.BindRequest(request, route); err != nil {
			return err
		}
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}
883284
func PostRequest(baseURL string, object *jsh.Object) (*http.Request, error) {
	u, err := url.Parse(baseURL)
	if err != nil {
		return nil, fmt.Errorf("Error parsing URL: %s", err.Error())
	}

	setPath(u, object.Type)

	request, err := NewRequest("POST", u.String(), nil)
	if err != nil {
		return nil, fmt.Errorf("Error building POST request: %s", err.Error())
	}

	err = prepareBody(request, object)
	if err != nil {
		return nil, err
	}

	return request, nil
}
285965
func (ctx *ShowAccountContext) BadRequest(r error) error {
	if ctx.ResponseData.Header().Get("Content-Type") == "" {
		ctx.ResponseData.Header().Set("Content-Type", "application/vnd.goa.error")
	}
	return ctx.ResponseData.Service.Send(ctx.Context, 400, r)
}
43603
func ValidateRequest(r *http.Request, usertoken string) bool {
	var token string
	if strings.Contains(r.Header.Get("content-type"), "/json") {
		decoder := json.NewDecoder(r.Body)
		var i bson.M
		err := decoder.Decode(i)
		if err != nil {
			return false
		}

		token = Decrypt([]byte(FormKey), i["formtoken"].(string))

	} else {
		token = Decrypt([]byte(FormKey), r.FormValue("formtoken"))
	}
	return strings.Contains(token, usertoken) && strings.Contains(token, r.URL.Path)
}
413335
func (q *Query) Bind(params Params) *Query {
	if len(q.params) == 0 {
		q.params = params
	} else {
		for k, v := range params {
			q.params[k] = v
		}
	}
	return q
}
486326
func BadRequest(w http.ResponseWriter, r *http.Request, err *Error) {
	handlerMu.RLock()
	f, ok := handlerMap[http.StatusBadRequest]
	handlerMu.RUnlock()
	if ok {
		r = ctxSetErr(r, err)
		f.ServeHTTP(w, r)
	} else {
		defaultBadRequest(w, r, err)
	}
}
264939
func (v *Validator) ValidRequest(r *http.Request) error {
	ts := r.Header.Get(tsHeader)
	rs := r.Header.Get(sHeader)
	if ts == "" || rs == "" {
		return fmt.Errorf("Unknown host: %s", r.Host)
	}
	b, _ := ioutil.ReadAll(r.Body)
	if v.validTimestamp(ts) == false || v.validSignature(ts, r.URL.RawQuery, b, rs) == false {
		return fmt.Errorf("Unknown host: %s", r.Host)
	}
	r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
	return nil
}
1245438
func Validate(request *restful.Request, params []*restful.Parameter) (map[string][]string, error) {
	res := make(map[string][]string)

	for _, p := range params {
		var t string
		values := []string{}

		switch p.Kind() {
		case restful.QueryParameterKind:
			t = "Query"
			if ql, ok := request.Request.URL.Query()[p.Data().Name]; ok {
				for _, q := range ql {
					values = append(values, q)
				}
			}

		case restful.PathParameterKind:
			t = "Path"
			values = append(values, request.PathParameter(p.Data().Name))
		}

		if p.Data().Required && len(values) == 0 {
			return res, errors.New(t + "-Parameter '" + p.Data().Name + "' is required but missing")
		}

		for _, v := range values {
			res[p.Data().Name] = append(res[p.Data().Name], decodeParam(v))
		}
	}

	return res, nil
}
363380
func binder(r *http.Request, v Binder) error {
	rv := reflect.ValueOf(v)
	if rv.Kind() == reflect.Ptr {
		rv = rv.Elem()
	}

	// Call Binder on non-struct types right away
	if rv.Kind() != reflect.Struct {
		return v.Bind(r)
	}

	// For structs, we call Bind on each field that implements Binder
	for i := 0; i < rv.NumField(); i++ {
		f := rv.Field(i)
		if f.Type().Implements(binderType) {

			if isNil(f) {
				continue
			}

			fv := f.Interface().(Binder)
			if err := binder(r, fv); err != nil {
				return err
			}
		}
	}

	// We call it bottom-up
	if err := v.Bind(r); err != nil {
		return err
	}

	return nil
}
393390
func (request Request) Validate() error {
	if err := ValidateString(request.Holder); err != nil {
		return errors.Annotatef(err, "invalid holder")
	}
	if request.Duration <= 0 {
		return errors.Errorf("invalid duration")
	}
	return nil
}
966083
func (p *Params) Bind(dest interface{}, name string) {
	value := reflect.ValueOf(dest)
	if value.Kind() != reflect.Ptr {
		paramsLogger.Panic("Bind: revel/params: non-pointer passed to Bind: " + name)
	}
	value = value.Elem()
	if !value.CanSet() {
		paramsLogger.Panic("Bind: revel/params: non-settable variable passed to Bind: " + name)
	}

	// Remove the json from the Params, this will stop the binder from attempting
	// to use the json data to populate the destination interface. We do not want
	// to do this on a named bind directly against the param, it is ok to happen when
	// the action is invoked.
	jsonData := p.JSON
	p.JSON = nil
	value.Set(Bind(p, name, value.Type()))
	p.JSON = jsonData
}
954124
func (p *Params) BindJSON(dest interface{}) error {
	value := reflect.ValueOf(dest)
	if value.Kind() != reflect.Ptr {
		paramsLogger.Warn("BindJSON: Not a pointer")
		return errors.New("BindJSON not a pointer")
	}
	if err := json.Unmarshal(p.JSON, dest); err != nil {
		paramsLogger.Warn("BindJSON: Unable to unmarshal request:", "error", err)
		return err
	}
	return nil
}
630685
func ValidTemplateRequest(template *chronograf.Template) error {
	switch template.Type {
	default:
		return fmt.Errorf("unknown template type %s", template.Type)
	case "constant", "csv", "fieldKeys", "tagKeys", "tagValues", "measurements", "databases", "map", "influxql", "text":
	}

	for _, v := range template.Values {
		switch v.Type {
		default:
			return fmt.Errorf("unknown template variable type %s", v.Type)
		case "csv", "map", "fieldKey", "tagKey", "tagValue", "measurement", "database", "constant", "influxql":
		}

		if template.Type == "map" && v.Key == "" {
			return fmt.Errorf("templates of type 'map' require a 'key'")
		}
	}

	if template.Type == "influxql" && template.Query == nil {
		return fmt.Errorf("no query set for template of type 'influxql'")
	}

	return nil
}
786432
func (m *CreateRequest) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateAlgorithm(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateKeyID(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateUse(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}
77439
func (s *SelectQuery) Bind(params Params) *SelectQuery {
	s.params = params
	return s
}
1150357
func NewSimpleBindRequest(username string, password string, controls []Control) *SimpleBindRequest {
	return &SimpleBindRequest{
		Username: username,
		Password: password,
		Controls: controls,
	}
}
612088
func validateRequest(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	devFlag := r.URL.Query().Get("_dev")
	isDev := devFlag != ""
	if !isDev && !IsValidAlexaRequest(w, r) {
		log.Println("Request invalid")
		return
	}
	next(w, r)
}