Skip to content

Commit d6ce4b5

Browse files
committed
Merge branch 'lsbardel-lsbardel'
2 parents 7fd29f2 + fa87503 commit d6ce4b5

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

http_parser/pyparser.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def execute(self, data, length):
224224
try:
225225
to_parse = b("").join(self._buf)
226226
ret = self._parse_headers(to_parse)
227-
if type(ret) is bool and not ret:
227+
228+
if ret is False:
228229
return length
229230
nb_parsed = nb_parsed + (len(to_parse) - ret)
230231
except InvalidHeader as e:
@@ -324,6 +325,10 @@ def _parse_request_line(self, line):
324325
"SERVER_PROTOCOL": bits[2]})
325326

326327
def _parse_headers(self, data):
328+
if data == b'\r\n':
329+
self.__on_headers_complete = 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"):
@@ -407,6 +412,11 @@ def _parse_body(self):
407412
return
408413
elif not self._chunked:
409414
body_part = b("").join(self._buf)
415+
#
416+
if not body_part and self._clen is None:
417+
if not self._status: # message complete only for servers
418+
self.__on_message_complete = True
419+
return
410420
self._clen_rest -= len(body_part)
411421

412422
# maybe decompress

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_client_no_headers():
29+
_test_no_headers(HttpParser())
30+
31+
def test_client_no_headers_py():
32+
_test_no_headers(PyHttpParser())
33+
34+
def test_client_headers():
35+
_test_headers(HttpParser())
36+
37+
def test_client_headers_py():
38+
_test_headers(PyHttpParser())
39+

testing/test_server.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
assert not parser.is_headers_complete()
10+
data = b'GET /forum/bla?page=1#post1 HTTP/1.1\r\n\r\n'
11+
assert parser.execute(data, len(data)), len(data)
12+
assert parser.get_fragment(), 'post1'
13+
assert parser.is_message_begin()
14+
assert parser.is_headers_complete()
15+
assert parser.is_message_complete()
16+
assert not parser.get_headers()
17+
18+
def test_server_no_headers():
19+
_test_no_headers(HttpParser())
20+
21+
def test_server_no_headers_py():
22+
_test_no_headers(PyHttpParser())

0 commit comments

Comments
 (0)