Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: handle chunked encoding in /Discover POST body #116

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 16 additions & 2 deletions opendrop/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,22 @@ def do_GET(self):
self.wfile.write(body)

def handle_discover(self):
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
if content_length := self.headers.get("Content-Length"):
post_data = self.rfile.read(int(content_length))
elif self.headers.get("Transfer-Encoding") == "chunked":
post_data = bytearray()
while True:
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding#chunked_encoding
chunk_size = int(self.rfile.readline().rstrip(b"\r\n"),)
if chunk_size == 0: # end of chunks
self.rfile.read() # consume rest of the input
break
chunk_bytes = self.rfile.read(chunk_size)
post_data.extend(chunk_bytes)
assert self.rfile.read(2) == b"\r\n"
else:
raise Exception(f"Can't handle post body! Headers: {self.headers}")


AirDropUtil.write_debug(
self.config, post_data, "receive_discover_request.plist"
Expand Down