[issue29891] urllib.request.Request accepts but doesn't check bytes headers

2017-03-24 Thread Martin Panter

Martin Panter added the comment:

If you enable BytesWarning (python -b) you do get an error:

>>> urlopen(req, data)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.5/urllib/request.py", line 162, in urlopen
return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 463, in open
req = meth(req)
  File "/usr/lib/python3.5/urllib/request.py", line 1171, in do_request_
if not request.has_header('Content-type'):
  File "/usr/lib/python3.5/urllib/request.py", line 356, in has_header
return (header_name in self.headers or
BytesWarning: Comparison between bytes and string

I believe the “urllib.request” module is only written with text (str) field 
names in mind, not byte strings. Same for 
http.client.HTTPConnection.request(headers=...). But the lower-level 
HTTPConnection.putheader method has special code to handle byte strings: 
, although this is not 
documented either.

--
nosy: +martin.panter

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29891] urllib.request.Request accepts but doesn't check bytes headers

2017-03-23 Thread Maciej Szulik

Changes by Maciej Szulik :


--
nosy: +maciej.szulik

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29891] urllib.request.Request accepts but doesn't check bytes headers

2017-03-23 Thread Ezio Melotti

New submission from Ezio Melotti:

urllib.request.Request allows the user to create a request object like:
  req = Request(url, headers={b'Content-Type': b'application/json'})

When calling urlopen(req, data), urllib will check if a 'Content-Type' header 
is present and fail to recognize b'Content-Type' because it's bytes.
urrlib will therefore add the default Content-Type 
'application/x-www-form-urlencoded', and the request will then be sent with 
both Content-Types.  This will result in difficult-to-debug errors because the 
server will sometimes pick one and sometimes the other, depending on the order.

urllib should either reject bytes headers, or check for both bytes and strings. 
 The docs also don't seem to specify that the headers should be strings.

--
components: Library (Lib)
messages: 290063
nosy: ezio.melotti, orsenthil
priority: normal
severity: normal
stage: test needed
status: open
title: urllib.request.Request accepts but doesn't check bytes headers
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com