Clean up models.http a bit

- We don't need a deprecation warning here
- Bring imports inline with policy
This commit is contained in:
Aldo Cortesi 2016-10-12 10:57:40 +13:00
parent fdb6a44245
commit c8f2f1019d
1 changed files with 17 additions and 31 deletions

View File

@ -1,30 +1,14 @@
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import cgi import cgi
import warnings
from mitmproxy.models.flow import Flow from mitmproxy.models import flow
from netlib import http
from netlib import version from netlib import version
from netlib.http import Headers from netlib import tcp
from netlib.http import Request
from netlib.http import Response
from netlib.http import status_codes
from netlib.tcp import Address
class MessageMixin(object): class HTTPRequest(http.Request):
def get_decoded_content(self):
"""
Returns the decoded content based on the current Content-Encoding
header.
Doesn't change the message iteself or its headers.
"""
warnings.warn(".get_decoded_content() is deprecated, please use .content directly instead.", DeprecationWarning)
return self.content
class HTTPRequest(MessageMixin, Request):
""" """
A mitmproxy HTTP request. A mitmproxy HTTP request.
@ -49,7 +33,7 @@ class HTTPRequest(MessageMixin, Request):
stickycookie=False, stickycookie=False,
stickyauth=False, stickyauth=False,
): ):
Request.__init__( http.Request.__init__(
self, self,
first_line_format, first_line_format,
method, method,
@ -110,7 +94,7 @@ class HTTPRequest(MessageMixin, Request):
return id(self) return id(self)
class HTTPResponse(MessageMixin, Response): class HTTPResponse(http.Response):
""" """
A mitmproxy HTTP response. A mitmproxy HTTP response.
@ -129,7 +113,7 @@ class HTTPResponse(MessageMixin, Response):
timestamp_end=None, timestamp_end=None,
is_replay=False is_replay=False
): ):
Response.__init__( http.Response.__init__(
self, self,
http_version, http_version,
status_code, status_code,
@ -161,7 +145,7 @@ class HTTPResponse(MessageMixin, Response):
return resp return resp
class HTTPFlow(Flow): class HTTPFlow(flow.Flow):
""" """
A HTTPFlow is a collection of objects representing a single HTTP A HTTPFlow is a collection of objects representing a single HTTP
@ -188,7 +172,7 @@ class HTTPFlow(Flow):
self.response = None self.response = None
"""@type: HTTPResponse""" """@type: HTTPResponse"""
_stateobject_attributes = Flow._stateobject_attributes.copy() _stateobject_attributes = flow.Flow._stateobject_attributes.copy()
_stateobject_attributes.update( _stateobject_attributes.update(
request=HTTPRequest, request=HTTPRequest,
response=HTTPResponse response=HTTPResponse
@ -225,7 +209,7 @@ class HTTPFlow(Flow):
def make_error_response(status_code, message, headers=None): def make_error_response(status_code, message, headers=None):
response = status_codes.RESPONSES.get(status_code, "Unknown") response = http.status_codes.RESPONSES.get(status_code, "Unknown")
body = """ body = """
<html> <html>
<head> <head>
@ -237,7 +221,7 @@ def make_error_response(status_code, message, headers=None):
body = body.encode("utf8", "replace") body = body.encode("utf8", "replace")
if not headers: if not headers:
headers = Headers( headers = http.Headers(
Server=version.MITMPROXY, Server=version.MITMPROXY,
Connection="close", Connection="close",
Content_Length=str(len(body)), Content_Length=str(len(body)),
@ -254,10 +238,10 @@ def make_error_response(status_code, message, headers=None):
def make_connect_request(address): def make_connect_request(address):
address = Address.wrap(address) address = tcp.Address.wrap(address)
return HTTPRequest( return HTTPRequest(
"authority", b"CONNECT", None, address.host, address.port, None, b"HTTP/1.1", "authority", b"CONNECT", None, address.host, address.port, None, b"HTTP/1.1",
Headers(), b"" http.Headers(), b""
) )
@ -268,8 +252,10 @@ def make_connect_response(http_version):
http_version, http_version,
200, 200,
b"Connection established", b"Connection established",
Headers(), http.Headers(),
b"", b"",
) )
expect_continue_response = HTTPResponse(b"HTTP/1.1", 100, b"Continue", Headers(), b"") expect_continue_response = HTTPResponse(
b"HTTP/1.1", 100, b"Continue", http.Headers(), b""
)