<div dir="ltr"><div>Hi Python devs,</div><div><br></div><div>I am trying to download an HTML document. I get an HTTP 301 (Moved Permanently) with a UTF-8 encoded Location header and http.client decodes it as iso-8859-1. When there's a non-ASCII character in the redirect URL then I can't download the document.</div>
<div><br></div><div>In client.py def parse_headers() I see the call to decode('iso-8859-1'). My personal  hack is to use whatever charset is defined in the Content-Type HTTP header (utf8) or fall back into iso-8859-1. </div>
<div><br></div><div>At this point I am not sure where/how a fix should occur  so I thought I'd run it by you in case I should file a bug. Note that I don't use http.client directly, but through the python-requests library.</div>
<div><br></div><div>I include some code to reproduce the problem below.</div><div><br></div><div>Cheers,</div><div><br></div><div>Hugo</div><div><br></div><div>-----</div><div><br></div><div>#!/usr/bin/env python3</div><div>
<br></div><div># Trying to replicate what wget does with a 301 redirect:</div><div># wget --server-response <a href="http://www.starbucks.com/store/158/AT/Karntnerstrasse/K%c3%a4rntnerstrasse-49-Vienna-9-1010">www.starbucks.com/store/158/AT/Karntnerstrasse/K%c3%a4rntnerstrasse-49-Vienna-9-1010</a></div>
<div><br></div><div>import http.client</div><div>import urllib.parse</div><div><br></div><div>s2='/store/158/AT/Karntnerstrasse/K%c3%a4rntnerstrasse-49-Vienna-9-1010'</div><div>s3='<a href="http://www.starbucks.com/store/158/at/karntnerstrasse/k%C3%A4rntnerstrasse-49-vienna-9-1010">http://www.starbucks.com/store/158/at/karntnerstrasse/k%C3%A4rntnerstrasse-49-vienna-9-1010</a>'</div>
<div><br></div><div>conn = http.client.HTTPConnection('<a href="http://www.starbucks.com">www.starbucks.com</a>')</div><div>conn.request('GET', s2)</div><div>r = conn.getresponse()</div><div>print('Location', r.headers.get('Location'))</div>
<div>print('Expected', urllib.parse.unquote(s3))</div><div>assert r.status == 301</div><div>assert r.headers.get('Location') == urllib.parse.unquote(s3), \</div><div>    'decoded as iso-8859-1 instead of utf8'</div>
<div><br></div><div>conn = http.client.HTTPConnection('<a href="http://www.starbucks.com">www.starbucks.com</a>')</div><div>conn.request('GET', s3)</div><div>r = conn.getresponse()</div><div>assert r.status == 200</div>
<div><br></div></div>