mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 22:20:50 +00:00
context: improve the formParse
This commit is contained in:
parent
deb28dd873
commit
73d757e3f4
@ -2,6 +2,7 @@ package context
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
@ -92,6 +93,41 @@ func (input *BeegoInput) Is(method string) bool {
|
||||
return input.Method() == method
|
||||
}
|
||||
|
||||
// Is this a GET method request?
|
||||
func (input *BeegoInput) IsGet() bool {
|
||||
return input.Is("GET")
|
||||
}
|
||||
|
||||
// Is this a POST method request?
|
||||
func (input *BeegoInput) IsPost() bool {
|
||||
return input.Is("POST")
|
||||
}
|
||||
|
||||
// Is this a Head method request?
|
||||
func (input *BeegoInput) IsHead() bool {
|
||||
return input.Is("HEAD")
|
||||
}
|
||||
|
||||
// Is this a OPTIONS method request?
|
||||
func (input *BeegoInput) IsOptions() bool {
|
||||
return input.Is("OPTIONS")
|
||||
}
|
||||
|
||||
// Is this a PUT method request?
|
||||
func (input *BeegoInput) IsPut() bool {
|
||||
return input.Is("PUT")
|
||||
}
|
||||
|
||||
// Is this a DELETE method request?
|
||||
func (input *BeegoInput) IsDelete() bool {
|
||||
return input.Is("DELETE")
|
||||
}
|
||||
|
||||
// Is this a PATCH method request?
|
||||
func (input *BeegoInput) IsPatch() bool {
|
||||
return input.Is("PATCH")
|
||||
}
|
||||
|
||||
// IsAjax returns boolean of this request is generated by ajax.
|
||||
func (input *BeegoInput) IsAjax() bool {
|
||||
return input.Header("X-Requested-With") == "XMLHttpRequest"
|
||||
@ -109,7 +145,7 @@ func (input *BeegoInput) IsWebsocket() bool {
|
||||
|
||||
// IsSecure returns boolean of whether file uploads in this request or not..
|
||||
func (input *BeegoInput) IsUpload() bool {
|
||||
return input.Request.MultipartForm != nil
|
||||
return input.Header("Content-Type") == "multipart/form-data"
|
||||
}
|
||||
|
||||
// IP returns request client ip.
|
||||
@ -175,7 +211,9 @@ func (input *BeegoInput) Param(key string) string {
|
||||
|
||||
// Query returns input data item string by a given string.
|
||||
func (input *BeegoInput) Query(key string) string {
|
||||
input.Request.ParseForm()
|
||||
if input.Request.Form == nil {
|
||||
input.Request.ParseForm()
|
||||
}
|
||||
return input.Request.Form.Get(key)
|
||||
}
|
||||
|
||||
@ -200,7 +238,7 @@ func (input *BeegoInput) Session(key interface{}) interface{} {
|
||||
}
|
||||
|
||||
// Body returns the raw request body data as bytes.
|
||||
func (input *BeegoInput) Body() []byte {
|
||||
func (input *BeegoInput) CopyBody() []byte {
|
||||
requestbody, _ := ioutil.ReadAll(input.Request.Body)
|
||||
input.Request.Body.Close()
|
||||
bf := bytes.NewBuffer(requestbody)
|
||||
@ -222,3 +260,21 @@ func (input *BeegoInput) GetData(key interface{}) interface{} {
|
||||
func (input *BeegoInput) SetData(key, val interface{}) {
|
||||
input.Data[key] = val
|
||||
}
|
||||
|
||||
func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
|
||||
// Parse the body depending on the content type.
|
||||
switch input.Header("Content-Type") {
|
||||
case "application/x-www-form-urlencoded":
|
||||
// Typical form.
|
||||
if err := input.Request.ParseForm(); err != nil {
|
||||
return errors.New("Error parsing request body:" + err.Error())
|
||||
}
|
||||
|
||||
case "multipart/form-data":
|
||||
if err := input.Request.ParseMultipartForm(maxMemory); err != nil {
|
||||
return errors.New("Error parsing request body:" + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user