def _read_request_header(self) -> Request: request = Request() for dummy in range(100): line = yield from self._reader.readline() _logger.debug(__('Got line {0}', line)) if line[-1:] != b'\n': return if not line.strip(): break request.parse(line) else: raise ProtocolError('Request has too many headers.') return request
def test_request_parse(self): request = Request() request.parse(b'GET /robots.txt HTTP/1.1\r\n') request.parse(b'Host: example.com\r\n') request.parse('Accept: éxample\r\n'.encode('utf_8')) request.parse(b'\r\n') self.assertEqual('http://example.com/robots.txt', request.url) self.assertEqual('example.com', request.fields['host']) self.assertEqual('éxample'.encode('utf-8').decode('latin-1'), request.fields['accept']) request = Request() request.parse(b'GET https://example.com/robots.txt HTTP/1.1\r\n') request.parse(b'Host: example.com\r\n') request.parse(b'Accept: \xffexample\r\n') request.parse(b'\r\n') self.assertEqual('https://example.com/robots.txt', request.url) self.assertEqual('example.com', request.fields['host']) self.assertEqual('\xffexample', request.fields['accept'])