HTTPretty
HTTPretty copied to clipboard
parsed_body doesn't work with py26
It appears in python 2.6 a request is sent in two pieces, the headers and then the body. This is catered for with the parse_requestline and is_parsing_headers handling in sendall()
In the event that a request is sent through all at once then the request is created with a body then the parse_request_body at the end of HTTPrettyRequest.init kicks in and correctly parses it.
When it is multiple calls the logic does:
body = utf8(self._sent_data[-1])
if meta.get('transfer-encoding', '') == 'chunked':
if not body.isdigit() and body != b'\r\n' and body != b'0\r\n\r\n':
self._entry.request.body += body
else:
self._entry.request.body += body
So the body string is extended but init is not called so the parsed_body is not retriggered.
This means that when using a callback the parsed_body field is empty even though the body is correct.
I don't understand why it's a Python 2.6-only issue. Why does it work with 2.7, 3.3 but not 2.6 ?
Also, it would be nice to have a testcase for this.
I'm not sure why it only shows up on python 2.6, i'm guessing it's something to do with the way httplib handles connections. In python2.7+ you get sent the entire request through in one socket call, in python 2.6 you get one socket send with the headers, and then another socket send with the body.
The logic for recombining the two calls into one request is a bit clunky but works, it's just that as you can see from the snippet above it simply appends the body onto the existing body - meaning that parsed_body is not recalculated.
As i said in the PR i put forward i'm not sure if it is the best approach, there might be a way to calculate parsed_body just before being sent to the handlers rather than on init that would work better. I was just trying to understand what was broken about my test suite and found this one.