in http/get_compressed/python/server/server.py [0:0]
def do_GET(self):
# HTTP/1.0 requests don't get chunked responses
if self.request_version == "HTTP/1.0":
self.protocol_version = "HTTP/1.0"
chunked = False
else:
self.protocol_version = "HTTP/1.1"
chunked = True
# if client's intent cannot be derived from the headers, return
# uncompressed data for HTTP/1.0 requests and compressed data for
# HTTP/1.1 requests with the safest compression format choice: "gzip".
default_compression = (
"identity"
if self.request_version == "HTTP/1.0" or ("gzip" not in AVAILABLE_CODINGS)
else "gzip"
)
try:
compression = pick_compression(
self.headers,
AVAILABLE_IPC_CODECS,
AVAILABLE_CODINGS,
default_compression,
)
if compression is None:
self._send_not_acceptable()
return
except ValueError as e:
self._send_not_acceptable(str(e))
return
### in a real application the data would be resolved from a database or
### another source like a file and error handling would be done here
### before the 200 OK response starts being sent to the client.
source = self._resolve_batches()
self.send_response(200)
### set these headers if testing with a local browser-based client:
# self.send_header('Access-Control-Allow-Origin', 'http://localhost:8008')
# self.send_header('Access-Control-Allow-Methods', 'GET')
# self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.send_header(
"Content-Type",
(
f"{ARROW_STREAM_FORMAT}; codecs={compression[9:]}"
if compression.startswith("identity+")
else ARROW_STREAM_FORMAT
),
)
# suggest a default filename in case this response is saved by the user
self.send_header("Content-Disposition", r'attachment; filename="output.arrows"')
if not compression.startswith("identity"):
self.send_header("Content-Encoding", compression)
if chunked:
self.send_header("Transfer-Encoding", "chunked")
self.end_headers()
for buffer in generate_chunk_buffers(the_schema, source, compression):
self.wfile.write(f"{len(buffer):X}\r\n".encode("utf-8"))
self.wfile.write(buffer)
self.wfile.write("\r\n".encode("utf-8"))
self.wfile.write("0\r\n\r\n".encode("utf-8"))
else:
self.end_headers()
sink = SocketWriterSink(self.wfile)
for buffer in generate_chunk_buffers(the_schema, source, compression):
sink.write(buffer)