Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parser_request_error_in_cb_on_url(self):
class Error(Exception):
pass
m = mock.Mock()
m.on_url.side_effect = Error()
p = httptools.HttpRequestParser(m)
try:
p.feed_data(UPGRADE_REQUEST1)
except httptools.HttpParserCallbackError as ex:
self.assertIsInstance(ex.__context__, Error)
else:
self.fail('HttpParserCallbackError was not raised')
def test_parser_request_3(self):
p = httptools.HttpRequestParser(None)
with self.assertRaises(httptools.HttpParserInvalidURLError):
p.feed_data(b'POST HTTP/1.2')
def connection_made(self, transport):
self.parser = httptools.HttpRequestParser(self)
self.transport = transport
def data_received(self, data):
self.cancel_timeout_keep_alive_task()
try:
if self.parser is None:
self.headers = []
self.parser = httptools.HttpRequestParser(self)
self.parser.feed_data(data)
except httptools.parser.errors.HttpParserError as exc:
msg = "Invalid HTTP request received."
if self.debug:
msg += "\n" + traceback.format_exc()
self.logger.error(msg)
self.on_response(msg)
except httptools.HttpParserUpgrade as exc:
#self.handle_upgrade()
pass
def __init__(self, protocol: BaseProtocol):
super().__init__(HttpRequestParser)
self.protocol = protocol
self.buffer = bytes()
def _build_parser(callbacks):
return httptools.HttpRequestParser(callbacks)
def _extract_from_parser(parser):
async def _parse_request(self, request_reader, response_writer):
limit = 2 ** 16
req = Request()
parser = HttpRequestParser(req)
while True:
data = await request_reader.read(limit)
parser.feed_data(data)
if req.finished or not data:
break
elif req.needs_write_continue:
response_writer.write(b'HTTP/1.1 100 (Continue)\r\n\r\n')
req.reset_state()
if req.path is None:
# connected without a formed HTTP request
return
handler, args = self.get_handler(req.path)
r = BadRequest()
elif isinstance(exc, httptools.HttpParserUpgrade):
r = BadRequest(description="Invalid upgrade header.")
else:
# internal server error
r = InternalServerError()
# Make a fake environment.
new_environ = to_wsgi_environment(headers=self.headers, method="", path="/",
http_version="1.0", body=None)
new_environ["SERVER_NAME"] = self.component.get_server_name()
new_environ["SERVER_PORT"] = str(self.server_port)
new_environ["REMOTE_ADDR"] = self.ip
self.raw_write(get_formatted_response(r, new_environ))
self.parser = httptools.HttpRequestParser(self)
self.close()
def data_received(self, data):
# Check for the request itself getting too large and exceeding
# memory limits
self._total_request_size += len(data)
if self._total_request_size > self.request_max_size:
self.write_error(PayloadTooLarge("Payload Too Large"))
# Create parser if this is the first time we're receiving data
if self.parser is None:
assert self.request is None
self.headers = []
self.parser = HttpRequestParser(self)
# requests count
self.state["requests_count"] = self.state["requests_count"] + 1
# Parse request chunk or close connection
try:
self.parser.feed_data(data)
except HttpParserError:
message = "Bad Request"
if self._debug:
message += "\n" + traceback.format_exc()
self.write_error(InvalidUsage(message))
def __init__(self, socket):
self.complete = False
self.headers_complete = False
self.parser = HttpRequestParser(self)
self.request = None
self.socket = socket
self.reader = self._reader()