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

URL parsing is broken #4

Closed
wolever opened this issue Jun 19, 2011 · 4 comments
Closed

URL parsing is broken #4

wolever opened this issue Jun 19, 2011 · 4 comments

Comments

@wolever
Copy link

wolever commented Jun 19, 2011

With Python 2.5 and 2.6, url parsing is broken because urlparse doesn't know how to interpret the ws scheme. Here's a fixed version that works correctly:

def _parse_url(url):
    """
    parse url and the result is tuple of 
    (hostname, port, resource path and the flag of secure mode)
    """
    if ":" not in url:
        raise ValueError("url is invalid")

    scheme, url = url.split(":", 1)
    url = url.rstrip("/")

    parsed = urlparse(url, scheme="http")
    if parsed.hostname:
        hostname = parsed.hostname
    else:
        raise ValueError("hostname is invalid")
    port = 0
    if parsed.port:
        port = parsed.port

    is_secure = False
    if scheme == "ws":
        if not port:
            port = 80
    elif scheme == "wss":
        is_secure = True
        if not port:
            port  = 443
    else:
        raise ValueError("scheme %s is invalid" % scheme)

    if parsed.path:
        resource = parsed.path
    else:
        resource = "/"

    return (hostname, port, resource, is_secure)
@liris
Copy link
Collaborator

liris commented Jun 19, 2011

It sounds strange. My python 2.6.1 and 2.5.4 on Mac woks fine.
urlparse method doesn't care of scheme type.

Please tell me the traceback or exception detail.

@wolever
Copy link
Author

wolever commented Jun 19, 2011

Wow, that is strange. Neither the 2.6.1 or 2.5.4 on my Mac work.

To recreate:

$ python --version
Python 2.6.1
$ cd /tmp/liris-websocket-client-673470f/
$ cp websocket.py examples/
$ cd examples/
$ python echo_client.py 
/private/tmp/liris-websocket-client-673470f/examples/websocket.py:27: DeprecationWarning: the md5 module is deprecated; use hashlib instead
  import md5
Traceback (most recent call last):
  File "echo_client.py", line 5, in 
    ws = websocket.create_connection("ws://localhost:5000/chat")
  File "/private/tmp/liris-websocket-client-673470f/examples/websocket.py", line 110, in create_connection
    websock.connect(url, **options)
  File "/private/tmp/liris-websocket-client-673470f/examples/websocket.py", line 205, in connect
    hostname, port, resource, is_secure = _parse_url(url)
  File "/private/tmp/liris-websocket-client-673470f/examples/websocket.py", line 76, in _parse_url
    raise ValueError("hostname is invalid")
ValueError: hostname is invalid

The behaviour with 2.5 is identical. The distribution is from https://github.com/liris/websocket-client/zipball/v0.4.1 .

@liris
Copy link
Collaborator

liris commented Jun 19, 2011

Oh, I see the problem. The parsed hostname is broken.

@liris liris closed this as completed in 912f43d Jun 20, 2011
@brandonmoser
Copy link
Contributor

This issue was reintroduced in a recent version. Changing the Scheme back to HTTP when parsing the URL allows the library to work on 2.6.1 (I haven't tried 2.5.x).

engn33r added a commit that referenced this issue Jan 26, 2021
Capitalize default connection header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants