-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
urllib.request: opener not resetting content-length #60668
Comments
Code based on python-list post by a do-not-wish-to-register urllib user. import urllib.request
opener = urllib.request.build_opener()
request = urllib.request.Request("http://example.com/", headers =
{"Content-Type": "application/x-www-form-urlencoded"})
print(request.data, '\n', request.header_items())
opener.open(request, "1".encode("us-ascii"))
print(request.data, '\n', request.header_items())
opener.open(request, "123456789".encode("us-ascii"))
print(request.data, '\n', request.header_items())
>>>
None
[('Content-type', 'application/x-www-form-urlencoded')]
b'1'
[('Content-length', '1'), ('Host', 'example.com'), ('User-agent', 'Python-urllib/3.3'), ('Content-type', 'application/x-www-form-urlencoded')]
b'123456789'
[('Content-length', '1'), ('Host', 'example.com'), ('User-agent', 'Python-urllib/3.3'), ('Content-type', 'application/x-www-form-urlencoded')] The first opener.open adds data and several headers to request, including content-length. The second changes the data but not the content-length. The docs do not say anything about this either way. |
This is special case for more "general" problem. When request is executed with HTTP client and data is not None, it calculates content length and adds special header to request. Then one can change request.data attribute value, but header "Content-length" is not changed in this case. Patch is attached. I implemented request.data as property and added method "remove_header" to deal problem. Test cases are also provided. |
I'm agree with solution, see my comments in review for the patch. |
Perhaps it's a bit new behavior and should be applied to 3.4 only. |
Fixed patch is attached. |
New changeset 618ea5612e83 by Andrew Svetlov in branch 'default': |
Pushed. |
New changeset b1579eb4e1bc by R David Murray in branch 'default': |
New changeset e6d862886e5c by R David Murray in branch 'default': |
changeset: 89857:ad0c75b7bd7d changeset: 89856:68335b8afb1f |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: