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.py38
1 files changed, 18 insertions, 20 deletions
diff --git a/libmproxy/protocol2/http.py b/libmproxy/protocol2/http.py
index 1e774648..f629a6b0 100644
--- a/libmproxy/protocol2/http.py
+++ b/libmproxy/protocol2/http.py
@@ -4,8 +4,7 @@ from .. import version
from ..exceptions import InvalidCredentials, HttpException, ProtocolException
from .layer import Layer, ServerConnectionMixin
from libmproxy import utils
-from .messages import ChangeServer, Connect, Reconnect, Kill
-from .http_proxy import HttpProxy, HttpUpstreamProxy
+from .messages import SetServer, Connect, Reconnect, Kill
from libmproxy.protocol import KILL
from libmproxy.protocol.http import HTTPFlow
@@ -66,20 +65,15 @@ def make_connect_response(httpversion):
)
-class HttpLayer(Layer, ServerConnectionMixin):
+class HttpLayer(Layer):
+
"""
HTTP 1 Layer
"""
- def __init__(self, ctx):
+ def __init__(self, ctx, mode):
super(HttpLayer, self).__init__(ctx)
- if any(isinstance(l, HttpProxy) for l in self.layers):
- self.mode = "regular"
- elif any(isinstance(l, HttpUpstreamProxy) for l in self.layers):
- self.mode = "upstream"
- else:
- # also includes socks or reverse mode, which are handled similarly on this layer.
- self.mode = "transparent"
+ self.mode = mode
def __call__(self):
while True:
@@ -100,7 +94,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
# Regular Proxy Mode: Handle CONNECT
if self.mode == "regular" and request.form_in == "authority":
- self.server_address = (request.host, request.port)
+ yield SetServer((request.host, request.port), False, None)
self.send_to_client(make_connect_response(request.httpversion))
layer = self.ctx.next_layer(self)
for message in layer():
@@ -199,7 +193,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
self.send_to_server(flow.request)
flow.response = HTTP1.read_response(
- self.server_conn.protocol,
+ self.server_conn,
flow.request.method,
body_size_limit=self.config.body_size_limit,
include_body=False,
@@ -215,6 +209,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
flow.response.content = CONTENT_MISSING
else:
flow.response.content = HTTP1.read_http_body(
+ self.server_conn,
flow.response.headers,
self.config.body_size_limit,
flow.request.method,
@@ -250,7 +245,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
else:
flow.request.host = self.ctx.server_address.host
flow.request.port = self.ctx.server_address.port
- flow.request.scheme = self.server_conn.tls_established
+ flow.request.scheme = "https" if self.server_conn.tls_established else "http"
# TODO: Expose ChangeServer functionality to inline scripts somehow? (yield_from_callback?)
request_reply = self.channel.ask("request", flow)
@@ -266,8 +261,8 @@ class HttpLayer(Layer, ServerConnectionMixin):
tls = (flow.request.scheme == "https")
if self.mode == "regular" or self.mode == "transparent":
# If there's an existing connection that doesn't match our expectations, kill it.
- if self.server_address != address or tls != self.server_address.ssl_established:
- yield ChangeServer(address, tls, address.host)
+ if self.server_address != address or tls != self.server_conn.ssl_established:
+ yield SetServer(address, tls, address.host)
# Establish connection is neccessary.
if not self.server_conn:
yield Connect()
@@ -303,7 +298,7 @@ class HttpLayer(Layer, ServerConnectionMixin):
expected_request_forms = {
"regular": ("absolute",), # an authority request would already be handled.
"upstream": ("authority", "absolute"),
- "transparent": ("regular",)
+ "transparent": ("relative",)
}
allowed_request_forms = expected_request_forms[self.mode]
@@ -314,6 +309,9 @@ class HttpLayer(Layer, ServerConnectionMixin):
self.send_to_client(make_error_response(400, err_message))
raise HttpException(err_message)
+ if self.mode == "regular":
+ request.form_out = "relative"
+
def authenticate(self, request):
if self.config.authenticator:
if self.config.authenticator.authenticate(request.headers):
@@ -327,10 +325,10 @@ class HttpLayer(Layer, ServerConnectionMixin):
raise InvalidCredentials("Proxy Authentication Required")
def send_to_server(self, message):
- self.server_conn.wfile.wrie(message)
+ self.server_conn.send(HTTP1.assemble(message))
+
def send_to_client(self, message):
# FIXME
# - possibly do some http2 stuff here
- # - fix message assembly.
- self.client_conn.wfile.write(message)
+ self.client_conn.send(HTTP1.assemble(message))