aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/wsgi.py
blob: 9fb5f0c56e9056b71f0d91a09652afcc69d521b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import cStringIO, urllib, time, sys
import version, flow

def date_time_string():
    """Return the current date and time formatted for a message header."""
    WEEKS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    MONTHS = [None,
                 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    now = time.time()
    year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
    s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
            WEEKS[wd],
            day, MONTHS[month], year,
            hh, mm, ss)
    return s


class WSGIAdaptor:
    def __init__(self, app, domain, port):
        self.app, self.domain, self.port = app, domain, port

    def make_environ(self, request, errsoc):
        if '?' in request.path:
            path_info, query = request.path.split('?', 1)
        else:
            path_info = request.path
            query = ''
        environ = {
            'wsgi.version':         (1, 0),
            'wsgi.url_scheme':      request.scheme,
            'wsgi.input':           cStringIO.StringIO(request.content),
            'wsgi.errors':          errsoc,
            'wsgi.multithread':     True,
            'wsgi.multiprocess':    False,
            'wsgi.run_once':        False,
            'SERVER_SOFTWARE':      version.NAMEVERSION,
            'REQUEST_METHOD':       request.method,
            'SCRIPT_NAME':          '',
            'PATH_INFO':            urllib.unquote(path_info),
            'QUERY_STRING':         query,
            'CONTENT_TYPE':         request.headers.get('Content-Type', [''])[0],
            'CONTENT_LENGTH':       request.headers.get('Content-Length', [''])[0],
            'SERVER_NAME':          self.domain,
            'SERVER_PORT':          self.port,
            # FIXME: We need to pick up the protocol read from the request.
            'SERVER_PROTOCOL':      "HTTP/1.1",
        }
        if request.client_conn.address:
            environ["REMOTE_ADDR"], environ["REMOTE_PORT"] = request.client_conn.address

        for key, value in request.headers.items():
            key = 'HTTP_' + key.upper().replace('-', '_')
            if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'):
                environ[key] = value
        return environ

    def serve(self, request, soc):
        state = dict(
            response_started = False,
            headers_sent = False,
            status = None,
            headers = None
        )
        def write(data):
            if not state["headers_sent"]:
                soc.write("HTTP/1.1 %s\r\n"%state["status"])
                h = state["headers"]
                if 'server' not in h:
                    h["Server"] = [version.NAMEVERSION]
                if 'date' not in h:
                    h["Date"] = [date_time_string()]
                soc.write(str(h))
                soc.write("\r\n")
                state["headers_sent"] = True
            soc.write(data)
            soc.flush()

        def start_response(status, headers, exc_info=None):
            if exc_info:
                try:
                    if state["headers_sent"]:
                        raise exc_info[0], exc_info[1], exc_info[2]
                finally:
                    exc_info = None
            elif state["status"]:
                raise AssertionError('Response already started')
            state["status"] = status
            state["headers"] = flow.ODictCaseless(headers)
            return write

        errs = cStringIO.StringIO()
        try:
            dataiter = self.app(self.make_environ(request, errs), start_response)
            for i in dataiter:
                write(i)
            if not state["headers_sent"]:
                write("")
        except Exception, v:
            print v
            try:
                # Serve internal server error page
                pass
            except Exception, v:
                pass
        return errs.getvalue()


class AppRegistry:
    def __init__(self):
        self.apps = {}

    def add(self, app, domain, port):
        """
            Add a WSGI app to the registry, to be served for requests to the
            specified domain, on the specified port.
        """
        self.apps[(domain, port)] = WSGIAdaptor(app, domain, port)

    def get(self, request):
        """
            Returns an WSGIAdaptor instance if request matches an app, or None.
        """
        return self.apps.get((request.host, request.port), None)