aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/proxy.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-06-10 10:31:04 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-06-10 10:31:04 +1200
commita3b47e0cb59205fd2512e96ae9058e371c8a5760 (patch)
tree9319b8de5c614b19b833a0f12b3f7deb459025a3 /libmproxy/proxy.py
parent8254187bf316e4cdda32e1be0e07e73bc2446243 (diff)
downloadmitmproxy-a3b47e0cb59205fd2512e96ae9058e371c8a5760.tar.gz
mitmproxy-a3b47e0cb59205fd2512e96ae9058e371c8a5760.tar.bz2
mitmproxy-a3b47e0cb59205fd2512e96ae9058e371c8a5760.zip
Consolidate HTTP major and minor versions into a single variable.
Diffstat (limited to 'libmproxy/proxy.py')
-rw-r--r--libmproxy/proxy.py45
1 files changed, 24 insertions, 21 deletions
diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py
index 0c032bb0..4eeb17c9 100644
--- a/libmproxy/proxy.py
+++ b/libmproxy/proxy.py
@@ -149,10 +149,10 @@ def parse_init_connect(line):
except ValueError:
return None
port = int(port)
- mm = parse_http_protocol(protocol)
- if not mm:
+ httpversion = parse_http_protocol(protocol)
+ if not httpversion:
return None
- return host, port, mm[0], mm[1]
+ return host, port, httpversion
def parse_init_proxy(line):
@@ -164,26 +164,29 @@ def parse_init_proxy(line):
if not parts:
return None
scheme, host, port, path = parts
- mm = parse_http_protocol(protocol)
- if not mm:
+ httpversion = parse_http_protocol(protocol)
+ if not httpversion:
return None
- return method, scheme, host, port, path, mm[0], mm[1]
+ return method, scheme, host, port, path, httpversion
def parse_init_http(line):
+ """
+ Returns (method, url, httpversion)
+ """
try:
method, url, protocol = string.split(line)
except ValueError:
return None
if not (url.startswith("/") or url == "*"):
return None
- mm = parse_http_protocol(protocol)
- if not mm:
+ httpversion = parse_http_protocol(protocol)
+ if not httpversion:
return None
- return method, url, mm[0], mm[1]
+ return method, url, httpversion
-def should_connection_close(httpmajor, httpminor, headers):
+def should_connection_close(httpversion, headers):
"""
Checks the HTTP version and headers to see if this connection should be
closed.
@@ -196,7 +199,7 @@ def should_connection_close(httpmajor, httpminor, headers):
elif value == "keep-alive":
return False
# HTTP 1.1 connections are assumed to be persistent
- if httpmajor == 1 and httpminor == 1:
+ if httpversion == (1, 1):
return False
return True
@@ -430,16 +433,16 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
self.rfile = FileLike(self.connection)
self.wfile = FileLike(self.connection)
- def read_contents(self, client_conn, headers, httpminor):
+ def read_contents(self, client_conn, headers, httpversion):
if "expect" in headers:
# FIXME: Should be forwarded upstream
expect = ",".join(headers['expect'])
- if expect == "100-continue" and httpminor >= 1:
+ if expect == "100-continue" and httpversion >= (1, 1):
self.wfile.write('HTTP/1.1 100 Continue\r\n')
self.wfile.write('Proxy-agent: %s\r\n'%version.NAMEVERSION)
self.wfile.write('\r\n')
del headers['expect']
- if httpminor == 0:
+ if httpversion < (1, 1):
client_conn.close = True
if "connection" in headers:
for value in ",".join(headers['connection']).split(","):
@@ -459,12 +462,12 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
if self.config.reverse_proxy:
scheme, host, port = self.config.reverse_proxy
- method, path, httpmajor, httpminor = parse_init_http(line)
+ method, path, httpversion = parse_init_http(line)
headers = read_headers(self.rfile)
- content = self.read_contents(client_conn, headers, httpminor)
+ content = self.read_contents(client_conn, headers, httpversion)
return flow.Request(client_conn, host, port, "http", method, path, headers, content)
elif line.startswith("CONNECT"):
- host, port, httpmajor, httpminor = parse_init_connect(line)
+ host, port, httpversion = parse_init_connect(line)
# FIXME: Discard additional headers sent to the proxy. Should I expose
# these to users?
while 1:
@@ -480,14 +483,14 @@ class ProxyHandler(SocketServer.StreamRequestHandler):
certfile = self.find_cert(host, port)
self.convert_to_ssl(certfile)
- method, path, httpmajor, httpminor = parse_init_http(self.rfile.readline(line))
+ method, path, httpversion = parse_init_http(self.rfile.readline(line))
headers = read_headers(self.rfile)
- content = self.read_contents(client_conn, headers, httpminor)
+ content = self.read_contents(client_conn, headers, httpversion)
return flow.Request(client_conn, host, port, "https", method, path, headers, content)
else:
- method, scheme, host, port, path, httpmajor, httpminor = parse_init_proxy(line)
+ method, scheme, host, port, path, httpversion = parse_init_proxy(line)
headers = read_headers(self.rfile)
- content = self.read_contents(client_conn, headers, httpminor)
+ content = self.read_contents(client_conn, headers, httpversion)
return flow.Request(client_conn, host, port, scheme, method, path, headers, content)
def send_response(self, response):