webob.Response.from_file parsing failure #250

Closed
rtilder opened this Issue May 19, 2016 · 3 comments

Projects

None yet

3 participants

@rtilder
rtilder commented May 19, 2016

webob.Request's from_file method seems have to an incorrect call to str.split()

    import io, webob
    s = "HTTP/1.0 200 Ok\nConnection: close\n\n"
    webob.Request.from_file(io.BytesIO(s))  # success
    s = "HTTP/1.0 404 Not Found\nConnection: close\n\n"
    webob.Request.from_file(io.BytesIO(s))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/rtilder/sandbox/horked_from_file/lib/python2.7/site-packages/webob/response.py", line 179, in from_file
        (http_ver, status_num, status_text) = status.split()
    ValueError: too many values to unpack

Relevant code is here. The existing code works just fine on a 200 response but not on the faux 404

(http_ver, status_num, status_text) = status.split()

I suspect that this is inadvertent and that the following was probably intended.

(http_ver, status_num, status_text) = status.split(None, 2)

I was a little surprised I ran into this until I realized that parsing HTTP responses is probably not a terribly common use case.

@mmerickel
Member

You're parsing responses and linked to webob.response code but your code example uses Request.from_file. Is that actually correct?

@bertjwregeer bertjwregeer added a commit that closed this issue May 20, 2016
@bertjwregeer bertjwregeer Split on first two whitespaces in Response.from_file
This fixes #250 by not attempting to split the entire string. This is
required for any status codes that are not just <number> <one word
status text>

Things like 404 Not Found or 402 Payment Required.
17e170d
@bertjwregeer
Member

Request.from_file is doing the right thing: https://github.com/Pylons/webob/blob/master/webob/request.py#L1208

I've fixe this issue in 17e170d and added a test in 9aae43c so this can't regress.

Thanks for reporting. I'll back port this to 1.6 this weekend.

@bertjwregeer bertjwregeer added this to the 1.7.0 milestone May 20, 2016
@rtilder
rtilder commented May 20, 2016

Sorry about the mispasted code. webob.Request.from_file parsing correctly, yes. webob.Response.from_file fails. Thanks for the quick fix.

@bertjwregeer bertjwregeer changed the title from webob.Request.from_file parsing failure to webob.Response.from_file parsing failure May 20, 2016
@bertjwregeer bertjwregeer added a commit that referenced this issue May 20, 2016
@bertjwregeer bertjwregeer Split on first two whitespaces in Response.from_file
This fixes #250 by not attempting to split the entire string. This is
required for any status codes that are not just <number> <one word
status text>

Things like 404 Not Found or 402 Payment Required.
e099068
@bertjwregeer bertjwregeer added a commit that referenced this issue May 20, 2016
@bertjwregeer bertjwregeer Update CHANGES for #250 e638dc5
@bertjwregeer bertjwregeer added a commit that referenced this issue May 20, 2016
@bertjwregeer bertjwregeer Add CHANGES.txt for #250 29cef56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment