Update WS protocol to have ASGI format

This commit is contained in:
Andrew Godwin 2016-02-05 17:33:02 -08:00
parent f85be12408
commit b86c8a0164
2 changed files with 17 additions and 16 deletions

View File

@ -36,6 +36,17 @@ class WebRequest(http.Request):
upgrade_header = None upgrade_header = None
if self.requestHeaders.hasHeader("Upgrade"): if self.requestHeaders.hasHeader("Upgrade"):
upgrade_header = self.requestHeaders.getRawHeaders("Upgrade")[0] upgrade_header = self.requestHeaders.getRawHeaders("Upgrade")[0]
# Calculate query string
self.query_string = ""
if b"?" in self.uri:
self.query_string = self.uri.split(b"?", 1)[1]
# Sanitize headers
self.headers = {}
for name, value in self.requestHeaders.getAllRawHeaders():
# Prevent CVE-2015-0219
if b"_" in name:
continue
self.headers[name.lower().decode("latin1")] = value[0]
# Is it WebSocket? IS IT?! # Is it WebSocket? IS IT?!
if upgrade_header == "websocket": if upgrade_header == "websocket":
# Make WebSocket protocol to hand off to # Make WebSocket protocol to hand off to
@ -68,17 +79,6 @@ class WebRequest(http.Request):
else: else:
logging.debug("HTTP %s request for %s", self.method, self.reply_channel) logging.debug("HTTP %s request for %s", self.method, self.reply_channel)
self.content.seek(0, 0) self.content.seek(0, 0)
# Calculate query string
query_string = ""
if b"?" in self.uri:
query_string = self.uri.split(b"?", 1)[1]
# Sanitize headers
headers = {}
for name, value in self.requestHeaders.getAllRawHeaders():
# Prevent CVE-2015-0219
if b"_" in name:
continue
headers[name.lower().decode("latin1")] = value[0]
# Send message # Send message
self.factory.channel_layer.send("http.request", { self.factory.channel_layer.send("http.request", {
"reply_channel": self.reply_channel, "reply_channel": self.reply_channel,
@ -87,8 +87,8 @@ class WebRequest(http.Request):
"method": self.method.decode("ascii"), "method": self.method.decode("ascii"),
"path": self.path, "path": self.path,
"scheme": "http", "scheme": "http",
"query_string": query_string, "query_string": self.query_string,
"headers": headers, "headers": self.headers,
"body": self.content.read(), "body": self.content.read(),
"client": [self.client.host, self.client.port], "client": [self.client.host, self.client.port],
"server": [self.host.host, self.host.port], "server": [self.host.host, self.host.port],

View File

@ -4,7 +4,6 @@ import time
import logging import logging
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from django.http import parse_cookie
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -22,8 +21,10 @@ class WebSocketProtocol(WebSocketServerProtocol):
def onConnect(self, request): def onConnect(self, request):
self.request_info = { self.request_info = {
"path": request.path, "path": request.path,
"get": request.params, "headers": self.headers,
"cookies": parse_cookie(request.headers.get('cookie', '')) "query_string": request.query_string,
"client": [request.client.host, request.client.port],
"server": [request.host.host, request.host.port],
} }
def onOpen(self): def onOpen(self):