context: improve the formParse
Showing
1 changed file
with
59 additions
and
3 deletions
| ... | @@ -2,6 +2,7 @@ package context | ... | @@ -2,6 +2,7 @@ package context |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "errors" | ||
| 5 | "io/ioutil" | 6 | "io/ioutil" |
| 6 | "net/http" | 7 | "net/http" |
| 7 | "reflect" | 8 | "reflect" |
| ... | @@ -92,6 +93,41 @@ func (input *BeegoInput) Is(method string) bool { | ... | @@ -92,6 +93,41 @@ func (input *BeegoInput) Is(method string) bool { |
| 92 | return input.Method() == method | 93 | return input.Method() == method |
| 93 | } | 94 | } |
| 94 | 95 | ||
| 96 | // Is this a GET method request? | ||
| 97 | func (input *BeegoInput) IsGet() bool { | ||
| 98 | return input.Is("GET") | ||
| 99 | } | ||
| 100 | |||
| 101 | // Is this a POST method request? | ||
| 102 | func (input *BeegoInput) IsPost() bool { | ||
| 103 | return input.Is("POST") | ||
| 104 | } | ||
| 105 | |||
| 106 | // Is this a Head method request? | ||
| 107 | func (input *BeegoInput) IsHead() bool { | ||
| 108 | return input.Is("HEAD") | ||
| 109 | } | ||
| 110 | |||
| 111 | // Is this a OPTIONS method request? | ||
| 112 | func (input *BeegoInput) IsOptions() bool { | ||
| 113 | return input.Is("OPTIONS") | ||
| 114 | } | ||
| 115 | |||
| 116 | // Is this a PUT method request? | ||
| 117 | func (input *BeegoInput) IsPut() bool { | ||
| 118 | return input.Is("PUT") | ||
| 119 | } | ||
| 120 | |||
| 121 | // Is this a DELETE method request? | ||
| 122 | func (input *BeegoInput) IsDelete() bool { | ||
| 123 | return input.Is("DELETE") | ||
| 124 | } | ||
| 125 | |||
| 126 | // Is this a PATCH method request? | ||
| 127 | func (input *BeegoInput) IsPatch() bool { | ||
| 128 | return input.Is("PATCH") | ||
| 129 | } | ||
| 130 | |||
| 95 | // IsAjax returns boolean of this request is generated by ajax. | 131 | // IsAjax returns boolean of this request is generated by ajax. |
| 96 | func (input *BeegoInput) IsAjax() bool { | 132 | func (input *BeegoInput) IsAjax() bool { |
| 97 | return input.Header("X-Requested-With") == "XMLHttpRequest" | 133 | return input.Header("X-Requested-With") == "XMLHttpRequest" |
| ... | @@ -109,7 +145,7 @@ func (input *BeegoInput) IsWebsocket() bool { | ... | @@ -109,7 +145,7 @@ func (input *BeegoInput) IsWebsocket() bool { |
| 109 | 145 | ||
| 110 | // IsSecure returns boolean of whether file uploads in this request or not.. | 146 | // IsSecure returns boolean of whether file uploads in this request or not.. |
| 111 | func (input *BeegoInput) IsUpload() bool { | 147 | func (input *BeegoInput) IsUpload() bool { |
| 112 | return input.Request.MultipartForm != nil | 148 | return input.Header("Content-Type") == "multipart/form-data" |
| 113 | } | 149 | } |
| 114 | 150 | ||
| 115 | // IP returns request client ip. | 151 | // IP returns request client ip. |
| ... | @@ -175,7 +211,9 @@ func (input *BeegoInput) Param(key string) string { | ... | @@ -175,7 +211,9 @@ func (input *BeegoInput) Param(key string) string { |
| 175 | 211 | ||
| 176 | // Query returns input data item string by a given string. | 212 | // Query returns input data item string by a given string. |
| 177 | func (input *BeegoInput) Query(key string) string { | 213 | func (input *BeegoInput) Query(key string) string { |
| 178 | input.Request.ParseForm() | 214 | if input.Request.Form == nil { |
| 215 | input.Request.ParseForm() | ||
| 216 | } | ||
| 179 | return input.Request.Form.Get(key) | 217 | return input.Request.Form.Get(key) |
| 180 | } | 218 | } |
| 181 | 219 | ||
| ... | @@ -200,7 +238,7 @@ func (input *BeegoInput) Session(key interface{}) interface{} { | ... | @@ -200,7 +238,7 @@ func (input *BeegoInput) Session(key interface{}) interface{} { |
| 200 | } | 238 | } |
| 201 | 239 | ||
| 202 | // Body returns the raw request body data as bytes. | 240 | // Body returns the raw request body data as bytes. |
| 203 | func (input *BeegoInput) Body() []byte { | 241 | func (input *BeegoInput) CopyBody() []byte { |
| 204 | requestbody, _ := ioutil.ReadAll(input.Request.Body) | 242 | requestbody, _ := ioutil.ReadAll(input.Request.Body) |
| 205 | input.Request.Body.Close() | 243 | input.Request.Body.Close() |
| 206 | bf := bytes.NewBuffer(requestbody) | 244 | bf := bytes.NewBuffer(requestbody) |
| ... | @@ -222,3 +260,21 @@ func (input *BeegoInput) GetData(key interface{}) interface{} { | ... | @@ -222,3 +260,21 @@ func (input *BeegoInput) GetData(key interface{}) interface{} { |
| 222 | func (input *BeegoInput) SetData(key, val interface{}) { | 260 | func (input *BeegoInput) SetData(key, val interface{}) { |
| 223 | input.Data[key] = val | 261 | input.Data[key] = val |
| 224 | } | 262 | } |
| 263 | |||
| 264 | func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error { | ||
| 265 | // Parse the body depending on the content type. | ||
| 266 | switch input.Header("Content-Type") { | ||
| 267 | case "application/x-www-form-urlencoded": | ||
| 268 | // Typical form. | ||
| 269 | if err := input.Request.ParseForm(); err != nil { | ||
| 270 | return errors.New("Error parsing request body:" + err.Error()) | ||
| 271 | } | ||
| 272 | |||
| 273 | case "multipart/form-data": | ||
| 274 | if err := input.Request.ParseMultipartForm(maxMemory); err != nil { | ||
| 275 | return errors.New("Error parsing request body:" + err.Error()) | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | return nil | ||
| 280 | } | ... | ... |
-
Please register or sign in to post a comment