File: http_proxy.py

package info (click to toggle)
libtorrent-rasterbar 2.0.11-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (550 lines) | stat: -rwxr-xr-x 17,850 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/env python3

# The author disclaims copyright to this source code. Please see the
# accompanying UNLICENSE file.
"""A HTTP proxy module suitable for testing.

See http://github.com/AllSeeingEyeTolledEweSew/http_proxy for more information
about this project.
"""

import argparse
import base64
import http.client
import http.server
import select
import socket
import socketserver
import traceback
import urllib.parse


class ChunkError(Exception):
    """Raised while processing chunked encoding, for invalid chunk sizes."""


class _HTTPError(Exception):
    """Raised internally to simplify processing.

    Attributes:
        code: The HTTP error code (4xx or 5xx) we should return.
        message: The "reason"/"message" part we should return in the status
            line.
        explain: The full body of the error message, usually a traceback.
    """

    def __init__(self, code, message=None, explain=None):
        super().__init__()
        self.code = code
        self.message = message
        self.explain = explain


def read_to_end_of_chunks(file_like):
    """Reads a chunked-encoded stream from a file-like object.

    This will read up to the end of the chunked encoding, including chunk
    delimiters, trailers, and the terminal empty line.

    The stream will be returned as an iterator of bytes objects. The split
    between bytes objects is arbitrary.

    Args:
        file_like: A file-like object with read() and readline() methods.

    Yields:
        bytes objects.

    Raises:
        ChunkError: if an invalid chunk size is encountered.
    """

    def inner():
        while True:
            size_line = file_like.readline()
            yield size_line
            try:
                size = int(size_line, 16)
            except ValueError:
                raise ChunkError("Invalid chunk size: %r" % size_line)
            if size < 0:
                raise ChunkError("Invalid chunk size: %d" % size)
            if size == 0:
                # Allow trailers, if any
                while True:
                    line = file_like.readline()
                    yield line
                    if line in (b"\r\n", b"", b"\n"):
                        return
            # Chunk size + crlf
            chunk = file_like.read(size + 2)
            yield chunk

    # Interpret any empty read as a closed connection, and stop
    for chunk in inner():
        if not chunk:
            return
        yield chunk


def read_to_limit(file_like, limit, buffer_size):
    """Reads a file-like object up to a number of bytes.

    This will read up to the given number of bytes from the given file. The
    stream will be returned as an iterator of bytes objects, having size up to
    the given buffer_size.

    Args:
        file_like: A file-like object with a read() method.
        limit: The total number of bytes to read.
        buffer_size: Read data chunks of this size.

    Yields:
        bytes objects.
    """
    offset = 0
    while offset < limit:
        amount = min(limit - offset, buffer_size)
        buf = file_like.read(amount)
        if not buf:
            return
        yield buf
        offset += len(buf)


def read_all(file_like, buffer_size):
    """Reads a file-like object to its end.

    This will read an entire file. The stream will be returned as an iterator
    of bytes objects, having size up to the given buffer_size.

    Args:
        file_like: A file-like object with a read() method.
        buffer_size: Read data chunks of this size.

    Yields:
        bytes objects.
    """
    while True:
        buf = file_like.read(buffer_size)
        if not buf:
            return
        yield buf


