From 65f04bf4d103ea7ab9307946ccc11a17de51d93f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 22 Apr 2015 09:31:22 +1200 Subject: [PATCH] First-pass websocket client handshake --- libpathod/language.py | 11 +++++++++-- test/test_language.py | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/libpathod/language.py b/libpathod/language.py index b7b95ed8f..29d2ade89 100644 --- a/libpathod/language.py +++ b/libpathod/language.py @@ -7,7 +7,7 @@ import time import copy import abc import contrib.pyparsing as pp -from netlib import http_status, tcp, http_uastrings +from netlib import http_status, tcp, http_uastrings, websockets import utils @@ -1006,6 +1006,13 @@ class Request(_Message): def resolve(self, settings, **kwargs): tokens = self.tokens[:] + if self.method.string().lower() == "ws": + tokens[0] = Method("get") + for i in websockets.client_handshake_headers().lst: + if not utils.get_header(i[0], self.headers): + tokens.append( + Header(ValueLiteral(i[0]), ValueLiteral(i[1])) + ) if not self.raw: if not utils.get_header("Content-Length", self.headers): if self.body: @@ -1063,7 +1070,7 @@ class WebsocketFrame(_Message): atom = pp.MatchFirst(parts) resp = pp.And( [ - pp.Literal("ws"), + pp.Literal("wf"), Sep, pp.ZeroOrMore(Sep + atom) ] diff --git a/test/test_language.py b/test/test_language.py index cd7f703e6..4dd3d8acc 100644 --- a/test/test_language.py +++ b/test/test_language.py @@ -600,6 +600,11 @@ class TestRequest: r = parse_request("GET:@100").freeze({}) assert len(r.spec()) > 100 + def test_websocket(self): + r = parse_request('ws:"/foo"') + res = r.resolve({}) + assert utils.get_header("upgrade", res.headers) + class TestWriteValues: def test_send_chunk(self):