Mercurial > hg
annotate tests/tinyproxy.py @ 29555:121d11814c62
hgweb: use sslutil.wrapserversocket()
This patch transitions the built-in HTTPS server to use sslutil for
creating the server socket.
As part of this transition, we implement developer-only config options
to control CA loading and whether to require client certificates. This
eliminates the need for the custom extension in test-https.t to define
these.
There is a slight change in behavior with regards to protocol
selection. Before, we would always use the TLS 1.0 constant to define
the protocol version. This would *only* use TLS 1.0. sslutil defaults
to TLS 1.0+. So this patch improves the security of `hg serve` out of
the box by allowing it to use TLS 1.1 and 1.2 (if available).
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 12 Jul 2016 23:12:03 -0700 |
parents | 33770d2b6cf9 |
children | 143d21a7343e |
rev | line source |
---|---|
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
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 |
27302
faca4adfed0a
tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25660
diff
changeset
|
17 import BaseHTTPServer |
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 |
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28778
diff
changeset
|
23 from mercurial import util |
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28778
diff
changeset
|
24 |
80880ad3fccd
py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28778
diff
changeset
|
25 urlparse = util.urlparse |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
26 socketserver = util.socketserver |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
27 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
28 class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler): |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
29 __base = BaseHTTPServer.BaseHTTPRequestHandler |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
30 __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
|
31 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
32 server_version = "TinyHTTPProxy/" + __version__ |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
33 rbufsize = 0 # self.rfile Be unbuffered |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
34 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
35 def handle(self): |
27637
b502138f5faa
cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents:
27302
diff
changeset
|
36 (ip, port) = self.client_address |
14971
0b21ae0a2366
tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14494
diff
changeset
|
37 allowed = getattr(self, 'allowed_clients', None) |
0b21ae0a2366
tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
14494
diff
changeset
|
38 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
|
39 self.raw_requestline = self.rfile.readline() |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
40 if self.parse_request(): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
41 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
|
42 else: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
43 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
|
44 |
14093
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
10282
diff
changeset
|
45 def log_request(self, code='-', size='-'): |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
10282
diff
changeset
|
46 xheaders = [h for h in self.headers.items() if h[0].startswith('x-')] |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
10282
diff
changeset
|
47 self.log_message('"%s" %s %s%s', |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
10282
diff
changeset
|
48 self.requestline, str(code), str(size), |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
10282
diff
changeset
|
49 ''.join([' %s:%s' % h for h in sorted(xheaders)])) |
ce99d887585f
httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents:
10282
diff
changeset
|
50 |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 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
|
52 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
|
53 if i >= 0: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
54 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
|
55 else: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
56 host_port = netloc, 80 |
28646
f452c1cf7a8f
tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27637
diff
changeset
|
57 print("\t" "connect to %s:%d" % host_port) |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
58 try: soc.connect(host_port) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18519
diff
changeset
|
59 except socket.error as arg: |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 try: msg = arg[1] |
16703
7292a4618f46
cleanup: replace more naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents:
16300
diff
changeset
|
61 except (IndexError, TypeError): 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
|
62 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
|
63 return 0 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 return 1 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 def do_CONNECT(self): |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
67 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
68 try: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
69 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
|
70 self.log_request(200) |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
71 self.wfile.write(self.protocol_version + |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
72 " 200 Connection established\r\n") |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
73 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string()) |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
74 self.wfile.write("\r\n") |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
75 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
|
76 finally: |
28646
f452c1cf7a8f
tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27637
diff
changeset
|
77 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
|
78 soc.close() |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
79 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
|
80 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 def do_GET(self): |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
82 (scm, netloc, path, params, query, fragment) = urlparse.urlparse( |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
83 self.path, 'http') |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
84 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
|
85 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
|
86 return |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
87 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
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(netloc, 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() |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
91 soc.send("%s %s %s\r\n" % ( |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 self.command, |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 urlparse.urlunparse(('', '', path, params, query, '')), |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 self.request_version)) |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 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
|
96 del self.headers['Proxy-Connection'] |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
97 for key_val in self.headers.items(): |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 soc.send("%s: %s\r\n" % key_val) |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
99 soc.send("\r\n") |
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) |
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 _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
|
107 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
|
108 ow = [] |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
109 count = 0 |
14494
1ffeeb91c55d
check-code: flag 0/1 used as constant Boolean expression
Martin Geisler <mg@lazybytes.net>
parents:
14093
diff
changeset
|
110 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
|
111 count += 1 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
112 (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
|
113 if exs: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
114 break |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
115 if ins: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 else: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
120 out = soc |
18519
ca430fb6a668
tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents:
16703
diff
changeset
|
121 try: |
ca430fb6a668
tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents:
16703
diff
changeset
|
122 data = i.recv(8192) |
ca430fb6a668
tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents:
16703
diff
changeset
|
123 except socket.error: |
ca430fb6a668
tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents:
16703
diff
changeset
|
124 break |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 if data: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
126 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
|
127 count = 0 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
128 else: |
28646
f452c1cf7a8f
tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27637
diff
changeset
|
129 print("\t" "idle", count) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
130 if count == max_idling: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
131 break |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
132 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
133 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
|
134 do_POST = do_GET |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
135 do_PUT = do_GET |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
2337
diff
changeset
|
136 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
|
137 |
29433
33770d2b6cf9
py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
29431
diff
changeset
|
138 class ThreadingHTTPServer (socketserver.ThreadingMixIn, |
16300
74e114ac6ec1
tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents:
14971
diff
changeset
|
139 BaseHTTPServer.HTTPServer): |
74e114ac6ec1
tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents:
14971
diff
changeset
|
140 def __init__(self, *args, **kwargs): |
74e114ac6ec1
tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents:
14971
diff
changeset
|
141 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs) |
74e114ac6ec1
tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents:
14971
diff
changeset
|
142 a = open("proxy.pid", "w") |
74e114ac6ec1
tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents:
14971
diff
changeset
|
143 a.write(str(os.getpid()) + "\n") |
74e114ac6ec1
tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents:
14971
diff
changeset
|
144 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
|
145 |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
146 if __name__ == '__main__': |
28778
256d90bb12fa
tests: make tinyproxy.py not import sys.argv by name
Yuya Nishihara <yuya@tcha.org>
parents:
28773
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 else: |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
151 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
|
152 allowed = [] |
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
153 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
|
154 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
|
155 allowed.append(client) |
28646
f452c1cf7a8f
tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27637
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 else: |
28646
f452c1cf7a8f
tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27637
diff
changeset
|
160 print("Any clients will be served...") |
2337
3f24bc5dee81
http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
161 BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer) |