diff options
author | Thomas Kriechbaumer <Kriechi@users.noreply.github.com> | 2016-02-04 10:51:18 +0100 |
---|---|---|
committer | Thomas Kriechbaumer <Kriechi@users.noreply.github.com> | 2016-02-04 10:51:18 +0100 |
commit | 023026e032f7f78a53a598eb7bd130d1b14930d2 (patch) | |
tree | 01982db6659f75f2bfe737a81b82022550e48e28 /libmproxy/protocol/http1.py | |
parent | ae4a1dd6dec9d95e450837db8937e3fb7604c121 (diff) | |
parent | 98f54d21b6156ceaa8450072be9f53dfde137248 (diff) | |
download | mitmproxy-023026e032f7f78a53a598eb7bd130d1b14930d2.tar.gz mitmproxy-023026e032f7f78a53a598eb7bd130d1b14930d2.tar.bz2 mitmproxy-023026e032f7f78a53a598eb7bd130d1b14930d2.zip |
Merge pull request #883 from mitmproxy/hyper-h2
HTTP/2: Implementation using hyper-h2
Diffstat (limited to 'libmproxy/protocol/http1.py')
-rw-r--r-- | libmproxy/protocol/http1.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/libmproxy/protocol/http1.py b/libmproxy/protocol/http1.py new file mode 100644 index 00000000..fc2cf07a --- /dev/null +++ b/libmproxy/protocol/http1.py @@ -0,0 +1,70 @@ +from __future__ import (absolute_import, print_function, division) + +import six + +from netlib import tcp +from netlib.http import http1 + +from .http import _HttpTransmissionLayer, HttpLayer +from .. import utils +from ..models import HTTPRequest, HTTPResponse + + +class Http1Layer(_HttpTransmissionLayer): + + def __init__(self, ctx, mode): + super(Http1Layer, self).__init__(ctx) + self.mode = mode + + def read_request(self): + req = http1.read_request(self.client_conn.rfile, body_size_limit=self.config.body_size_limit) + return HTTPRequest.wrap(req) + + def read_request_body(self, request): + expected_size = http1.expected_http_body_size(request) + return http1.read_body(self.client_conn.rfile, expected_size, self.config.body_size_limit) + + def send_request(self, request): + self.server_conn.wfile.write(http1.assemble_request(request)) + self.server_conn.wfile.flush() + + def read_response_headers(self): + resp = http1.read_response_head(self.server_conn.rfile) + return HTTPResponse.wrap(resp) + + def read_response_body(self, request, response): + expected_size = http1.expected_http_body_size(request, response) + return http1.read_body(self.server_conn.rfile, expected_size, self.config.body_size_limit) + + def send_response_headers(self, response): + raw = http1.assemble_response_head(response) + self.client_conn.wfile.write(raw) + self.client_conn.wfile.flush() + + def send_response_body(self, response, chunks): + for chunk in http1.assemble_body(response.headers, chunks): + self.client_conn.wfile.write(chunk) + self.client_conn.wfile.flush() + + def check_close_connection(self, flow): + request_close = http1.connection_close( + flow.request.http_version, + flow.request.headers + ) + response_close = http1.connection_close( + flow.response.http_version, + flow.response.headers + ) + read_until_eof = http1.expected_http_body_size(flow.request, flow.response) == -1 + close_connection = request_close or response_close or read_until_eof + if flow.request.form_in == "authority" and flow.response.status_code == 200: + # Workaround for https://github.com/mitmproxy/mitmproxy/issues/313: + # Charles Proxy sends a CONNECT response with HTTP/1.0 + # and no Content-Length header + + return False + return close_connection + + def __call__(self): + layer = HttpLayer(self, self.mode) + layer() |