Hi,

  I am trying to add WebSocket `permessage-deflate` extension support to my 
django channels consumer. As I know if a WebSocket server supports an 
extension (which was provided in the request headers), the server has to 
include the extension headers in the handshake response. Which I was not 
able to do.

  The default accept() method implementation of AsyncWebsocketConsumer does 
not have headers parameter: 
https://github.com/django/channels/blob/master/channels/generic/websocket.py#L184
  However, the asgiref specification states that websocket.accept message 
can have "headers" section: 
https://asgi.readthedocs.io/en/latest/specs/www.html#accept-send-event
  So when a client initiates WebSocket connection with 
"Sec-WebSocket-Extensions: permessage-deflate" headers, I use this code to 
accept the connection:

class MyConsumer(AsyncWebsocketConsumer):
      async def connect(self):
await self.base_send({
    "type": "websocket.accept",
    "subprotocol": None,
    "headers": [
        (b'sec-websocket-extensions', b'permessage-deflate')
    ]
})

  Connection gets upgraded to WS protocol, however the client does not 
receive the Sec-Websocket-Extensions headers and does not expect messages 
to be compressed. For example I am providing request/response headers.
  Request headers:
GET ws://localhost:8080/ws/my_route/ HTTP/1.1
Host: localhost:8080
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/83.0.4103.97 Safari/537.36
Upgrade: websocket
Origin: http://localhost:8200
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Sec-WebSocket-Key: XXXXXXXXXXXXXXXXXXXXXX==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

  Response headers:
HTTP/1.1 101 Switching Protocols
Server: Daphne
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: XXXXXXXXXXXXXXXXXXXXXXXXXXX=

  This behaviour is observed when using manage.py runserver on RedHat7 
linux and newest Chrome/Firefox browsers. Main package from my pip freeze 
are:
aiohttp==3.6.2
aioredis==1.2.0
asgiref==3.2.9
async-timeout==3.0.1
asyncio==3.4.3
billiard==3.6.0.0
channels==2.4.0
channels-redis==2.4.0
daphne==2.3.0
Django==2.2
django-cors-headers==2.3.0
django-crequest==2018.5.11
gunicorn==19.9.0
pika==0.12.0
redis==3.2.1
tornado==5.1
Twisted==18.9.0
txaio==18.8.1
urllib3==1.24.1

  I am not sure whether its a bug, or am I doing something wrong?

Best regards,
Albertas
  

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8e346f91-ed71-4ce0-97bb-d83a72f4409fo%40googlegroups.com.

Reply via email to