annotate tests/tinyproxy.py @ 46803:13d973690ecd

debugdiscovery: document relevant config option These config option are here for people to experiment with it. So lets document them in the command that is used for experimentation. Differential Revision: https://phab.mercurial-scm.org/D10231
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 17 Mar 2021 18:40:50 +0100
parents c102b704edb5
children 23f5ed6dbcb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
1 #!/usr/bin/env python3
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
2
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
3 from __future__ import absolute_import, print_function
27302
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
4
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
5 __doc__ = """Tiny HTTP Proxy.
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
6
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
7 This module implements GET, HEAD, POST, PUT and DELETE methods
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
8 on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
9 method is also implemented experimentally, but has not been
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
10 tested yet.
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
11
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
12 Any help will be greatly appreciated. SUZUKI Hisao
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
13 """
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
14
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
15 __version__ = "0.2.1"
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
16
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
17 import optparse
27302
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
18 import os
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
19 import select
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
20 import socket
28778
256d90bb12fa tests: make tinyproxy.py not import sys.argv by name
Yuya Nishihara <yuya@tcha.org>
parents: 28773
diff changeset
21 import sys
29431
80880ad3fccd py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28778
diff changeset
22
41709
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
23 from mercurial import (
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
24 pycompat,
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
25 util,
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
26 )
29431
80880ad3fccd py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28778
diff changeset
27
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
28 httpserver = util.httpserver
29433
33770d2b6cf9 py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29431
diff changeset
29 socketserver = util.socketserver
31571
b2a41a826d71 tests: use urlreq in tinyproxy.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31005
diff changeset
30 urlreq = util.urlreq
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
31
31005
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
32 if os.environ.get('HGIPV6', '0') == '1':
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
33 family = socket.AF_INET6
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
34 else:
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
35 family = socket.AF_INET
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
36
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
37
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
38 class ProxyHandler(httpserver.basehttprequesthandler):
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
39 __base = httpserver.basehttprequesthandler
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
40 __base_handle = __base.handle
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
41
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
42 server_version = "TinyHTTPProxy/" + __version__
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
43 rbufsize = 0 # self.rfile Be unbuffered
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
44
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
45 def handle(self):
27637
b502138f5faa cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents: 27302
diff changeset
46 (ip, port) = self.client_address
14971
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14494
diff changeset
47 allowed = getattr(self, 'allowed_clients', None)
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14494
diff changeset
48 if allowed is not None and ip not in allowed:
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
49 self.raw_requestline = self.rfile.readline()
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
50 if self.parse_request():
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
51 self.send_error(403)
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
52 else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
53 self.__base_handle()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
54
14093
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
55 def log_request(self, code='-', size='-'):
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
56 xheaders = [h for h in self.headers.items() if h[0].startswith('x-')]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
57 self.log_message(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
58 '"%s" %s %s%s',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
59 self.requestline,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
60 str(code),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
61 str(size),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
62 ''.join([' %s:%s' % h for h in sorted(xheaders)]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
63 )
32906
23b07333a8b2 tinyproxy: explicitly flush logged messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 31571
diff changeset
64 # Flush for Windows, so output isn't lost on TerminateProcess()
32916
88c1d13b637b test-http-proxy: redirect proxy stdout to /dev/null
Matt Harbison <matt_harbison@yahoo.com>
parents: 32906
diff changeset
65 sys.stdout.flush()
32906
23b07333a8b2 tinyproxy: explicitly flush logged messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 31571
diff changeset
66 sys.stderr.flush()
14093
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
67
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
68 def _connect_to(self, netloc, soc):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
69 i = netloc.find(':')
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
70 if i >= 0:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
71 host_port = netloc[:i], int(netloc[i + 1 :])
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
72 else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
73 host_port = netloc, 80
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
74 print("\t" "connect to %s:%d" % host_port)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
75 try:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
76 soc.connect(host_port)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18519
diff changeset
77 except socket.error as arg:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
78 try:
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
79 msg = arg[1]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
80 except (IndexError, TypeError):
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
81 msg = arg
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
82 self.send_error(404, msg)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
83 return 0
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
84 return 1
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
85
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
86 def do_CONNECT(self):
31005
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
87 soc = socket.socket(family, socket.SOCK_STREAM)
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
88 try:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
89 if self._connect_to(self.path, soc):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
90 self.log_request(200)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
91 self.wfile.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
92 pycompat.bytestr(self.protocol_version)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
93 + b" 200 Connection established\r\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
94 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
95 self.wfile.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
96 b"Proxy-agent: %s\r\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
97 % pycompat.bytestr(self.version_string())
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
98 )
41709
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
99 self.wfile.write(b"\r\n")
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
100 self._read_write(soc, 300)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
101 finally:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
102 print("\t" "bye")
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
103 soc.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
104 self.connection.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
105
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
106 def do_GET(self):
31571
b2a41a826d71 tests: use urlreq in tinyproxy.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31005
diff changeset
107 (scm, netloc, path, params, query, fragment) = urlreq.urlparse(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
108 self.path, 'http'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
109 )
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
110 if scm != 'http' or fragment or not netloc:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
111 self.send_error(400, "bad url %s" % self.path)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
112 return
31005
d8d698bcdcd6 tinyproxy: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents: 29566
diff changeset
113 soc = socket.socket(family, socket.SOCK_STREAM)
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
114 try:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
115 if self._connect_to(netloc, soc):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
116 self.log_request()
41709
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
117 url = urlreq.urlunparse(('', '', path, params, query, ''))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
118 soc.send(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
119 b"%s %s %s\r\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
120 % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
121 pycompat.bytestr(self.command),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
122 pycompat.bytestr(url),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
123 pycompat.bytestr(self.request_version),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
124 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
125 )
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
126 self.headers['Connection'] = 'close'
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
127 del self.headers['Proxy-Connection']
41709
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
128 for key, val in self.headers.items():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
129 soc.send(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
130 b"%s: %s\r\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
131 % (pycompat.bytestr(key), pycompat.bytestr(val))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
132 )
41709
97e2442a4595 py3: port tinyproxy.py to work with Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32916
diff changeset
133 soc.send(b"\r\n")
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
134 self._read_write(soc)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
135 finally:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
136 print("\t" "bye")
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
137 soc.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
138 self.connection.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
139
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
140 def _read_write(self, soc, max_idling=20):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
141 iw = [self.connection, soc]
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
142 ow = []
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
143 count = 0
14494
1ffeeb91c55d check-code: flag 0/1 used as constant Boolean expression
Martin Geisler <mg@lazybytes.net>
parents: 14093
diff changeset
144 while True:
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
145 count += 1
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
146 (ins, _, exs) = select.select(iw, ow, iw, 3)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
147 if exs:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
148 break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
149 if ins:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
150 for i in ins:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
151 if i is soc:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
152 out = self.connection
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
153 else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
154 out = soc
18519
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
155 try:
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
156 data = i.recv(8192)
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
157 except socket.error:
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
158 break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
159 if data:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
160 out.send(data)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
161 count = 0
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
162 else:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
163 print("\t" "idle", count)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
164 if count == max_idling:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
165 break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
166
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
167 do_HEAD = do_GET
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
168 do_POST = do_GET
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
169 do_PUT = do_GET
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
170 do_DELETE = do_GET
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
171
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
172
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
173 class ThreadingHTTPServer(socketserver.ThreadingMixIn, httpserver.httpserver):
16300
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
174 def __init__(self, *args, **kwargs):
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
175 httpserver.httpserver.__init__(self, *args, **kwargs)
16300
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
176 a = open("proxy.pid", "w")
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
177 a.write(str(os.getpid()) + "\n")
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
178 a.close()
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
179
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
180
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
181 def runserver(port=8000, bind=""):
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
182 server_address = (bind, port)
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
183 ProxyHandler.protocol_version = "HTTP/1.0"
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
184 httpd = ThreadingHTTPServer(server_address, ProxyHandler)
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
185 sa = httpd.socket.getsockname()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
186 print("Serving HTTP on", sa[0], "port", sa[1], "...")
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
187 try:
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
188 httpd.serve_forever()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
189 except KeyboardInterrupt:
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
190 print("\nKeyboard interrupt received, exiting.")
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
191 httpd.server_close()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
192 sys.exit(0)
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
193
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
194
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
195 if __name__ == '__main__':
28778
256d90bb12fa tests: make tinyproxy.py not import sys.argv by name
Yuya Nishihara <yuya@tcha.org>
parents: 28773
diff changeset
196 argv = sys.argv
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
197 if argv[1:] and argv[1] in ('-h', '--help'):
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
198 print(argv[0], "[port [allowed_client_name ...]]")
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
199 else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
200 if argv[2:]:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
201 allowed = []
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
202 for name in argv[2:]:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
203 client = socket.gethostbyname(name)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
204 allowed.append(client)
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
205 print("Accept: %s (%s)" % (client, name))
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
206 ProxyHandler.allowed_clients = allowed
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
207 del argv[2:]
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
208 else:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
209 print("Any clients will be served...")
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
210
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
211 parser = optparse.OptionParser()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
212 parser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
213 '-b',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
214 '--bind',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
215 metavar='ADDRESS',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
216 help='Specify alternate bind address ' '[default: all interfaces]',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
217 default='',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41709
diff changeset
218 )
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
219 (options, args) = parser.parse_args()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
220 port = 8000
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
221 if len(args) == 1:
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
222 port = int(args[0])
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
223 runserver(port, options.bind)