Merge pull request #11 from mitmproxy/refactor_read_http_body

always return Content-Length: 0 if r is not set
This commit is contained in:
Aldo Cortesi 2014-01-04 14:48:12 -08:00
commit a9f8dcf755
4 changed files with 17 additions and 16 deletions

View File

@ -765,11 +765,12 @@ class _Message(object):
def resolve(self, settings, request_host):
tokens = self.tokens[:]
if not self.raw:
if self.body and not utils.get_header("Content-Length", self.headers):
if not utils.get_header("Content-Length", self.headers):
length = 0 if not self.body else len(self.body.value.get_generator(settings))
tokens.append(
Header(
ValueLiteral("Content-Length"),
ValueLiteral(str(len(self.body.value.get_generator(settings)))),
ValueLiteral(str(length)),
)
)
if request_host:

View File

@ -33,19 +33,19 @@ class Pathoc(tcp.TCPClient):
self.clientcert = clientcert
self.sslversion = SSLVERSIONS[sslversion]
def http_connect(self, connect_to, wfile, rfile):
wfile.write(
def http_connect(self, connect_to):
self.wfile.write(
'CONNECT %s:%s HTTP/1.1\r\n'%tuple(connect_to) +
'\r\n'
)
wfile.flush()
l = rfile.readline()
self.wfile.flush()
l = self.rfile.readline()
if not l:
raise PathocError("Proxy CONNECT failed")
parsed = http.parse_response_line(l)
if not parsed[1] == 200:
raise PathocError("Proxy CONNECT failed: %s - %s"%(parsed[1], parsed[2]))
headers = http.read_headers(rfile)
headers = http.read_headers(self.rfile)
def connect(self, connect_to=None):
"""
@ -54,7 +54,7 @@ class Pathoc(tcp.TCPClient):
"""
tcp.TCPClient.connect(self)
if connect_to:
self.http_connect(connect_to, self.wfile, self.rfile)
self.http_connect(connect_to)
if self.ssl:
try:
self.convert_to_ssl(sni=self.sni, cert=self.clientcert, method=self.sslversion)

View File

@ -123,8 +123,8 @@ class PathodHandler(tcp.BaseHandler):
)
try:
content = http.read_http_body_request(
self.rfile, self.wfile, headers, httpversion, None
content = http.read_http_body(
self.rfile, headers, None, True
)
except http.HttpError, s:
s = str(s)

View File

@ -136,16 +136,16 @@ class TestDaemon(_TestDaemon):
def test_connect_fail(self):
to = ("foobar", 80)
c = pathoc.Pathoc("127.0.0.1", self.d.port)
r, w = cStringIO.StringIO(), cStringIO.StringIO()
tutils.raises("connect failed", c.http_connect, to, w, r)
r = cStringIO.StringIO(
c.rfile, c.wfile = cStringIO.StringIO(), cStringIO.StringIO()
tutils.raises("connect failed", c.http_connect, to)
c.rfile = cStringIO.StringIO(
"HTTP/1.1 500 OK\r\n"
)
tutils.raises("connect failed", c.http_connect, to, w, r)
r = cStringIO.StringIO(
tutils.raises("connect failed", c.http_connect, to)
c.rfile = cStringIO.StringIO(
"HTTP/1.1 200 OK\r\n"
)
c.http_connect(to, w, r)
c.http_connect(to)