tests/tinyproxy.py
author Pulkit Goyal <7895pulkit@gmail.com>
Thu, 01 Dec 2016 13:12:04 +0530
changeset 30570 c4c51fd0e11d
parent 29566 075146e85bb6
child 31005 d8d698bcdcd6
permissions -rwxr-xr-x
py3: use pycompat.sysstr() in __import__() __import__() on Python 3 accepts strings which are different from that of Python 2. Used pycompat.sysstr() to get string accordingly.
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
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
     3
from __future__ import absolute_import, print_function
27302
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     4
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     5
__doc__ = """Tiny HTTP Proxy.
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     6
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     7
This module implements GET, HEAD, POST, PUT and DELETE methods
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     8
on BaseHTTPServer, and behaves as an HTTP proxy.  The CONNECT
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     9
method is also implemented experimentally, but has not been
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    10
tested yet.
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    11
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    12
Any help will be greatly appreciated.           SUZUKI Hisao
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    13
"""
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    14
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    15
__version__ = "0.2.1"
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    16
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
    17
import optparse
27302
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    18
import os
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    19
import select
faca4adfed0a tests: use absolute_import in tinyproxy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    20
import socket
28778
256d90bb12fa tests: make tinyproxy.py not import sys.argv by name
Yuya Nishihara <yuya@tcha.org>
parents: 28773
diff changeset
    21
import sys
29431
80880ad3fccd py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28778
diff changeset
    22
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
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
    25
httpserver = util.httpserver
29431
80880ad3fccd py3: conditionalize the urlparse import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28778
diff changeset
    26
urlparse = util.urlparse
29433
33770d2b6cf9 py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29431
diff changeset
    27
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
    28
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
    29
class ProxyHandler (httpserver.basehttprequesthandler):
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
    30
    __base = httpserver.basehttprequesthandler
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    31
    __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
    32
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    33
    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
    34
    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
    35
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    36
    def handle(self):
27637
b502138f5faa cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents: 27302
diff changeset
    37
        (ip, port) = self.client_address
14971
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14494
diff changeset
    38
        allowed = getattr(self, 'allowed_clients', None)
0b21ae0a2366 tests: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14494
diff changeset
    39
        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
    40
            self.raw_requestline = self.rfile.readline()
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
    41
            if self.parse_request():
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
    42
                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
    43
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    44
            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
    45
14093
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    46
    def log_request(self, code='-', size='-'):
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    47
        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
    48
        self.log_message('"%s" %s %s%s',
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    49
                         self.requestline, str(code), str(size),
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    50
                         ''.join([' %s:%s' % h for h in sorted(xheaders)]))
ce99d887585f httprepo: long arguments support (issue2126)
Steven Brown <StevenGBrown@gmail.com>
parents: 10282
diff changeset
    51
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    52
    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
    53
        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
    54
        if i >= 0:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
    55
            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
    56
        else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    57
            host_port = netloc, 80
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
    58
        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
    59
        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
    60
        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
    61
            try: msg = arg[1]
16703
7292a4618f46 cleanup: replace more naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16300
diff changeset
    62
            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
    63
            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
    64
            return 0
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    65
        return 1
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    66
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    67
    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
    68
        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
    69
        try:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    70
            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
    71
                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
    72
                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
    73
                                 " 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
    74
                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
    75
                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
    76
                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
    77
        finally:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
    78
            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
    79
            soc.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    80
            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
    81
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    82
    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
    83
        (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
    84
            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
    85
        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
    86
            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
    87
            return
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    88
        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
    89
        try:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    90
            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
    91
                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
    92
                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
    93
                    self.command,
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    94
                    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
    95
                    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
    96
                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
    97
                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
    98
                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
    99
                    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
   100
                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
   101
                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
   102
        finally:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
   103
            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
   104
            soc.close()
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   105
            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
   106
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   107
    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
   108
        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
   109
        ow = []
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   110
        count = 0
14494
1ffeeb91c55d check-code: flag 0/1 used as constant Boolean expression
Martin Geisler <mg@lazybytes.net>
parents: 14093
diff changeset
   111
        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
   112
            count += 1
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   113
            (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
   114
            if exs:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   115
                break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   116
            if ins:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   117
                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
   118
                    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
   119
                        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
   120
                    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   121
                        out = soc
18519
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   122
                    try:
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   123
                        data = i.recv(8192)
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   124
                    except socket.error:
ca430fb6a668 tests: fix toctou race in tinyproxy.py (issue3795)
Mads Kiilerich <madski@unity3d.com>
parents: 16703
diff changeset
   125
                        break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   126
                    if data:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   127
                        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
   128
                        count = 0
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   129
            else:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
   130
                print("\t" "idle", count)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   131
            if count == max_idling:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   132
                break
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   133
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   134
    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
   135
    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
   136
    do_PUT  = do_GET
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 2337
diff changeset
   137
    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
   138
29433
33770d2b6cf9 py3: conditionalize SocketServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29431
diff changeset
   139
class ThreadingHTTPServer (socketserver.ThreadingMixIn,
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
   140
                           httpserver.httpserver):
16300
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   141
    def __init__(self, *args, **kwargs):
29566
075146e85bb6 py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29565
diff changeset
   142
        httpserver.httpserver.__init__(self, *args, **kwargs)
16300
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   143
        a = open("proxy.pid", "w")
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   144
        a.write(str(os.getpid()) + "\n")
74e114ac6ec1 tests: fix startup/shutdown races in test-https
Matt Mackall <mpm@selenic.com>
parents: 14971
diff changeset
   145
        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
   146
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   147
def runserver(port=8000, bind=""):
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   148
    server_address = (bind, port)
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   149
    ProxyHandler.protocol_version = "HTTP/1.0"
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   150
    httpd = ThreadingHTTPServer(server_address, ProxyHandler)
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   151
    sa = httpd.socket.getsockname()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   152
    print("Serving HTTP on", sa[0], "port", sa[1], "...")
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   153
    try:
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   154
        httpd.serve_forever()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   155
    except KeyboardInterrupt:
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   156
        print("\nKeyboard interrupt received, exiting.")
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   157
        httpd.server_close()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   158
        sys.exit(0)
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   159
2337
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   160
if __name__ == '__main__':
28778
256d90bb12fa tests: make tinyproxy.py not import sys.argv by name
Yuya Nishihara <yuya@tcha.org>
parents: 28773
diff changeset
   161
    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
   162
    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
   163
        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
   164
    else:
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   165
        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
   166
            allowed = []
3f24bc5dee81 http: fix many problems with url parsing and auth. added proxy test.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   167
            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
   168
                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
   169
                allowed.append(client)
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
   170
                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
   171
            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
   172
            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
   173
        else:
28646
f452c1cf7a8f tests: make tinyproxy.py use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27637
diff changeset
   174
            print("Any clients will be served...")
29565
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   175
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   176
        parser = optparse.OptionParser()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   177
        parser.add_option('-b', '--bind', metavar='ADDRESS',
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   178
                          help='Specify alternate bind address '
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   179
                               '[default: all interfaces]', default='')
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   180
        (options, args) = parser.parse_args()
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   181
        port = 8000
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   182
        if len(args) == 1:
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   183
            port = int(args[0])
143d21a7343e py3: re-implement the BaseHTTPServer.test() function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29433
diff changeset
   184
        runserver(port, options.bind)