From 2f285a6015abaf75ced34231f698537ef210ffed Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sat, 26 Mar 2016 11:26:42 +0800 Subject: [PATCH 1/5] Setting CONTENT_MISSING to None --- netlib/http/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netlib/http/message.py b/netlib/http/message.py index e3d8ce375..4ddc1b818 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -7,7 +7,7 @@ import six from .headers import Headers from .. import encoding, utils -CONTENT_MISSING = 0 +CONTENT_MISSING = None if six.PY2: # pragma: nocover _native = lambda x: x From 08ff00f36d49ef53b1d20d7635a74fa46a249c17 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sat, 26 Mar 2016 16:00:51 +0800 Subject: [PATCH 2/5] replace CONTENT_MISSING with None. --- mitmproxy/console/common.py | 7 +++---- mitmproxy/console/flowview.py | 6 +++--- mitmproxy/dump.py | 5 ++--- mitmproxy/flow.py | 4 ++-- mitmproxy/models/http.py | 10 ++++------ mitmproxy/protocol/http.py | 8 ++++---- mitmproxy/web/app.py | 3 +-- netlib/http/__init__.py | 4 ++-- netlib/http/http1/assemble.py | 10 ++++------ netlib/http/message.py | 2 -- test/mitmproxy/test_dump.py | 7 +++---- test/mitmproxy/test_flow.py | 6 +++--- test/mitmproxy/test_server.py | 6 +++--- test/netlib/http/http1/test_assemble.py | 6 +++--- 14 files changed, 37 insertions(+), 47 deletions(-) diff --git a/mitmproxy/console/common.py b/mitmproxy/console/common.py index c29ffddc6..746a67f17 100644 --- a/mitmproxy/console/common.py +++ b/mitmproxy/console/common.py @@ -4,7 +4,6 @@ import urwid import urwid.util import os -from netlib.http import CONTENT_MISSING import netlib.utils from .. import utils @@ -256,7 +255,7 @@ def copy_flow_format_data(part, scope, flow): else: data = "" if scope in ("q", "a"): - if flow.request.content is None or flow.request.content == CONTENT_MISSING: + if flow.request.content is None: return None, "Request content is missing" with decoded(flow.request): if part == "h": @@ -269,7 +268,7 @@ def copy_flow_format_data(part, scope, flow): # Add padding between request and response data += "\r\n" * 2 if scope in ("s", "a") and flow.response: - if flow.response.content is None or flow.response.content == CONTENT_MISSING: + if flow.response.content is None: return None, "Response content is missing" with decoded(flow.response): if part == "h": @@ -418,7 +417,7 @@ def format_flow(f, focus, extended=False, hostheader=False, marked=False): if f.response: if f.response.content: contentdesc = netlib.utils.pretty_size(len(f.response.content)) - elif f.response.content == CONTENT_MISSING: + elif f.response.content is None: contentdesc = "[content missing]" else: contentdesc = "[no content]" diff --git a/mitmproxy/console/flowview.py b/mitmproxy/console/flowview.py index f74ab1405..3b86c9206 100644 --- a/mitmproxy/console/flowview.py +++ b/mitmproxy/console/flowview.py @@ -7,7 +7,7 @@ import math import urwid from netlib import odict -from netlib.http import CONTENT_MISSING, Headers +from netlib.http import Headers from . import common, grideditor, signals, searchable, tabs from . import flowdetailview from .. import utils, controller, contentviews @@ -169,7 +169,7 @@ class FlowView(tabs.Tabs): self.show() def content_view(self, viewmode, message): - if message.content == CONTENT_MISSING: + if message.content is None: msg, body = "", [urwid.Text([("error", "[content missing]")])] return msg, body else: @@ -510,7 +510,7 @@ class FlowView(tabs.Tabs): def delete_body(self, t): if t == "m": - val = CONTENT_MISSING + val = None else: val = None if self.tab_offset == TAB_REQ: diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index 7b4609b49..631e4d2e9 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -5,7 +5,6 @@ import click import itertools from netlib import tcp -from netlib.http import CONTENT_MISSING import netlib.utils from . import flow, filt, contentviews from .exceptions import ContentViewException @@ -180,7 +179,7 @@ class DumpMaster(flow.FlowMaster): ) self.echo(headers, indent=4) if self.o.flow_detail >= 3: - if message.content == CONTENT_MISSING: + if message.content is None: self.echo("(content missing)", indent=4) elif message.content: self.echo("") @@ -283,7 +282,7 @@ class DumpMaster(flow.FlowMaster): code = click.style(str(code), fg=code_color, bold=True, blink=(code == 418)) reason = click.style(flow.response.reason, fg=code_color, bold=True) - if flow.response.content == CONTENT_MISSING: + if flow.response.content is None: size = "(content missing)" else: size = netlib.utils.pretty_size(len(flow.response.content)) diff --git a/mitmproxy/flow.py b/mitmproxy/flow.py index d656bc4db..fbf102b51 100644 --- a/mitmproxy/flow.py +++ b/mitmproxy/flow.py @@ -16,7 +16,7 @@ from six.moves import urllib from netlib import wsgi from netlib.exceptions import HttpException -from netlib.http import CONTENT_MISSING, Headers, http1 +from netlib.http import Headers, http1 from . import controller, tnetstring, filt, script, version, flow_format_compat from .onboarding import app from .proxy.config import HostMatcher @@ -942,7 +942,7 @@ class FlowMaster(controller.Master): return "Can't replay live request." if f.intercepted: return "Can't replay while intercepting..." - if f.request.content == CONTENT_MISSING: + if f.request.content is None: return "Can't replay request with missing content..." if f.request: f.backup() diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py index a80e11b03..428b1ba66 100644 --- a/mitmproxy/models/http.py +++ b/mitmproxy/models/http.py @@ -84,9 +84,8 @@ class HTTPRequest(MessageMixin, Request): headers: Headers object - content: Content of the request, None, or CONTENT_MISSING if there - is content associated, but not present. CONTENT_MISSING evaluates - to False to make checking for the presence of content natural. + content: Content of the request, the value is None if there is content + associated, but not present. form_in: The request form which mitmproxy has received. The following values are possible: @@ -226,9 +225,8 @@ class HTTPResponse(MessageMixin, Response): headers: Headers object - content: Content of the request, None, or CONTENT_MISSING if there - is content associated, but not present. CONTENT_MISSING evaluates - to False to make checking for the presence of content natural. + content: Content of the request, the value is None if there is content + associated, but not present. timestamp_start: Timestamp indicating when request transmission started diff --git a/mitmproxy/protocol/http.py b/mitmproxy/protocol/http.py index a2745eac7..7f134efe0 100644 --- a/mitmproxy/protocol/http.py +++ b/mitmproxy/protocol/http.py @@ -6,7 +6,7 @@ import six from netlib import tcp from netlib.exceptions import HttpException, HttpReadDisconnect, NetlibException -from netlib.http import Headers, CONTENT_MISSING +from netlib.http import Headers from h2.exceptions import H2Error @@ -50,8 +50,8 @@ class _HttpTransmissionLayer(Layer): yield "this is a generator" # pragma: no cover def send_response(self, response): - if response.content == CONTENT_MISSING: - raise HttpException("Cannot assemble flow with CONTENT_MISSING") + if response.content == None: + raise HttpException("Cannot assemble flow with None content") self.send_response_headers(response) self.send_response_body(response, [response.content]) @@ -318,7 +318,7 @@ class HttpLayer(Layer): raise Kill() if flow.response.stream: - flow.response.data.content = CONTENT_MISSING + flow.response.data.content = None else: flow.response.data.content = b"".join(self.read_response_body( flow.request, diff --git a/mitmproxy/web/app.py b/mitmproxy/web/app.py index 2cac2ab9e..8a8329e72 100644 --- a/mitmproxy/web/app.py +++ b/mitmproxy/web/app.py @@ -8,7 +8,6 @@ import logging import json import base64 -from netlib.http import CONTENT_MISSING from .. import version, filt @@ -26,7 +25,7 @@ def _strip_content(flow_state): continue if message["content"]: message["contentLength"] = len(message["content"]) - elif message["content"] == CONTENT_MISSING: + elif message["content"] is None: message["contentLength"] = None else: message["contentLength"] = 0 diff --git a/netlib/http/__init__.py b/netlib/http/__init__.py index fd632cd5f..917080f7a 100644 --- a/netlib/http/__init__.py +++ b/netlib/http/__init__.py @@ -2,13 +2,13 @@ from __future__ import absolute_import, print_function, division from .request import Request from .response import Response from .headers import Headers -from .message import decoded, CONTENT_MISSING +from .message import decoded from . import http1, http2 __all__ = [ "Request", "Response", "Headers", - "decoded", "CONTENT_MISSING", + "decoded", "http1", "http2", ] diff --git a/netlib/http/http1/assemble.py b/netlib/http/http1/assemble.py index 785ee8d3e..db5a49ce2 100644 --- a/netlib/http/http1/assemble.py +++ b/netlib/http/http1/assemble.py @@ -3,12 +3,10 @@ from __future__ import absolute_import, print_function, division from ... import utils import itertools from ...exceptions import HttpException -from .. import CONTENT_MISSING - def assemble_request(request): - if request.content == CONTENT_MISSING: - raise HttpException("Cannot assemble flow with CONTENT_MISSING") + if request.content == None: + raise HttpException("Cannot assemble flow with None content") head = assemble_request_head(request) body = b"".join(assemble_body(request.data.headers, [request.data.content])) return head + body @@ -21,8 +19,8 @@ def assemble_request_head(request): def assemble_response(response): - if response.content == CONTENT_MISSING: - raise HttpException("Cannot assemble flow with CONTENT_MISSING") + if response.content == None: + raise HttpException("Cannot assemble flow with None content") head = assemble_response_head(response) body = b"".join(assemble_body(response.data.headers, [response.data.content])) return head + body diff --git a/netlib/http/message.py b/netlib/http/message.py index 4ddc1b818..1df0f087f 100644 --- a/netlib/http/message.py +++ b/netlib/http/message.py @@ -7,8 +7,6 @@ import six from .headers import Headers from .. import encoding, utils -CONTENT_MISSING = None - if six.PY2: # pragma: nocover _native = lambda x: x _always_bytes = lambda x: x diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py index 2228a7320..7e7728814 100644 --- a/test/mitmproxy/test_dump.py +++ b/test/mitmproxy/test_dump.py @@ -4,7 +4,6 @@ from mitmproxy.exceptions import ContentViewException from mitmproxy.models import HTTPResponse import netlib.tutils -from netlib.http import CONTENT_MISSING from mitmproxy import dump, flow from mitmproxy.proxy import Log @@ -38,7 +37,7 @@ def test_strfuncs(): flow.request.stickycookie = True flow.client_conn = mock.MagicMock() flow.client_conn.address.host = "foo" - flow.response = netlib.tutils.tresp(content=CONTENT_MISSING) + flow.response = netlib.tutils.tresp(content=None) flow.response.is_replay = True flow.response.status_code = 300 m.echo_flow(flow) @@ -104,10 +103,10 @@ class TestDumpMaster: o = dump.Options(flow_detail=3) m = dump.DumpMaster(None, o, outfile=cs) f = tutils.tflow() - f.request.content = CONTENT_MISSING + f.request.content = None m.handle_request(f) f.response = HTTPResponse.wrap(netlib.tutils.tresp()) - f.response.content = CONTENT_MISSING + f.response.content = None m.handle_response(f) assert "content missing" in cs.getvalue() diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 33d2b8f91..2353935bf 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -8,7 +8,7 @@ import mock import netlib.utils from netlib import odict -from netlib.http import CONTENT_MISSING, Headers +from netlib.http import Headers from mitmproxy import filt, controller, tnetstring, flow from mitmproxy.models import Error from mitmproxy.models import Flow @@ -465,7 +465,7 @@ class TestFlow(object): def test_replace_no_content(self): f = tutils.tflow() - f.request.content = CONTENT_MISSING + f.request.content = None assert f.replace("foo", "bar") == 0 def test_replace(self): @@ -751,7 +751,7 @@ class TestFlowMaster: s = flow.State() fm = flow.FlowMaster(None, s) f = tutils.tflow(resp=True) - f.request.content = CONTENT_MISSING + f.request.content = None assert "missing" in fm.replay_request(f) f.intercepted = True diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 26e53e8ae..dc72f032b 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -8,7 +8,7 @@ from netlib.tcp import Address import netlib.tutils from netlib import tcp, http, socks from netlib.certutils import SSLCert -from netlib.http import authentication, CONTENT_MISSING, http1 +from netlib.http import authentication, http1 from netlib.tutils import raises from pathod import pathoc, pathod @@ -281,7 +281,7 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin, AppMixin): self.pathod("200:b@3k") assert self.master.state.view[-1].response.stream - assert self.master.state.view[-1].response.content == CONTENT_MISSING + assert self.master.state.view[-1].response.content is None self.master.set_stream_large_bodies(None) def test_stream_modify(self): @@ -816,7 +816,7 @@ class MasterIncomplete(tservers.TestMaster): def handle_request(self, f): resp = HTTPResponse.wrap(netlib.tutils.tresp()) - resp.content = CONTENT_MISSING + resp.content = None f.reply(resp) diff --git a/test/netlib/http/http1/test_assemble.py b/test/netlib/http/http1/test_assemble.py index 31a62438e..8dcbae8ee 100644 --- a/test/netlib/http/http1/test_assemble.py +++ b/test/netlib/http/http1/test_assemble.py @@ -1,6 +1,6 @@ from __future__ import absolute_import, print_function, division from netlib.exceptions import HttpException -from netlib.http import CONTENT_MISSING, Headers +from netlib.http import Headers from netlib.http.http1.assemble import ( assemble_request, assemble_request_head, assemble_response, assemble_response_head, _assemble_request_line, _assemble_request_headers, @@ -20,7 +20,7 @@ def test_assemble_request(): ) with raises(HttpException): - assemble_request(treq(content=CONTENT_MISSING)) + assemble_request(treq(content=None)) def test_assemble_request_head(): @@ -41,7 +41,7 @@ def test_assemble_response(): ) with raises(HttpException): - assemble_response(tresp(content=CONTENT_MISSING)) + assemble_response(tresp(content=None)) def test_assemble_response_head(): From 53e15f778df316a223847cb81e4f2eb25d29a539 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sat, 26 Mar 2016 16:17:55 +0800 Subject: [PATCH 3/5] update document for the removal of CONTENT_MISSING --- docs/features/responsestreaming.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/features/responsestreaming.rst b/docs/features/responsestreaming.rst index 8975c1f80..9dc27bf48 100644 --- a/docs/features/responsestreaming.rst +++ b/docs/features/responsestreaming.rst @@ -48,8 +48,7 @@ Implementation Details ---------------------- When response streaming is enabled, portions of the code which would have otherwise performed -changes on the response body will see an empty response body instead -(:py:data:`netlib.http.CONTENT_MISSING`). Any modifications will be ignored. +changes on the response body will see an empty response body. Any modifications will be ignored. Streamed responses are usually sent in chunks of 4096 bytes. If the response is sent with a ``Transfer-Encoding: chunked`` header, the response will be streamed one chunk at a time. From 66bd27e6f9018832f99be1c211e76c8f71165c92 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sat, 26 Mar 2016 17:49:22 +0800 Subject: [PATCH 4/5] update comments --- mitmproxy/models/http.py | 2 +- mitmproxy/protocol/http.py | 4 ++-- netlib/http/http1/assemble.py | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py index 428b1ba66..40460182e 100644 --- a/mitmproxy/models/http.py +++ b/mitmproxy/models/http.py @@ -225,7 +225,7 @@ class HTTPResponse(MessageMixin, Response): headers: Headers object - content: Content of the request, the value is None if there is content + content: Content of the response, the value is None if there is content associated, but not present. timestamp_start: Timestamp indicating when request transmission started diff --git a/mitmproxy/protocol/http.py b/mitmproxy/protocol/http.py index 7f134efe0..22e714226 100644 --- a/mitmproxy/protocol/http.py +++ b/mitmproxy/protocol/http.py @@ -50,8 +50,8 @@ class _HttpTransmissionLayer(Layer): yield "this is a generator" # pragma: no cover def send_response(self, response): - if response.content == None: - raise HttpException("Cannot assemble flow with None content") + if response.content is None: + raise HttpException("Cannot assemble flow with missing content") self.send_response_headers(response) self.send_response_body(response, [response.content]) diff --git a/netlib/http/http1/assemble.py b/netlib/http/http1/assemble.py index db5a49ce2..f06ad5a10 100644 --- a/netlib/http/http1/assemble.py +++ b/netlib/http/http1/assemble.py @@ -5,8 +5,8 @@ import itertools from ...exceptions import HttpException def assemble_request(request): - if request.content == None: - raise HttpException("Cannot assemble flow with None content") + if request.content is None: + raise HttpException("Cannot assemble flow with missing content") head = assemble_request_head(request) body = b"".join(assemble_body(request.data.headers, [request.data.content])) return head + body @@ -19,8 +19,8 @@ def assemble_request_head(request): def assemble_response(response): - if response.content == None: - raise HttpException("Cannot assemble flow with None content") + if response.content is None: + raise HttpException("Cannot assemble flow with missing content") head = assemble_response_head(response) body = b"".join(assemble_body(response.data.headers, [response.data.content])) return head + body From 6e4af64050fe9a0efbfa73193131065b96feca3c Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Sun, 27 Mar 2016 09:16:40 +0800 Subject: [PATCH 5/5] minor fix about if-else statement --- mitmproxy/web/app.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mitmproxy/web/app.py b/mitmproxy/web/app.py index 8a8329e72..cf3c8bdd9 100644 --- a/mitmproxy/web/app.py +++ b/mitmproxy/web/app.py @@ -25,10 +25,8 @@ def _strip_content(flow_state): continue if message["content"]: message["contentLength"] = len(message["content"]) - elif message["content"] is None: - message["contentLength"] = None else: - message["contentLength"] = 0 + message["contentLength"] = None del message["content"] if "backup" in flow_state: