input.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // Copyright 2014 beego Author. All Rights Reserved.
  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 context
  15. import (
  16. "bytes"
  17. "compress/gzip"
  18. "errors"
  19. "io"
  20. "io/ioutil"
  21. "net"
  22. "net/http"
  23. "net/url"
  24. "reflect"
  25. "regexp"
  26. "strconv"
  27. "strings"
  28. "github.com/cnlh/nps/vender/github.com/astaxie/beego/session"
  29. )
  30. // Regexes for checking the accept headers
  31. // TODO make sure these are correct
  32. var (
  33. acceptsHTMLRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`)
  34. acceptsXMLRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`)
  35. acceptsJSONRegex = regexp.MustCompile(`(application/json)(?:,|$)`)
  36. acceptsYAMLRegex = regexp.MustCompile(`(application/x-yaml)(?:,|$)`)
  37. maxParam = 50
  38. )
  39. // BeegoInput operates the http request header, data, cookie and body.
  40. // it also contains router params and current session.
  41. type BeegoInput struct {
  42. Context *Context
  43. CruSession session.Store
  44. pnames []string
  45. pvalues []string
  46. data map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
  47. RequestBody []byte
  48. RunMethod string
  49. RunController reflect.Type
  50. }
  51. // NewInput return BeegoInput generated by Context.
  52. func NewInput() *BeegoInput {
  53. return &BeegoInput{
  54. pnames: make([]string, 0, maxParam),
  55. pvalues: make([]string, 0, maxParam),
  56. data: make(map[interface{}]interface{}),
  57. }
  58. }
  59. // Reset init the BeegoInput
  60. func (input *BeegoInput) Reset(ctx *Context) {
  61. input.Context = ctx
  62. input.CruSession = nil
  63. input.pnames = input.pnames[:0]
  64. input.pvalues = input.pvalues[:0]
  65. input.data = nil
  66. input.RequestBody = []byte{}
  67. }
  68. // Protocol returns request protocol name, such as HTTP/1.1 .
  69. func (input *BeegoInput) Protocol() string {
  70. return input.Context.Request.Proto
  71. }
  72. // URI returns full request url with query string, fragment.
  73. func (input *BeegoInput) URI() string {
  74. return input.Context.Request.RequestURI
  75. }
  76. // URL returns request url path (without query string, fragment).
  77. func (input *BeegoInput) URL() string {
  78. return input.Context.Request.URL.Path
  79. }
  80. // Site returns base site url as scheme://domain type.
  81. func (input *BeegoInput) Site() string {
  82. return input.Scheme() + "://" + input.Domain()
  83. }
  84. // Scheme returns request scheme as "http" or "https".
  85. func (input *BeegoInput) Scheme() string {
  86. if scheme := input.Header("X-Forwarded-Proto"); scheme != "" {
  87. return scheme
  88. }
  89. if input.Context.Request.URL.Scheme != "" {
  90. return input.Context.Request.URL.Scheme
  91. }
  92. if input.Context.Request.TLS == nil {
  93. return "http"
  94. }
  95. return "https"
  96. }
  97. // Domain returns host name.
  98. // Alias of Host method.
  99. func (input *BeegoInput) Domain() string {
  100. return input.Host()
  101. }
  102. // Host returns host name.
  103. // if no host info in request, return localhost.
  104. func (input *BeegoInput) Host() string {
  105. if input.Context.Request.Host != "" {
  106. if hostPart, _, err := net.SplitHostPort(input.Context.Request.Host); err == nil {
  107. return hostPart
  108. }
  109. return input.Context.Request.Host
  110. }
  111. return "localhost"
  112. }
  113. // Method returns http request method.
  114. func (input *BeegoInput) Method() string {
  115. return input.Context.Request.Method
  116. }
  117. // Is returns boolean of this request is on given method, such as Is("POST").
  118. func (input *BeegoInput) Is(method string) bool {
  119. return input.Method() == method
  120. }
  121. // IsGet Is this a GET method request?
  122. func (input *BeegoInput) IsGet() bool {
  123. return input.Is("GET")
  124. }
  125. // IsPost Is this a POST method request?
  126. func (input *BeegoInput) IsPost() bool {
  127. return input.Is("POST")
  128. }
  129. // IsHead Is this a Head method request?
  130. func (input *BeegoInput) IsHead() bool {
  131. return input.Is("HEAD")
  132. }
  133. // IsOptions Is this a OPTIONS method request?
  134. func (input *BeegoInput) IsOptions() bool {
  135. return input.Is("OPTIONS")
  136. }
  137. // IsPut Is this a PUT method request?
  138. func (input *BeegoInput) IsPut() bool {
  139. return input.Is("PUT")
  140. }
  141. // IsDelete Is this a DELETE method request?
  142. func (input *BeegoInput) IsDelete() bool {
  143. return input.Is("DELETE")
  144. }
  145. // IsPatch Is this a PATCH method request?
  146. func (input *BeegoInput) IsPatch() bool {
  147. return input.Is("PATCH")
  148. }
  149. // IsAjax returns boolean of this request is generated by ajax.
  150. func (input *BeegoInput) IsAjax() bool {
  151. return input.Header("X-Requested-With") == "XMLHttpRequest"
  152. }
  153. // IsSecure returns boolean of this request is in https.
  154. func (input *BeegoInput) IsSecure() bool {
  155. return input.Scheme() == "https"
  156. }
  157. // IsWebsocket returns boolean of this request is in webSocket.
  158. func (input *BeegoInput) IsWebsocket() bool {
  159. return input.Header("Upgrade") == "websocket"
  160. }
  161. // IsUpload returns boolean of whether file uploads in this request or not..
  162. func (input *BeegoInput) IsUpload() bool {
  163. return strings.Contains(input.Header("Content-Type"), "multipart/form-data")
  164. }
  165. // AcceptsHTML Checks if request accepts html response
  166. func (input *BeegoInput) AcceptsHTML() bool {
  167. return acceptsHTMLRegex.MatchString(input.Header("Accept"))
  168. }
  169. // AcceptsXML Checks if request accepts xml response
  170. func (input *BeegoInput) AcceptsXML() bool {
  171. return acceptsXMLRegex.MatchString(input.Header("Accept"))
  172. }
  173. // AcceptsJSON Checks if request accepts json response
  174. func (input *BeegoInput) AcceptsJSON() bool {
  175. return acceptsJSONRegex.MatchString(input.Header("Accept"))
  176. }
  177. // AcceptsYAML Checks if request accepts json response
  178. func (input *BeegoInput) AcceptsYAML() bool {
  179. return acceptsYAMLRegex.MatchString(input.Header("Accept"))
  180. }
  181. // IP returns request client ip.
  182. // if in proxy, return first proxy id.
  183. // if error, return RemoteAddr.
  184. func (input *BeegoInput) IP() string {
  185. ips := input.Proxy()
  186. if len(ips) > 0 && ips[0] != "" {
  187. rip, _, err := net.SplitHostPort(ips[0])
  188. if err != nil {
  189. rip = ips[0]
  190. }
  191. return rip
  192. }
  193. if ip, _, err := net.SplitHostPort(input.Context.Request.RemoteAddr); err == nil {
  194. return ip
  195. }
  196. return input.Context.Request.RemoteAddr
  197. }
  198. // Proxy returns proxy client ips slice.
  199. func (input *BeegoInput) Proxy() []string {
  200. if ips := input.Header("X-Forwarded-For"); ips != "" {
  201. return strings.Split(ips, ",")
  202. }
  203. return []string{}
  204. }
  205. // Referer returns http referer header.
  206. func (input *BeegoInput) Referer() string {
  207. return input.Header("Referer")
  208. }
  209. // Refer returns http referer header.
  210. func (input *BeegoInput) Refer() string {
  211. return input.Referer()
  212. }
  213. // SubDomains returns sub domain string.
  214. // if aa.bb.domain.com, returns aa.bb .
  215. func (input *BeegoInput) SubDomains() string {
  216. parts := strings.Split(input.Host(), ".")
  217. if len(parts) >= 3 {
  218. return strings.Join(parts[:len(parts)-2], ".")
  219. }
  220. return ""
  221. }
  222. // Port returns request client port.
  223. // when error or empty, return 80.
  224. func (input *BeegoInput) Port() int {
  225. if _, portPart, err := net.SplitHostPort(input.Context.Request.Host); err == nil {
  226. port, _ := strconv.Atoi(portPart)
  227. return port
  228. }
  229. return 80
  230. }
  231. // UserAgent returns request client user agent string.
  232. func (input *BeegoInput) UserAgent() string {
  233. return input.Header("User-Agent")
  234. }
  235. // ParamsLen return the length of the params
  236. func (input *BeegoInput) ParamsLen() int {
  237. return len(input.pnames)
  238. }
  239. // Param returns router param by a given key.
  240. func (input *BeegoInput) Param(key string) string {
  241. for i, v := range input.pnames {
  242. if v == key && i <= len(input.pvalues) {
  243. return input.pvalues[i]
  244. }
  245. }
  246. return ""
  247. }
  248. // Params returns the map[key]value.
  249. func (input *BeegoInput) Params() map[string]string {
  250. m := make(map[string]string)
  251. for i, v := range input.pnames {
  252. if i <= len(input.pvalues) {
  253. m[v] = input.pvalues[i]
  254. }
  255. }
  256. return m
  257. }
  258. // SetParam will set the param with key and value
  259. func (input *BeegoInput) SetParam(key, val string) {
  260. // check if already exists
  261. for i, v := range input.pnames {
  262. if v == key && i <= len(input.pvalues) {
  263. input.pvalues[i] = val
  264. return
  265. }
  266. }
  267. input.pvalues = append(input.pvalues, val)
  268. input.pnames = append(input.pnames, key)
  269. }
  270. // ResetParams clears any of the input's Params
  271. // This function is used to clear parameters so they may be reset between filter
  272. // passes.
  273. func (input *BeegoInput) ResetParams() {
  274. input.pnames = input.pnames[:0]
  275. input.pvalues = input.pvalues[:0]
  276. }
  277. // Query returns input data item string by a given string.
  278. func (input *BeegoInput) Query(key string) string {
  279. if val := input.Param(key); val != "" {
  280. return val
  281. }
  282. if input.Context.Request.Form == nil {
  283. input.Context.Request.ParseForm()
  284. }
  285. return input.Context.Request.Form.Get(key)
  286. }
  287. // Header returns request header item string by a given string.
  288. // if non-existed, return empty string.
  289. func (input *BeegoInput) Header(key string) string {
  290. return input.Context.Request.Header.Get(key)
  291. }
  292. // Cookie returns request cookie item string by a given key.
  293. // if non-existed, return empty string.
  294. func (input *BeegoInput) Cookie(key string) string {
  295. ck, err := input.Context.Request.Cookie(key)
  296. if err != nil {
  297. return ""
  298. }
  299. return ck.Value
  300. }
  301. // Session returns current session item value by a given key.
  302. // if non-existed, return nil.
  303. func (input *BeegoInput) Session(key interface{}) interface{} {
  304. return input.CruSession.Get(key)
  305. }
  306. // CopyBody returns the raw request body data as bytes.
  307. func (input *BeegoInput) CopyBody(MaxMemory int64) []byte {
  308. if input.Context.Request.Body == nil {
  309. return []byte{}
  310. }
  311. var requestbody []byte
  312. safe := &io.LimitedReader{R: input.Context.Request.Body, N: MaxMemory}
  313. if input.Header("Content-Encoding") == "gzip" {
  314. reader, err := gzip.NewReader(safe)
  315. if err != nil {
  316. return nil
  317. }
  318. requestbody, _ = ioutil.ReadAll(reader)
  319. } else {
  320. requestbody, _ = ioutil.ReadAll(safe)
  321. }
  322. input.Context.Request.Body.Close()
  323. bf := bytes.NewBuffer(requestbody)
  324. input.Context.Request.Body = http.MaxBytesReader(input.Context.ResponseWriter, ioutil.NopCloser(bf), MaxMemory)
  325. input.RequestBody = requestbody
  326. return requestbody
  327. }
  328. // Data return the implicit data in the input
  329. func (input *BeegoInput) Data() map[interface{}]interface{} {
  330. if input.data == nil {
  331. input.data = make(map[interface{}]interface{})
  332. }
  333. return input.data
  334. }
  335. // GetData returns the stored data in this context.
  336. func (input *BeegoInput) GetData(key interface{}) interface{} {
  337. if v, ok := input.data[key]; ok {
  338. return v
  339. }
  340. return nil
  341. }
  342. // SetData stores data with given key in this context.
  343. // This data are only available in this context.
  344. func (input *BeegoInput) SetData(key, val interface{}) {
  345. if input.data == nil {
  346. input.data = make(map[interface{}]interface{})
  347. }
  348. input.data[key] = val
  349. }
  350. // ParseFormOrMulitForm parseForm or parseMultiForm based on Content-type
  351. func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
  352. // Parse the body depending on the content type.
  353. if strings.Contains(input.Header("Content-Type"), "multipart/form-data") {
  354. if err := input.Context.Request.ParseMultipartForm(maxMemory); err != nil {
  355. return errors.New("Error parsing request body:" + err.Error())
  356. }
  357. } else if err := input.Context.Request.ParseForm(); err != nil {
  358. return errors.New("Error parsing request body:" + err.Error())
  359. }
  360. return nil
  361. }
  362. // Bind data from request.Form[key] to dest
  363. // like /?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie
  364. // var id int beegoInput.Bind(&id, "id") id ==123
  365. // var isok bool beegoInput.Bind(&isok, "isok") isok ==true
  366. // var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2
  367. // ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2]
  368. // ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str array]
  369. // user struct{Name} beegoInput.Bind(&user, "user") user == {Name:"astaxie"}
  370. func (input *BeegoInput) Bind(dest interface{}, key string) error {
  371. value := reflect.ValueOf(dest)
  372. if value.Kind() != reflect.Ptr {
  373. return errors.New("beego: non-pointer passed to Bind: " + key)
  374. }
  375. value = value.Elem()
  376. if !value.CanSet() {
  377. return errors.New("beego: non-settable variable passed to Bind: " + key)
  378. }
  379. typ := value.Type()
  380. // Get real type if dest define with interface{}.
  381. // e.g var dest interface{} dest=1.0
  382. if value.Kind() == reflect.Interface {
  383. typ = value.Elem().Type()
  384. }
  385. rv := input.bind(key, typ)
  386. if !rv.IsValid() {
  387. return errors.New("beego: reflect value is empty")
  388. }
  389. value.Set(rv)
  390. return nil
  391. }
  392. func (input *BeegoInput) bind(key string, typ reflect.Type) reflect.Value {
  393. if input.Context.Request.Form == nil {
  394. input.Context.Request.ParseForm()
  395. }
  396. rv := reflect.Zero(typ)
  397. switch typ.Kind() {
  398. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  399. val := input.Query(key)
  400. if len(val) == 0 {
  401. return rv
  402. }
  403. rv = input.bindInt(val, typ)
  404. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  405. val := input.Query(key)
  406. if len(val) == 0 {
  407. return rv
  408. }
  409. rv = input.bindUint(val, typ)
  410. case reflect.Float32, reflect.Float64:
  411. val := input.Query(key)
  412. if len(val) == 0 {
  413. return rv
  414. }
  415. rv = input.bindFloat(val, typ)
  416. case reflect.String:
  417. val := input.Query(key)
  418. if len(val) == 0 {
  419. return rv
  420. }
  421. rv = input.bindString(val, typ)
  422. case reflect.Bool:
  423. val := input.Query(key)
  424. if len(val) == 0 {
  425. return rv
  426. }
  427. rv = input.bindBool(val, typ)
  428. case reflect.Slice:
  429. rv = input.bindSlice(&input.Context.Request.Form, key, typ)
  430. case reflect.Struct:
  431. rv = input.bindStruct(&input.Context.Request.Form, key, typ)
  432. case reflect.Ptr:
  433. rv = input.bindPoint(key, typ)
  434. case reflect.Map:
  435. rv = input.bindMap(&input.Context.Request.Form, key, typ)
  436. }
  437. return rv
  438. }
  439. func (input *BeegoInput) bindValue(val string, typ reflect.Type) reflect.Value {
  440. rv := reflect.Zero(typ)
  441. switch typ.Kind() {
  442. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  443. rv = input.bindInt(val, typ)
  444. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  445. rv = input.bindUint(val, typ)
  446. case reflect.Float32, reflect.Float64:
  447. rv = input.bindFloat(val, typ)
  448. case reflect.String:
  449. rv = input.bindString(val, typ)
  450. case reflect.Bool:
  451. rv = input.bindBool(val, typ)
  452. case reflect.Slice:
  453. rv = input.bindSlice(&url.Values{"": {val}}, "", typ)
  454. case reflect.Struct:
  455. rv = input.bindStruct(&url.Values{"": {val}}, "", typ)
  456. case reflect.Ptr:
  457. rv = input.bindPoint(val, typ)
  458. case reflect.Map:
  459. rv = input.bindMap(&url.Values{"": {val}}, "", typ)
  460. }
  461. return rv
  462. }
  463. func (input *BeegoInput) bindInt(val string, typ reflect.Type) reflect.Value {
  464. intValue, err := strconv.ParseInt(val, 10, 64)
  465. if err != nil {
  466. return reflect.Zero(typ)
  467. }
  468. pValue := reflect.New(typ)
  469. pValue.Elem().SetInt(intValue)
  470. return pValue.Elem()
  471. }
  472. func (input *BeegoInput) bindUint(val string, typ reflect.Type) reflect.Value {
  473. uintValue, err := strconv.ParseUint(val, 10, 64)
  474. if err != nil {
  475. return reflect.Zero(typ)
  476. }
  477. pValue := reflect.New(typ)
  478. pValue.Elem().SetUint(uintValue)
  479. return pValue.Elem()
  480. }
  481. func (input *BeegoInput) bindFloat(val string, typ reflect.Type) reflect.Value {
  482. floatValue, err := strconv.ParseFloat(val, 64)
  483. if err != nil {
  484. return reflect.Zero(typ)
  485. }
  486. pValue := reflect.New(typ)
  487. pValue.Elem().SetFloat(floatValue)
  488. return pValue.Elem()
  489. }
  490. func (input *BeegoInput) bindString(val string, typ reflect.Type) reflect.Value {
  491. return reflect.ValueOf(val)
  492. }
  493. func (input *BeegoInput) bindBool(val string, typ reflect.Type) reflect.Value {
  494. val = strings.TrimSpace(strings.ToLower(val))
  495. switch val {
  496. case "true", "on", "1":
  497. return reflect.ValueOf(true)
  498. }
  499. return reflect.ValueOf(false)
  500. }
  501. type sliceValue struct {
  502. index int // Index extracted from brackets. If -1, no index was provided.
  503. value reflect.Value // the bound value for this slice element.
  504. }
  505. func (input *BeegoInput) bindSlice(params *url.Values, key string, typ reflect.Type) reflect.Value {
  506. maxIndex := -1
  507. numNoIndex := 0
  508. sliceValues := []sliceValue{}
  509. for reqKey, vals := range *params {
  510. if !strings.HasPrefix(reqKey, key+"[") {
  511. continue
  512. }
  513. // Extract the index, and the index where a sub-key starts. (e.g. field[0].subkey)
  514. index := -1
  515. leftBracket, rightBracket := len(key), strings.Index(reqKey[len(key):], "]")+len(key)
  516. if rightBracket > leftBracket+1 {
  517. index, _ = strconv.Atoi(reqKey[leftBracket+1 : rightBracket])
  518. }
  519. subKeyIndex := rightBracket + 1
  520. // Handle the indexed case.
  521. if index > -1 {
  522. if index > maxIndex {
  523. maxIndex = index
  524. }
  525. sliceValues = append(sliceValues, sliceValue{
  526. index: index,
  527. value: input.bind(reqKey[:subKeyIndex], typ.Elem()),
  528. })
  529. continue
  530. }
  531. // It's an un-indexed element. (e.g. element[])
  532. numNoIndex += len(vals)
  533. for _, val := range vals {
  534. // Unindexed values can only be direct-bound.
  535. sliceValues = append(sliceValues, sliceValue{
  536. index: -1,
  537. value: input.bindValue(val, typ.Elem()),
  538. })
  539. }
  540. }
  541. resultArray := reflect.MakeSlice(typ, maxIndex+1, maxIndex+1+numNoIndex)
  542. for _, sv := range sliceValues {
  543. if sv.index != -1 {
  544. resultArray.Index(sv.index).Set(sv.value)
  545. } else {
  546. resultArray = reflect.Append(resultArray, sv.value)
  547. }
  548. }
  549. return resultArray
  550. }
  551. func (input *BeegoInput) bindStruct(params *url.Values, key string, typ reflect.Type) reflect.Value {
  552. result := reflect.New(typ).Elem()
  553. fieldValues := make(map[string]reflect.Value)
  554. for reqKey, val := range *params {
  555. var fieldName string
  556. if strings.HasPrefix(reqKey, key+".") {
  557. fieldName = reqKey[len(key)+1:]
  558. } else if strings.HasPrefix(reqKey, key+"[") && reqKey[len(reqKey)-1] == ']' {
  559. fieldName = reqKey[len(key)+1 : len(reqKey)-1]
  560. } else {
  561. continue
  562. }
  563. if _, ok := fieldValues[fieldName]; !ok {
  564. // Time to bind this field. Get it and make sure we can set it.
  565. fieldValue := result.FieldByName(fieldName)
  566. if !fieldValue.IsValid() {
  567. continue
  568. }
  569. if !fieldValue.CanSet() {
  570. continue
  571. }
  572. boundVal := input.bindValue(val[0], fieldValue.Type())
  573. fieldValue.Set(boundVal)
  574. fieldValues[fieldName] = boundVal
  575. }
  576. }
  577. return result
  578. }
  579. func (input *BeegoInput) bindPoint(key string, typ reflect.Type) reflect.Value {
  580. return input.bind(key, typ.Elem()).Addr()
  581. }
  582. func (input *BeegoInput) bindMap(params *url.Values, key string, typ reflect.Type) reflect.Value {
  583. var (
  584. result = reflect.MakeMap(typ)
  585. keyType = typ.Key()
  586. valueType = typ.Elem()
  587. )
  588. for paramName, values := range *params {
  589. if !strings.HasPrefix(paramName, key+"[") || paramName[len(paramName)-1] != ']' {
  590. continue
  591. }
  592. key := paramName[len(key)+1 : len(paramName)-1]
  593. result.SetMapIndex(input.bindValue(key, keyType), input.bindValue(values[0], valueType))
  594. }
  595. return result
  596. }