Skip to content

Commit ae324ae

Browse files
trollfot1st1
authored andcommitted
Added flag reading method for upgrade, to allow early discovery of an upgrade request (pre-raise, in on_message_complete)
1 parent bed2404 commit ae324ae

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

httptools/parser/parser.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ cdef class HttpParser:
151151
def should_keep_alive(self):
152152
return bool(cparser.http_should_keep_alive(self._cparser))
153153

154+
def should_upgrade(self):
155+
return bool(self._cparser.upgrade)
156+
154157
def feed_data(self, data):
155158
cdef:
156159
size_t data_len

tests/test_parser.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,26 @@ def test_parser_request_upgrade_1(self):
348348
b'Host': b'example.com',
349349
b'Upgrade': b'WebSocket'})
350350

351+
def test_parser_request_upgrade_flag(self):
352+
m = mock.Mock()
353+
p = httptools.HttpRequestParser(m)
354+
355+
def on_headers_complete():
356+
self.assertEqual(p.should_upgrade(), False)
357+
358+
def on_message_complete():
359+
self.assertEqual(p.should_upgrade(), True)
360+
361+
m.on_headers_complete = on_headers_complete
362+
m.on_message_complete = on_message_complete
363+
364+
try:
365+
p.feed_data(UPGRADE_REQUEST1)
366+
except httptools.HttpParserUpgrade as ex:
367+
offset = ex.args[0]
368+
else:
369+
self.fail('HttpParserUpgrade was not raised')
370+
351371
def test_parser_request_error_in_on_header(self):
352372
class Error(Exception):
353373
pass

0 commit comments

Comments
 (0)