Browse Source

Python3 SSL socket handling

Sebastian Lohff 9 months ago
parent
commit
6e27ecfe8c
2 changed files with 24 additions and 2 deletions
  1. 14
    2
      servefile
  2. 10
    0
      tests/test_servefile.py

+ 14
- 2
servefile View File

@@ -13,6 +13,7 @@ import argparse
13 13
 import base64
14 14
 import cgi
15 15
 import datetime
16
+import io
16 17
 import mimetypes
17 18
 import os
18 19
 import re
@@ -771,8 +772,19 @@ class SecureThreadedHTTPServer(ThreadedHTTPServer):
771 772
 class SecureHandler():
772 773
 	def setup(self):
773 774
 		self.connection = self.request
774
-		self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
775
-		self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
775
+
776
+		if sys.version_info[0] > 2:
777
+			# python3 SocketIO (replacement for socket._fileobject)
778
+			raw_read_sock = socket.SocketIO(self.request, 'rb')
779
+			raw_write_sock = socket.SocketIO(self.request, 'wb')
780
+			rbufsize = self.rbufsize > 0 and self.rbufsize or io.DEFAULT_BUFFER_SIZE
781
+			wbufsize = self.wbufsize > 0 and self.wbufsize or io.DEFAULT_BUFFER_SIZE
782
+			self.rfile = io.BufferedReader(raw_read_sock, rbufsize)
783
+			self.wfile = io.BufferedWriter(raw_write_sock, wbufsize)
784
+		else:
785
+			# python2 does not have SocketIO
786
+			self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
787
+			self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
776 788
 
777 789
 class ServeFileException(Exception):
778 790
 	pass

+ 10
- 0
tests/test_servefile.py View File

@@ -274,3 +274,13 @@ def test_https(run_servefile, datadir):
274 274
     # assert fingerprint
275 275
     urllib3.disable_warnings()
276 276
     check_download(data, protocol='https', verify=False)
277
+
278
+def test_https_big_download(run_servefile, datadir):
279
+    # test with about 10 mb of data
280
+    data = "x" * (10 * 1024 ** 2)
281
+    p = datadir({'testfile': data}) / 'testfile'
282
+    run_servefile(['--ssl', str(p)])
283
+    time.sleep(0.2)  # time for generating ssl certificates
284
+
285
+    urllib3.disable_warnings()
286
+    check_download(data, protocol='https', verify=False)

Loading…
Cancel
Save