minor fixes

This commit is contained in:
Maximilian Hils 2015-08-16 12:43:15 +02:00
parent a175572447
commit 4c31ffd90f
3 changed files with 48 additions and 47 deletions

View File

@ -12,9 +12,10 @@ from libmproxy.protocol.http_wrappers import HTTPResponse, HTTPRequest
from libmproxy.protocol2.http_protocol_mock import HTTP1 from libmproxy.protocol2.http_protocol_mock import HTTP1
from libmproxy.protocol2.tls import TlsLayer from libmproxy.protocol2.tls import TlsLayer
from netlib import tcp from netlib import tcp
from netlib.http import status_codes, http1 from netlib.http import status_codes, http1, HttpErrorConnClosed
from netlib.http.semantics import CONTENT_MISSING from netlib.http.semantics import CONTENT_MISSING
from netlib import odict from netlib import odict
from netlib.tcp import NetLibError
def make_error_response(status_code, message, headers=None): def make_error_response(status_code, message, headers=None):
@ -66,7 +67,6 @@ def make_connect_response(httpversion):
class HttpLayer(Layer): class HttpLayer(Layer):
""" """
HTTP 1 Layer HTTP 1 Layer
""" """
@ -78,51 +78,55 @@ class HttpLayer(Layer):
def __call__(self): def __call__(self):
while True: while True:
try: try:
request = HTTP1.read_request( try:
self.client_conn, request = HTTP1.read_request(
body_size_limit=self.config.body_size_limit self.client_conn,
) body_size_limit=self.config.body_size_limit
except tcp.NetLibError: )
# don't throw an error for disconnects that happen except tcp.NetLibError:
# before/between requests. # don't throw an error for disconnects that happen
return # before/between requests.
return
self.log("request", "debug", [repr(request)]) self.log("request", "debug", [repr(request)])
# Handle Proxy Authentication # Handle Proxy Authentication
self.authenticate(request) self.authenticate(request)
# Regular Proxy Mode: Handle CONNECT # Regular Proxy Mode: Handle CONNECT
if self.mode == "regular" and request.form_in == "authority": if self.mode == "regular" and request.form_in == "authority":
yield SetServer((request.host, request.port), False, None) yield SetServer((request.host, request.port), False, None)
self.send_to_client(make_connect_response(request.httpversion)) self.send_to_client(make_connect_response(request.httpversion))
layer = self.ctx.next_layer(self) layer = self.ctx.next_layer(self)
for message in layer(): for message in layer():
if not self._handle_server_message(message): if not self._handle_server_message(message):
yield message
return
# Make sure that the incoming request matches our expectations
self.validate_request(request)
flow = HTTPFlow(self.client_conn, self.server_conn)
flow.request = request
for message in self.process_request_hook(flow):
yield message
if not flow.response:
for message in self.establish_server_connection(flow):
yield message
for message in self.get_response_from_server(flow):
yield message yield message
return
# Make sure that the incoming request matches our expectations self.send_response_to_client(flow)
self.validate_request(request)
flow = HTTPFlow(self.client_conn, self.server_conn) if self.check_close_connection(flow):
flow.request = request return
for message in self.process_request_hook(flow):
yield message
if not flow.response: if flow.request.form_in == "authority" and flow.response.code == 200:
for message in self.establish_server_connection(flow): raise NotImplementedError("Upstream mode CONNECT not implemented")
yield message except (HttpErrorConnClosed, NetLibError) as e:
for message in self.get_response_from_server(flow): make_error_response(502, repr(e))
yield message raise ProtocolException(repr(e), e)
self.send_response_to_client(flow)
if self.check_close_connection(flow):
return
if flow.request.form_in == "authority" and flow.response.code == 200:
raise NotImplementedError("Upstream mode CONNECT not implemented")
def check_close_connection(self, flow): def check_close_connection(self, flow):
""" """
@ -144,7 +148,7 @@ class HttpLayer(Layer):
False, False,
flow.request.method, flow.request.method,
flow.response.code) == -1 flow.response.code) == -1
) )
if flow.request.form_in == "authority" and flow.response.code == 200: if flow.request.form_in == "authority" and flow.response.code == 200:
# Workaround for # Workaround for
# https://github.com/mitmproxy/mitmproxy/issues/313: Some # https://github.com/mitmproxy/mitmproxy/issues/313: Some
@ -189,7 +193,7 @@ class HttpLayer(Layer):
flow.response.timestamp_end = utils.timestamp() flow.response.timestamp_end = utils.timestamp()
def get_response_from_server(self, flow): def get_response_from_server(self, flow):
# TODO: Add second attempt.
self.send_to_server(flow.request) self.send_to_server(flow.request)
flow.response = HTTP1.read_response( flow.response = HTTP1.read_response(
@ -327,7 +331,6 @@ class HttpLayer(Layer):
def send_to_server(self, message): def send_to_server(self, message):
self.server_conn.send(HTTP1.assemble(message)) self.server_conn.send(HTTP1.assemble(message))
def send_to_client(self, message): def send_to_client(self, message):
# FIXME # FIXME
# - possibly do some http2 stuff here # - possibly do some http2 stuff here

View File

@ -1,6 +1,7 @@
from __future__ import (absolute_import, print_function, division) from __future__ import (absolute_import, print_function, division)
from .layer import Layer, ServerConnectionMixin from .layer import Layer, ServerConnectionMixin
from .http import HttpLayer
class HttpProxy(Layer, ServerConnectionMixin): class HttpProxy(Layer, ServerConnectionMixin):
@ -20,6 +21,4 @@ class HttpUpstreamProxy(Layer, ServerConnectionMixin):
layer = HttpLayer(self, "upstream") layer = HttpLayer(self, "upstream")
for message in layer(): for message in layer():
if not self._handle_server_message(message): if not self._handle_server_message(message):
yield message yield message
from .http import HttpLayer

View File

@ -200,7 +200,6 @@ def yield_from_callback(fun):
if msg is True: if msg is True:
break break
elif isinstance(msg, Exception): elif isinstance(msg, Exception):
# TODO: Include func name?
raise ProtocolException("Error in %s: %s" % (fun.__name__, repr(msg)), msg) raise ProtocolException("Error in %s: %s" % (fun.__name__, repr(msg)), msg)
else: else:
yield msg yield msg