1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "github.com/gorilla/mux"
19 func guessAndParse(k, v string) (interface{}, error) {
20 // All of these form values arrive as strings, so we need some
21 // type-guessing to accept non-string inputs:
23 // Values for parameters that take ints (limit=1) or bools
24 // (include_trash=1) are parsed accordingly.
26 // "null" and "" are nil.
28 // Values that look like JSON objects, arrays, or strings are
31 // The rest are left as strings.
34 return strconv.ParseInt(v, 10, 64)
36 return stringToBool(v), nil
37 case v == "null" || v == "":
39 case strings.HasPrefix(v, "["):
41 err := json.Unmarshal([]byte(v), &j)
43 case strings.HasPrefix(v, "{"):
44 var j map[string]interface{}
45 err := json.Unmarshal([]byte(v), &j)
47 case strings.HasPrefix(v, "\""):
49 err := json.Unmarshal([]byte(v), &j)
54 // TODO: Need to accept "?foo[]=bar&foo[]=baz" as
58 // Parse req as an Arvados V1 API request and return the request
61 // If the request has a parameter whose name is attrsKey (e.g.,
62 // "collection"), it is renamed to "attrs".
63 func (rtr *router) loadRequestParams(req *http.Request, attrsKey string) (map[string]interface{}, error) {
64 err := req.ParseForm()
66 if err.Error() == "http: request body too large" {
67 return nil, httpError(http.StatusRequestEntityTooLarge, err)
69 return nil, httpError(http.StatusBadRequest, err)
72 params := map[string]interface{}{}
74 // Load parameters from req.Form, which (after
75 // req.ParseForm()) includes the query string and -- when
76 // Content-Type is application/x-www-form-urlencoded -- the
78 for k, values := range req.Form {
79 for _, v := range values {
80 params[k], err = guessAndParse(k, v)
87 // Decode body as JSON if Content-Type request header is
88 // missing or application/json.
89 mt := req.Header.Get("Content-Type")
90 if ct, _, err := mime.ParseMediaType(mt); err != nil && mt != "" {
91 return nil, fmt.Errorf("error parsing media type %q: %s", mt, err)
92 } else if (ct == "application/json" || mt == "") && req.ContentLength != 0 {
93 jsonParams := map[string]interface{}{}
94 err := json.NewDecoder(req.Body).Decode(&jsonParams)
96 return nil, httpError(http.StatusBadRequest, err)
98 for k, v := range jsonParams {
99 switch v := v.(type) {
101 // The Ruby "arv" cli tool sends a
102 // JSON-encode params map with
103 // JSON-encoded values.
104 dec, err := guessAndParse(k, v)
114 if attrsKey != "" && params[attrsKey] == nil {
115 // Copy top-level parameters from JSON request
116 // body into params[attrsKey]. Some SDKs rely
117 // on this Rails API feature; see
118 // https://api.rubyonrails.org/v5.2.1/classes/ActionController/ParamsWrapper.html
119 params[attrsKey] = jsonParams
123 for k, v := range mux.Vars(req) {
127 if v, ok := params[attrsKey]; ok && attrsKey != "" {
129 delete(params, attrsKey)
132 if order, ok := params["order"].(string); ok {
133 // We must accept strings ("foo, bar desc") and arrays
134 // (["foo", "bar desc"]) because RailsAPI does.
135 // Convert to an array here before trying to unmarshal
136 // into options structs.
138 delete(params, "order")
140 params["order"] = strings.Split(order, ",")
147 // Copy src to dst, using json as an intermediate format in order to
148 // invoke src's json-marshaling and dst's json-unmarshaling behaviors.
149 func (rtr *router) transcode(src interface{}, dst interface{}) error {
154 errw = json.NewEncoder(pw).Encode(src)
157 err := json.NewDecoder(pr).Decode(dst)
164 var intParams = map[string]bool{
169 var boolParams = map[string]bool{
171 "ensure_unique_name": true,
172 "include_trash": true,
173 "include_old_versions": true,
174 "redirect_to_new_user": true,
175 "send_notification_email": true,
176 "bypass_federation": true,
178 "exclude_home_project": true,
181 func stringToBool(s string) bool {
183 case "", "false", "0":