Skip to content

let your agent control your phone

push_file sends the upload headers the service names

ShawnPanadb3f239b940f
diff --git a/src/phone_harness/transfer.py b/src/phone_harness/transfer.py
index 6eb0169..bf4a11c 100644
--- a/src/phone_harness/transfer.py
+++ b/src/phone_harness/transfer.py
@@ -39,9 +39,11 @@ def _session(what, backend):
return sess
-def _put(url, path, content_type):
+def _put(url, path, headers):
+ """PUT the file to its signed link with exactly the headers the service
+ named: the store signed them into the link and refuses a PUT without them."""
data = Path(path).read_bytes()
- req = urllib.request.Request(url, data=data, method="PUT", headers={"Content-Type": content_type})
+ req = urllib.request.Request(url, data=data, method="PUT", headers=dict(headers))
try:
with urllib.request.urlopen(req, timeout=max(60, len(data) / 200_000)) as reply:
if reply.status // 100 != 2:
@@ -85,7 +87,8 @@ def push_file(path, backend):
content_type = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
transfer = cloud._api("POST", f"/sessions/{sess['sid']}/files",
{"filename": path.name, "content_type": content_type})
- size = _put(transfer["upload_url"], path, content_type)
+ headers = transfer.get("upload_headers") or {"Content-Type": content_type}
+ size = _put(transfer["upload_url"], path, headers)
backend.send("url.open", url=transfer["download_url"])
print(f"{transfer['filename']} ({size:,} bytes) is open in the phone's browser")
return {"filename": transfer["filename"], "size": size, "content_type": content_type,
diff --git a/tests/test_transfer.py b/tests/test_transfer.py
index adbd6c0..c428a3c 100644
--- a/tests/test_transfer.py
+++ b/tests/test_transfer.py
@@ -20,6 +20,7 @@ from phone_harness.transport import Unsupported # noqa: E402
class Store(BaseHTTPRequestHandler):
objects = {}
+ tags = {}
def log_message(self, *a):
pass
@@ -27,6 +28,7 @@ class Store(BaseHTTPRequestHandler):
def do_PUT(self):
n = int(self.headers.get("Content-Length") or 0)
Store.objects[self.path.split("?")[0]] = (self.rfile.read(n), self.headers.get("Content-Type"))
+ Store.tags[self.path.split("?")[0]] = self.headers.get("x-amz-tagging")
self.send_response(200); self.send_header("Content-Length", "0"); self.end_headers()
def do_GET(self):
@@ -86,6 +88,7 @@ class Transfer(unittest.TestCase):
src = Path(self.tmp.name) / "cat photo.jpg"; src.write_bytes(b"\xff\xd8jpeg")
self.api_replies[("POST", "/sessions/abc123def456/files")] = {
"id": "0123456789abcdef", "filename": "cat_photo.jpg", "content_type": "image/jpeg", "expires_at": 1_800_000_000,
+ "upload_headers": {"Content-Type": "image/jpeg", "x-amz-tagging": "class=transfer"},
"upload_url": self.base + "/t/file?sig=up", "download_url": self.base + "/t/file?sig=down"}
out = io.StringIO()
with redirect_stdout(out):
@@ -93,6 +96,7 @@ class Transfer(unittest.TestCase):
self.assertEqual(result, {"filename": "cat_photo.jpg", "size": 6, "content_type": "image/jpeg", "expires_at": 1_800_000_000})
self.assertEqual(self.calls, [("POST", "/sessions/abc123def456/files", {"filename": "cat photo.jpg", "content_type": "image/jpeg"})])
self.assertEqual(Store.objects["/t/file"], (b"\xff\xd8jpeg", "image/jpeg"))
+ self.assertEqual(Store.tags.get("/t/file"), "class=transfer") # the signed header went with the PUT
self.assertEqual(self.phone.opened, [("url.open", {"url": self.base + "/t/file?sig=down"})])
self.assertIn("cat_photo.jpg", out.getvalue())
self.assertNotIn("sig=", out.getvalue()) # links are credentials: never printed