Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Generate valid chunked response when returning short circuit answer #1001

Merged
merged 7 commits into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/codegate/providers/copilot/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,29 @@ class HttpResponse:
headers: List[str]
body: Optional[bytes] = None

def reconstruct(self) -> bytes:
"""Reconstruct HTTP response from stored details"""
def reconstruct_headers(self) -> bytes:
"""
Reconstruct the HTTP header of the response.
"""
headers = "\r\n".join(self.headers)
status_line = f"{self.version} {self.status_code} {self.reason}\r\n"
header_block = f"{status_line}{headers}\r\n\r\n"
return header_block.encode("utf-8")

# Convert header block to bytes and combine with body
result = header_block.encode("utf-8")
def reconstruct_body(self) -> bytes:
"""
Reconstruct the body of the response.
"""
result = b""
if self.body:
result += self.body

result += b"\n\n"
return result

def reconstruct(self) -> bytes:
"""Reconstruct HTTP response from stored details"""
return self.reconstruct_headers() + self.reconstruct_body()


def extract_path(full_path: str) -> str:
"""Extract clean path from full URL or path string"""
Expand Down Expand Up @@ -387,8 +397,12 @@ async def _forward_data_to_target(self, data: bytes) -> None:
# request to the target
self.target_transport.close()

# Send the shortcut response data in a chunk
chunk = pipeline_output.reconstruct()
# Send the HTTP headers
# Note that this representation ends with \r\n\r\n
self.transport.write(pipeline_output.reconstruct_headers())

# Encode the body, send its length, then the data
chunk = pipeline_output.reconstruct_body()
chunk_size = hex(len(chunk))[2:] + "\r\n"
self.transport.write(chunk_size.encode())
self.transport.write(chunk)
Expand Down
Loading