Skip to content

Commit

Permalink
Modify request body field (#1735)
Browse files Browse the repository at this point in the history
Added new byte `raw_body` `byte` field to coprocess object which filled all the time. And `body` field now filled only if its valid utf (previously just paniced)
  • Loading branch information
matiasinsaurralde authored and buger committed Jun 15, 2018
1 parent 23a1a6c commit 7172b91
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 82 deletions.
19 changes: 9 additions & 10 deletions coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"encoding/json"
"strings"
"unicode/utf8"

"github.com/Sirupsen/logrus"

Expand Down Expand Up @@ -67,15 +68,6 @@ type CoProcessor struct {

// ObjectFromRequest constructs a CoProcessObject from a given http.Request.
func (c *CoProcessor) ObjectFromRequest(r *http.Request) *coprocess.Object {
var body string
if r.Body == nil {
body = ""
} else {
defer r.Body.Close()
originalBody, _ := ioutil.ReadAll(r.Body)
body = string(originalBody)
}

headers := ProtoMap(r.Header)

host := r.Host
Expand All @@ -90,7 +82,6 @@ func (c *CoProcessor) ObjectFromRequest(r *http.Request) *coprocess.Object {
Headers: headers,
SetHeaders: map[string]string{},
DeleteHeaders: []string{},
Body: body,
Url: r.URL.Path,
Params: ProtoMap(r.URL.Query()),
AddParams: map[string]string{},
Expand All @@ -104,6 +95,14 @@ func (c *CoProcessor) ObjectFromRequest(r *http.Request) *coprocess.Object {
Scheme: r.URL.Scheme,
}

if r.Body != nil {
defer r.Body.Close()
miniRequestObject.RawBody, _ = ioutil.ReadAll(r.Body)
if utf8.Valid(miniRequestObject.RawBody) {
miniRequestObject.Body = string(miniRequestObject.RawBody)
}
}

object := &coprocess.Object{
Request: miniRequestObject,
HookName: c.Middleware.HookName,
Expand Down
31 changes: 19 additions & 12 deletions coprocess/bindings/python/coprocess_mini_request_object_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 38 additions & 28 deletions coprocess/coprocess_mini_request_object.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coprocess/proto/coprocess_mini_request_object.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ message MiniRequestObject {
string method = 11;
string request_uri = 12;
string scheme = 13;
bytes raw_body = 14;
}
Loading

0 comments on commit 7172b91

Please sign in to comment.