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

Add websocket subprotocol support #150

Merged
merged 3 commits into from
Oct 9, 2014
Merged

Conversation

flying-sheep
Copy link
Contributor

Rationale:

We have to manually do this currently even while it should be part of the handshake.

This leads to ignorance of this feature, and chrome not working due to a missing header when testing only in firefox (which ignores if none of sent subprotocols come back)

Implementation and usage:

I’m adding a kwarg to do_handshake which allows the user to specifiy subprotocols his/her client speaks. do_handshake returns an additional header with the chosen protocol if the handshake is successful. This doesn’t break the API in any way.

If the server speaks any of the supplied subprotocols, the first protocol spoken by both is selected from the the client-supplied list, else the handshake fails. Essentially this means that you order the protocols sequence by preference: best protocol first, worst last.

Open Questions:

  1. The agreed-upon protocol is valuable information not only for the Server, but the user: If we speak multiple protocols, we want to know which one the handshake selected. The user has to extract it from the headers, which are simply a sequence and no dict, making this impractical.

    Should we change the response tuple to include the protocol as fifth mamber? This would break unpacking assignments like this:

    >>> code, response_headers, parser, writer = do_handshake('get', headers, transport)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: too many values to unpack (expected 4)

    We could also return something unpackable to 4 elemets that also has a “protocol” field, but that would make the code more complex (tuples are very useful to avoid creating Java-style result-classes).

  2. Autobahn|Python forbids duplicate mentions of protocols in the comma-separated list sent by the server. Should we also fail the handshake when this happens or be more lenient?

@fafhrd91
Copy link
Member

PR looks good.

i don't think anybody uses aiohttp websocket implementation, so i think it's fine if we break api.
could you just add some tests for new code?
also please add yourself to contributors list.

thanks!

## Rationale:

We have to manually do this currently even while it should be part of the handshake.

This leads to ignorance of this feature, and chrome not working due to a missing header when testing only in firefox (which ignores if none of sent subprotocols come back)

## Implementation and usage:

I’m adding a kwarg to `do_handshake` which allows the user to specifiy subprotocols his/her client speaks. `do_handshake` returns an additional header with the chosen protocol if the handshake is successful. This doesn’t break the API in any way.

If the server speaks any of the supplied subprotocols, the first protocol spoken by both is selected from the the client-supplied list, else the handshake fails. Essentially this means that you order the `protocols` sequence by preference: best protocol first, worst last.

## Open Questions:

1. The agreed-upon protocol is valuable information not only for the Server, but the user: If we speak multiple protocols, we want to know which one the handshake selected. The user has to extract it from the headers, which are simply a sequence and no dict, making this impractical.

    Should we change the response tuple to include the protocol as fifth mamber? This would break unpacking assignments like this:

        ```python
        >>> code, response_headers, parser, writer = do_handshake('get', headers, transport)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        ValueError: too many values to unpack (expected 4)
        ```

        We could also return something unpackable to 4 elemets that also has a “protocol” field, but that would make the code more complex (tuples are very useful to avoid creating Java-style result-classes).

2. Autobahn|Python forbids duplicate mentions of protocols in the comma-separated list sent by the server. Should we also fail the handshake when this happens or be more lenient?
@flying-sheep
Copy link
Contributor Author

old API is ded, long live the new API :)

tests pass, and i’m in the contributor list (Phil Schaf is not my real name, therefore the “A.”)

@kxepal
Copy link
Member

kxepal commented Sep 29, 2014

Looks good for me. Is this ready for merge?

@flying-sheep
Copy link
Contributor Author

you tell me. if the tests aren’t sufficient, i can add more, and if they are, well, you see the travis build passed.

protocol = proto
break
else:
raise errors.HttpBadRequest(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpBadRequest case is not test-covered as far as I see

fafhrd91 added a commit that referenced this pull request Oct 9, 2014
Add websocket subprotocol support
@fafhrd91 fafhrd91 merged commit d2286ff into aio-libs:master Oct 9, 2014
@flying-sheep flying-sheep deleted the patch-1 branch October 9, 2014 19:18
@lock
Copy link

lock bot commented Oct 30, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 30, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants