diff --git a/src/calibre/srv/http_request.py b/src/calibre/srv/http_request.py index 98df2e8b19..8b18489f36 100644 --- a/src/calibre/srv/http_request.py +++ b/src/calibre/srv/http_request.py @@ -125,13 +125,16 @@ class HTTPHeaderParser(object): val = existing + ', ' + val self.hdict[key] = val + if self.finished: + raise ValueError('Header block already terminated') + if line == b'\r\n': # Normal end of headers commit() self.finished = True return - if line[0] in b' \t': + if line and line[0] in b' \t': # It's a continuation line. if not self.lines: raise ValueError('Orphaned continuation line') diff --git a/src/calibre/srv/tests/http.py b/src/calibre/srv/tests/http.py index 3a5a55563a..88995a76d2 100644 --- a/src/calibre/srv/tests/http.py +++ b/src/calibre/srv/tests/http.py @@ -42,16 +42,16 @@ class TestHTTP(BaseTest): 'accept-Encoding: two', '\r\n', accept_encoding='one, two') - def parse(line): - HTTPHeaderParser()(line) + def parse(*lines): + lines = list(lines) + lines.append(b'\r\n') + self.assertRaises(ValueError, HTTPHeaderParser().push, *lines) - with self.assertRaises(ValueError): - parse('Connection:mūs\r\n') - parse('Connection\r\n') - parse('Connection:a\r\n') - parse('Connection:a\n') - parse(' Connection:a\n') - parse(':a\n') + parse(b'Connection:mūs\r\n') + parse(b'Connection\r\n') + parse(b'Connection:a\r\n', b'\r\n') + parse(b' Connection:a\n') + parse(b':a\n') # }}} def test_accept_encoding(self): # {{{