aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Kriechbaumer <thomas@kriechbaumer.name>2015-07-08 09:20:25 +0200
committerThomas Kriechbaumer <thomas@kriechbaumer.name>2015-07-22 15:30:50 +0200
commit6dcfc35011208f4bfde7f37a63d7b980f6c41ce0 (patch)
tree6478a1b7bf2445419855f0f00415130e87dab7d9
parente316a9cdb44444667e26938f8c1c3969e56c2f0e (diff)
downloadmitmproxy-6dcfc35011208f4bfde7f37a63d7b980f6c41ce0.tar.gz
mitmproxy-6dcfc35011208f4bfde7f37a63d7b980f6c41ce0.tar.bz2
mitmproxy-6dcfc35011208f4bfde7f37a63d7b980f6c41ce0.zip
introduce http_semantics module
used for generic HTTP representation everything should apply for HTTP/1 and HTTP/2
-rw-r--r--netlib/http.py16
-rw-r--r--netlib/http_semantics.py23
-rw-r--r--test/test_http.py14
3 files changed, 32 insertions, 21 deletions
diff --git a/netlib/http.py b/netlib/http.py
index a2af9e49..073e9a3f 100644
--- a/netlib/http.py
+++ b/netlib/http.py
@@ -4,7 +4,7 @@ import string
import urlparse
import binascii
import sys
-from . import odict, utils, tcp, http_status
+from . import odict, utils, tcp, http_semantics, http_status
class HttpError(Exception):
@@ -527,18 +527,6 @@ def read_request(rfile, include_body=True, body_size_limit=None, wfile=None):
)
-Response = collections.namedtuple(
- "Response",
- [
- "httpversion",
- "code",
- "msg",
- "headers",
- "content"
- ]
-)
-
-
def read_response(rfile, request_method, body_size_limit, include_body=True):
"""
Return an (httpversion, code, msg, headers, content) tuple.
@@ -580,7 +568,7 @@ def read_response(rfile, request_method, body_size_limit, include_body=True):
# if include_body==False then a None content means the body should be
# read separately
content = None
- return Response(httpversion, code, msg, headers, content)
+ return http_semantics.Response(httpversion, code, msg, headers, content)
def request_preamble(method, resource, http_major="1", http_minor="1"):
diff --git a/netlib/http_semantics.py b/netlib/http_semantics.py
new file mode 100644
index 00000000..e8313e3c
--- /dev/null
+++ b/netlib/http_semantics.py
@@ -0,0 +1,23 @@
+class Response(object):
+
+ def __init__(
+ self,
+ httpversion,
+ status_code,
+ msg,
+ headers,
+ content,
+ sslinfo=None,
+ ):
+ self.httpversion = httpversion
+ self.status_code = status_code
+ self.msg = msg
+ self.headers = headers
+ self.content = content
+ self.sslinfo = sslinfo
+
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+
+ def __repr__(self):
+ return "Response(%s - %s)" % (self.status_code, self.msg)
diff --git a/test/test_http.py b/test/test_http.py
index 2ad81d24..bbc78847 100644
--- a/test/test_http.py
+++ b/test/test_http.py
@@ -1,7 +1,7 @@
import cStringIO
import textwrap
import binascii
-from netlib import http, odict, tcp
+from netlib import http, http_semantics, odict, tcp
from . import tutils, tservers
@@ -307,13 +307,13 @@ def test_read_response():
data = """
HTTP/1.1 200 OK
"""
- assert tst(data, "GET", None) == (
+ assert tst(data, "GET", None) == http_semantics.Response(
(1, 1), 200, 'OK', odict.ODictCaseless(), ''
)
data = """
HTTP/1.1 200
"""
- assert tst(data, "GET", None) == (
+ assert tst(data, "GET", None) == http_semantics.Response(
(1, 1), 200, '', odict.ODictCaseless(), ''
)
data = """
@@ -330,7 +330,7 @@ def test_read_response():
HTTP/1.1 200 OK
"""
- assert tst(data, "GET", None) == (
+ assert tst(data, "GET", None) == http_semantics.Response(
(1, 1), 100, 'CONTINUE', odict.ODictCaseless(), ''
)
@@ -340,8 +340,8 @@ def test_read_response():
foo
"""
- assert tst(data, "GET", None)[4] == 'foo'
- assert tst(data, "HEAD", None)[4] == ''
+ assert tst(data, "GET", None).content == 'foo'
+ assert tst(data, "HEAD", None).content == ''
data = """
HTTP/1.1 200 OK
@@ -357,7 +357,7 @@ def test_read_response():
foo
"""
- assert tst(data, "GET", None, include_body=False)[4] is None
+ assert tst(data, "GET", None, include_body=False).content is None
def test_parse_url():