diff options
author | Maximilian Hils <git@maximilianhils.com> | 2015-07-24 17:43:41 +0200 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2015-07-24 17:43:41 +0200 |
commit | f8b4d666870163d6770aabefe212b8ecd981d490 (patch) | |
tree | 8c6f2fc9ef4c06c40a04514f651785540d278bf3 /libpathod/protocols/http.py | |
parent | 9a1bee31d6d7b92bf5b4cb68d853c856acdfb036 (diff) | |
parent | 96c9c4459f0bb9b76ab34f8c8d03d0e5d28621f4 (diff) | |
download | mitmproxy-f8b4d666870163d6770aabefe212b8ecd981d490.tar.gz mitmproxy-f8b4d666870163d6770aabefe212b8ecd981d490.tar.bz2 mitmproxy-f8b4d666870163d6770aabefe212b8ecd981d490.zip |
Merge pull request #31 from Kriechi/protocol-refactor
HTTP protocol refactoring
Diffstat (limited to 'libpathod/protocols/http.py')
-rw-r--r-- | libpathod/protocols/http.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/libpathod/protocols/http.py b/libpathod/protocols/http.py new file mode 100644 index 00000000..ca2b28b4 --- /dev/null +++ b/libpathod/protocols/http.py @@ -0,0 +1,72 @@ +from netlib import tcp, http, wsgi +from netlib.http import http1 +from .. import version, app, language, utils, log + +class HTTPProtocol: + + 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) + + def handle_http_app(self, method, path, headers, body, lg): + """ + Handle a request to the built-in app. + """ + if self.pathod_handler.server.noweb: + crafted = self.pathod_handler.make_http_error_response("Access Denied") + language.serve(crafted, self.pathod_handler.wfile, self.pathod_handler.settings) + return None, dict( + type="error", + msg="Access denied: web interface disabled" + ) + lg("app: %s %s" % (method, path)) + req = wsgi.Request("http", method, path, headers, body) + flow = wsgi.Flow(self.pathod_handler.address, req) + sn = self.pathod_handler.connection.getsockname() + a = wsgi.WSGIAdaptor( + self.pathod_handler.server.app, + sn[0], + self.pathod_handler.server.address.port, + version.NAMEVERSION + ) + a.serve(flow, self.pathod_handler.wfile) + return self.pathod_handler.handle_http_request, None + + def handle_http_connect(self, connect, lg): + """ + Handle a CONNECT request. + """ + + self.pathod_handler.wfile.write( + 'HTTP/1.1 200 Connection established\r\n' + + ('Proxy-agent: %s\r\n' % version.NAMEVERSION) + + '\r\n' + ) + self.pathod_handler.wfile.flush() + if not self.pathod_handler.server.ssloptions.not_after_connect: + try: + cert, key, chain_file_ = self.pathod_handler.server.ssloptions.get_cert( + connect[0] + ) + self.pathod_handler.convert_to_ssl( + cert, + key, + handle_sni=self.pathod_handler.handle_sni, + request_client_cert=self.pathod_handler.server.ssloptions.request_client_cert, + cipher_list=self.pathod_handler.server.ssloptions.ciphers, + method=self.pathod_handler.server.ssloptions.ssl_version, + alpn_select=self.pathod_handler.server.ssloptions.alpn_select, + ) + except tcp.NetLibError as v: + s = str(v) + lg(s) + return None, dict(type="error", msg=s) + return self.pathod_handler.handle_http_request, None + + def read_request(self, lg=None): + return self.wire_protocol.read_request(allow_empty=True) |