Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
fix: context.RequestBodyString() 구현부 tee reader 잘못 사용 한부분 픽스 및 불필요한 패…
Browse files Browse the repository at this point in the history
…키지 삭제
  • Loading branch information
2rebi committed Oct 6, 2022
1 parent 903b5c3 commit 20c844b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
32 changes: 29 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ type Context interface {

Request() *http.Request

RequestBody() io.ReadCloser

RequestBodyString() string

RequestBodyBytes() []byte

Response() *Response

Scheme() string
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
9 changes: 1 addition & 8 deletions golam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,10 +21,6 @@ const (
lambdaHeaderHost = "host"
)

var (
headerCasesTitle = cases.Title(language.English)
)

func New() (g *Golam) {
g = &Golam{
isLambdaRuntime: isLambdaRuntime(),
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 20c844b

Please sign in to comment.