resolved RESOLVED PASS · 20 tool calls · 138 s · encode/httpx
Server connection handling. * Add `HTTPParser.keep_alive`. * Server... always read request to completion on keep alives. * `HTTPParser.complete` -> `.reset` * Close streams on server exit. * Don't raise `KeyboardException` on server exit.
| # | Tool | Arguments | Result |
|---|---|---|---|
| 1 | run_command | {
"command": "grep -rn \"HTTPParser\" src/httpx/_parsers.py src/httpx/_server.py"
} | {"status": "ok", "stdout": "src/httpx/_parsers.py:5:__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\nsrc/httpx/_parsers.py:72:class HTTPParser:\nsrc/httpx/_pafull{"status": "ok", "stdout": "src/httpx/_parsers.py:5:__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\nsrc/httpx/_parsers.py:72:class HTTPParser:\nsrc/httpx/_parsers.py:76: client = HTTPParser(writer, reader)\nsrc/httpx/_parsers.py:426: return f'<HTTPParser [{detail}]>'\nsrc/httpx/_server.py:6:from ._parsers import HTTPParser\nsrc/httpx/_server.py:27: self._parser = HTTPParser(stream, mode='SERVER')\n", "stderr": "", "exit_code": 0} |
| 2 | read_file | {
"filepath": "src/httpx/_parsers.py",
"start_line": "72"
} | {"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}full{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"} |
| 3 | read_file | {
"filepath": "src/httpx/_parsers.py"
} | {"status": "ok", "filepath": "src/httpx/_parsers.py", "content": "import enum\n\nfrom ._streams import Stream\n\n__all__ = ['HTTPParser', 'Mode', 'ProtocolErrorfull{"status": "ok", "filepath": "src/httpx/_parsers.py", "content": "import enum\n\nfrom ._streams import Stream\n\n__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\n\n\n# TODO...\n\n# * Upgrade\n# * CONNECT\n\n#\u00a0* Support 'Expect: 100 Continue'\n# * Add 'Error' state transitions\n# * Add tests to trickle data\n# * Add type annotations\n\n# * Optional... HTTP/1.0 support\n# * Read trailing headers on Transfer-Encoding: chunked. Not just '\\r\\n'.\n# * When writing Transfer-Encoding: chunked, split large writes into buffer size.\n# * When reading Transfer-Encoding: chunked, handle incomplete reads from large chunk sizes.\n# * .read() doesn't document if will always return maximum available.\n\n# * validate method, target, protocol in request line\n# * validate protocol, status_code, reason_phrase in response line\n# * validate name, value on headers\n\n\nclass State(enum.Enum):\n WAIT = 0\n SEND_METHOD_LINE = 1\n SEND_STATUS_LINE = 2\n SEND_HEADERS = 3\n SEND_BODY = 4\n RECV_METHOD_LINE = 5\n RECV_STATUS_LINE = 6\n RECV_HEADERS = 7\n RECV_BODY = 8\n DONE = 9\n CLOSED = 10\n\n\nclass Mode(enum.Enum):\n CLIENT = 0\n SERVER = 1\n\n\n# The usual transitions will be...\n\n# IDLE, IDLE\n# SEND_HEADERS, IDLE\n# SEND_BODY, IDLE\n# DONE, IDLE\n# DONE, SEND_HEADERS\n# DONE, SEND_BODY\n# DONE, DONE\n\n# Then either back to IDLE, IDLE\n# or move to CLOSED, CLOSED\n\n# 1. It is also valid for the server to start\n# sending the response without waiting for the\n# complete request.\n# 2. 1xx status codes are interim states, and\n# transition from SEND_HEADERS back to IDLE\n# 3. ...\n\nclass ProtocolError(Exception):\n pass\n\n\nclass HTTPParser:\n \"\"\"\n Usage...\n\n client = HTTPParser(writer, reader)\n client.send_method_line()\n client.send_headers()\n client.send_body()\n client.recv_status_line()\n client.recv_headers()\n client.recv_body()\n client.complete()\n client.close()\n \"\"\"\ |
| 4 | read_file | {
"filepath": "src/httpx/_parsers.py",
"start_line": "151"
} | {"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}full{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"} |
| 5 | run_command | {
"command": "cat src/httpx/_parsers.py"
} | {"status": "ok", "stdout": "import enum\n\nfrom ._streams import Stream\n\n__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\n\n\n# TODO...\n\n# * Upgrade\n# * full{"status": "ok", "stdout": "import enum\n\nfrom ._streams import Stream\n\n__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\n\n\n# TODO...\n\n# * Upgrade\n# * CONNECT\n\n#\u00a0* Support 'Expect: 100 Continue'\n# * Add 'Error' state transitions\n# * Add tests to trickle data\n# * Add type annotations\n\n# * Optional... HTTP/1.0 support\n# * Read trailing headers on Transfer-Encoding: chunked. Not just '\\r\\n'.\n# * When writing Transfer-Encoding: chunked, split large writes into buffer size.\n# * When reading Transfer-Encoding: chunked, handle incomplete reads from large chunk sizes.\n# * .read() doesn't document if will always return maximum available.\n\n# * validate method, target, protocol in request line\n# * validate protocol, status_code, reason_phrase in response line\n# * validate name, value on headers\n\n\nclass State(enum.Enum):\n WAIT = 0\n SEND_METHOD_LINE = 1\n SEND_STATUS_LINE = 2\n SEND_HEADERS = 3\n SEND_BODY = 4\n RECV_METHOD_LINE = 5\n RECV_STATUS_LINE = 6\n RECV_HEADERS = 7\n RECV_BODY = 8\n DONE = 9\n CLOSED = 10\n\n\nclass Mode(enum.Enum):\n CLIENT = 0\n SERVER = 1\n\n\n# The usual transitions will be...\n\n# IDLE, IDLE\n# SEND_HEADERS, IDLE\n# SEND_BODY, IDLE\n# DONE, IDLE\n# DONE, SEND_HEADERS\n# DONE, SEND_BODY\n# DONE, DONE\n\n# Then either back to IDLE, IDLE\n# or move to CLOSED, CLOSED\n\n# 1. It is also valid for the server to start\n# sending the response without waiting for the\n# complete request.\n# 2. 1xx status codes are interim states, and\n# transition from SEND_HEADERS back to IDLE\n# 3. ...\n\nclass ProtocolError(Exception):\n pass\n\n\nclass HTTPParser:\n \"\"\"\n Usage...\n\n client = HTTPParser(writer, reader)\n client.send_method_line()\n client.send_headers()\n client.send_body()\n client.recv_status_line()\n client.recv_headers()\n client.recv_body()\n client.complete()\n client.close()\n \"\"\"\n def __init__(self, stream: Stream |
| 6 | run_command | {
"command": "grep -n \"def complete\" src/httpx/_parsers.py"
} | {"status": "ok", "stdout": "378: def complete(self):\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "378: def complete(self):\n", "stderr": "", "exit_code": 0} |
| 7 | run_command | {
"command": "grep -n \"def reset\" src/httpx/_parsers.py"
} | {"status": "error", "error_type": "CommandError", "error_message": "", "details": {"stdout": "", "stderr": "", "exit_code": 1}}full{"status": "error", "error_type": "CommandError", "error_message": "", "details": {"stdout": "", "stderr": "", "exit_code": 1}} |
| 8 | run_command | {
"command": "grep -n \"def keep_alive\" src/httpx/_parsers.py"
} | {"status": "error", "error_type": "CommandError", "error_message": "", "details": {"stdout": "", "stderr": "", "exit_code": 1}}full{"status": "error", "error_type": "CommandError", "error_message": "", "details": {"stdout": "", "stderr": "", "exit_code": 1}} |
| 9 | run_command | {
"command": "cat src/httpx/_server.py"
} | {"status": "ok", "stdout": "import contextlib\nimport logging\nimport time\n\nfrom ._content import Text\nfrom ._parsers import HTTPParser\nfrom ._request imporfull{"status": "ok", "stdout": "import contextlib\nimport logging\nimport time\n\nfrom ._content import Text\nfrom ._parsers import HTTPParser\nfrom ._request import Request\nfrom ._response import Response\nfrom ._network import NetworkBackend, sleep\nfrom ._streams import HTTPStream\n\n__all__ = [\n \"serve_http\", \"run\"\n]\n\nlogger = logging.getLogger(\"httpx.server\")\n\n\nclass ConnectionClosed(Exception):\n pass\n\n\nclass HTTPConnection:\n def __init__(self, stream, endpoint):\n self._stream = stream\n self._endpoint = endpoint\n self._parser = HTTPParser(stream, mode='SERVER')\n self._keepalive_duration = 5.0\n self._idle_expiry = time.monotonic() + self._keepalive_duration\n\n # API entry points...\n def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\"\n logger.info(status_line)\n except Exception:\n logger.error(\"Internal Server Error\", exc_info=True)\n content = Text(\"Internal Server Error\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n except Exception:\n logger.error(\"Internal Server Error\", exc_info=True)\n\n def close(self):\n self |
| 10 | run_command | {
"command": "cat src/httpx/_parsers.py"
} | {"status": "ok", "stdout": "import enum\n\nfrom ._streams import Stream\n\n__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\n\n\n# TODO...\n\n# * Upgrade\n# * full{"status": "ok", "stdout": "import enum\n\nfrom ._streams import Stream\n\n__all__ = ['HTTPParser', 'Mode', 'ProtocolError']\n\n\n# TODO...\n\n# * Upgrade\n# * CONNECT\n\n#\u00a0* Support 'Expect: 100 Continue'\n# * Add 'Error' state transitions\n# * Add tests to trickle data\n# * Add type annotations\n\n# * Optional... HTTP/1.0 support\n# * Read trailing headers on Transfer-Encoding: chunked. Not just '\\r\\n'.\n# * When writing Transfer-Encoding: chunked, split large writes into buffer size.\n# * When reading Transfer-Encoding: chunked, handle incomplete reads from large chunk sizes.\n# * .read() doesn't document if will always return maximum available.\n\n# * validate method, target, protocol in request line\n# * validate protocol, status_code, reason_phrase in response line\n# * validate name, value on headers\n\n\nclass State(enum.Enum):\n WAIT = 0\n SEND_METHOD_LINE = 1\n SEND_STATUS_LINE = 2\n SEND_HEADERS = 3\n SEND_BODY = 4\n RECV_METHOD_LINE = 5\n RECV_STATUS_LINE = 6\n RECV_HEADERS = 7\n RECV_BODY = 8\n DONE = 9\n CLOSED = 10\n\n\nclass Mode(enum.Enum):\n CLIENT = 0\n SERVER = 1\n\n\n# The usual transitions will be...\n\n# IDLE, IDLE\n# SEND_HEADERS, IDLE\n# SEND_BODY, IDLE\n# DONE, IDLE\n# DONE, SEND_HEADERS\n# DONE, SEND_BODY\n# DONE, DONE\n\n# Then either back to IDLE, IDLE\n# or move to CLOSED, CLOSED\n\n# 1. It is also valid for the server to start\n# sending the response without waiting for the\n# complete request.\n# 2. 1xx status codes are interim states, and\n# transition from SEND_HEADERS back to IDLE\n# 3. ...\n\nclass ProtocolError(Exception):\n pass\n\n\nclass HTTPParser:\n \"\"\"\n Usage...\n\n client = HTTPParser(writer, reader)\n client.send_method_line()\n client.send_headers()\n client.send_body()\n client.recv_status_line()\n client.recv_headers()\n client.recv_body()\n client.complete()\n client.close()\n \"\"\"\n def __init__(self, stream: Stream |
| 11 | run_command | {
"command": "cat src/httpx/_parsers.py | tail -n 200"
} | {"status": "ok", "stdout": " self.recv_keep_alive = False\n elif lname == b'transfer-encoding' and value == b'chunked':\n self.recv_content_length = None\n\n iffull{"status": "ok", "stdout": " self.recv_keep_alive = False\n elif lname == b'transfer-encoding' and value == b'chunked':\n self.recv_content_length = None\n\n if self.mode == Mode.SERVER and not seen_host:\n raise ProtocolError(\"Request missing 'Host' header\")\n\n if self.processing_1xx:\n # 1xx status codes preceed the final response status code\n self.processing_1xx = False\n self.recv_state = State.RECV_STATUS_LINE\n else:\n self.recv_state = State.RECV_BODY\n return headers\n\n def recv_body(self) -> bytes:\n \"\"\"\n Receive the response body. An empty byte string indicates the end of the stream:\n\n >>> buffer = bytearray()\n >>> while body := p.recv_body()\n >>> buffer.extend(body)\n\n The server will switch to DONE.\n \"\"\"\n if self.recv_state != State.RECV_BODY:\n msg = f\"Called 'recv_body' in invalid state {self.recv_state}\"\n raise ProtocolError(msg)\n\n if self.recv_content_length is None:\n # Transfer-Encoding: chunked\n exc_text = 'reading chunk size'\n line = self.parser.read_until(b\"\\r\\n\", max_size=4096, exc_text=exc_text)\n sizestr, _, _ = line.partition(b\";\")\n\n exc_text = \"Received invalid chunk size\"\n size = bounded_hex(sizestr, max_digits=8, exc_text=exc_text)\n if size > 0:\n body = self.parser.read(size=size)\n exc_text = 'reading chunk data'\n self.parser.read_until(b\"\\r\\n\", max_size=2, exc_text=exc_text)\n self.recv_seen_length += len(body)\n else:\n body = b''\n exc_text = 'reading chunk termination'\n self.parser.read_until(b\"\\r\\n\", max_size=2, exc_text=exc_text)\n\n else:\n # Content-Length: xxx\n |
| 12 | edit_file | {
"filepath": "src/httpx/_parsers.py",
"new_string": " def reset(self):\n is_fully_complete = self.send_state == State.DONE and self.recv_state == State.DONE\n is_keepalive = self.send_keep_alive and self.recv_keep_alive\n\n if not (is_fully_complete and is_keepalive):\n self.close()\n return\n\n if self.mode == Mode.CLIENT:\n self.send_state = State.SEND_METHOD_LINE\n self.recv_state = State.WAIT\n else:\n self.recv_state = State.RECV_METHOD_LINE\n self.send_state = State.WAIT\n\n self.send_content_length = 0\n self.recv_content_length = 0\n self.send_seen_length = 0\n self.recv_seen_length = 0\n self.send_keep_alive = True\n self.recv_keep_alive = True\n self.processing_1xx = False\n\n @property\n def keep_alive(self) -> bool:\n return self.send_keep_alive and self.recv_keep_alive",
"old_string": " def complete(self):\n is_fully_complete = self.send_state == State.DONE and self.recv_state == State.DONE\n is_keepalive = self.send_keep_alive and self.recv_keep_alive\n\n if not (is_fully_complete and is_keepalive):\n self.close()\n return\n\n if self.mode == Mode.CLIENT:\n self.send_state = State.SEND_METHOD_LINE\n self.recv_state = State.WAIT\n else:\n self.recv_state = State.RECV_METHOD_LINE\n self.send_state = State.WAIT\n\n self.send_content_length = 0\n self.recv_content_length = 0\n self.send_seen_length = 0\n self.recv_seen_length = 0\n self.send_keep_alive = True\n self.recv_keep_alive = True\n self.processing_1xx = False"
} | {"status": "ok", "filepath": "src/httpx/_parsers.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_parsers.py\n+++ b/src/httpx/_parsers.py\nfull{"status": "ok", "filepath": "src/httpx/_parsers.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_parsers.py\n+++ b/src/httpx/_parsers.py\n@@ -375,7 +375,7 @@\n self.recv_state = State.DONE\n return body\n \n- def complete(self):\n+ def reset(self):\n is_fully_complete = self.send_state == State.DONE and self.recv_state == State.DONE\n is_keepalive = self.send_keep_alive and self.recv_keep_alive\n \n@@ -397,6 +397,10 @@\n self.send_keep_alive = True\n self.recv_keep_alive = True\n self.processing_1xx = False\n+\n+ @property\n+ def keep_alive(self) -> bool:\n+ return self.send_keep_alive and self.recv_keep_alive\n \n def close(self):\n if self.send_state != State.CLOSED:\n", "is_truncated": false} |
| 13 | edit_file | {
"filepath": "src/httpx/_server.py",
"new_string": " # Start it all over again...\n def _complete(self):\n self._parser.reset()\n self._idle_expiry = time.monotonic() + self._keepalive_duration",
"old_string": " # Start it all over again...\n def _complete(self):\n self._parser.complete\n self._idle_expiry = time.monotonic() + self._keepalive_duration"
} | {"status": "ok", "filepath": "src/httpx/_server.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_server.py\n+++ b/src/httpx/_server.py\n@@ full{"status": "ok", "filepath": "src/httpx/_server.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_server.py\n+++ b/src/httpx/_server.py\n@@ -89,7 +89,7 @@\n \n # Start it all over again...\n def _complete(self):\n- self._parser.complete\n+ self._parser.reset()\n self._idle_expiry = time.monotonic() + self._keepalive_duration\n \n \n", "is_truncated": false} |
| 14 | edit_file | {
"filepath": "src/httpx/_server.py",
"new_string": " def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\\\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\\\"\n logger.info(status_line)\n except Exception:\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n content = Text(\\\"Internal Server Error\\\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n except (Exception, KeyboardInterrupt):\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n finally:\n self.close()",
"old_string": " def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\\\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\\\"\n logger.info(status_line)\n except Exception:\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n content = Text(\\\"Internal Server Error\\\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n except Exception:\n logger.error(\\\"Internal Server Error\\\", exc_info=True)"
} | {"status": "error", "error_type": "FileEditError", "error_message": "Failed to replace: old_string not found. Ensure you're not escaping content incorrectly andfull{"status": "error", "error_type": "FileEditError", "error_message": "Failed to replace: old_string not found. Ensure you're not escaping content incorrectly and check whitespace, indentation, and context."} |
| 15 | read_file | {
"filepath": "src/httpx/_server.py",
"start_line": "25",
"end_line": "55"
} | {"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}full{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"} |
| 16 | run_command | {
"command": "cat src/httpx/_server.py"
} | {"status": "ok", "stdout": "import contextlib\nimport logging\nimport time\n\nfrom ._content import Text\nfrom ._parsers import HTTPParser\nfrom ._request imporfull{"status": "ok", "stdout": "import contextlib\nimport logging\nimport time\n\nfrom ._content import Text\nfrom ._parsers import HTTPParser\nfrom ._request import Request\nfrom ._response import Response\nfrom ._network import NetworkBackend, sleep\nfrom ._streams import HTTPStream\n\n__all__ = [\n \"serve_http\", \"run\"\n]\n\nlogger = logging.getLogger(\"httpx.server\")\n\n\nclass ConnectionClosed(Exception):\n pass\n\n\nclass HTTPConnection:\n def __init__(self, stream, endpoint):\n self._stream = stream\n self._endpoint = endpoint\n self._parser = HTTPParser(stream, mode='SERVER')\n self._keepalive_duration = 5.0\n self._idle_expiry = time.monotonic() + self._keepalive_duration\n\n # API entry points...\n def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\"\n logger.info(status_line)\n except Exception:\n logger.error(\"Internal Server Error\", exc_info=True)\n content = Text(\"Internal Server Error\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n except Exception:\n logger.error(\"Internal Server Error\", exc_info=True)\n\n def close(self):\n self |
| 17 | edit_file | {
"filepath": "src/httpx/_server.py",
"new_string": " except (Exception, KeyboardInterrupt):\n logger.error(\"Internal Server Error\", exc_info=True)\n finally:\n self.close()",
"old_string": " except Exception:\n logger.error(\"Internal Server Error\", exc_info=True)"
} | {"status": "ok", "filepath": "src/httpx/_server.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_server.py\n+++ b/src/httpx/_server.py\n@@ full{"status": "ok", "filepath": "src/httpx/_server.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_server.py\n+++ b/src/httpx/_server.py\n@@ -49,8 +49,10 @@\n else:\n self._send_head(response)\n self._send_body(response)\n- except Exception:\n+ except (Exception, KeyboardInterrupt):\n logger.error(\"Internal Server Error\", exc_info=True)\n+ finally:\n+ self.close()\n \n def close(self):\n self._parser.close()\n", "is_truncated": false} |
| 18 | edit_file | {
"filepath": "src/httpx/_server.py",
"new_string": " def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\\\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\\\"\n logger.info(status_line)\n except Exception:\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n content = Text(\\\"Internal Server Error\\\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n \n # Always read request to completion on keep alives\n if self._parser.keep_alive:\n while not self._parser.is_idle():\n self._recv_body()\n except (Exception, KeyboardInterrupt):\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n finally:\n self.close()",
"old_string": " def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\\\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\\\"\n logger.info(status_line)\n except Exception:\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n content = Text(\\\"Internal Server Error\\\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n except (Exception, KeyboardInterrupt):\n logger.error(\\\"Internal Server Error\\\", exc_info=True)\n finally:\n self.close()"
} | {"status": "error", "error_type": "FileEditError", "error_message": "Failed to replace: old_string not found. Ensure you're not escaping content incorrectly andfull{"status": "error", "error_type": "FileEditError", "error_message": "Failed to replace: old_string not found. Ensure you're not escaping content incorrectly and check whitespace, indentation, and context."} |
| 19 | run_command | {
"command": "cat src/httpx/_server.py"
} | {"status": "ok", "stdout": "import contextlib\nimport logging\nimport time\n\nfrom ._content import Text\nfrom ._parsers import HTTPParser\nfrom ._request imporfull{"status": "ok", "stdout": "import contextlib\nimport logging\nimport time\n\nfrom ._content import Text\nfrom ._parsers import HTTPParser\nfrom ._request import Request\nfrom ._response import Response\nfrom ._network import NetworkBackend, sleep\nfrom ._streams import HTTPStream\n\n__all__ = [\n \"serve_http\", \"run\"\n]\n\nlogger = logging.getLogger(\"httpx.server\")\n\n\nclass ConnectionClosed(Exception):\n pass\n\n\nclass HTTPConnection:\n def __init__(self, stream, endpoint):\n self._stream = stream\n self._endpoint = endpoint\n self._parser = HTTPParser(stream, mode='SERVER')\n self._keepalive_duration = 5.0\n self._idle_expiry = time.monotonic() + self._keepalive_duration\n\n # API entry points...\n def handle_requests(self):\n try:\n while not self._parser.is_closed():\n method, url, headers = self._recv_head()\n stream = HTTPStream(self._recv_body, self._complete)\n # TODO: Handle endpoint exceptions\n with Request(method, url, headers=headers, content=stream) as request:\n try:\n response = self._endpoint(request)\n status_line = f\"{request.method} {request.url.target} [{response.status_code} {response.reason_phrase}]\"\n logger.info(status_line)\n except Exception:\n logger.error(\"Internal Server Error\", exc_info=True)\n content = Text(\"Internal Server Error\")\n err = Response(code=500, content=content)\n self._send_head(err)\n self._send_body(err)\n else:\n self._send_head(response)\n self._send_body(response)\n except (Exception, KeyboardInterrupt):\n logger.error(\"Internal Server Error\", exc_info=True)\n finally |
| 20 | edit_file | {
"filepath": "src/httpx/_server.py",
"new_string": " else:\n self._send_head(response)\n self._send_body(response)\n\n # Always read request to completion on keep alives\n if self._parser.keep_alive:\n while not self._parser.is_idle():\n self._recv_body()\n except (Exception, KeyboardInterrupt):",
"old_string": " else:\n self._send_head(response)\n self._send_body(response)\n except (Exception, KeyboardInterrupt):"
} | {"status": "ok", "filepath": "src/httpx/_server.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_server.py\n+++ b/src/httpx/_server.py\n@@ full{"status": "ok", "filepath": "src/httpx/_server.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/src/httpx/_server.py\n+++ b/src/httpx/_server.py\n@@ -49,6 +49,11 @@\n else:\n self._send_head(response)\n self._send_body(response)\n+\n+ # Always read request to completion on keep alives\n+ if self._parser.keep_alive:\n+ while not self._parser.is_idle():\n+ self._recv_body()\n except (Exception, KeyboardInterrupt):\n logger.error(\"Internal Server Error\", exc_info=True)\n finally:\n", "is_truncated": false} |
| 21 | submit_patch | {} | {"status": "ok", "patch_size": 1803, "files_changed": 2}full{"status": "ok", "patch_size": 1803, "files_changed": 2} |
diff --git a/src/httpx/_parsers.py b/src/httpx/_parsers.py
index 830fccd..6bd1cc6 100644
--- a/src/httpx/_parsers.py
+++ b/src/httpx/_parsers.py
@@ -375,7 +375,7 @@ class HTTPParser:
self.recv_state = State.DONE
return body
- def complete(self):
+ def reset(self):
is_fully_complete = self.send_state == State.DONE and self.recv_state == State.DONE
is_keepalive = self.send_keep_alive and self.recv_keep_alive
@@ -398,6 +398,10 @@ class HTTPParser:
self.recv_keep_alive = True
self.processing_1xx = False
+ @property
+ def keep_alive(self) -> bool:
+ return self.send_keep_alive and self.recv_keep_alive
+
def close(self):
if self.send_state != State.CLOSED:
self.send_state = State.CLOSED
diff --git a/src/httpx/_server.py b/src/httpx/_server.py
index 95226d9..382869c 100644
--- a/src/httpx/_server.py
+++ b/src/httpx/_server.py
@@ -49,8 +49,15 @@ class HTTPConnection:
else:
self._send_head(response)
self._send_body(response)
- except Exception:
+
+ # Always read request to completion on keep alives
+ if self._parser.keep_alive:
+ while not self._parser.is_idle():
+ self._recv_body()
+ except (Exception, KeyboardInterrupt):
logger.error("Internal Server Error", exc_info=True)
+ finally:
+ self.close()
def close(self):
self._parser.close()
@@ -89,7 +96,7 @@ class HTTPConnection:
# Start it all over again...
def _complete(self):
- self._parser.complete
+ self._parser.reset()
self._idle_expiry = time.monotonic() + self._keepalive_duration
........................ [100%] 24 passed in 0.08s