Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Length is not being proxied #129

Closed
montenegrodr opened this issue Apr 24, 2018 · 6 comments
Closed

Content-Length is not being proxied #129

montenegrodr opened this issue Apr 24, 2018 · 6 comments
Labels

Comments

@montenegrodr
Copy link

montenegrodr commented Apr 24, 2018

Running the simplest example copied from Library Usage - the same exact code. With this configuration.json:

{
  "version": 2,
  "name": "My lovely gateway",
  "port": 8080,
  "host": [
    "http://127.0.0.1:8080"
  ],
  "endpoints": [
    {
      "endpoint": "/",
      "method": "POST",
      "backend": [
        {
          "host": [
            "http://127.0.0.1:8000"
          ],
          "encoding": "json"
        }
      ],
      "headers_to_pass": [
        "Content-Length",
        "Content-Type",
        "Accept",
        "Host",
        "User-Agent"
      ]
    }
  ]
}

I am not getting Content-Length on my CherryPy service. It's possible to reproduce with this request:

curl --fail -X POST -H 'Content-Type: application/json' -H 'Content-Length: 22' -H 'Accept: json' -d '{"text": "0000000000"}' 'http://localhost:8080' 

All other headers and content are proxied correctly. The problem is only with Content-Length.

@montenegrodr montenegrodr changed the title Content-Length is not being proxied Content-Length is not being proxied Apr 24, 2018
@kpacha
Copy link
Member

kpacha commented Apr 24, 2018

hi @montenegrodr , welcome to the KrakenD community.

The behaviour is related with the net/http package:

For client requests, certain headers such as Content-Length and Connection are automatically written when needed and values in Header may be ignored.

https://golang.org/src/net/http/request.go?s=#L157

All the layers in the pipe do propagate all the defined headers, but the internal newTransferWriter function of the net/http package assigns the value t.ContentLength = rr.outgoingLength() and does not propagate the received Content-Length header.

Following the white rabbit into the hole, I found the NewHTTPProxyDetailed middleware is not defining the requestToBackend.ContentLength property because it always sends chunked requests.

Without reading the request body before forwarding it, the only way I can see to allow the propagation of the Content-Length header is to include some 'nasty hack', such as parsing the received header and if it contains an integer, setting the requestToBackend.ContentLength property... allowing some other weird situations if the clients do not send the proper value for the header.

Any ideas?

@montenegrodr
Copy link
Author

Thanks for your prompt reply @kpacha.

Could you point me to an example showing how can I intercept the request and modify the header before forward it?

@kpacha
Copy link
Member

kpacha commented Apr 24, 2018

here is an example of my dumb proposal: b9093c0

reading the actual request.Body would require some extra calculations and allocations, but the required code would be placed there too

@montenegrodr
Copy link
Author

montenegrodr commented Apr 26, 2018

In the end I decided to change my cherryPy service. Thanks anyway for the effort here. Leaving the issue open to you to decided to close or not.

@kpacha
Copy link
Member

kpacha commented Apr 2, 2019

fixed, thanks!

@kpacha kpacha closed this as completed Apr 2, 2019
@github-actions
Copy link

github-actions bot commented Apr 9, 2022

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.

@github-actions github-actions bot added the locked label Apr 9, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants