Skip to content
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.urlopen raises exception when 30X-redirect url contains non-ascii chars #66444

Closed
tomasgroth mannequin opened this issue Aug 22, 2014 · 3 comments
Closed
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tomasgroth
Copy link
Mannequin

tomasgroth mannequin commented Aug 22, 2014

BPO 22248
Nosy @orsenthil, @vadmium
Superseder
  • bpo-17214: http.client.HTTPConnection.putrequest encode error
  • 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:

    assignee = None
    closed_at = <Date 2015-04-11.11:58:11.510>
    created_at = <Date 2014-08-22.08:58:03.904>
    labels = ['type-bug', 'library']
    title = 'urllib.request.urlopen raises exception when 30X-redirect url contains non-ascii chars'
    updated_at = <Date 2015-04-11.11:58:11.508>
    user = 'https://bugs.python.org/tomasgroth'

    bugs.python.org fields:

    activity = <Date 2015-04-11.11:58:11.508>
    actor = 'martin.panter'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-04-11.11:58:11.510>
    closer = 'martin.panter'
    components = ['Library (Lib)']
    creation = <Date 2014-08-22.08:58:03.904>
    creator = 'tomasgroth'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 22248
    keywords = []
    message_count = 3.0
    messages = ['225651', '225652', '240472']
    nosy_count = 3.0
    nosy_names = ['orsenthil', 'martin.panter', 'tomasgroth']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '17214'
    type = 'behavior'
    url = 'https://bugs.python.org/issue22248'
    versions = ['Python 3.4']

    @tomasgroth
    Copy link
    Mannequin Author

    tomasgroth mannequin commented Aug 22, 2014

    Running this simple test script produces the traceback show below.

    import urllib.request
    page = urllib.request.urlopen('http://legacy.biblegateway.com/versions/?vid=DN1933&action=getVersionInfo#books')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/lib/python3.4/urllib/request.py", line 461, in open
        response = meth(req, response)
      File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/lib/python3.4/urllib/request.py", line 493, in error
        result = self._call_chain(*args)
      File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
        result = func(*args)
      File "/usr/lib/python3.4/urllib/request.py", line 676, in http_error_302
        return self.parent.open(new, timeout=req.timeout)
      File "/usr/lib/python3.4/urllib/request.py", line 455, in open
        response = self._open(req, data)
      File "/usr/lib/python3.4/urllib/request.py", line 473, in _open
        '_open', req)
      File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
        result = func(*args)
      File "/usr/lib/python3.4/urllib/request.py", line 1258, in http_open
        return self.do_open(http.client.HTTPConnection, req)
      File "/usr/lib/python3.4/urllib/request.py", line 1232, in do_open
        h.request(req.get_method(), req.selector, req.data, headers)
      File "/usr/lib/python3.4/http/client.py", line 1065, in request
        self._send_request(method, url, body, headers)
      File "/usr/lib/python3.4/http/client.py", line 1093, in _send_request
        self.putrequest(method, url, **skips)
      File "/usr/lib/python3.4/http/client.py", line 957, in putrequest
        self._output(request.encode('ascii'))
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: ordinal not in range(128)

    Using curl we can see that there is a redirect to an url with a special char:
    $ curl -vs "http://legacy.biblegateway.com/versions/?vid=DN1933&action=getVersionInfo#books" >DN1933

    • Hostname was NOT found in DNS cache
    • Trying 23.23.93.211...
    • Connected to legacy.biblegateway.com (23.23.93.211) port 80 (#0)

    GET /versions/?vid=DN1933&action=getVersionInfo HTTP/1.1
    User-Agent: curl/7.35.0
    Host: legacy.biblegateway.com
    Accept: */*

    < HTTP/1.1 301 Moved Permanently

    • Server nginx/1.4.7 is not blacklisted
      < Server: nginx/1.4.7
      < Date: Fri, 22 Aug 2014 08:35:30 GMT
      < Content-Type: text/html; charset=UTF-8
      < Content-Length: 0
      < Connection: keep-alive
      < X-Powered-By: PHP/5.5.7
      < Set-Cookie: bg_id=1b9a80d5e6d545487cfd153d6df65c4e; path=/; domain=.biblegateway.com
      < Set-Cookie: a9gl=0; path=/; domain=.biblegateway.com
      < Location: http://legacy.biblegateway.com/versions/Dette-er-Biblen-på-dansk-1933/
      <
    • Connection #0 to host legacy.biblegateway.com left intact

    When the redirect-url doesn't contain special chars everything works as expected, like with this url: "http://legacy.biblegateway.com/versions/?vid=DNB1930&action=getVersionInfo#books"

    @tomasgroth tomasgroth mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 22, 2014
    @tomasgroth
    Copy link
    Mannequin Author

    tomasgroth mannequin commented Aug 22, 2014

    Small correction. Use this url for a working redirect instead of the one given at the end of the first comment:
    http://legacy.biblegateway.com/versions/?vid=ESV&action=getVersionInfo#books

    @vadmium
    Copy link
    Member

    vadmium commented Apr 11, 2015

    Same as bpo-17214

    @vadmium vadmium closed this as completed Apr 11, 2015
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant