py3: port tinyproxy.py to work with Python 3
There were various str/bytes mismatches in the code. This caused
the proxy server to misbehave at run-time. The manifestation
was typically premature socket disconnect from the perspective
of the client.
Differential Revision: https://phab.mercurial-scm.org/D5951
--- a/tests/tinyproxy.py Tue Feb 12 12:13:56 2019 -0800
+++ b/tests/tinyproxy.py Tue Feb 12 14:29:56 2019 -0800
@@ -20,7 +20,10 @@
import socket
import sys
-from mercurial import util
+from mercurial import (
+ pycompat,
+ util,
+)
httpserver = util.httpserver
socketserver = util.socketserver
@@ -77,10 +80,11 @@
try:
if self._connect_to(self.path, soc):
self.log_request(200)
- self.wfile.write(self.protocol_version +
- " 200 Connection established\r\n")
- self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
- self.wfile.write("\r\n")
+ self.wfile.write(pycompat.bytestr(self.protocol_version) +
+ b" 200 Connection established\r\n")
+ self.wfile.write(b"Proxy-agent: %s\r\n" %
+ pycompat.bytestr(self.version_string()))
+ self.wfile.write(b"\r\n")
self._read_write(soc, 300)
finally:
print("\t" "bye")
@@ -97,15 +101,17 @@
try:
if self._connect_to(netloc, soc):
self.log_request()
- soc.send("%s %s %s\r\n" % (
- self.command,
- urlreq.urlunparse(('', '', path, params, query, '')),
- self.request_version))
+ url = urlreq.urlunparse(('', '', path, params, query, ''))
+ soc.send(b"%s %s %s\r\n" % (
+ pycompat.bytestr(self.command),
+ pycompat.bytestr(url),
+ pycompat.bytestr(self.request_version)))
self.headers['Connection'] = 'close'
del self.headers['Proxy-Connection']
- for key_val in self.headers.items():
- soc.send("%s: %s\r\n" % key_val)
- soc.send("\r\n")
+ for key, val in self.headers.items():
+ soc.send(b"%s: %s\r\n" % (pycompat.bytestr(key),
+ pycompat.bytestr(val)))
+ soc.send(b"\r\n")
self._read_write(soc)
finally:
print("\t" "bye")