diff options
| author | Maximilian Hils <git@maximilianhils.com> | 2015-09-16 20:22:19 +0200 |
|---|---|---|
| committer | Maximilian Hils <git@maximilianhils.com> | 2015-09-16 20:22:19 +0200 |
| commit | a7291a7e7802633735cfcf40695527185d4fb837 (patch) | |
| tree | cfc04eb1b998c8ca4c1428eae52974f1865f6bf7 /libpathod | |
| parent | b889e9e1a2576b0f0aadb4180dd6f4c75db204d6 (diff) | |
| parent | 1a6e41c2002773bb82a5d95d8d9a8ab3aa422c98 (diff) | |
| download | mitmproxy-a7291a7e7802633735cfcf40695527185d4fb837.tar.gz mitmproxy-a7291a7e7802633735cfcf40695527185d4fb837.tar.bz2 mitmproxy-a7291a7e7802633735cfcf40695527185d4fb837.zip | |
Merge pull request #34 from mitmproxy/netlib-http1-refactor
Adjust to netlib changes
Diffstat (limited to 'libpathod')
| -rw-r--r-- | libpathod/language/http2.py | 7 | ||||
| -rw-r--r-- | libpathod/log.py | 2 | ||||
| -rw-r--r-- | libpathod/pathoc.py | 31 | ||||
| -rw-r--r-- | libpathod/pathod.py | 16 | ||||
| -rw-r--r-- | libpathod/protocols/http.py | 14 | ||||
| -rw-r--r-- | libpathod/protocols/http2.py | 2 |
6 files changed, 36 insertions, 36 deletions
diff --git a/libpathod/language/http2.py b/libpathod/language/http2.py index 2c3f1786..829a05db 100644 --- a/libpathod/language/http2.py +++ b/libpathod/language/http2.py @@ -1,6 +1,7 @@ import pyparsing as pp -from netlib.http import user_agents, semantics, Headers +from netlib import http +from netlib.http import user_agents, Headers from . import base, message """ @@ -184,7 +185,7 @@ class Response(_HTTP2Message): if body: body = body.string() - resp = semantics.Response( + resp = http.Response( (2, 0), self.code.string(), '', @@ -267,7 +268,7 @@ class Request(_HTTP2Message): if body: body = body.string() - req = semantics.Request( + req = http.Request( '', self.method.string(), '', diff --git a/libpathod/log.py b/libpathod/log.py index 69229adf..f203542f 100644 --- a/libpathod/log.py +++ b/libpathod/log.py @@ -63,7 +63,7 @@ class LogCtx(object): for line in netlib.utils.hexdump(data): self("\t%s %s %s" % line) else: - for i in netlib.utils.cleanBin(data).split("\n"): + for i in netlib.utils.clean_bin(data).split("\n"): self("\t%s" % i) def __call__(self, line): diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index ac0b0e4d..d616761e 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -10,8 +10,10 @@ import time import threading import OpenSSL.crypto +import six from netlib import tcp, http, certutils, websockets, socks +from netlib.exceptions import HttpException from netlib.http import http1, http2 import language.http @@ -19,7 +21,7 @@ import language.websockets from . import utils, log import logging -from netlib.http.http1 import HTTP1Protocol +from netlib.tutils import treq logging.getLogger("hpack").setLevel(logging.WARNING) @@ -213,7 +215,7 @@ class Pathoc(tcp.TCPClient): ) self.protocol = http2.HTTP2Protocol(self, dump_frames=self.http2_framedump) else: - self.protocol = http1.HTTP1Protocol(self) + self.protocol = http1 self.settings = language.Settings( is_client=True, @@ -229,15 +231,14 @@ class Pathoc(tcp.TCPClient): '\r\n' ) self.wfile.flush() - l = self.rfile.readline() - if not l: - raise PathocError("Proxy CONNECT failed") - parsed = self.protocol.parse_response_line(l) - if not parsed[1] == 200: - raise PathocError( - "Proxy CONNECT failed: %s - %s" % (parsed[1], parsed[2]) - ) - self.protocol.read_headers() + try: + resp = self.protocol.read_response(self.rfile, treq(method="CONNECT")) + if resp.status_code != 200: + raise HttpException("Unexpected status code: %s" % resp.status_code) + except HttpException as e: + six.reraise(PathocError, PathocError( + "Proxy CONNECT failed: %s" % repr(e) + )) def socks_connect(self, connect_to): try: @@ -288,9 +289,9 @@ class Pathoc(tcp.TCPClient): self.sslinfo = None if self.ssl: try: - alpn_protos = [HTTP1Protocol.ALPN_PROTO_HTTP1] + alpn_protos = [http.ALPN_PROTO_HTTP1] if self.use_http2: - alpn_protos.append(http2.HTTP2Protocol.ALPN_PROTO_H2) + alpn_protos.append(http.ALPN_PROTO_H2) self.convert_to_ssl( sni=self.sni, @@ -408,9 +409,9 @@ class Pathoc(tcp.TCPClient): req = language.serve(r, self.wfile, self.settings) self.wfile.flush() - resp = self.protocol.read_response(req["method"], None) + resp = self.protocol.read_response(self.rfile, treq(method=req["method"])) resp.sslinfo = self.sslinfo - except http.HttpError as v: + except HttpException as v: lg("Invalid server response: %s" % v) raise except tcp.NetLibTimeout: diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 4b94ec91..6478fe4f 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -6,7 +6,8 @@ import threading import urllib from netlib import tcp, http, certutils, websockets -from netlib.http import http1, http2 +from netlib.exceptions import HttpException, HttpReadDisconnect +from netlib.http import ALPN_PROTO_HTTP1, ALPN_PROTO_H2 from . import version, app, language, utils, log, protocols import language.http @@ -40,7 +41,7 @@ class SSLOptions(object): ssl_options=tcp.SSL_DEFAULT_OPTIONS, ciphers=None, certs=None, - alpn_select=http2.HTTP2Protocol.ALPN_PROTO_H2, + alpn_select=ALPN_PROTO_H2, ): self.confdir = confdir self.cn = cn @@ -124,15 +125,14 @@ class PathodHandler(tcp.BaseHandler): """ with logger.ctx() as lg: try: - req = self.protocol.read_request() - except http.HttpError as s: + req = self.protocol.read_request(self.rfile) + except HttpReadDisconnect: + return None, None + except HttpException as s: s = str(s) lg(s) return None, dict(type="error", msg=s) - if isinstance(req, http.EmptyRequest): - return None, None - if req.method == 'CONNECT': return self.protocol.handle_http_connect([req.host, req.port, req.httpversion], lg) @@ -259,7 +259,7 @@ class PathodHandler(tcp.BaseHandler): return alp = self.get_alpn_proto_negotiated() - if alp == http2.HTTP2Protocol.ALPN_PROTO_H2: + if alp == ALPN_PROTO_H2: self.protocol = protocols.http2.HTTP2Protocol(self) self.use_http2 = True diff --git a/libpathod/protocols/http.py b/libpathod/protocols/http.py index 0539b68d..531854d6 100644 --- a/libpathod/protocols/http.py +++ b/libpathod/protocols/http.py @@ -1,14 +1,12 @@ -from netlib import tcp, http, wsgi -from netlib.http import http1 -from .. import version, app, language, utils, log +from netlib import tcp, wsgi +from netlib.exceptions import HttpReadDisconnect +from netlib.http import http1, Request +from .. import version, language -class HTTPProtocol: +class HTTPProtocol(object): def __init__(self, pathod_handler): self.pathod_handler = pathod_handler - self.wire_protocol = http1.HTTP1Protocol( - self.pathod_handler - ) def make_error_response(self, reason, body): return language.http.make_error_response(reason, body) @@ -70,4 +68,4 @@ class HTTPProtocol: return self.pathod_handler.handle_http_request, None def read_request(self, lg=None): - return self.wire_protocol.read_request(allow_empty=True) + return http1.read_request(self.pathod_handler.rfile) diff --git a/libpathod/protocols/http2.py b/libpathod/protocols/http2.py index f57f56f8..a098a14e 100644 --- a/libpathod/protocols/http2.py +++ b/libpathod/protocols/http2.py @@ -14,7 +14,7 @@ class HTTP2Protocol: def read_request(self, lg=None): self.wire_protocol.perform_server_connection_preface() - return self.wire_protocol.read_request() + return self.wire_protocol.read_request(self.pathod_handler.rfile) def assemble(self, message): return self.wire_protocol.assemble(message) |
