Skip to content

Commit

Permalink
AWS Lambda: Remove MultiValueHeaders
Browse files Browse the repository at this point in the history
Removes 'MultiValueHeaders' as some lambda do not support these.
Only sets 'Content-Type' when Content-Length > 0, and correctly checks
to body for the case when 'application/x-www-form-urlencoded' is set.

Signed-off-by: Trevor Bramwell <[email protected]>
  • Loading branch information
bramwelt committed May 2, 2024
1 parent 4a9598b commit f9b6f51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
40 changes: 21 additions & 19 deletions pkg/middlewares/awslambda/aws_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (a *awsLambda) GetTracingInformation() (string, ext.SpanKindEnum) {
func (a *awsLambda) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
logger := log.FromContext(middlewares.GetLoggerCtx(req.Context(), a.name, typeName))

base64Encoded, contentType, body, err := bodyToBase64(req)
base64Encoded, contentType, reqBody, err := bodyToBase64(req)
if err != nil {
msg := fmt.Sprintf("Error encoding Lambda request body: %v", err)
logger.Error(msg)
Expand All @@ -149,25 +149,28 @@ func (a *awsLambda) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}

// If Content-Type is set, isn't set, assume it's JSON
rCt := req.Header.Get("Content-Type")
switch rCt {
case "":
logger.Debug("Content-Type not set")
if !strings.HasPrefix(contentType, "text") {
logger.Debugf("Content-Type not like text, setting to :%s", contentType)
req.Header.Set("Content-Type", contentType)
} else {
req.Header.Set("Content-Type", "application/json")
}
case "application/x-www-form-urlencoded":
if isJSON(rCt) {
req.Header.Set("Content-Type", "application/json")
if req.ContentLength > 0 {
rCt := req.Header.Get("Content-Type")
switch rCt {
case "":
logger.Debug("Content-Type not set")
if !strings.HasPrefix(contentType, "text") {
logger.Debugf("Content-Type not like text, setting to :%s", contentType)
req.Header.Set("Content-Type", contentType)
} else {
req.Header.Set("Content-Type", "application/json")
}
case "application/x-www-form-urlencoded":
// If sending data through cURL on the commandline and
// the content-type header is missed, orr for
// applications that aren't explicitly setting Content-Type,
// override to 'application/json' if the body looks like JSON
if isJSON(reqBody) {
req.Header.Set("Content-Type", "application/json")
}
}
default:
req.Header.Set("Content-Type", "application/json")
logger.Debugf("Content-Type set to: %s, originally %s", req.Header.Get("Content-Type"), rCt)
}
logger.Debugf("Content-Type set to: %s, originally %s", req.Header.Get("Content-Type"), rCt)

// Ensure tracing headers are included in the request before copying
// them to the lambda request
Expand All @@ -179,7 +182,6 @@ func (a *awsLambda) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
QueryStringParameters: valuesToMap(req.URL.Query()),
MultiValueQueryStringParameters: valuesToMultiMap(req.URL.Query()),
Headers: headersToMap(req.Header),
MultiValueHeaders: headersToMultiMap(req.Header),
Body: reqBody,
IsBase64Encoded: base64Encoded,
RequestContext: events.APIGatewayProxyRequestContext{
Expand Down
1 change: 0 additions & 1 deletion pkg/middlewares/awslambda/aws_lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func Test_AWSLambdaMiddleware_InvokeBasic(t *testing.T) {
assert.Equal(t, map[string]string{"a": "1", "b": "2"}, lReq.QueryStringParameters)
assert.Equal(t, map[string][]string{"c": {"3", "4"}, "d[]": {"5", "6"}}, lReq.MultiValueQueryStringParameters)
assert.Equal(t, map[string]string{"Content-Type": "application/json"}, lReq.Headers)
assert.Equal(t, map[string][]string{"X-Test": {"foo", "foobar"}}, lReq.MultiValueHeaders)
assert.Equal(t, "This is the body", lReq.Body)

res.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit f9b6f51

Please sign in to comment.