7bfb4126 by astaxie

support copy requestbody

1 parent 2abe584b
...@@ -45,6 +45,7 @@ var ( ...@@ -45,6 +45,7 @@ var (
45 HttpServerTimeOut int64 45 HttpServerTimeOut int64
46 ErrorsShow bool 46 ErrorsShow bool
47 XSRFKEY string 47 XSRFKEY string
48 CopyRequestBody bool
48 ) 49 )
49 50
50 func init() { 51 func init() {
......
...@@ -186,6 +186,9 @@ func ParseConfig() (err error) { ...@@ -186,6 +186,9 @@ func ParseConfig() (err error) {
186 if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil { 186 if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil {
187 ErrorsShow = errorsshow 187 ErrorsShow = errorsshow
188 } 188 }
189 if copyrequestbody, err := AppConfig.Bool("copyrequestbody"); err == nil {
190 CopyRequestBody = copyrequestbody
191 }
189 if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" { 192 if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" {
190 XSRFKEY = xsrfkey 193 XSRFKEY = xsrfkey
191 } 194 }
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
11 type Context struct { 11 type Context struct {
12 ResponseWriter http.ResponseWriter 12 ResponseWriter http.ResponseWriter
13 Request *http.Request 13 Request *http.Request
14 RequestBody []byte
14 Params map[string]string 15 Params map[string]string
15 } 16 }
16 17
......
1 package beego 1 package beego
2 2
3 import ( 3 import (
4 "bytes"
4 "fmt" 5 "fmt"
6 "io/ioutil"
5 "net/http" 7 "net/http"
6 "net/url" 8 "net/url"
7 "os" 9 "os"
...@@ -258,6 +260,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -258,6 +260,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
258 260
259 requestPath := r.URL.Path 261 requestPath := r.URL.Path
260 262
263 var requestbody []byte
264
265 if CopyRequestBody {
266 requestbody, _ = ioutil.ReadAll(r.Body)
267
268 r.Body.Close()
269
270 bf := bytes.NewBuffer(requestbody)
271
272 r.Body = ioutil.NopCloser(bf)
273 }
274
261 r.ParseMultipartForm(MaxMemory) 275 r.ParseMultipartForm(MaxMemory)
262 276
263 //user defined Handler 277 //user defined Handler
...@@ -346,7 +360,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -346,7 +360,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
346 params[route.params[i]] = match 360 params[route.params[i]] = match
347 } 361 }
348 //reassemble query params and add to RawQuery 362 //reassemble query params and add to RawQuery
349 r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery 363 r.URL.RawQuery = url.Values(values).Encode()
350 //r.URL.RawQuery = url.Values(values).Encode() 364 //r.URL.RawQuery = url.Values(values).Encode()
351 } 365 }
352 runrouter = route 366 runrouter = route
...@@ -370,7 +384,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -370,7 +384,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
370 //call the controller init function 384 //call the controller init function
371 init := vc.MethodByName("Init") 385 init := vc.MethodByName("Init")
372 in := make([]reflect.Value, 2) 386 in := make([]reflect.Value, 2)
373 ct := &Context{ResponseWriter: w, Request: r, Params: params} 387 ct := &Context{ResponseWriter: w, Request: r, Params: params, RequestBody: requestbody}
374 388
375 in[0] = reflect.ValueOf(ct) 389 in[0] = reflect.ValueOf(ct)
376 in[1] = reflect.ValueOf(runrouter.controllerType.Name()) 390 in[1] = reflect.ValueOf(runrouter.controllerType.Name())
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!