class Handler(http.server.BaseHTTPRequestHandler):
    """An HTTP proxy Handler class, for use with http.server classes.

    Attributes:
        timeout: Timeout value in seconds. Applies to upstream connections,
            idle timeouts for CONNECT-method streams, and reading data from
            both client and upstream.
        basic_auth: If set, proxy will require basic authorization with this
            credential.
    """

    # BaseHTTPRequestHandler tests this value
    protocol_version = "HTTP/1.1"

    BUFLEN = 8192

    # This is really here to keep pylint happy
    close_connection = True
    timeout = 30
    basic_auth = None

    def authorize(self):
        """Returns whether the request is authorized."""
        if not self.basic_auth:
            return True

        header = self.headers.get("Proxy-Authorization", "")
        split = header.split(None, 1)
        if len(split) != 2:
            return False
        scheme, credentials = split
        if scheme.lower() != "basic":
            return False
        return credentials == self.basic_auth

    def do_auth(self):
        """Fail the request if unauthorized.

        Should be called early from the do_* method handler method.

        Returns:
            False if the request was unauthorized and we sent an error
                response, True otherwise.
        """
        if self.authorize():
            return True

        # send_error doesn't let us send headers, so do it by hand
        self.log_error("code %d, message %s", 407,
                       "Proxy authorization required")
        self.send_response(407, "Proxy authorization required")
        self.send_header("Connection", "close")
        self.send_header("Proxy-Authenticate", "Basic")
        self.end_headers()
        return False

    def connect_request(self):
        """Connect to the upstream, for a CONNECT request.

        Should be called from the do_CONNECT handler method.

        Returns:
            A socket connection to the upstream.

        Raises:
            _HTTPError: If the CONNECT target was invalid, or there was an
                error connecting to the upstream.
        """
        split = self.path.split(":")
        if len(split) != 2:
            raise _HTTPError(400, explain="Target must be host:port")
        host, port = split

        try:
            return socket.create_connection((host, port), self.timeout)
        except socket.timeout:
            raise _HTTPError(504, explain=traceback.format_exc())
        except OSError:
            raise _HTTPError(502, explain=traceback.format_exc())

    def bidirectional_proxy(self, upstream):
        """Forward data between the client and the given upstream.

        Should be called from the do_CONNECT method handler.

        Runs forever, until either upstream or client close their side of the
        connection, or the idle timeout expires.

        Args:
            upstream: A socket connection to the upstream.
        """
        socks = (upstream, self.request)
        while True:
            (rlist, _, xlist) = select.select(socks, (), socks, self.timeout)
            if xlist:
                return
            if not rlist:
                return
            for sock in rlist:
                data = sock.recv(self.BUFLEN)
                if not data:
                    return
                if sock is upstream:
                    self.request.sendall(data)
                else:
                    upstream.sendall(data)

    # pylint:disable=invalid-name
    def do_CONNECT(self):
        """Handler for the CONNECT method.

        Should be called from the superclass handler logic.
        """
        upstream = None
        try:
            if not self.do_auth():
                return

            upstream = self.connect_request()
        except _HTTPError as err:
            self.send_error(err.code, message=err.message, explain=err.explain)
        except Exception:
            self.log_error("%s", traceback.format_exc())
            self.send_error(500, explain=traceback.format_exc())

        if upstream is None:
            return

        self.send_response(200)
        self.send_header("Connection", "close")
        self.end_headers()

        try:
            self.bidirectional_proxy(upstream)
        except Exception:
            self.log_error("%s", traceback.format_exc())
            self.close_connection = True

        upstream.close()

    def proxy_request(self):
        """Forward a normal HTTP request.

        Should be called from the do_* handlers for normal HTTP requests (not
        CONNECT).

        Returns:
            A tuple of (HTTPConnection, HTTPResponse).

        Raises:
            _HTTPError: If the request does not conform to HTTP/1.1
                expectations.
        """
        url = urllib.parse.urlsplit(self.path)

        if url.scheme != "http":
            raise _HTTPError(400, message="Target scheme is not http")

        # We need to read only the expected amount from the client
        # https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
        if self.headers.get("Transfer-Encoding", "identity") != "identity":
            # BaseHTTPHandler never parses chunked encoding itself
            message_body = read_to_end_of_chunks(self.rfile)
        elif "Content-Length" in self.headers:
            try:
                length = int(self.headers["Content-Length"])
            except ValueError:
                raise _HTTPError(411)
            message_body = read_to_limit(self.rfile, length, self.BUFLEN)
        elif self.command not in ("PATCH", "POST", "PUT"):
            # Not expecting a body
            message_body = None
        else:
            raise _HTTPError(411)

        try:
            upstream = http.client.HTTPConnection(url.netloc,
                                                  timeout=self.timeout)
        except http.client.InvalidURL as exc:
            raise _HTTPError(400,
                             message=str(exc),
                             explain=traceback.format_exc())

        path = urllib.parse.urlunsplit(("", "", url.path, url.query, ""))
        upstream.putrequest(self.command,
                            path,
                            skip_host=True,
                            skip_accept_encoding=True)

        connection_tokens = []
        filter_headers = set(
            ("proxy-authorization", "connection", "keep-alive"))
        pass_headers = set(("transfer-encoding", "te", "trailer"))
        if "Connection" in self.headers:
            request_connection_tokens = [
                token.strip() for token in self.headers["Connection"].split(",")
            ]
        else:
            request_connection_tokens = []
        for token in request_connection_tokens:
            # Better parsing than base class, I think
            if token.lower() == "keep-alive":
                self.close_connection = False
                filter_headers.add(token.lower())
            elif token.lower() == "close":
                self.close_connection = True
            elif token.lower() in pass_headers:
                connection_tokens.append(token)
            else:
                filter_headers.add(token.lower())

        for name, value in self.headers.items():
            if name.lower() in filter_headers:
                continue
            upstream.putheader(name, value)

        # No pipelineing to upstream
        if "close" not in connection_tokens:
            connection_tokens.append("close")

        upstream.putheader("Connection", ", ".join(connection_tokens))

        try:
            # Never use encode_chunked here, as we pass through
            # transfer-encoding from the client.
            # Calls socket.create_connection, so catch socket exceptions here.
            upstream.endheaders(message_body=message_body)
            # This parses the upstream response line and headers
            return (upstream, upstream.getresponse())
        except socket.timeout:
            raise _HTTPError(504, explain=traceback.format_exc())
        except (OSError, http.client.HTTPException):
            upstream.close()
            raise _HTTPError(502, explain=traceback.format_exc())
        except ChunkError as exc:
            upstream.close()
            raise _HTTPError(400,
                             message=str(exc),
                             explain=traceback.format_exc())

    def proxy_response(self, response):
        """Forwards an upstream response back to the client.

        Should be called from the do_* handlers for normal HTTP requests (not
        CONNECT).

        Args:
            response: An HTTPResponse from upstream.
        """
        # send_response supplies some headers unconditionally
        self.log_request(response.code)
        self.send_response_only(response.code, response.reason)

        connection_tokens = []
        filter_headers = set(
            ("proxy-authorization", "connection", "keep-alive"))
        pass_headers = set(("transfer-encoding", "te", "trailer"))
        if response.getheader("Connection"):
            response_connection_tokens = [
                token.strip()
                for token in response.getheader("Connection").split(",")
            ]
        else:
            response_connection_tokens = []
        for token in response_connection_tokens:
            if token.lower() == "close":
                continue
            if token.lower() in pass_headers:
                connection_tokens.append(token)
            else:
                filter_headers.add(token.lower())
        # Close the connection if the client requested it
        if self.close_connection:
            connection_tokens.append("close")
        for name, value in response.getheaders():
            if name.lower() in filter_headers:
                continue
            self.send_header(name, value)
        if connection_tokens:
            self.send_header("Connection", ", ".join(connection_tokens))

        self.end_headers()

        # HTTPResponse.read() will decode chunks, but we want to pass them
        # through. Use this "hack" to pass through the encoding, and just use
        # our own reader. Field is undocumented, but public.
        response.chunked = False

        # https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4
        if response.getheader("Transfer-Encoding", "identity") != "identity":
            body = read_to_end_of_chunks(response)
        elif response.getheader("Content-Length"):
            try:
                length = int(response.getheader("Content-Length"))
            except ValueError:
                body = read_all(response, self.BUFLEN)
            else:
                body = read_to_limit(response, length, self.BUFLEN)
        else:
            # May hang if the server wrongly keeps the connection alive
            body = read_all(response, self.BUFLEN)

        for chunk in body:
            self.wfile.write(chunk)

    def do_proxy(self):
        """Handles proxying any normal HTTP request (not CONNECT).

        This method is a generic implementation of the do_* handlers for normal
        HTTP methods.
        """
        upstream = None
        response = None
        try:
            if not self.do_auth():
                return

            upstream, response = self.proxy_request()
        except _HTTPError as exc:
            self.send_error(exc.code, message=exc.message, explain=exc.explain)
        except Exception:
            self.log_error("%s", traceback.format_exc())
            self.send_error(500, explain=traceback.format_exc())

        if not response:
            return

        try:
            self.proxy_response(response)
        except Exception:
            self.log_error("%s", traceback.format_exc())
            self.close_connection = True

        upstream.close()

    # pylint:disable=invalid-name
    def do_GET(self):
        """Handles a proxy GET request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_POST(self):
        """Handles a proxy POST request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_PUT(self):
        """Handles a proxy PUT request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_PATCH(self):
        """Handles a proxy PATCH request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_HEAD(self):
        """Handles a proxy HEAD request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_OPTIONS(self):
        """Handles a proxy OPTIONS request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_DELETE(self):
        """Handles a proxy DELETE request."""
        self.do_proxy()

    # pylint:disable=invalid-name
    def do_TRACE(self):
        """Handles a proxy TRACE request."""
        self.do_proxy()


class _ThreadingHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):

    daemon_threads = True


class Main:

    def __init__(self):
        self.parser = argparse.ArgumentParser("Simple HTTP proxy")
        self.parser.add_argument("--port", type=int, default=8080)
        self.parser.add_argument("--basic-auth")
        self.parser.add_argument("--timeout", type=int, default=30)
        self.parser.add_argument("--bind-host", default="localhost")

        self.args = None
        self.server = None
        self.address = None

    def run(self):
        """Command-line entry point for http_proxy."""
        self.args = self.parser.parse_args()

        self.address = (self.args.bind_host, self.args.port)

        if self.args.basic_auth:
            Handler.basic_auth = base64.b64encode(
                self.args.basic_auth.encode()).decode()
        else:
            Handler.basic_auth = None

        self.server = _ThreadingHTTPServer(self.address, Handler)
        self.server.serve_forever()

    def shutdown(self):
        if self.server is not None:
            self.server.shutdown()


if __name__ == "__main__":
    Main().run()