def read_response(self, response=None): '''Read the response's HTTP status line and header fields. Coroutine. ''' _logger.debug('Reading header.') if response is None: response = Response() header_lines = [] bytes_read = 0 while True: try: data = yield From(self._connection.readline()) except ValueError as error: raise ProtocolError( 'Invalid header: {0}'.format(error)) from error self._data_observer.notify('response', data) if not data.endswith(b'\n'): raise NetworkError('Connection closed.') elif data in (b'\r\n', b'\n'): break header_lines.append(data) assert data.endswith(b'\n') bytes_read += len(data) if bytes_read > 32768: raise ProtocolError('Header too big.') if not header_lines: raise ProtocolError('No header received.') response.parse(b''.join(header_lines)) raise Return(response)
def read_response(self, response=None): '''Read the response's HTTP status line and header fields. Coroutine. ''' _logger.debug('Reading header.') if response is None: response = Response() header_lines = [] bytes_read = 0 while True: try: data = yield From(self._connection.readline()) except ValueError as error: raise ProtocolError( 'Invalid header: {0}'.format(error)) from error self._data_observer.notify('response', data) if not data.endswith(b'\n'): raise NetworkError('Connection closed.') elif data in (b'\r\n', b'\n'): break header_lines.append(data) assert data.endswith(b'\n') bytes_read += len(data) if bytes_read > 32768: raise ProtocolError('Header too big.') if not header_lines: raise ProtocolError('No header received.') response.parse(b''.join(header_lines)) raise Return(response)
def read_response(self, response=None): """Read the response's HTTP status line and header fields. Coroutine. """ _logger.debug("Reading header.") if response is None: response = Response() header_lines = [] bytes_read = 0 while True: try: data = yield From(self._connection.readline()) except ValueError as error: raise ProtocolError("Invalid header: {0}".format(error)) from error self._data_observer.notify("response", data) if not data.endswith(b"\n"): raise NetworkError("Connection closed.") elif data in (b"\r\n", b"\n"): break header_lines.append(data) assert data.endswith(b"\n") bytes_read += len(data) if bytes_read > 32768: raise ProtocolError("Header too big.") if not header_lines: raise ProtocolError("No header received.") response.parse(b"".join(header_lines)) raise Return(response)
def test_response_status_codes(self): response = Response() response.parse(b'HTTP/1.0 0\r\n') response.parse(b'\r\n') self.assertEqual(0, response.status_code) response = Response() response.parse(b'HTTP/1.0 999\r\n') response.parse(b'\r\n') self.assertEqual(999, response.status_code) response = Response(0, '') self.assertEqual(0, response.status_code)
def test_response_status_codes(self): response = Response() response.parse(b'HTTP/1.0 0\r\n') response.parse(b'\r\n') self.assertEqual(0, response.status_code) response = Response() response.parse(b'HTTP/1.0 999\r\n') response.parse(b'\r\n') self.assertEqual(999, response.status_code) response = Response(0, '') self.assertEqual(0, response.status_code)
def test_response_empty_reason_line(self): response = Response() response.parse(b'HTTP/1.0 200\r\n') response.parse(b'Cake: dolphin\r\n') response.parse(b'\r\n') self.assertEqual(200, response.status_code) self.assertEqual('', response.reason) self.assertEqual('dolphin', response.fields['Cake'])
def test_response_empty_reason_line(self): response = Response() response.parse(b'HTTP/1.0 200\r\n') response.parse(b'Cake: dolphin\r\n') response.parse(b'\r\n') self.assertEqual(200, response.status_code) self.assertEqual('', response.reason) self.assertEqual('dolphin', response.fields['Cake'])
def test_response_parse(self): response = Response() response.parse(b'HTTP/1.0 200 OK\r\n') response.parse('Cake: dolphın\r\n'.encode('utf-8')) response.parse(b'\r\n') self.assertEqual(200, response.status_code) self.assertEqual('OK', response.reason) self.assertEqual('dolphın'.encode('utf-8').decode('latin-1'), response.fields['Cake']) response = Response() response.parse(b'HTTP/1.0 200 OK\r\n') response.parse(b'Cake: \xffdolphin\r\n') response.parse(b'\r\n') self.assertEqual(200, response.status_code) self.assertEqual('OK', response.reason) self.assertEqual('\xffdolphin', response.fields['Cake'])
def test_response_parse(self): response = Response() response.parse(b'HTTP/1.0 200 OK\r\n') response.parse('Cake: dolphın\r\n'.encode('utf-8')) response.parse(b'\r\n') self.assertEqual(200, response.status_code) self.assertEqual('OK', response.reason) self.assertEqual('dolphın'.encode('utf-8').decode('latin-1'), response.fields['Cake']) response = Response() response.parse(b'HTTP/1.0 200 OK\r\n') response.parse(b'Cake: \xffdolphin\r\n') response.parse(b'\r\n') self.assertEqual(200, response.status_code) self.assertEqual('OK', response.reason) self.assertEqual('\xffdolphin', response.fields['Cake'])