Skip to content

Commit

Permalink
Fix for handling request without headers (#238)
Browse files Browse the repository at this point in the history
- None of the HTTP headers are required in a HTTP/1.0 request.
- Circuits couldn't receive a request which doesn't have any headers
- For example, "GET / HTTP/1.0\r\n\r\n"
- This commit fixed this issue
  • Loading branch information
kwangswei authored and prologic committed Apr 19, 2018
1 parent cdd4f1a commit ecfbe00
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
14 changes: 12 additions & 2 deletions circuits/web/parsers/http.py
Expand Up @@ -298,9 +298,19 @@ def _parse_request_line(self, line):
"SERVER_PROTOCOL": bits[2]})

def _parse_headers(self, data):
idx = data.find(b("\r\n\r\n"))
if self._version == (1, 0):
idx = data.find(b("\r\n"))
if idx < 0: # an empty line is required
return False
if idx == 0: # we don't have all headers
rest = data[2:]
self._buf = [rest]
self.__on_headers_complete = True
return len(rest)

idx = data.find((b"\r\n\r\n"))
if idx < 0: # we don't have all headers
return False
raise InvalidHeader("No host header defined")

# Split lines on \r\n keeping the \r\n on each line
lines = [(str(line, 'unicode_escape') if PY3 else line) + "\r\n"
Expand Down
38 changes: 38 additions & 0 deletions tests/web/test_http.py
Expand Up @@ -48,3 +48,41 @@ def test(webapp):

s = client.buffer().decode('utf-8').split('\r\n')[0]
assert s == "HTTP/1.1 200 OK"


def test_http_1_0(webapp):
transport = TCPClient()
client = Client()
client += transport
client.start()

host, port, resource, secure = parse_url(webapp.server.http.base)
client.fire(connect(host, port))
assert pytest.wait_for(transport, "connected")

client.fire(write(b"GET / HTTP/1.0\r\n\r\n"))
assert pytest.wait_for(client, "done")

client.stop()

s = client.buffer().decode('utf-8').split('\r\n')[0]
assert s == "HTTP/1.0 200 OK"


def test_http_1_1_no_host_headers(webapp):
transport = TCPClient()
client = Client()
client += transport
client.start()

host, port, resource, secure = parse_url(webapp.server.http.base)
client.fire(connect(host, port))
assert pytest.wait_for(transport, "connected")

client.fire(write(b"GET / HTTP/1.1\r\n\r\n"))
assert pytest.wait_for(client, "done")

client.stop()

s = client.buffer().decode('utf-8').split('\r\n')[0]
assert s == "HTTP/1.1 400 Bad Request"

0 comments on commit ecfbe00

Please sign in to comment.