aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/protocol2/http.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmproxy/protocol2/http.py')
-rw-r--r--libmproxy/protocol2/http.py51
1 files changed, 39 insertions, 12 deletions
diff --git a/libmproxy/protocol2/http.py b/libmproxy/protocol2/http.py
index 7cc27652..cabec806 100644
--- a/libmproxy/protocol2/http.py
+++ b/libmproxy/protocol2/http.py
@@ -9,15 +9,42 @@ from libmproxy.protocol import KILL
from libmproxy.protocol.http import HTTPFlow
from libmproxy.protocol.http_wrappers import HTTPResponse, HTTPRequest
-from libmproxy.protocol2.http_protocol_mock import HTTP1
from libmproxy.protocol2.tls import TlsLayer
from netlib import tcp
from netlib.http import status_codes, http1, HttpErrorConnClosed
from netlib.http.semantics import CONTENT_MISSING
from netlib import odict
from netlib.tcp import NetLibError
+from netlib.http.http1 import HTTP1Protocol
+from netlib.http.http2 import HTTP2Protocol
+
+class Http1Layer(Layer):
+ def __init__(self, ctx, mode):
+ super(Http1Layer, self).__init__(ctx)
+ self.mode = mode
+ self.client_protocol = HTTP1Protocol(self.client_conn)
+ self.server_protocol = HTTP1Protocol(self.server_conn)
+
+ def __call__(self):
+ from .http import HttpLayer
+ layer = HttpLayer(self, self.mode)
+ for message in layer():
+ yield message
+class Http2Layer(Layer):
+ def __init__(self, ctx, mode):
+ super(Http2Layer, self).__init__(ctx)
+ self.mode = mode
+ self.client_protocol = HTTP2Protocol(self.client_conn, is_server=True)
+ self.server_protocol = HTTP2Protocol(self.server_conn, is_server=False)
+
+ def __call__(self):
+ from .http import HttpLayer
+ layer = HttpLayer(self, self.mode)
+ for message in layer():
+ yield message
+
def make_error_response(status_code, message, headers=None):
response = status_codes.RESPONSES.get(status_code, "Unknown")
body = """
@@ -79,8 +106,8 @@ class HttpLayer(Layer):
while True:
try:
try:
- request = HTTP1.read_request(
- self.client_conn,
+ request = HTTPRequest.from_protocol(
+ self.client_protocol,
body_size_limit=self.config.body_size_limit
)
except tcp.NetLibError:
@@ -168,12 +195,12 @@ class HttpLayer(Layer):
# streaming:
# First send the headers and then transfer the response
# incrementally:
- h = HTTP1._assemble_response_first_line(flow.response)
+ h = self.client_protocol._assemble_response_first_line(flow.response)
self.send_to_client(h + "\r\n")
- h = HTTP1._assemble_response_headers(flow.response, preserve_transfer_encoding=True)
+ h = self.client_protocol._assemble_response_headers(flow.response, preserve_transfer_encoding=True)
self.send_to_client(h + "\r\n")
- chunks = HTTP1.read_http_body_chunked(
+ chunks = self.client_protocol.read_http_body_chunked(
flow.response.headers,
self.config.body_size_limit,
flow.request.method,
@@ -196,8 +223,8 @@ class HttpLayer(Layer):
# TODO: Add second attempt.
self.send_to_server(flow.request)
- flow.response = HTTP1.read_response(
- self.server_conn,
+ flow.response = HTTPResponse.from_protocol(
+ self.server_protocol,
flow.request.method,
body_size_limit=self.config.body_size_limit,
include_body=False,
@@ -211,8 +238,8 @@ class HttpLayer(Layer):
if flow.response.stream:
flow.response.content = CONTENT_MISSING
- else:
- flow.response.content = HTTP1.read_http_body(
+ elif isinstance(self.server_protocol, http1.HTTP1Protocol):
+ flow.response.content = self.server_protocol.read_http_body(
self.server_conn,
flow.response.headers,
self.config.body_size_limit,
@@ -329,9 +356,9 @@ class HttpLayer(Layer):
raise InvalidCredentials("Proxy Authentication Required")
def send_to_server(self, message):
- self.server_conn.send(HTTP1.assemble(message))
+ self.server_conn.send(self.server_protocol.assemble(message))
def send_to_client(self, message):
# FIXME
# - possibly do some http2 stuff here
- self.client_conn.send(HTTP1.assemble(message))
+ self.client_conn.send(self.client_protocol.assemble(message))