diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-02-20 10:34:32 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-02-20 10:39:00 +1300 |
commit | 2616f490fee1b732aa0853318d67a550fc561cc4 (patch) | |
tree | b93ebc1f266682f422791fc63ad15ce3d5d0467d /libmproxy/proxy.py | |
parent | 25a06c3ec18a9ed49f34213024330aed59cb5322 (diff) | |
download | mitmproxy-2616f490fee1b732aa0853318d67a550fc561cc4.tar.gz mitmproxy-2616f490fee1b732aa0853318d67a550fc561cc4.tar.bz2 mitmproxy-2616f490fee1b732aa0853318d67a550fc561cc4.zip |
Rename Headers class to ODict
ODict is an ordered dictionary class that will be useful in many other parts of
our API.
Diffstat (limited to 'libmproxy/proxy.py')
-rw-r--r-- | libmproxy/proxy.py | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 43277a6c..ff5e3ec7 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -30,6 +30,31 @@ class ProxyConfig: self.reverse_proxy = reverse_proxy + +def read_headers(fp): + """ + Read a set of headers from a file pointer. Stop once a blank line + is reached. Return a ODict object. + """ + ret = [] + name = '' + while 1: + line = fp.readline() + if not line or line == '\r\n' or line == '\n': + break + if line[0] in ' \t': + # continued header + ret[-1][1] = ret[-1][1] + '\r\n ' + line.strip() + else: + i = line.find(':') + # We're being liberal in what we accept, here. + if i > 0: + name = line[:i] + value = line[i+1:].strip() + ret.append([name, value]) + return flow.ODict(ret) + + def read_chunked(fp, limit): content = "" total = 0 @@ -224,8 +249,7 @@ class ServerConnection: code = int(code) except ValueError: raise ProxyError(502, "Invalid server response: %s."%line) - headers = flow.Headers() - headers.read(self.rfile) + headers = read_headers(self.rfile) if code >= 100 and code <= 199: return self.read_response() if self.request.method == "HEAD" or code == 204 or code == 304: @@ -350,8 +374,7 @@ class ProxyHandler(SocketServer.StreamRequestHandler): method, scheme, host, port, path, httpminor = parse_request_line(self.rfile.readline()) if scheme is None: scheme = "https" - headers = flow.Headers() - headers.read(self.rfile) + headers = read_headers(self.rfile) if host is None and "host" in headers: netloc = headers["host"][0] if ':' in netloc: |