tests/tinyproxy.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Fri, 03 Oct 2014 12:52:17 -0500
changeset 22758 0fc52b01fb9e
parent 18519 ca430fb6a668
child 25660 328739ea70c3
permissions -rwxr-xr-x
revset: use _generatorset directly in _revdescendant _ascgeneratorset is going away.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     3
__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
     4
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     5
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
     6
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
     7
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
     8
tested yet.
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     9
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    10
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
    11
"""
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    12
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    13
__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
    14
16300
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
    15
import BaseHTTPServer, select, socket, SocketServer, urlparse, os
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    16
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    17
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
    18
    __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
    19
    __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
    20
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    21
    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
    22
    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
    23
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    24
    def handle(self):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    25
        (ip, port) =  self.client_address
14971
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14494
diff changeset
    26
        allowed = getattr(self, 'allowed_clients', None)
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14494
diff changeset
    27
        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
    28
            self.raw_requestline = self.rfile.readline()
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
    29
            if self.parse_request():
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
    30
                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
    31
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    32
            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
    33
14093
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    34
    def log_request(self, code='-', size='-'):
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    35
        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
    36
        self.log_message('"%s" %s %s%s',
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    37
                         self.requestline, str(code), str(size),
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    38
                         ''.join([' %s:%s' % h for h in sorted(xheaders)]))
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    39
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    40
    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
    41
        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
    42
        if i >= 0:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
    43
            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
    44
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    45
            host_port = netloc, 80
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    46
        print "\t" "connect to %s:%d" % host_port
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    47
        try: soc.connect(host_port)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    48
        except socket.error, arg:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    49
            try: msg = arg[1]
16703
7292a4618f46 cleanup: replace more naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16300
diff changeset
    50
            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
    51
            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
    52
            return 0
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    53
        return 1
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    54
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    55
    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
    56
        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
    57
        try:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    58
            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
    59
                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
    60
                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
    61
                                 " 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
    62
                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
    63
                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
    64
                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
    65
        finally:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    66
            print "\t" "bye"
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    67
            soc.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    68
            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
    69
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    70
    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
    71
        (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
    72
            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
    73
        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
    74
            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
    75
            return
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    76
        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
    77
        try:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    78
            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
    79
                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
    80
                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
    81
                    self.command,
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    82
                    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
    83
                    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
    84
                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
    85
                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
    86
                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
    87
                    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
    88
                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
    89
                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
    90
        finally:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    91
            print "\t" "bye"
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    92
            soc.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    93
            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
    94
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    95
    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
    96
        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
    97
        ow = []
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    98
        count = 0
14494
1ffeeb91c55d check-code: flag 0/1 used as constant Boolean expression
Martin Geisler <mg@lazybytes.net>
parents: 14093
diff changeset
    99
        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
   100
            count += 1
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   101
            (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
   102
            if exs:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   103
                break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   104
            if ins:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   105
                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
   106
                    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
   107
                        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
   108
                    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   109
                        out = soc
18519
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   110
                    try:
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   111
                        data = i.recv(8192)
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   112
                    except socket.error:
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   113
                        break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   114
                    if data:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   115
                        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
   116
                        count = 0
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   117
            else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   118
                print "\t" "idle", count
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   119
            if count == max_idling:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   120
                break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   121
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   122
    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
   123
    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
   124
    do_PUT  = do_GET
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   125
    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
   126
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   127
class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
16300
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   128
                           BaseHTTPServer.HTTPServer):
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   129
    def __init__(self, *args, **kwargs):
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   130
        BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   131
        a = open("proxy.pid", "w")
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   132
        a.write(str(os.getpid()) + "\n")
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   133
        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
   134
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   135
if __name__ == '__main__':
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   136
    from sys import argv
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   137
    if argv[1:] and argv[1] in ('-h', '--help'):
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   138
        print argv[0], "[port [allowed_client_name ...]]"
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   139
    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   140
        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
   141
            allowed = []
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   142
            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
   143
                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
   144
                allowed.append(client)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   145
                print "Accept: %s (%s)" % (client, name)
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   146
            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
   147
            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
   148
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   149
            print "Any clients will be served..."
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   150
        BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)