awslabs / aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.
Apache License 2.0
1.04k stars 197 forks source link

fiber adapter cannot pass some specific 'header' to fiber app. #88

Open drakejin opened 3 years ago

drakejin commented 3 years ago

Thanks gofiber team.

Sorry for my poor English. This issue about PR #69

Bug

My HTTP client(Chrome browser) transfers HTTP packets with a 'Content-Type' header to the AWS API gateway. But, My fiber application cannot read a 'Content-Type' header when using a lambda fiber adapter.

test code

below code is part of this link

       app.Get("/ping", func(c *fiber.Ctx) error {
        fmt.Println("Step 7------", string(c.Request().Header.Header())) // You can see "application/json" at this codes .
        // but you cannot find "Content-Type" using below method.
        // c.Get(fiber.HeaderContentType, "") print out "" empty string.
        Expect(fiber.MIMEApplicationJSON).To(Equal(c.Get(fiber.HeaderContentType, "")))
        Expect("drakejin").To(Equal(c.Get("MyNameIs", "")))

        log.Println("Handler!!")
        return c.SendString("pong")
    })

    adapter := fiberadaptor.New(app)

    req := events.APIGatewayProxyRequest{
        Path:       "/ping",
        HTTPMethod: "GET",
        Headers: map[string]string{
            "Content-Type": fiber.MIMEApplicationJSON,
            "MyNameIs":     "drakejin",
        },
    }

Plz, Check these codes fiberlambda_test.go, adapter.go. It explains what happened to lambda fiber adapter codes. ). It explains what happened to lambda fiber adapter codes.

Root cause

I think this root cause is below this line. It didn't consider about fasttext's header. fasttext/~~/header.go

    for key, val := range r.Header {
        for _, v := range val {
            req.Header.Add(key, v) // <===== this line
        }
    }

I don't have any idea to fix this problem. This below code is my best. sorry, It didn't help

    for key, val := range r.Header {
        for _, v := range val {
            req.Header.Add(key, v)
            if key == fiber.HeaderContentType {
                   req.Header.SetContentType(v) // this solution is wack but, It's my best. (i don't know fasttext 
            }
        }
    }

help @Fenny @kiyonlin

drakejin commented 3 years ago

Solution1. Fix fasttext.

https://github.com/valyala/fasthttp/blob/master/header.go#L1123-1126

func (h *RequestHeader) Add(key, value string) {
    k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
    h.h = appendArg(h.h, b2s(k), value, argsHasValue)
    switch(key) {
       case fiber.HeaderBlah:
          h.SetContentType(value)
    }
    ....
}

Solution2. Fix fiber adapter

https://github.com/drakejin/aws-lambda-go-api-proxy/blob/master/fiber/adapter.go#L88-L110

    for key, val := range r.Header {
        for _, v := range val {
            req.Header.Add(key, v)
            if key == fiber.HeaderContentType {
                   req.Header.SetContentType(v) // this solution is wack but, It's my best. (i don't know fasttext 
            }
        }
    }

Um....................... I've 2 solutions but, both are not good.

drakejin commented 3 years ago

Hm... I'll make a PR

ThalysonR commented 2 years ago

Just spent a few hours on this same error, came to the same conclusion after debugging please merge this