本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
12KB

  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package middleware
  15. import (
  16. "encoding"
  17. "encoding/base64"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "reflect"
  22. "strconv"
  23. "github.com/go-openapi/errors"
  24. "github.com/go-openapi/runtime"
  25. "github.com/go-openapi/spec"
  26. "github.com/go-openapi/strfmt"
  27. "github.com/go-openapi/swag"
  28. "github.com/go-openapi/validate"
  29. )
  30. const defaultMaxMemory = 32 << 20
  31. var textUnmarshalType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
  32. func newUntypedParamBinder(param spec.Parameter, spec *spec.Swagger, formats strfmt.Registry) *untypedParamBinder {
  33. binder := new(untypedParamBinder)
  34. binder.Name = param.Name
  35. binder.parameter = &param
  36. binder.formats = formats
  37. if param.In != "body" {
  38. binder.validator = validate.NewParamValidator(&param, formats)
  39. } else {
  40. binder.validator = validate.NewSchemaValidator(param.Schema, spec, param.Name, formats)
  41. }
  42. return binder
  43. }
  44. type untypedParamBinder struct {
  45. parameter *spec.Parameter
  46. formats strfmt.Registry
  47. Name string
  48. validator validate.EntityValidator
  49. }
  50. func (p *untypedParamBinder) Type() reflect.Type {
  51. return p.typeForSchema(p.parameter.Type, p.parameter.Format, p.parameter.Items)
  52. }
  53. func (p *untypedParamBinder) typeForSchema(tpe, format string, items *spec.Items) reflect.Type {
  54. switch tpe {
  55. case "boolean":
  56. return reflect.TypeOf(true)
  57. case "string":
  58. if tt, ok := p.formats.GetType(format); ok {
  59. return tt
  60. }
  61. return reflect.TypeOf("")
  62. case "integer":
  63. switch format {
  64. case "int8":
  65. return reflect.TypeOf(int8(0))
  66. case "int16":
  67. return reflect.TypeOf(int16(0))
  68. case "int32":
  69. return reflect.TypeOf(int32(0))
  70. case "int64":
  71. return reflect.TypeOf(int64(0))
  72. default:
  73. return reflect.TypeOf(int64(0))
  74. }
  75. case "number":
  76. switch format {
  77. case "float":
  78. return reflect.TypeOf(float32(0))
  79. case "double":
  80. return reflect.TypeOf(float64(0))
  81. }
  82. case "array":
  83. if items == nil {
  84. return nil
  85. }
  86. itemsType := p.typeForSchema(items.Type, items.Format, items.Items)
  87. if itemsType == nil {
  88. return nil
  89. }
  90. return reflect.MakeSlice(reflect.SliceOf(itemsType), 0, 0).Type()
  91. case "file":
  92. return reflect.TypeOf(&runtime.File{}).Elem()
  93. case "object":
  94. return reflect.TypeOf(map[string]interface{}{})
  95. }
  96. return nil
  97. }
  98. func (p *untypedParamBinder) allowsMulti() bool {
  99. return p.parameter.In == "query" || p.parameter.In == "formData"
  100. }
  101. func (p *untypedParamBinder) readValue(values runtime.Gettable, target reflect.Value) ([]string, bool, bool, error) {
  102. name, in, cf, tpe := p.parameter.Name, p.parameter.In, p.parameter.CollectionFormat, p.parameter.Type
  103. if tpe == "array" {
  104. if cf == "multi" {
  105. if !p.allowsMulti() {
  106. return nil, false, false, errors.InvalidCollectionFormat(name, in, cf)
  107. }
  108. vv, hasKey, _ := values.GetOK(name)
  109. return vv, false, hasKey, nil
  110. }
  111. v, hk, hv := values.GetOK(name)
  112. if !hv {
  113. return nil, false, hk, nil
  114. }
  115. d, c, e := p.readFormattedSliceFieldValue(v[len(v)-1], target)
  116. return d, c, hk, e
  117. }
  118. vv, hk, _ := values.GetOK(name)
  119. return vv, false, hk, nil
  120. }
  121. func (p *untypedParamBinder) Bind(request *http.Request, routeParams RouteParams, consumer runtime.Consumer, target reflect.Value) error {
  122. // fmt.Println("binding", p.name, "as", p.Type())
  123. switch p.parameter.In {
  124. case "query":
  125. data, custom, hasKey, err := p.readValue(runtime.Values(request.URL.Query()), target)
  126. if err != nil {
  127. return err
  128. }
  129. if custom {
  130. return nil
  131. }
  132. return p.bindValue(data, hasKey, target)
  133. case "header":
  134. data, custom, hasKey, err := p.readValue(runtime.Values(request.Header), target)
  135. if err != nil {
  136. return err
  137. }
  138. if custom {
  139. return nil
  140. }
  141. return p.bindValue(data, hasKey, target)
  142. case "path":
  143. data, custom, hasKey, err := p.readValue(routeParams, target)
  144. if err != nil {
  145. return err
  146. }
  147. if custom {
  148. return nil
  149. }
  150. return p.bindValue(data, hasKey, target)
  151. case "formData":
  152. var err error
  153. var mt string
  154. mt, _, e := runtime.ContentType(request.Header)
  155. if e != nil {
  156. // because of the interface conversion go thinks the error is not nil
  157. // so we first check for nil and then set the err var if it's not nil
  158. err = e
  159. }
  160. if err != nil {
  161. return errors.InvalidContentType("", []string{"multipart/form-data", "application/x-www-form-urlencoded"})
  162. }
  163. if mt != "multipart/form-data" && mt != "application/x-www-form-urlencoded" {
  164. return errors.InvalidContentType(mt, []string{"multipart/form-data", "application/x-www-form-urlencoded"})
  165. }
  166. if mt == "multipart/form-data" {
  167. if err = request.ParseMultipartForm(defaultMaxMemory); err != nil {
  168. return errors.NewParseError(p.Name, p.parameter.In, "", err)
  169. }
  170. }
  171. if err = request.ParseForm(); err != nil {
  172. return errors.NewParseError(p.Name, p.parameter.In, "", err)
  173. }
  174. if p.parameter.Type == "file" {
  175. file, header, ffErr := request.FormFile(p.parameter.Name)
  176. if ffErr != nil {
  177. return errors.NewParseError(p.Name, p.parameter.In, "", ffErr)
  178. }
  179. target.Set(reflect.ValueOf(runtime.File{Data: file, Header: header}))
  180. return nil
  181. }
  182. if request.MultipartForm != nil {
  183. data, custom, hasKey, rvErr := p.readValue(runtime.Values(request.MultipartForm.Value), target)
  184. if rvErr != nil {
  185. return rvErr
  186. }
  187. if custom {
  188. return nil
  189. }
  190. return p.bindValue(data, hasKey, target)
  191. }
  192. data, custom, hasKey, err := p.readValue(runtime.Values(request.PostForm), target)
  193. if err != nil {
  194. return err
  195. }
  196. if custom {
  197. return nil
  198. }
  199. return p.bindValue(data, hasKey, target)
  200. case "body":
  201. newValue := reflect.New(target.Type())
  202. if !runtime.HasBody(request) {
  203. if p.parameter.Default != nil {
  204. target.Set(reflect.ValueOf(p.parameter.Default))
  205. }
  206. return nil
  207. }
  208. if err := consumer.Consume(request.Body, newValue.Interface()); err != nil {
  209. if err == io.EOF && p.parameter.Default != nil {
  210. target.Set(reflect.ValueOf(p.parameter.Default))
  211. return nil
  212. }
  213. tpe := p.parameter.Type
  214. if p.parameter.Format != "" {
  215. tpe = p.parameter.Format
  216. }
  217. return errors.InvalidType(p.Name, p.parameter.In, tpe, nil)
  218. }
  219. target.Set(reflect.Indirect(newValue))
  220. return nil
  221. default:
  222. return errors.New(500, fmt.Sprintf("invalid parameter location %q", p.parameter.In))
  223. }
  224. }
  225. func (p *untypedParamBinder) bindValue(data []string, hasKey bool, target reflect.Value) error {
  226. if p.parameter.Type == "array" {
  227. return p.setSliceFieldValue(target, p.parameter.Default, data, hasKey)
  228. }
  229. var d string
  230. if len(data) > 0 {
  231. d = data[len(data)-1]
  232. }
  233. return p.setFieldValue(target, p.parameter.Default, d, hasKey)
  234. }
  235. func (p *untypedParamBinder) setFieldValue(target reflect.Value, defaultValue interface{}, data string, hasKey bool) error {
  236. tpe := p.parameter.Type
  237. if p.parameter.Format != "" {
  238. tpe = p.parameter.Format
  239. }
  240. if (!hasKey || (!p.parameter.AllowEmptyValue && data == "")) && p.parameter.Required && p.parameter.Default == nil {
  241. return errors.Required(p.Name, p.parameter.In)
  242. }
  243. ok, err := p.tryUnmarshaler(target, defaultValue, data)
  244. if err != nil {
  245. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  246. }
  247. if ok {
  248. return nil
  249. }
  250. defVal := reflect.Zero(target.Type())
  251. if defaultValue != nil {
  252. defVal = reflect.ValueOf(defaultValue)
  253. }
  254. if tpe == "byte" {
  255. if data == "" {
  256. if target.CanSet() {
  257. target.SetBytes(defVal.Bytes())
  258. }
  259. return nil
  260. }
  261. b, err := base64.StdEncoding.DecodeString(data)
  262. if err != nil {
  263. b, err = base64.URLEncoding.DecodeString(data)
  264. if err != nil {
  265. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  266. }
  267. }
  268. if target.CanSet() {
  269. target.SetBytes(b)
  270. }
  271. return nil
  272. }
  273. switch target.Kind() {
  274. case reflect.Bool:
  275. if data == "" {
  276. if target.CanSet() {
  277. target.SetBool(defVal.Bool())
  278. }
  279. return nil
  280. }
  281. b, err := swag.ConvertBool(data)
  282. if err != nil {
  283. return err
  284. }
  285. if target.CanSet() {
  286. target.SetBool(b)
  287. }
  288. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  289. if data == "" {
  290. if target.CanSet() {
  291. rd := defVal.Convert(reflect.TypeOf(int64(0)))
  292. target.SetInt(rd.Int())
  293. }
  294. return nil
  295. }
  296. i, err := strconv.ParseInt(data, 10, 64)
  297. if err != nil {
  298. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  299. }
  300. if target.OverflowInt(i) {
  301. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  302. }
  303. if target.CanSet() {
  304. target.SetInt(i)
  305. }
  306. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  307. if data == "" {
  308. if target.CanSet() {
  309. rd := defVal.Convert(reflect.TypeOf(uint64(0)))
  310. target.SetUint(rd.Uint())
  311. }
  312. return nil
  313. }
  314. u, err := strconv.ParseUint(data, 10, 64)
  315. if err != nil {
  316. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  317. }
  318. if target.OverflowUint(u) {
  319. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  320. }
  321. if target.CanSet() {
  322. target.SetUint(u)
  323. }
  324. case reflect.Float32, reflect.Float64:
  325. if data == "" {
  326. if target.CanSet() {
  327. rd := defVal.Convert(reflect.TypeOf(float64(0)))
  328. target.SetFloat(rd.Float())
  329. }
  330. return nil
  331. }
  332. f, err := strconv.ParseFloat(data, 64)
  333. if err != nil {
  334. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  335. }
  336. if target.OverflowFloat(f) {
  337. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  338. }
  339. if target.CanSet() {
  340. target.SetFloat(f)
  341. }
  342. case reflect.String:
  343. value := data
  344. if value == "" {
  345. value = defVal.String()
  346. }
  347. // validate string
  348. if target.CanSet() {
  349. target.SetString(value)
  350. }
  351. case reflect.Ptr:
  352. if data == "" && defVal.Kind() == reflect.Ptr {
  353. if target.CanSet() {
  354. target.Set(defVal)
  355. }
  356. return nil
  357. }
  358. newVal := reflect.New(target.Type().Elem())
  359. if err := p.setFieldValue(reflect.Indirect(newVal), defVal, data, hasKey); err != nil {
  360. return err
  361. }
  362. if target.CanSet() {
  363. target.Set(newVal)
  364. }
  365. default:
  366. return errors.InvalidType(p.Name, p.parameter.In, tpe, data)
  367. }
  368. return nil
  369. }
  370. func (p *untypedParamBinder) tryUnmarshaler(target reflect.Value, defaultValue interface{}, data string) (bool, error) {
  371. if !target.CanSet() {
  372. return false, nil
  373. }
  374. // When a type implements encoding.TextUnmarshaler we'll use that instead of reflecting some more
  375. if reflect.PtrTo(target.Type()).Implements(textUnmarshalType) {
  376. if defaultValue != nil && len(data) == 0 {
  377. target.Set(reflect.ValueOf(defaultValue))
  378. return true, nil
  379. }
  380. value := reflect.New(target.Type())
  381. if err := value.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(data)); err != nil {
  382. return true, err
  383. }
  384. target.Set(reflect.Indirect(value))
  385. return true, nil
  386. }
  387. return false, nil
  388. }
  389. func (p *untypedParamBinder) readFormattedSliceFieldValue(data string, target reflect.Value) ([]string, bool, error) {
  390. ok, err := p.tryUnmarshaler(target, p.parameter.Default, data)
  391. if err != nil {
  392. return nil, true, err
  393. }
  394. if ok {
  395. return nil, true, nil
  396. }
  397. return swag.SplitByFormat(data, p.parameter.CollectionFormat), false, nil
  398. }
  399. func (p *untypedParamBinder) setSliceFieldValue(target reflect.Value, defaultValue interface{}, data []string, hasKey bool) error {
  400. sz := len(data)
  401. if (!hasKey || (!p.parameter.AllowEmptyValue && (sz == 0 || (sz == 1 && data[0] == "")))) && p.parameter.Required && defaultValue == nil {
  402. return errors.Required(p.Name, p.parameter.In)
  403. }
  404. defVal := reflect.Zero(target.Type())
  405. if defaultValue != nil {
  406. defVal = reflect.ValueOf(defaultValue)
  407. }
  408. if !target.CanSet() {
  409. return nil
  410. }
  411. if sz == 0 {
  412. target.Set(defVal)
  413. return nil
  414. }
  415. value := reflect.MakeSlice(reflect.SliceOf(target.Type().Elem()), sz, sz)
  416. for i := 0; i < sz; i++ {
  417. if err := p.setFieldValue(value.Index(i), nil, data[i], hasKey); err != nil {
  418. return err
  419. }
  420. }
  421. target.Set(value)
  422. return nil
  423. }
上海开阖软件有限公司 沪ICP备12045867号-1