Browse Source

Fix PUT uploads

PUT uploads were broken on python 3.9 and were lacking tests.
Paweł Chojnacki 4 months ago
parent
commit
6537c054e5
2 changed files with 10 additions and 3 deletions
  1. 3
    3
      servefile/servefile.py
  2. 7
    0
      tests/test_servefile.py

+ 3
- 3
servefile/servefile.py View File

@@ -637,7 +637,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
637 637
 		http://host:8080/testfile will cause the file to be named testfile. If
638 638
 		no filename is given, a random name will be generated.
639 639
 
640
-		Files can be uploaded with e.g. curl -X POST -d @file <url> .
640
+		Files can be uploaded with e.g. curl -T file <url> .
641 641
 		"""
642 642
 		length = self.getContentLength()
643 643
 		if length < 0:
@@ -654,11 +654,11 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
654 654
 			return
655 655
 
656 656
 		# Sometimes clients want to be told to continue with their transfer
657
-		if self.headers.getheader("Expect") == "100-continue":
657
+		if self.headers.get("Expect") == "100-continue":
658 658
 			self.send_response(100)
659 659
 			self.end_headers()
660 660
 
661
-		target = open(cleanFileName, "w")
661
+		target = open(cleanFileName, "wb")
662 662
 		bytesLeft = int(self.headers['Content-Length'])
663 663
 		while bytesLeft > 0:
664 664
 			bytesToRead = min(self.blockSize, bytesLeft)

+ 7
- 0
tests/test_servefile.py View File

@@ -312,6 +312,13 @@ def test_upload(run_servefile, tmp_path):
312 312
     with open(str(uploaddir / 'haiku.txt(1)')) as f:
313 313
         assert f.read() == data
314 314
 
315
+    # upload file using PUT
316
+    r = make_request("/haiku.txt", method='put', data=data)
317
+    assert r.status_code == 201
318
+    assert 'OK!' in r.text
319
+    with open(str(uploaddir / 'haiku.txt(2)')) as f:
320
+        assert f.read() == data
321
+
315 322
 
316 323
 def test_upload_size_limit(run_servefile, tmp_path):
317 324
     uploaddir = tmp_path / 'upload'

Loading…
Cancel
Save