diff options
-rwxr-xr-x | libmproxy/proxy.py | 12 | ||||
-rw-r--r-- | test/test_proxy.py | 32 | ||||
-rw-r--r-- | test/test_server.py | 23 | ||||
-rw-r--r-- | test/tutils.py | 5 |
4 files changed, 61 insertions, 11 deletions
diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index cf006f60..db29f65a 100755 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -48,9 +48,9 @@ class ProxyConfig: self.reverse_proxy = reverse_proxy self.transparent_proxy = transparent_proxy self.authenticator = authenticator - self.certstore = certutils.CertStore(certdir) + class RequestReplayThread(threading.Thread): def __init__(self, config, flow, masterq): self.config, self.flow, self.masterq = config, flow, masterq @@ -86,7 +86,7 @@ class ServerConnection(tcp.TCPClient): clientcert = None if self.config.clientcerts: path = os.path.join(self.config.clientcerts, self.host) + ".pem" - if os.path.exists(clientcert): + if os.path.exists(path): clientcert = path try: self.convert_to_ssl(clientcert=clientcert, sni=self.host) @@ -95,10 +95,10 @@ class ServerConnection(tcp.TCPClient): def send(self, request): self.requestcount += 1 + d = request._assemble() + if not d: + raise ProxyError(502, "Cannot transmit an incomplete request.") try: - d = request._assemble() - if not d: - raise ProxyError(502, "Incomplete request could not not be readied for transmission.") self.wfile.write(d) self.wfile.flush() except socket.error, err: @@ -376,7 +376,7 @@ class ProxyHandler(tcp.BaseHandler): def send_response(self, response): d = response._assemble() if not d: - raise ProxyError(502, "Incomplete response could not not be readied for transmission.") + raise ProxyError(502, "Cannot transmit an incomplete response.") self.wfile.write(d) self.wfile.flush() diff --git a/test/test_proxy.py b/test/test_proxy.py index e137c687..89e5a825 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -1,5 +1,7 @@ -from libmproxy import proxy +from libmproxy import proxy, flow import tutils +from libpathod import test +from netlib import http def test_proxy_error(): @@ -26,3 +28,31 @@ def test_app_registry(): assert not ar.get(r) r.headers["host"] = ["domain"] assert ar.get(r) + + + +class TestServerConnection: + def setUp(self): + self.d = test.Daemon() + + def tearDown(self): + self.d.shutdown() + + def test_simple(self): + sc = proxy.ServerConnection(proxy.ProxyConfig(), self.d.IFACE, self.d.port) + sc.connect("http") + r = tutils.treq() + r.path = "/p/200:da" + sc.send(r) + assert http.read_response(sc.rfile, r.method, 1000) + assert self.d.last_log() + + r.content = flow.CONTENT_MISSING + tutils.raises("incomplete request", sc.send, r) + + def test_send_error(self): + sc = proxy.ServerConnection(proxy.ProxyConfig(), self.d.IFACE, self.d.port) + sc.connect("http") + r = tutils.treq() + sc.send(r) + diff --git a/test/test_server.py b/test/test_server.py index ee6873c7..3686a6a8 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -1,6 +1,7 @@ +import socket, time from netlib import tcp -from time import sleep -import tutils, socket +from libpathod import pathoc +import tutils """ Note that the choice of response code in these tests matters more than you @@ -53,6 +54,20 @@ class TestHTTP(tutils.HTTPProxTest, SanityMixin): t.wfile.flush() assert "Bad Request" in t.rfile.readline() + def test_upstream_ssl_error(self): + p = self.pathoc() + ret = p.request("get:'https://localhost:%s/'"%self.server.port) + assert ret[1] == 400 + + def test_http(self): + f = self.pathod("304") + assert f.status_code == 304 + + l = self.master.state.view[0] + assert l.request.client_conn.address + assert "host" in l.request.headers + assert l.response.code == 304 + class TestHTTPS(tutils.HTTPProxTest, SanityMixin): ssl = True @@ -93,7 +108,7 @@ class TestProxy(tutils.HTTPProxTest): # call pathod server, wait a second to complete the request connection.send("GET http://localhost:%d/p/304:b@1k HTTP/1.1\r\n"%self.server.port) - sleep(2.1) + time.sleep(2.1) connection.send("\r\n"); connection.recv(50000) connection.close() @@ -107,7 +122,7 @@ class TestProxy(tutils.HTTPProxTest): f = self.pathod("304:b@10k") assert f.status_code == 304 - sleep(1) + time.sleep(1) f = self.pathod("304:b@10k") assert f.status_code == 304 diff --git a/test/tutils.py b/test/tutils.py index 5ce56cb4..9b5ac0f1 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -127,6 +127,11 @@ class HTTPProxTest(ProxTestBase): def get_proxy_config(cls): return dict() + def pathoc(self, connect_to = None): + p = libpathod.pathoc.Pathoc("localhost", self.proxy.port) + p.connect(connect_to) + return p + def pathod(self, spec): """ Constructs a pathod request, with the appropriate base and proxy. |