bad content-length default
In werkzeug's WSGIRequestHandler, CONTENT-LENGTH defaults to an empty string ('') if the client did not provide a Content-Length header.
This is causing me problems in my flask web service, which is trying to proxy requests. The user creates a request without Content-Length, flask gives it to me and then I pass it along to another server, which then returns HTTP 400 due to the bad Content-Length that werkzeug has inserted. According to RFC 2616, Content-Length must be a number.
Initially I thought this should be considered a werkzeug bug but maybe the empty string was intended to be werkzeug's signifier for a non-existent Content-Length, rather than an actual Content-Length value that could be used directly. If that's the case, then I would modify flask so that Content-Length isn't included in the incoming request's headers if it is the empty string.
Bottom line, I believe that a client request that is missing a Content-Length header should not result in an incoming flask request with the Content-Length equal to the empty string.
I don't think the WSGI environment Werkzeug generates is wrong... according to https://www.python.org/dev/peps/pep-0333/#environ-variables, the empty string and the nonexistent value should be treated symonymously:
The following variables must be present, unless their value would be an empty string, in which case they may be omitted, except as otherwise noted below.
[...] May be empty or absent.
So either behavior is fine, I myself don't see a reason why we couldn't change Werkzeug's behavior to omit Content-Type (and also Content-Length) if they're == ''.
The beginner friendly tag might be too generous, but I'm trying to tag some stuff besides documentation and this seems like a reasonably self contained fix. That is, if we can get to a consensus on whether we actually want to change Werkzeug's behavior as you suggested @untitaker.
Is it the way you are creating the response? I have tested it by generating a response header without a Content-Length and then running it through flask which routes it through nginx. (Not sure exactly when this occurs naturally in Flask)
Result: When you throw the response just in flask alone, it works fine (follows PEP-333). But when you try to send it through nginx it'll crash when reading the response because it does not like what is passed, the empty content-length (follows RFC 2616). For improved compatibility with other applications, I agree with @untitaker that Flask omit empty Content-Type and or Content-Length.
We should let this @mitsuhiko decide... maybe Werkzeug's current behavior is also due to other software's behavior.
And what did he decide? I'd be glad to start contributing with some code.
We did not yet contact him.
I'm glad that you want to help @omarkhd -- feel free to ping mitsuhiko on the #pocoo IRC channel!
For anyone seeking a quick fix solution to this error, take a look at the Proposed Solution section under Issue 1289. Link to Proposed Solution
I'm fine with the proposed solution, but please let @mitsuhiko confirm it, on the unlikely off-chance that this behavior is actually intended.
@KWinston can I take this solution? Its the better way to solve the problem?
@matheuspoleza yes, you can use the solution I provided. The proposed solution should stop your application from crashing if you are having issues with CONTENT-LENGTH not being a number.
@KWinston ok, I'll do that
@mitsuhiko : Is the proposed solution ok to implement? @KWinston : May I use your solution?
I do not understand the proposed solution. Where do we generate an empty content length string?
In werkzeug's WSGIRequestHandler, CONTENT-LENGTH defaults to an empty string ('') if the client did not provide a Content-Length header.
When used with other applications that default to an empty string or a non-number passed through flask. It is more a compatibility issue with other apps.
Probably more of a documentation addition to incorporate this when using other apps while running flask?
I'm not sure I follow. Surely an empty string is a valid value for CONTENT_LENGTH so should this not be reported against whatever application fails?
Can someone make a minimal test case of what is wrong and what is expected? I'm at a loss here. WSGIRequestHandler handles incoming data only. So that should be irrelevant here, no?
@mitsuhiko according to my reading of rfc2616, you can either (1) have a content-length header, in which case it must be a number, or 2) if the header is not provided, it explains how to compute it. The situation here has the client does not provide the header ... but then flask/werkzeug artificially creates a header that is the empty string, which is not legal according to rfc 2616. I am traveling this weekend but can make a test case next week.
The HTTP RFC is irrelevant here. An empty WSGI environ value is to be treated as nonexistent according to the WSGI PEP.
However, if you find an abstraction over the WSGI env (within Flask/Werkzeug or outside) whose behavior differs when given an empty string instead of a nonexistent value, it's totally a bug.
@reteptilian as @untitaker mentioned the rfc does not matter here because this is an internal header on the incoming side and we hide it. This really needs a clear testcase.