##// END OF EJS Templates
tests: use absolute_import in tinyproxy...
Gregory Szorc -
r27302:faca4adf default
parent child Browse files
Show More
@@ -1,224 +1,223 b''
1 #require test-repo
1 #require test-repo
2
2
3 $ cd "$TESTDIR"/..
3 $ cd "$TESTDIR"/..
4
4
5 $ hg files 'set:(**.py)' | xargs python contrib/check-py3-compat.py
5 $ hg files 'set:(**.py)' | xargs python contrib/check-py3-compat.py
6 contrib/casesmash.py not using absolute_import
6 contrib/casesmash.py not using absolute_import
7 contrib/check-code.py not using absolute_import
7 contrib/check-code.py not using absolute_import
8 contrib/check-code.py requires print_function
8 contrib/check-code.py requires print_function
9 contrib/check-config.py not using absolute_import
9 contrib/check-config.py not using absolute_import
10 contrib/check-config.py requires print_function
10 contrib/check-config.py requires print_function
11 contrib/debugcmdserver.py not using absolute_import
11 contrib/debugcmdserver.py not using absolute_import
12 contrib/debugcmdserver.py requires print_function
12 contrib/debugcmdserver.py requires print_function
13 contrib/debugshell.py not using absolute_import
13 contrib/debugshell.py not using absolute_import
14 contrib/fixpax.py not using absolute_import
14 contrib/fixpax.py not using absolute_import
15 contrib/fixpax.py requires print_function
15 contrib/fixpax.py requires print_function
16 contrib/hgclient.py not using absolute_import
16 contrib/hgclient.py not using absolute_import
17 contrib/hgclient.py requires print_function
17 contrib/hgclient.py requires print_function
18 contrib/hgfixes/__init__.py not using absolute_import
18 contrib/hgfixes/__init__.py not using absolute_import
19 contrib/hgfixes/fix_bytes.py not using absolute_import
19 contrib/hgfixes/fix_bytes.py not using absolute_import
20 contrib/hgfixes/fix_bytesmod.py not using absolute_import
20 contrib/hgfixes/fix_bytesmod.py not using absolute_import
21 contrib/hgfixes/fix_leftover_imports.py not using absolute_import
21 contrib/hgfixes/fix_leftover_imports.py not using absolute_import
22 contrib/import-checker.py not using absolute_import
22 contrib/import-checker.py not using absolute_import
23 contrib/import-checker.py requires print_function
23 contrib/import-checker.py requires print_function
24 contrib/memory.py not using absolute_import
24 contrib/memory.py not using absolute_import
25 contrib/perf.py not using absolute_import
25 contrib/perf.py not using absolute_import
26 contrib/python-hook-examples.py not using absolute_import
26 contrib/python-hook-examples.py not using absolute_import
27 contrib/revsetbenchmarks.py not using absolute_import
27 contrib/revsetbenchmarks.py not using absolute_import
28 contrib/revsetbenchmarks.py requires print_function
28 contrib/revsetbenchmarks.py requires print_function
29 contrib/showstack.py not using absolute_import
29 contrib/showstack.py not using absolute_import
30 contrib/synthrepo.py not using absolute_import
30 contrib/synthrepo.py not using absolute_import
31 contrib/win32/hgwebdir_wsgi.py not using absolute_import
31 contrib/win32/hgwebdir_wsgi.py not using absolute_import
32 doc/check-seclevel.py not using absolute_import
32 doc/check-seclevel.py not using absolute_import
33 doc/gendoc.py not using absolute_import
33 doc/gendoc.py not using absolute_import
34 doc/hgmanpage.py not using absolute_import
34 doc/hgmanpage.py not using absolute_import
35 hgext/__init__.py not using absolute_import
35 hgext/__init__.py not using absolute_import
36 hgext/acl.py not using absolute_import
36 hgext/acl.py not using absolute_import
37 hgext/blackbox.py not using absolute_import
37 hgext/blackbox.py not using absolute_import
38 hgext/bugzilla.py not using absolute_import
38 hgext/bugzilla.py not using absolute_import
39 hgext/censor.py not using absolute_import
39 hgext/censor.py not using absolute_import
40 hgext/children.py not using absolute_import
40 hgext/children.py not using absolute_import
41 hgext/churn.py not using absolute_import
41 hgext/churn.py not using absolute_import
42 hgext/clonebundles.py not using absolute_import
42 hgext/clonebundles.py not using absolute_import
43 hgext/color.py not using absolute_import
43 hgext/color.py not using absolute_import
44 hgext/convert/__init__.py not using absolute_import
44 hgext/convert/__init__.py not using absolute_import
45 hgext/convert/bzr.py not using absolute_import
45 hgext/convert/bzr.py not using absolute_import
46 hgext/convert/common.py not using absolute_import
46 hgext/convert/common.py not using absolute_import
47 hgext/convert/convcmd.py not using absolute_import
47 hgext/convert/convcmd.py not using absolute_import
48 hgext/convert/cvs.py not using absolute_import
48 hgext/convert/cvs.py not using absolute_import
49 hgext/convert/cvsps.py not using absolute_import
49 hgext/convert/cvsps.py not using absolute_import
50 hgext/convert/darcs.py not using absolute_import
50 hgext/convert/darcs.py not using absolute_import
51 hgext/convert/filemap.py not using absolute_import
51 hgext/convert/filemap.py not using absolute_import
52 hgext/convert/git.py not using absolute_import
52 hgext/convert/git.py not using absolute_import
53 hgext/convert/gnuarch.py not using absolute_import
53 hgext/convert/gnuarch.py not using absolute_import
54 hgext/convert/hg.py not using absolute_import
54 hgext/convert/hg.py not using absolute_import
55 hgext/convert/monotone.py not using absolute_import
55 hgext/convert/monotone.py not using absolute_import
56 hgext/convert/p4.py not using absolute_import
56 hgext/convert/p4.py not using absolute_import
57 hgext/convert/subversion.py not using absolute_import
57 hgext/convert/subversion.py not using absolute_import
58 hgext/convert/transport.py not using absolute_import
58 hgext/convert/transport.py not using absolute_import
59 hgext/eol.py not using absolute_import
59 hgext/eol.py not using absolute_import
60 hgext/extdiff.py not using absolute_import
60 hgext/extdiff.py not using absolute_import
61 hgext/factotum.py not using absolute_import
61 hgext/factotum.py not using absolute_import
62 hgext/fetch.py not using absolute_import
62 hgext/fetch.py not using absolute_import
63 hgext/gpg.py not using absolute_import
63 hgext/gpg.py not using absolute_import
64 hgext/graphlog.py not using absolute_import
64 hgext/graphlog.py not using absolute_import
65 hgext/hgcia.py not using absolute_import
65 hgext/hgcia.py not using absolute_import
66 hgext/hgk.py not using absolute_import
66 hgext/hgk.py not using absolute_import
67 hgext/highlight/__init__.py not using absolute_import
67 hgext/highlight/__init__.py not using absolute_import
68 hgext/highlight/highlight.py not using absolute_import
68 hgext/highlight/highlight.py not using absolute_import
69 hgext/histedit.py not using absolute_import
69 hgext/histedit.py not using absolute_import
70 hgext/keyword.py not using absolute_import
70 hgext/keyword.py not using absolute_import
71 hgext/largefiles/__init__.py not using absolute_import
71 hgext/largefiles/__init__.py not using absolute_import
72 hgext/largefiles/basestore.py not using absolute_import
72 hgext/largefiles/basestore.py not using absolute_import
73 hgext/largefiles/lfcommands.py not using absolute_import
73 hgext/largefiles/lfcommands.py not using absolute_import
74 hgext/largefiles/lfutil.py not using absolute_import
74 hgext/largefiles/lfutil.py not using absolute_import
75 hgext/largefiles/localstore.py not using absolute_import
75 hgext/largefiles/localstore.py not using absolute_import
76 hgext/largefiles/overrides.py not using absolute_import
76 hgext/largefiles/overrides.py not using absolute_import
77 hgext/largefiles/proto.py not using absolute_import
77 hgext/largefiles/proto.py not using absolute_import
78 hgext/largefiles/remotestore.py not using absolute_import
78 hgext/largefiles/remotestore.py not using absolute_import
79 hgext/largefiles/reposetup.py not using absolute_import
79 hgext/largefiles/reposetup.py not using absolute_import
80 hgext/largefiles/uisetup.py not using absolute_import
80 hgext/largefiles/uisetup.py not using absolute_import
81 hgext/largefiles/wirestore.py not using absolute_import
81 hgext/largefiles/wirestore.py not using absolute_import
82 hgext/mq.py not using absolute_import
82 hgext/mq.py not using absolute_import
83 hgext/notify.py not using absolute_import
83 hgext/notify.py not using absolute_import
84 hgext/pager.py not using absolute_import
84 hgext/pager.py not using absolute_import
85 hgext/patchbomb.py not using absolute_import
85 hgext/patchbomb.py not using absolute_import
86 hgext/purge.py not using absolute_import
86 hgext/purge.py not using absolute_import
87 hgext/rebase.py not using absolute_import
87 hgext/rebase.py not using absolute_import
88 hgext/record.py not using absolute_import
88 hgext/record.py not using absolute_import
89 hgext/relink.py not using absolute_import
89 hgext/relink.py not using absolute_import
90 hgext/schemes.py not using absolute_import
90 hgext/schemes.py not using absolute_import
91 hgext/share.py not using absolute_import
91 hgext/share.py not using absolute_import
92 hgext/shelve.py not using absolute_import
92 hgext/shelve.py not using absolute_import
93 hgext/strip.py not using absolute_import
93 hgext/strip.py not using absolute_import
94 hgext/transplant.py not using absolute_import
94 hgext/transplant.py not using absolute_import
95 hgext/win32mbcs.py not using absolute_import
95 hgext/win32mbcs.py not using absolute_import
96 hgext/win32text.py not using absolute_import
96 hgext/win32text.py not using absolute_import
97 hgext/zeroconf/Zeroconf.py not using absolute_import
97 hgext/zeroconf/Zeroconf.py not using absolute_import
98 hgext/zeroconf/Zeroconf.py requires print_function
98 hgext/zeroconf/Zeroconf.py requires print_function
99 hgext/zeroconf/__init__.py not using absolute_import
99 hgext/zeroconf/__init__.py not using absolute_import
100 i18n/check-translation.py not using absolute_import
100 i18n/check-translation.py not using absolute_import
101 i18n/polib.py not using absolute_import
101 i18n/polib.py not using absolute_import
102 mercurial/byterange.py not using absolute_import
102 mercurial/byterange.py not using absolute_import
103 mercurial/cmdutil.py not using absolute_import
103 mercurial/cmdutil.py not using absolute_import
104 mercurial/commands.py not using absolute_import
104 mercurial/commands.py not using absolute_import
105 mercurial/commandserver.py not using absolute_import
105 mercurial/commandserver.py not using absolute_import
106 mercurial/context.py not using absolute_import
106 mercurial/context.py not using absolute_import
107 mercurial/destutil.py not using absolute_import
107 mercurial/destutil.py not using absolute_import
108 mercurial/dirstate.py not using absolute_import
108 mercurial/dirstate.py not using absolute_import
109 mercurial/dispatch.py requires print_function
109 mercurial/dispatch.py requires print_function
110 mercurial/encoding.py not using absolute_import
110 mercurial/encoding.py not using absolute_import
111 mercurial/exchange.py not using absolute_import
111 mercurial/exchange.py not using absolute_import
112 mercurial/help.py not using absolute_import
112 mercurial/help.py not using absolute_import
113 mercurial/httpclient/__init__.py not using absolute_import
113 mercurial/httpclient/__init__.py not using absolute_import
114 mercurial/httpclient/_readers.py not using absolute_import
114 mercurial/httpclient/_readers.py not using absolute_import
115 mercurial/httpclient/socketutil.py not using absolute_import
115 mercurial/httpclient/socketutil.py not using absolute_import
116 mercurial/httpconnection.py not using absolute_import
116 mercurial/httpconnection.py not using absolute_import
117 mercurial/keepalive.py not using absolute_import
117 mercurial/keepalive.py not using absolute_import
118 mercurial/keepalive.py requires print_function
118 mercurial/keepalive.py requires print_function
119 mercurial/localrepo.py not using absolute_import
119 mercurial/localrepo.py not using absolute_import
120 mercurial/lsprof.py requires print_function
120 mercurial/lsprof.py requires print_function
121 mercurial/lsprofcalltree.py not using absolute_import
121 mercurial/lsprofcalltree.py not using absolute_import
122 mercurial/lsprofcalltree.py requires print_function
122 mercurial/lsprofcalltree.py requires print_function
123 mercurial/mail.py requires print_function
123 mercurial/mail.py requires print_function
124 mercurial/manifest.py not using absolute_import
124 mercurial/manifest.py not using absolute_import
125 mercurial/mdiff.py not using absolute_import
125 mercurial/mdiff.py not using absolute_import
126 mercurial/obsolete.py not using absolute_import
126 mercurial/obsolete.py not using absolute_import
127 mercurial/patch.py not using absolute_import
127 mercurial/patch.py not using absolute_import
128 mercurial/pure/__init__.py not using absolute_import
128 mercurial/pure/__init__.py not using absolute_import
129 mercurial/pure/base85.py not using absolute_import
129 mercurial/pure/base85.py not using absolute_import
130 mercurial/pure/bdiff.py not using absolute_import
130 mercurial/pure/bdiff.py not using absolute_import
131 mercurial/pure/diffhelpers.py not using absolute_import
131 mercurial/pure/diffhelpers.py not using absolute_import
132 mercurial/pure/mpatch.py not using absolute_import
132 mercurial/pure/mpatch.py not using absolute_import
133 mercurial/pure/osutil.py not using absolute_import
133 mercurial/pure/osutil.py not using absolute_import
134 mercurial/pure/parsers.py not using absolute_import
134 mercurial/pure/parsers.py not using absolute_import
135 mercurial/pvec.py not using absolute_import
135 mercurial/pvec.py not using absolute_import
136 mercurial/py3kcompat.py not using absolute_import
136 mercurial/py3kcompat.py not using absolute_import
137 mercurial/revlog.py not using absolute_import
137 mercurial/revlog.py not using absolute_import
138 mercurial/scmposix.py not using absolute_import
138 mercurial/scmposix.py not using absolute_import
139 mercurial/scmutil.py not using absolute_import
139 mercurial/scmutil.py not using absolute_import
140 mercurial/scmwindows.py not using absolute_import
140 mercurial/scmwindows.py not using absolute_import
141 mercurial/similar.py not using absolute_import
141 mercurial/similar.py not using absolute_import
142 mercurial/store.py not using absolute_import
142 mercurial/store.py not using absolute_import
143 mercurial/util.py not using absolute_import
143 mercurial/util.py not using absolute_import
144 mercurial/windows.py not using absolute_import
144 mercurial/windows.py not using absolute_import
145 setup.py not using absolute_import
145 setup.py not using absolute_import
146 tests/filterpyflakes.py requires print_function
146 tests/filterpyflakes.py requires print_function
147 tests/generate-working-copy-states.py requires print_function
147 tests/generate-working-copy-states.py requires print_function
148 tests/get-with-headers.py requires print_function
148 tests/get-with-headers.py requires print_function
149 tests/heredoctest.py requires print_function
149 tests/heredoctest.py requires print_function
150 tests/hypothesishelpers.py not using absolute_import
150 tests/hypothesishelpers.py not using absolute_import
151 tests/hypothesishelpers.py requires print_function
151 tests/hypothesishelpers.py requires print_function
152 tests/killdaemons.py not using absolute_import
152 tests/killdaemons.py not using absolute_import
153 tests/md5sum.py not using absolute_import
153 tests/md5sum.py not using absolute_import
154 tests/mockblackbox.py not using absolute_import
154 tests/mockblackbox.py not using absolute_import
155 tests/printenv.py not using absolute_import
155 tests/printenv.py not using absolute_import
156 tests/readlink.py not using absolute_import
156 tests/readlink.py not using absolute_import
157 tests/readlink.py requires print_function
157 tests/readlink.py requires print_function
158 tests/revlog-formatv0.py not using absolute_import
158 tests/revlog-formatv0.py not using absolute_import
159 tests/run-tests.py not using absolute_import
159 tests/run-tests.py not using absolute_import
160 tests/seq.py not using absolute_import
160 tests/seq.py not using absolute_import
161 tests/seq.py requires print_function
161 tests/seq.py requires print_function
162 tests/silenttestrunner.py not using absolute_import
162 tests/silenttestrunner.py not using absolute_import
163 tests/silenttestrunner.py requires print_function
163 tests/silenttestrunner.py requires print_function
164 tests/sitecustomize.py not using absolute_import
164 tests/sitecustomize.py not using absolute_import
165 tests/svn-safe-append.py not using absolute_import
165 tests/svn-safe-append.py not using absolute_import
166 tests/svnxml.py not using absolute_import
166 tests/svnxml.py not using absolute_import
167 tests/test-ancestor.py requires print_function
167 tests/test-ancestor.py requires print_function
168 tests/test-atomictempfile.py not using absolute_import
168 tests/test-atomictempfile.py not using absolute_import
169 tests/test-batching.py not using absolute_import
169 tests/test-batching.py not using absolute_import
170 tests/test-batching.py requires print_function
170 tests/test-batching.py requires print_function
171 tests/test-bdiff.py not using absolute_import
171 tests/test-bdiff.py not using absolute_import
172 tests/test-bdiff.py requires print_function
172 tests/test-bdiff.py requires print_function
173 tests/test-context.py not using absolute_import
173 tests/test-context.py not using absolute_import
174 tests/test-context.py requires print_function
174 tests/test-context.py requires print_function
175 tests/test-demandimport.py not using absolute_import
175 tests/test-demandimport.py not using absolute_import
176 tests/test-demandimport.py requires print_function
176 tests/test-demandimport.py requires print_function
177 tests/test-dispatch.py not using absolute_import
177 tests/test-dispatch.py not using absolute_import
178 tests/test-dispatch.py requires print_function
178 tests/test-dispatch.py requires print_function
179 tests/test-doctest.py not using absolute_import
179 tests/test-doctest.py not using absolute_import
180 tests/test-duplicateoptions.py not using absolute_import
180 tests/test-duplicateoptions.py not using absolute_import
181 tests/test-duplicateoptions.py requires print_function
181 tests/test-duplicateoptions.py requires print_function
182 tests/test-filecache.py not using absolute_import
182 tests/test-filecache.py not using absolute_import
183 tests/test-filecache.py requires print_function
183 tests/test-filecache.py requires print_function
184 tests/test-filelog.py not using absolute_import
184 tests/test-filelog.py not using absolute_import
185 tests/test-filelog.py requires print_function
185 tests/test-filelog.py requires print_function
186 tests/test-hg-parseurl.py not using absolute_import
186 tests/test-hg-parseurl.py not using absolute_import
187 tests/test-hg-parseurl.py requires print_function
187 tests/test-hg-parseurl.py requires print_function
188 tests/test-hgweb-auth.py not using absolute_import
188 tests/test-hgweb-auth.py not using absolute_import
189 tests/test-hgweb-auth.py requires print_function
189 tests/test-hgweb-auth.py requires print_function
190 tests/test-hgwebdir-paths.py not using absolute_import
190 tests/test-hgwebdir-paths.py not using absolute_import
191 tests/test-hybridencode.py not using absolute_import
191 tests/test-hybridencode.py not using absolute_import
192 tests/test-hybridencode.py requires print_function
192 tests/test-hybridencode.py requires print_function
193 tests/test-lrucachedict.py not using absolute_import
193 tests/test-lrucachedict.py not using absolute_import
194 tests/test-lrucachedict.py requires print_function
194 tests/test-lrucachedict.py requires print_function
195 tests/test-manifest.py not using absolute_import
195 tests/test-manifest.py not using absolute_import
196 tests/test-minirst.py not using absolute_import
196 tests/test-minirst.py not using absolute_import
197 tests/test-minirst.py requires print_function
197 tests/test-minirst.py requires print_function
198 tests/test-parseindex2.py not using absolute_import
198 tests/test-parseindex2.py not using absolute_import
199 tests/test-parseindex2.py requires print_function
199 tests/test-parseindex2.py requires print_function
200 tests/test-pathencode.py not using absolute_import
200 tests/test-pathencode.py not using absolute_import
201 tests/test-pathencode.py requires print_function
201 tests/test-pathencode.py requires print_function
202 tests/test-propertycache.py not using absolute_import
202 tests/test-propertycache.py not using absolute_import
203 tests/test-propertycache.py requires print_function
203 tests/test-propertycache.py requires print_function
204 tests/test-revlog-ancestry.py not using absolute_import
204 tests/test-revlog-ancestry.py not using absolute_import
205 tests/test-revlog-ancestry.py requires print_function
205 tests/test-revlog-ancestry.py requires print_function
206 tests/test-run-tests.py not using absolute_import
206 tests/test-run-tests.py not using absolute_import
207 tests/test-simplemerge.py not using absolute_import
207 tests/test-simplemerge.py not using absolute_import
208 tests/test-status-inprocess.py not using absolute_import
208 tests/test-status-inprocess.py not using absolute_import
209 tests/test-status-inprocess.py requires print_function
209 tests/test-status-inprocess.py requires print_function
210 tests/test-symlink-os-yes-fs-no.py not using absolute_import
210 tests/test-symlink-os-yes-fs-no.py not using absolute_import
211 tests/test-trusted.py not using absolute_import
211 tests/test-trusted.py not using absolute_import
212 tests/test-trusted.py requires print_function
212 tests/test-trusted.py requires print_function
213 tests/test-ui-color.py not using absolute_import
213 tests/test-ui-color.py not using absolute_import
214 tests/test-ui-color.py requires print_function
214 tests/test-ui-color.py requires print_function
215 tests/test-ui-config.py not using absolute_import
215 tests/test-ui-config.py not using absolute_import
216 tests/test-ui-config.py requires print_function
216 tests/test-ui-config.py requires print_function
217 tests/test-ui-verbosity.py not using absolute_import
217 tests/test-ui-verbosity.py not using absolute_import
218 tests/test-ui-verbosity.py requires print_function
218 tests/test-ui-verbosity.py requires print_function
219 tests/test-url.py not using absolute_import
219 tests/test-url.py not using absolute_import
220 tests/test-url.py requires print_function
220 tests/test-url.py requires print_function
221 tests/test-walkrepo.py requires print_function
221 tests/test-walkrepo.py requires print_function
222 tests/test-wireproto.py requires print_function
222 tests/test-wireproto.py requires print_function
223 tests/tinyproxy.py not using absolute_import
224 tests/tinyproxy.py requires print_function
223 tests/tinyproxy.py requires print_function
@@ -1,150 +1,157 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 from __future__ import absolute_import
4
3 __doc__ = """Tiny HTTP Proxy.
5 __doc__ = """Tiny HTTP Proxy.
4
6
5 This module implements GET, HEAD, POST, PUT and DELETE methods
7 This module implements GET, HEAD, POST, PUT and DELETE methods
6 on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
8 on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
7 method is also implemented experimentally, but has not been
9 method is also implemented experimentally, but has not been
8 tested yet.
10 tested yet.
9
11
10 Any help will be greatly appreciated. SUZUKI Hisao
12 Any help will be greatly appreciated. SUZUKI Hisao
11 """
13 """
12
14
13 __version__ = "0.2.1"
15 __version__ = "0.2.1"
14
16
15 import BaseHTTPServer, select, socket, SocketServer, urlparse, os
17 import BaseHTTPServer
18 import os
19 import select
20 import socket
21 import SocketServer
22 import urlparse
16
23
17 class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
24 class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
18 __base = BaseHTTPServer.BaseHTTPRequestHandler
25 __base = BaseHTTPServer.BaseHTTPRequestHandler
19 __base_handle = __base.handle
26 __base_handle = __base.handle
20
27
21 server_version = "TinyHTTPProxy/" + __version__
28 server_version = "TinyHTTPProxy/" + __version__
22 rbufsize = 0 # self.rfile Be unbuffered
29 rbufsize = 0 # self.rfile Be unbuffered
23
30
24 def handle(self):
31 def handle(self):
25 (ip, port) = self.client_address
32 (ip, port) = self.client_address
26 allowed = getattr(self, 'allowed_clients', None)
33 allowed = getattr(self, 'allowed_clients', None)
27 if allowed is not None and ip not in allowed:
34 if allowed is not None and ip not in allowed:
28 self.raw_requestline = self.rfile.readline()
35 self.raw_requestline = self.rfile.readline()
29 if self.parse_request():
36 if self.parse_request():
30 self.send_error(403)
37 self.send_error(403)
31 else:
38 else:
32 self.__base_handle()
39 self.__base_handle()
33
40
34 def log_request(self, code='-', size='-'):
41 def log_request(self, code='-', size='-'):
35 xheaders = [h for h in self.headers.items() if h[0].startswith('x-')]
42 xheaders = [h for h in self.headers.items() if h[0].startswith('x-')]
36 self.log_message('"%s" %s %s%s',
43 self.log_message('"%s" %s %s%s',
37 self.requestline, str(code), str(size),
44 self.requestline, str(code), str(size),
38 ''.join([' %s:%s' % h for h in sorted(xheaders)]))
45 ''.join([' %s:%s' % h for h in sorted(xheaders)]))
39
46
40 def _connect_to(self, netloc, soc):
47 def _connect_to(self, netloc, soc):
41 i = netloc.find(':')
48 i = netloc.find(':')
42 if i >= 0:
49 if i >= 0:
43 host_port = netloc[:i], int(netloc[i + 1:])
50 host_port = netloc[:i], int(netloc[i + 1:])
44 else:
51 else:
45 host_port = netloc, 80
52 host_port = netloc, 80
46 print "\t" "connect to %s:%d" % host_port
53 print "\t" "connect to %s:%d" % host_port
47 try: soc.connect(host_port)
54 try: soc.connect(host_port)
48 except socket.error as arg:
55 except socket.error as arg:
49 try: msg = arg[1]
56 try: msg = arg[1]
50 except (IndexError, TypeError): msg = arg
57 except (IndexError, TypeError): msg = arg
51 self.send_error(404, msg)
58 self.send_error(404, msg)
52 return 0
59 return 0
53 return 1
60 return 1
54
61
55 def do_CONNECT(self):
62 def do_CONNECT(self):
56 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
63 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
57 try:
64 try:
58 if self._connect_to(self.path, soc):
65 if self._connect_to(self.path, soc):
59 self.log_request(200)
66 self.log_request(200)
60 self.wfile.write(self.protocol_version +
67 self.wfile.write(self.protocol_version +
61 " 200 Connection established\r\n")
68 " 200 Connection established\r\n")
62 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
69 self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
63 self.wfile.write("\r\n")
70 self.wfile.write("\r\n")
64 self._read_write(soc, 300)
71 self._read_write(soc, 300)
65 finally:
72 finally:
66 print "\t" "bye"
73 print "\t" "bye"
67 soc.close()
74 soc.close()
68 self.connection.close()
75 self.connection.close()
69
76
70 def do_GET(self):
77 def do_GET(self):
71 (scm, netloc, path, params, query, fragment) = urlparse.urlparse(
78 (scm, netloc, path, params, query, fragment) = urlparse.urlparse(
72 self.path, 'http')
79 self.path, 'http')
73 if scm != 'http' or fragment or not netloc:
80 if scm != 'http' or fragment or not netloc:
74 self.send_error(400, "bad url %s" % self.path)
81 self.send_error(400, "bad url %s" % self.path)
75 return
82 return
76 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
83 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77 try:
84 try:
78 if self._connect_to(netloc, soc):
85 if self._connect_to(netloc, soc):
79 self.log_request()
86 self.log_request()
80 soc.send("%s %s %s\r\n" % (
87 soc.send("%s %s %s\r\n" % (
81 self.command,
88 self.command,
82 urlparse.urlunparse(('', '', path, params, query, '')),
89 urlparse.urlunparse(('', '', path, params, query, '')),
83 self.request_version))
90 self.request_version))
84 self.headers['Connection'] = 'close'
91 self.headers['Connection'] = 'close'
85 del self.headers['Proxy-Connection']
92 del self.headers['Proxy-Connection']
86 for key_val in self.headers.items():
93 for key_val in self.headers.items():
87 soc.send("%s: %s\r\n" % key_val)
94 soc.send("%s: %s\r\n" % key_val)
88 soc.send("\r\n")
95 soc.send("\r\n")
89 self._read_write(soc)
96 self._read_write(soc)
90 finally:
97 finally:
91 print "\t" "bye"
98 print "\t" "bye"
92 soc.close()
99 soc.close()
93 self.connection.close()
100 self.connection.close()
94
101
95 def _read_write(self, soc, max_idling=20):
102 def _read_write(self, soc, max_idling=20):
96 iw = [self.connection, soc]
103 iw = [self.connection, soc]
97 ow = []
104 ow = []
98 count = 0
105 count = 0
99 while True:
106 while True:
100 count += 1
107 count += 1
101 (ins, _, exs) = select.select(iw, ow, iw, 3)
108 (ins, _, exs) = select.select(iw, ow, iw, 3)
102 if exs:
109 if exs:
103 break
110 break
104 if ins:
111 if ins:
105 for i in ins:
112 for i in ins:
106 if i is soc:
113 if i is soc:
107 out = self.connection
114 out = self.connection
108 else:
115 else:
109 out = soc
116 out = soc
110 try:
117 try:
111 data = i.recv(8192)
118 data = i.recv(8192)
112 except socket.error:
119 except socket.error:
113 break
120 break
114 if data:
121 if data:
115 out.send(data)
122 out.send(data)
116 count = 0
123 count = 0
117 else:
124 else:
118 print "\t" "idle", count
125 print "\t" "idle", count
119 if count == max_idling:
126 if count == max_idling:
120 break
127 break
121
128
122 do_HEAD = do_GET
129 do_HEAD = do_GET
123 do_POST = do_GET
130 do_POST = do_GET
124 do_PUT = do_GET
131 do_PUT = do_GET
125 do_DELETE = do_GET
132 do_DELETE = do_GET
126
133
127 class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
134 class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
128 BaseHTTPServer.HTTPServer):
135 BaseHTTPServer.HTTPServer):
129 def __init__(self, *args, **kwargs):
136 def __init__(self, *args, **kwargs):
130 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
137 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
131 a = open("proxy.pid", "w")
138 a = open("proxy.pid", "w")
132 a.write(str(os.getpid()) + "\n")
139 a.write(str(os.getpid()) + "\n")
133 a.close()
140 a.close()
134
141
135 if __name__ == '__main__':
142 if __name__ == '__main__':
136 from sys import argv
143 from sys import argv
137 if argv[1:] and argv[1] in ('-h', '--help'):
144 if argv[1:] and argv[1] in ('-h', '--help'):
138 print argv[0], "[port [allowed_client_name ...]]"
145 print argv[0], "[port [allowed_client_name ...]]"
139 else:
146 else:
140 if argv[2:]:
147 if argv[2:]:
141 allowed = []
148 allowed = []
142 for name in argv[2:]:
149 for name in argv[2:]:
143 client = socket.gethostbyname(name)
150 client = socket.gethostbyname(name)
144 allowed.append(client)
151 allowed.append(client)
145 print "Accept: %s (%s)" % (client, name)
152 print "Accept: %s (%s)" % (client, name)
146 ProxyHandler.allowed_clients = allowed
153 ProxyHandler.allowed_clients = allowed
147 del argv[2:]
154 del argv[2:]
148 else:
155 else:
149 print "Any clients will be served..."
156 print "Any clients will be served..."
150 BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)
157 BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)
General Comments 0
You need to be logged in to leave comments. Login now