Commit 73d757e3 authored by astaxie's avatar astaxie

context: improve the formParse

parent deb28dd8
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment