From 73d757e3f47657d74f2d5641e22d2340a20c6b5d Mon Sep 17 00:00:00 2001 From: astaxie Date: Sun, 6 Apr 2014 00:08:03 +0800 Subject: [PATCH] context: improve the formParse --- context/input.go | 62 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/context/input.go b/context/input.go index 0a3c8535..df0680e5 100644 --- a/context/input.go +++ b/context/input.go @@ -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 +}