Micro & pluggable web framework for Go
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.

288 lines
8.6KB

  1. // Copyright 2016 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "errors"
  7. "html/template"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. )
  13. // Forms a new enhancement of http.Request
  14. type Forms http.Request
  15. var _ Set = &Forms{}
  16. // Values returns http.Request values
  17. func (f *Forms) Values() url.Values {
  18. return (*http.Request)(f).Form
  19. }
  20. // String returns request form as string
  21. func (f *Forms) String(key string) (string, error) {
  22. return (*http.Request)(f).FormValue(key), nil
  23. }
  24. // TrimSpace returns request form as string with trimed spaces left and right
  25. func (f *Forms) Trimmed(key string) (string, error) {
  26. return strings.TrimSpace((*http.Request)(f).FormValue(key)), nil
  27. }
  28. // Strings returns request form as strings
  29. func (f *Forms) Strings(key string) ([]string, error) {
  30. if (*http.Request)(f).Form == nil {
  31. (*http.Request)(f).ParseMultipartForm(32 << 20)
  32. }
  33. if v, ok := (*http.Request)(f).Form[key]; ok {
  34. return v, nil
  35. }
  36. return nil, errors.New("not exist")
  37. }
  38. // Escape returns request form as escaped string
  39. func (f *Forms) Escape(key string) (string, error) {
  40. return template.HTMLEscapeString((*http.Request)(f).FormValue(key)), nil
  41. }
  42. // Int returns request form as int
  43. func (f *Forms) Int(key string) (int, error) {
  44. return strconv.Atoi((*http.Request)(f).FormValue(key))
  45. }
  46. // Int32 returns request form as int32
  47. func (f *Forms) Int32(key string) (int32, error) {
  48. v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 32)
  49. return int32(v), err
  50. }
  51. // Int64 returns request form as int64
  52. func (f *Forms) Int64(key string) (int64, error) {
  53. return strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 64)
  54. }
  55. // Uint returns request form as uint
  56. func (f *Forms) Uint(key string) (uint, error) {
  57. v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
  58. return uint(v), err
  59. }
  60. // Uint32 returns request form as uint32
  61. func (f *Forms) Uint32(key string) (uint32, error) {
  62. v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 32)
  63. return uint32(v), err
  64. }
  65. // Uint64 returns request form as uint64
  66. func (f *Forms) Uint64(key string) (uint64, error) {
  67. return strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
  68. }
  69. // Bool returns request form as bool
  70. func (f *Forms) Bool(key string) (bool, error) {
  71. return strconv.ParseBool((*http.Request)(f).FormValue(key))
  72. }
  73. // Float32 returns request form as float32
  74. func (f *Forms) Float32(key string) (float32, error) {
  75. v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 64)
  76. return float32(v), err
  77. }
  78. // Float64 returns request form as float64
  79. func (f *Forms) Float64(key string) (float64, error) {
  80. return strconv.ParseFloat((*http.Request)(f).FormValue(key), 64)
  81. }
  82. // MustString returns request form as string with default
  83. func (f *Forms) MustString(key string, defaults ...string) string {
  84. if v := (*http.Request)(f).FormValue(key); len(v) > 0 {
  85. return v
  86. }
  87. if len(defaults) > 0 {
  88. return defaults[0]
  89. }
  90. return ""
  91. }
  92. // MustTrimmed returns request form as string with default
  93. func (f *Forms) MustTrimmed(key string, defaults ...string) string {
  94. return strings.TrimSpace(f.MustString(key, defaults...))
  95. }
  96. // MustStrings returns request form as strings with default
  97. func (f *Forms) MustStrings(key string, defaults ...[]string) []string {
  98. if (*http.Request)(f).Form == nil {
  99. (*http.Request)(f).ParseMultipartForm(32 << 20)
  100. }
  101. if v, ok := (*http.Request)(f).Form[key]; ok {
  102. return v
  103. }
  104. if len(defaults) > 0 {
  105. return defaults[0]
  106. }
  107. return []string{}
  108. }
  109. // MustEscape returns request form as escaped string with default
  110. func (f *Forms) MustEscape(key string, defaults ...string) string {
  111. if v := (*http.Request)(f).FormValue(key); len(v) > 0 {
  112. return template.HTMLEscapeString(v)
  113. }
  114. if len(defaults) > 0 {
  115. return defaults[0]
  116. }
  117. return ""
  118. }
  119. // MustInt returns request form as int with default
  120. func (f *Forms) MustInt(key string, defaults ...int) int {
  121. v, err := strconv.Atoi((*http.Request)(f).FormValue(key))
  122. if len(defaults) > 0 && err != nil {
  123. return defaults[0]
  124. }
  125. return v
  126. }
  127. // MustInt32 returns request form as int32 with default
  128. func (f *Forms) MustInt32(key string, defaults ...int32) int32 {
  129. v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 32)
  130. if len(defaults) > 0 && err != nil {
  131. return defaults[0]
  132. }
  133. return int32(v)
  134. }
  135. // MustInt64 returns request form as int64 with default
  136. func (f *Forms) MustInt64(key string, defaults ...int64) int64 {
  137. v, err := strconv.ParseInt((*http.Request)(f).FormValue(key), 10, 64)
  138. if len(defaults) > 0 && err != nil {
  139. return defaults[0]
  140. }
  141. return v
  142. }
  143. // MustUint returns request form as uint with default
  144. func (f *Forms) MustUint(key string, defaults ...uint) uint {
  145. v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
  146. if len(defaults) > 0 && err != nil {
  147. return defaults[0]
  148. }
  149. return uint(v)
  150. }
  151. // MustUint32 returns request form as uint32 with default
  152. func (f *Forms) MustUint32(key string, defaults ...uint32) uint32 {
  153. v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 32)
  154. if len(defaults) > 0 && err != nil {
  155. return defaults[0]
  156. }
  157. return uint32(v)
  158. }
  159. // MustUint64 returns request form as uint64 with default
  160. func (f *Forms) MustUint64(key string, defaults ...uint64) uint64 {
  161. v, err := strconv.ParseUint((*http.Request)(f).FormValue(key), 10, 64)
  162. if len(defaults) > 0 && err != nil {
  163. return defaults[0]
  164. }
  165. return v
  166. }
  167. // MustFloat32 returns request form as float32 with default
  168. func (f *Forms) MustFloat32(key string, defaults ...float32) float32 {
  169. v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 32)
  170. if len(defaults) > 0 && err != nil {
  171. return defaults[0]
  172. }
  173. return float32(v)
  174. }
  175. // MustFloat64 returns request form as float64 with default
  176. func (f *Forms) MustFloat64(key string, defaults ...float64) float64 {
  177. v, err := strconv.ParseFloat((*http.Request)(f).FormValue(key), 64)
  178. if len(defaults) > 0 && err != nil {
  179. return defaults[0]
  180. }
  181. return v
  182. }
  183. // MustBool returns request form as bool with default
  184. func (f *Forms) MustBool(key string, defaults ...bool) bool {
  185. v, err := strconv.ParseBool((*http.Request)(f).FormValue(key))
  186. if len(defaults) > 0 && err != nil {
  187. return defaults[0]
  188. }
  189. return v
  190. }
  191. // Form returns request form as string with default
  192. func (ctx *Context) Form(key string, defaults ...string) string {
  193. return (*Forms)(ctx.req).MustString(key, defaults...)
  194. }
  195. // FormTrimmed returns request form as string with default and trimmed spaces
  196. func (ctx *Context) FormTrimmed(key string, defaults ...string) string {
  197. return (*Forms)(ctx.req).MustTrimmed(key, defaults...)
  198. }
  199. // FormStrings returns request form as strings with default
  200. func (ctx *Context) FormStrings(key string, defaults ...[]string) []string {
  201. return (*Forms)(ctx.req).MustStrings(key, defaults...)
  202. }
  203. // FormEscape returns request form as escaped string with default
  204. func (ctx *Context) FormEscape(key string, defaults ...string) string {
  205. return (*Forms)(ctx.req).MustEscape(key, defaults...)
  206. }
  207. // FormInt returns request form as int with default
  208. func (ctx *Context) FormInt(key string, defaults ...int) int {
  209. return (*Forms)(ctx.req).MustInt(key, defaults...)
  210. }
  211. // FormInt32 returns request form as int32 with default
  212. func (ctx *Context) FormInt32(key string, defaults ...int32) int32 {
  213. return (*Forms)(ctx.req).MustInt32(key, defaults...)
  214. }
  215. // FormInt64 returns request form as int64 with default
  216. func (ctx *Context) FormInt64(key string, defaults ...int64) int64 {
  217. return (*Forms)(ctx.req).MustInt64(key, defaults...)
  218. }
  219. // FormUint returns request form as uint with default
  220. func (ctx *Context) FormUint(key string, defaults ...uint) uint {
  221. return (*Forms)(ctx.req).MustUint(key, defaults...)
  222. }
  223. // FormUint32 returns request form as uint32 with default
  224. func (ctx *Context) FormUint32(key string, defaults ...uint32) uint32 {
  225. return (*Forms)(ctx.req).MustUint32(key, defaults...)
  226. }
  227. // FormUint64 returns request form as uint64 with default
  228. func (ctx *Context) FormUint64(key string, defaults ...uint64) uint64 {
  229. return (*Forms)(ctx.req).MustUint64(key, defaults...)
  230. }
  231. // FormFloat32 returns request form as float32 with default
  232. func (ctx *Context) FormFloat32(key string, defaults ...float32) float32 {
  233. return (*Forms)(ctx.req).MustFloat32(key, defaults...)
  234. }
  235. // FormFloat64 returns request form as float64 with default
  236. func (ctx *Context) FormFloat64(key string, defaults ...float64) float64 {
  237. return (*Forms)(ctx.req).MustFloat64(key, defaults...)
  238. }
  239. // FormBool returns request form as bool with default
  240. func (ctx *Context) FormBool(key string, defaults ...bool) bool {
  241. return (*Forms)(ctx.req).MustBool(key, defaults...)
  242. }