1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 05:50:55 +00:00

context: improve the formParse

This commit is contained in:
astaxie 2014-04-06 00:08:03 +08:00
parent deb28dd873
commit 73d757e3f4

View File

@ -2,6 +2,7 @@ package context
import ( import (
"bytes" "bytes"
"errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"reflect" "reflect"
@ -92,6 +93,41 @@ func (input *BeegoInput) Is(method string) bool {
return input.Method() == method 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. // IsAjax returns boolean of this request is generated by ajax.
func (input *BeegoInput) IsAjax() bool { func (input *BeegoInput) IsAjax() bool {
return input.Header("X-Requested-With") == "XMLHttpRequest" 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.. // IsSecure returns boolean of whether file uploads in this request or not..
func (input *BeegoInput) IsUpload() bool { func (input *BeegoInput) IsUpload() bool {
return input.Request.MultipartForm != nil return input.Header("Content-Type") == "multipart/form-data"
} }
// IP returns request client ip. // 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. // Query returns input data item string by a given string.
func (input *BeegoInput) Query(key string) 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) 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. // 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) requestbody, _ := ioutil.ReadAll(input.Request.Body)
input.Request.Body.Close() input.Request.Body.Close()
bf := bytes.NewBuffer(requestbody) bf := bytes.NewBuffer(requestbody)
@ -222,3 +260,21 @@ func (input *BeegoInput) GetData(key interface{}) interface{} {
func (input *BeegoInput) SetData(key, val interface{}) { func (input *BeegoInput) SetData(key, val interface{}) {
input.Data[key] = val 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
}