Refactoring of proxy.py

- Correctly pass HTTP request version on to upstream servers
- Adjust tests not to hang due to a pathod response with no content-length
This commit is contained in:
Aldo Cortesi 2012-06-10 13:17:18 +12:00
parent 55ddf853cd
commit 52779d9db9
4 changed files with 19 additions and 13 deletions

View File

@ -536,7 +536,6 @@ class Request(HTTPMsg):
'proxy-connection', 'proxy-connection',
'keep-alive', 'keep-alive',
'connection', 'connection',
'content-length',
'transfer-encoding' 'transfer-encoding'
] ]
) )
@ -551,15 +550,15 @@ class Request(HTTPMsg):
headers["connection"] = ["close"] headers["connection"] = ["close"]
if not _proxy: if not _proxy:
return FMT % ( return FMT % (
self.method, self.method,
self.path, self.path,
self.httpversion[0], self.httpversion[0],
self.httpversion[1], self.httpversion[1],
str(headers), str(headers),
content content
) )
else: else:
return FMT_PROXY % ( return FMT_PROXY % (
self.method, self.method,
self.scheme, self.scheme,
self.host, self.host,
@ -738,7 +737,7 @@ class Response(HTTPMsg):
headers = self.headers.copy() headers = self.headers.copy()
utils.del_all( utils.del_all(
headers, headers,
['proxy-connection', 'connection', 'keep-alive', 'transfer-encoding'] ['proxy-connection', 'transfer-encoding']
) )
content = self.content content = self.content
if content: if content:

View File

@ -123,7 +123,6 @@ def read_http_body(rfile, connection, headers, all, limit):
content = rfile.read(limit if limit else None) content = rfile.read(limit if limit else None)
connection.close = True connection.close = True
else: else:
connection.close = True
content = "" content = ""
return content return content

View File

@ -3,9 +3,17 @@ import time
import libpathod.test, requests import libpathod.test, requests
import tutils import tutils
"""
Note that the choice of response code in these tests matters more than you
might think. libcurl treats a 304 response code differently from, say, a
200 response code - it will correctly terminate a 304 response with no
content-length header, whereas it will block forever waiting for content
for a 200 response.
"""
class Sanity(tutils.ProxTest): class Sanity(tutils.ProxTest):
def test_http(self): def test_http(self):
assert self.pathod("205").status_code == 205 assert self.pathod("304").status_code == 304
assert self.log() assert self.log()
@ -23,10 +31,10 @@ class TestReverse(Sanity):
class TestProxy(tutils.ProxTest): class TestProxy(tutils.ProxTest):
def test_http(self): def test_http(self):
f = self.pathod("205") f = self.pathod("304")
assert f.status_code == 205 assert f.status_code == 304
l = self.log() l = self.log()
assert l[0].address assert l[0].address
assert "host" in l[1].headers assert "host" in l[1].headers
assert l[2].code == 205 assert l[2].code == 304

View File

@ -121,7 +121,7 @@ class ProxTest:
r = hurl.get( r = hurl.get(
"http://127.0.0.1:%s"%self.proxy.port + "/p/" + spec, "http://127.0.0.1:%s"%self.proxy.port + "/p/" + spec,
validate_cert=False, validate_cert=False,
#debug=hurl.utils.stdout_debug debug=hurl.utils.stdout_debug
) )
return r return r
else: else:
@ -129,7 +129,7 @@ class ProxTest:
self.urlbase + "/p/" + spec, self.urlbase + "/p/" + spec,
proxy=self.proxies, proxy=self.proxies,
validate_cert=False, validate_cert=False,
#debug=hurl.utils.stdout_debug debug=hurl.utils.stdout_debug
) )
@property @property