From 20c844bcd94fa5240ab9713626464482b4ee64dc Mon Sep 17 00:00:00 2001 From: HellP Date: Thu, 6 Oct 2022 20:49:41 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20context.RequestBodyString()=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=EB=B6=80=20tee=20reader=20=EC=9E=98=EB=AA=BB=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20=ED=95=9C=EB=B6=80=EB=B6=84=20=ED=94=BD?= =?UTF-8?q?=EC=8A=A4=20=EB=B0=8F=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context.go | 32 +++++++++++++++++++++++++++++--- go.mod | 2 -- go.sum | 16 ++-------------- golam.go | 9 +-------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/context.go b/context.go index 9d36888..a881b84 100644 --- a/context.go +++ b/context.go @@ -28,8 +28,12 @@ type Context interface { Request() *http.Request + RequestBody() io.ReadCloser + RequestBodyString() string + RequestBodyBytes() []byte + Response() *Response Scheme() string @@ -81,7 +85,7 @@ type Context interface { ResultStream(status int, contentType string, reader io.Reader) error Result(status int, contentType string, b []byte) error - + Write(b []byte) (int, error) // TODO @@ -104,6 +108,7 @@ var _ Context = (*contextImpl)(nil) type contextImpl struct { ctx context.Context request *http.Request + requestBodyBytes []byte response *Response path string pathParams PathParams @@ -148,10 +153,31 @@ func (c *contextImpl) Request() *http.Request { return c.request } +func (c *contextImpl) RequestBody() io.ReadCloser { + return c.Request().Body +} + func (c *contextImpl) RequestBodyString() string { + return string(c.RequestBodyBytes()) +} + +func (c *contextImpl) RequestBodyBytes() []byte { + if c.requestBodyBytes != nil { + return c.requestBodyBytes + } + var buf bytes.Buffer - c.request.Body = io.NopCloser(io.TeeReader(c.request.Body, &buf)) - return buf.String() + tee := io.TeeReader(c.request.Body, &buf) + + var err error + c.requestBodyBytes, err = io.ReadAll(tee) + if err != nil { + //TODO: logging + return []byte{} + } + + c.request.Body = io.NopCloser(&buf) + return c.requestBodyBytes } func (c *contextImpl) Response() *Response { diff --git a/go.mod b/go.mod index 16502fe..c544edb 100644 --- a/go.mod +++ b/go.mod @@ -3,5 +3,3 @@ module github.com/unsafe-risk/golam go 1.18 require github.com/aws/aws-lambda-go v1.33.0 - -require golang.org/x/text v0.3.7 // indirect diff --git a/go.sum b/go.sum index 0964ae7..6e9b771 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,6 @@ -github.com/aws/aws-lambda-go v1.32.0 h1:i8MflawW1hoyYp85GMH7LhvAs4cqzL7LOS6fSv8l2KM= -github.com/aws/aws-lambda-go v1.32.0/go.mod h1:IF5Q7wj4VyZyUFnZ54IQqeWtctHQ9tz+KhcbDenr220= github.com/aws/aws-lambda-go v1.33.0 h1:n4kw3zie82vPpLLN58ahlYHBz9k8QeK2svQep+jGnB8= github.com/aws/aws-lambda-go v1.33.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= -gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/golam.go b/golam.go index 5f7745e..5b06fda 100644 --- a/golam.go +++ b/golam.go @@ -8,8 +8,6 @@ import ( "errors" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" - "golang.org/x/text/cases" - "golang.org/x/text/language" "io" "net/http" "net/url" @@ -23,10 +21,6 @@ const ( lambdaHeaderHost = "host" ) -var ( - headerCasesTitle = cases.Title(language.English) -) - func New() (g *Golam) { g = &Golam{ isLambdaRuntime: isLambdaRuntime(), @@ -208,8 +202,7 @@ func (d *defaultLambdaHandler) Invoke(ctx context.Context, payload []byte) ([]by req.ProtoMajor, req.ProtoMinor = getProtoVersion(&request) req.RemoteAddr = request.RequestContext.HTTP.SourceIP for k, v := range request.Headers { - k = headerCasesTitle.String(k) - if val := req.Header.Get(k); len(val) > 0 { + if req.Header.Get(k) == v { continue }