comparison tests/tinyproxy.py @ 41709:97e2442a4595

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
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 12 Feb 2019 14:29:56 -0800
parents 88c1d13b637b
children 2372284d9457
comparison
equal deleted inserted replaced
41708:d20f1594ff4a 41709:97e2442a4595
18 import os 18 import os
19 import select 19 import select
20 import socket 20 import socket
21 import sys 21 import sys
22 22
23 from mercurial import util 23 from mercurial import (
24 pycompat,
25 util,
26 )
24 27
25 httpserver = util.httpserver 28 httpserver = util.httpserver
26 socketserver = util.socketserver 29 socketserver = util.socketserver
27 urlreq = util.urlreq 30 urlreq = util.urlreq
28 31
75 def do_CONNECT(self): 78 def do_CONNECT(self):
76 soc = socket.socket(family, socket.SOCK_STREAM) 79 soc = socket.socket(family, socket.SOCK_STREAM)
77 try: 80 try:
78 if self._connect_to(self.path, soc): 81 if self._connect_to(self.path, soc):
79 self.log_request(200) 82 self.log_request(200)
80 self.wfile.write(self.protocol_version + 83 self.wfile.write(pycompat.bytestr(self.protocol_version) +
81 " 200 Connection established\r\n") 84 b" 200 Connection established\r\n")
82 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string()) 85 self.wfile.write(b"Proxy-agent: %s\r\n" %
83 self.wfile.write("\r\n") 86 pycompat.bytestr(self.version_string()))
87 self.wfile.write(b"\r\n")
84 self._read_write(soc, 300) 88 self._read_write(soc, 300)
85 finally: 89 finally:
86 print("\t" "bye") 90 print("\t" "bye")
87 soc.close() 91 soc.close()
88 self.connection.close() 92 self.connection.close()
95 return 99 return
96 soc = socket.socket(family, socket.SOCK_STREAM) 100 soc = socket.socket(family, socket.SOCK_STREAM)
97 try: 101 try:
98 if self._connect_to(netloc, soc): 102 if self._connect_to(netloc, soc):
99 self.log_request() 103 self.log_request()
100 soc.send("%s %s %s\r\n" % ( 104 url = urlreq.urlunparse(('', '', path, params, query, ''))
101 self.command, 105 soc.send(b"%s %s %s\r\n" % (
102 urlreq.urlunparse(('', '', path, params, query, '')), 106 pycompat.bytestr(self.command),
103 self.request_version)) 107 pycompat.bytestr(url),
108 pycompat.bytestr(self.request_version)))
104 self.headers['Connection'] = 'close' 109 self.headers['Connection'] = 'close'
105 del self.headers['Proxy-Connection'] 110 del self.headers['Proxy-Connection']
106 for key_val in self.headers.items(): 111 for key, val in self.headers.items():
107 soc.send("%s: %s\r\n" % key_val) 112 soc.send(b"%s: %s\r\n" % (pycompat.bytestr(key),
108 soc.send("\r\n") 113 pycompat.bytestr(val)))
114 soc.send(b"\r\n")
109 self._read_write(soc) 115 self._read_write(soc)
110 finally: 116 finally:
111 print("\t" "bye") 117 print("\t" "bye")
112 soc.close() 118 soc.close()
113 self.connection.close() 119 self.connection.close()