2006-05-23 02:42:49 +04:00
|
|
|
#!/usr/bin/env python
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
# @lint-ignore-every FBPYTHON4
|
2006-05-23 02:42:49 +04:00
|
|
|
|
2016-03-27 23:27:44 +03:00
|
|
|
from __future__ import absolute_import, print_function
|
2015-12-07 09:20:08 +03:00
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
|
2019-01-30 03:25:33 +03:00
|
|
|
from edenscm.mercurial import util
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
|
|
|
|
|
2006-05-23 02:42:49 +04:00
|
|
|
__doc__ = """Tiny HTTP Proxy.
|
|
|
|
|
|
|
|
This module implements GET, HEAD, POST, PUT and DELETE methods
|
|
|
|
on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
|
|
|
|
method is also implemented experimentally, but has not been
|
|
|
|
tested yet.
|
|
|
|
|
|
|
|
Any help will be greatly appreciated. SUZUKI Hisao
|
|
|
|
"""
|
|
|
|
|
|
|
|
__version__ = "0.2.1"
|
|
|
|
|
2016-06-27 13:46:10 +03:00
|
|
|
|
2016-07-13 21:08:29 +03:00
|
|
|
httpserver = util.httpserver
|
2016-06-27 14:18:54 +03:00
|
|
|
socketserver = util.socketserver
|
2017-03-22 08:45:02 +03:00
|
|
|
urlreq = util.urlreq
|
2006-05-23 02:42:49 +04:00
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
if os.environ.get("HGIPV6", "0") == "1":
|
2017-02-16 09:53:45 +03:00
|
|
|
family = socket.AF_INET6
|
|
|
|
else:
|
|
|
|
family = socket.AF_INET
|
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
|
|
|
|
class ProxyHandler(httpserver.basehttprequesthandler):
|
2016-07-13 21:08:29 +03:00
|
|
|
__base = httpserver.basehttprequesthandler
|
2006-05-23 02:42:49 +04:00
|
|
|
__base_handle = __base.handle
|
|
|
|
|
|
|
|
server_version = "TinyHTTPProxy/" + __version__
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
rbufsize = 0 # self.rfile Be unbuffered
|
2006-05-23 02:42:49 +04:00
|
|
|
|
|
|
|
def handle(self):
|
2015-12-31 11:16:59 +03:00
|
|
|
(ip, port) = self.client_address
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
allowed = getattr(self, "allowed_clients", None)
|
2011-07-26 01:37:18 +04:00
|
|
|
if allowed is not None and ip not in allowed:
|
2006-05-23 02:42:49 +04:00
|
|
|
self.raw_requestline = self.rfile.readline()
|
2010-01-25 09:05:27 +03:00
|
|
|
if self.parse_request():
|
|
|
|
self.send_error(403)
|
2006-05-23 02:42:49 +04:00
|
|
|
else:
|
|
|
|
self.__base_handle()
|
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
def log_request(self, code="-", size="-"):
|
|
|
|
xheaders = [h for h in self.headers.items() if h[0].startswith("x-")]
|
|
|
|
self.log_message(
|
|
|
|
'"%s" %s %s%s',
|
|
|
|
self.requestline,
|
|
|
|
str(code),
|
|
|
|
str(size),
|
|
|
|
"".join([" %s:%s" % h for h in sorted(xheaders)]),
|
|
|
|
)
|
2017-06-19 04:31:53 +03:00
|
|
|
# Flush for Windows, so output isn't lost on TerminateProcess()
|
2017-06-20 04:53:54 +03:00
|
|
|
sys.stdout.flush()
|
2017-06-19 04:31:53 +03:00
|
|
|
sys.stderr.flush()
|
2011-04-30 21:04:37 +04:00
|
|
|
|
2006-05-23 02:42:49 +04:00
|
|
|
def _connect_to(self, netloc, soc):
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
i = netloc.find(":")
|
2006-05-23 02:42:49 +04:00
|
|
|
if i >= 0:
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
host_port = netloc[:i], int(netloc[i + 1 :])
|
2006-05-23 02:42:49 +04:00
|
|
|
else:
|
|
|
|
host_port = netloc, 80
|
2016-03-27 23:27:44 +03:00
|
|
|
print("\t" "connect to %s:%d" % host_port)
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
try:
|
|
|
|
soc.connect(host_port)
|
2015-06-24 08:20:08 +03:00
|
|
|
except socket.error as arg:
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
try:
|
|
|
|
msg = arg[1]
|
|
|
|
except (IndexError, TypeError):
|
|
|
|
msg = arg
|
2006-05-23 02:42:49 +04:00
|
|
|
self.send_error(404, msg)
|
|
|
|
return 0
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def do_CONNECT(self):
|
2017-02-16 09:53:45 +03:00
|
|
|
soc = socket.socket(family, socket.SOCK_STREAM)
|
2006-05-23 02:42:49 +04:00
|
|
|
try:
|
|
|
|
if self._connect_to(self.path, soc):
|
|
|
|
self.log_request(200)
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
self.wfile.write(
|
|
|
|
self.protocol_version + " 200 Connection established\r\n"
|
|
|
|
)
|
2006-05-23 02:42:49 +04:00
|
|
|
self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
|
|
|
|
self.wfile.write("\r\n")
|
|
|
|
self._read_write(soc, 300)
|
|
|
|
finally:
|
2016-03-27 23:27:44 +03:00
|
|
|
print("\t" "bye")
|
2006-05-23 02:42:49 +04:00
|
|
|
soc.close()
|
|
|
|
self.connection.close()
|
|
|
|
|
|
|
|
def do_GET(self):
|
2017-03-22 08:45:02 +03:00
|
|
|
(scm, netloc, path, params, query, fragment) = urlreq.urlparse(
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
self.path, "http"
|
|
|
|
)
|
|
|
|
if scm != "http" or fragment or not netloc:
|
2006-05-23 02:42:49 +04:00
|
|
|
self.send_error(400, "bad url %s" % self.path)
|
|
|
|
return
|
2017-02-16 09:53:45 +03:00
|
|
|
soc = socket.socket(family, socket.SOCK_STREAM)
|
2006-05-23 02:42:49 +04:00
|
|
|
try:
|
|
|
|
if self._connect_to(netloc, soc):
|
|
|
|
self.log_request()
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
soc.send(
|
|
|
|
"%s %s %s\r\n"
|
|
|
|
% (
|
|
|
|
self.command,
|
|
|
|
urlreq.urlunparse(("", "", path, params, query, "")),
|
|
|
|
self.request_version,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.headers["Connection"] = "close"
|
|
|
|
del self.headers["Proxy-Connection"]
|
2006-05-23 02:42:49 +04:00
|
|
|
for key_val in self.headers.items():
|
|
|
|
soc.send("%s: %s\r\n" % key_val)
|
|
|
|
soc.send("\r\n")
|
|
|
|
self._read_write(soc)
|
|
|
|
finally:
|
2016-03-27 23:27:44 +03:00
|
|
|
print("\t" "bye")
|
2006-05-23 02:42:49 +04:00
|
|
|
soc.close()
|
|
|
|
self.connection.close()
|
|
|
|
|
|
|
|
def _read_write(self, soc, max_idling=20):
|
|
|
|
iw = [self.connection, soc]
|
|
|
|
ow = []
|
|
|
|
count = 0
|
2011-06-01 14:38:46 +04:00
|
|
|
while True:
|
2006-05-23 02:42:49 +04:00
|
|
|
count += 1
|
|
|
|
(ins, _, exs) = select.select(iw, ow, iw, 3)
|
2010-01-25 09:05:27 +03:00
|
|
|
if exs:
|
|
|
|
break
|
2006-05-23 02:42:49 +04:00
|
|
|
if ins:
|
|
|
|
for i in ins:
|
|
|
|
if i is soc:
|
|
|
|
out = self.connection
|
|
|
|
else:
|
|
|
|
out = soc
|
2013-01-31 22:13:13 +04:00
|
|
|
try:
|
|
|
|
data = i.recv(8192)
|
|
|
|
except socket.error:
|
|
|
|
break
|
2006-05-23 02:42:49 +04:00
|
|
|
if data:
|
|
|
|
out.send(data)
|
|
|
|
count = 0
|
|
|
|
else:
|
2016-03-27 23:27:44 +03:00
|
|
|
print("\t" "idle", count)
|
2010-01-25 09:05:27 +03:00
|
|
|
if count == max_idling:
|
|
|
|
break
|
2006-05-23 02:42:49 +04:00
|
|
|
|
|
|
|
do_HEAD = do_GET
|
|
|
|
do_POST = do_GET
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
do_PUT = do_GET
|
2010-01-25 09:05:27 +03:00
|
|
|
do_DELETE = do_GET
|
2006-05-23 02:42:49 +04:00
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
|
|
|
|
class ThreadingHTTPServer(socketserver.ThreadingMixIn, httpserver.httpserver):
|
2012-03-29 04:23:25 +04:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-07-13 21:08:29 +03:00
|
|
|
httpserver.httpserver.__init__(self, *args, **kwargs)
|
2012-03-29 04:23:25 +04:00
|
|
|
a = open("proxy.pid", "w")
|
|
|
|
a.write(str(os.getpid()) + "\n")
|
|
|
|
a.close()
|
2006-05-23 02:42:49 +04:00
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
|
2018-07-27 22:02:21 +03:00
|
|
|
def runserver(portfile="tinyproxy.port", bind=""):
|
|
|
|
server_address = (bind, 0)
|
2016-07-15 20:30:31 +03:00
|
|
|
ProxyHandler.protocol_version = "HTTP/1.0"
|
|
|
|
httpd = ThreadingHTTPServer(server_address, ProxyHandler)
|
|
|
|
sa = httpd.socket.getsockname()
|
|
|
|
print("Serving HTTP on", sa[0], "port", sa[1], "...")
|
2018-07-27 22:02:21 +03:00
|
|
|
with open(portfile + ".tmp", "w") as f:
|
|
|
|
f.write("%s" % sa[1])
|
|
|
|
os.rename(portfile + ".tmp", portfile)
|
2016-07-15 20:30:31 +03:00
|
|
|
try:
|
|
|
|
httpd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("\nKeyboard interrupt received, exiting.")
|
|
|
|
httpd.server_close()
|
|
|
|
sys.exit(0)
|
|
|
|
|
codemod: join the auto-formatter party
Summary:
Turned on the auto formatter. Ran `arc lint --apply-patches --take BLACK **/*.py`.
Then run `arc lint` again so some other autofixers like spellchecker etc. looked
at the code base. Manually accept the changes whenever they make sense, or use
a workaround (ex. changing "dict()" to "dict constructor") where autofix is false
positive. Disabled linters on files that are hard (i18n/polib.py) to fix, or less
interesting to fix (hgsubversion tests), or cannot be fixed without breaking
OSS build (FBPYTHON4).
Conflicted linters (test-check-module-imports.t, part of test-check-code.t,
test-check-pyflakes.t) are removed or disabled.
Duplicated linters (test-check-pyflakes.t, test-check-pylint.t) are removed.
An issue of the auto-formatter is lines are no longer guarnateed to be <= 80
chars. But that seems less important comparing with the benefit auto-formatter
provides.
As we're here, also remove test-check-py3-compat.t, as it is currently broken
if `PYTHON3=/bin/python3` is set.
Reviewed By: wez, phillco, simpkins, pkaush, singhsrb
Differential Revision: D8173629
fbshipit-source-id: 90e248ae0c5e6eaadbe25520a6ee42d32005621b
2018-05-26 07:34:37 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-07-27 22:02:21 +03:00
|
|
|
allowed = []
|
|
|
|
for name in ["localhost"]:
|
|
|
|
client = socket.gethostbyname(name)
|
|
|
|
allowed.append(client)
|
|
|
|
print("Accept: %s (%s)" % (client, name))
|
|
|
|
ProxyHandler.allowed_clients = allowed
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option(
|
|
|
|
"-b",
|
|
|
|
"--bind",
|
|
|
|
metavar="ADDRESS",
|
|
|
|
help="Specify alternate bind address [default: all interfaces]",
|
|
|
|
default="",
|
|
|
|
)
|
|
|
|
parser.add_option(
|
|
|
|
"-f",
|
|
|
|
"--portfile",
|
|
|
|
metavar="PORTFILE",
|
|
|
|
help="Specify the port file [default: tinyproxy.port]",
|
|
|
|
default="tinyproxy.port",
|
|
|
|
)
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
runserver(options.portfile, options.bind)
|