Skip to content

Commit 47e9970

Browse files
quantmindbenoitc
authored andcommitted
Python parser to behave like c parser when no headers in message
1 parent 7fd29f2 commit 47e9970

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

http_parser/pyparser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ def _parse_request_line(self, line):
324324
"SERVER_PROTOCOL": bits[2]})
325325

326326
def _parse_headers(self, data):
327+
if data == b'\r\n':
328+
self.__on_headers_complete = True
329+
self.__on_message_begin = True
330+
self._buf = []
331+
return 0
327332
idx = data.find(b("\r\n\r\n"))
328333
if idx < 0: # we don't have all headers
329334
if self._status_code == 204 and data == b("\r\n"):

testing/test_client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
import io
3+
4+
from http_parser.http import HttpStream
5+
from http_parser.parser import HttpParser
6+
from http_parser.pyparser import HttpParser as PyHttpParser
7+
8+
def _test_no_headers(parser):
9+
data = b'HTTP/1.1 200 Connection established\r\n\r\n'
10+
assert parser.execute(data, len(data)) == len(data)
11+
assert parser.is_headers_complete()
12+
assert parser.is_message_begin()
13+
assert not parser.is_partial_body()
14+
assert not parser.is_message_complete()
15+
16+
def _test_headers(parser):
17+
data = (b'HTTP/1.1 200 OK\r\n'
18+
b'Connection: Keep-Alive\r\n'
19+
b'Content-Length: 4\r\n'
20+
b'Content-type: text/plain\r\n\r\n'
21+
b'ciao')
22+
assert parser.execute(data, len(data)) == len(data)
23+
assert parser.is_headers_complete()
24+
assert parser.is_message_begin()
25+
assert parser.is_partial_body()
26+
assert parser.is_message_complete()
27+
28+
def test_no_headers():
29+
_test_no_headers(HttpParser())
30+
31+
def test_no_headers_py():
32+
_test_no_headers(PyHttpParser())
33+
34+
def test_headers():
35+
_test_headers(HttpParser())
36+
37+
def test_headers_py():
38+
_test_headers(PyHttpParser())
39+

0 commit comments

Comments
 (0)