From f61109db84a673caeaaabf9e3c7faedfd37b2b34 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 17 Sep 2015 02:13:42 +0200 Subject: [PATCH] use new netlib exceptions --- libpathod/language/writer.py | 3 ++- libpathod/pathoc.py | 19 ++++++++++--------- libpathod/pathod.py | 14 +++++++------- libpathod/protocols/http.py | 4 ++-- test/test_log.py | 3 ++- test/test_pathoc.py | 4 ++-- test/test_pathod.py | 4 ++-- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/libpathod/language/writer.py b/libpathod/language/writer.py index 5e2119773..1a27e1efd 100644 --- a/libpathod/language/writer.py +++ b/libpathod/language/writer.py @@ -1,4 +1,5 @@ import time +from netlib.exceptions import TcpDisconnect import netlib.tcp BLOCKSIZE = 1024 @@ -62,5 +63,5 @@ def write_values(fp, vals, actions, sofar=0, blocksize=BLOCKSIZE): return True elif a[1] == "inject": send_chunk(fp, a[2], blocksize, 0, len(a[2])) - except netlib.tcp.NetLibDisconnect: # pragma: no cover + except TcpDisconnect: # pragma: no cover return True diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index d616761e8..2dcb60824 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -13,7 +13,8 @@ import OpenSSL.crypto import six from netlib import tcp, http, certutils, websockets, socks -from netlib.exceptions import HttpException +from netlib.exceptions import HttpException, TcpDisconnect, TcpTimeout, TlsException, TcpException, \ + NetlibException from netlib.http import http1, http2 import language.http @@ -123,7 +124,7 @@ class WebsocketFrameReader(threading.Thread): with self.logger.ctx() as log: try: frm = websockets.Frame.from_file(self.rfile) - except tcp.NetLibDisconnect: + except TcpDisconnect: return self.frames_queue.put(frm) log("<< %s" % frm.header.human_readable()) @@ -270,7 +271,7 @@ class Pathoc(tcp.TCPClient): connect_reply.msg, "SOCKS server error" ) - except (socks.SocksError, tcp.NetLibDisconnect) as e: + except (socks.SocksError, TcpDisconnect) as e: raise PathocError(str(e)) def connect(self, connect_to=None, showssl=False, fp=sys.stdout): @@ -301,7 +302,7 @@ class Pathoc(tcp.TCPClient): cipher_list=self.ciphers, alpn_protos=alpn_protos ) - except tcp.NetLibError as v: + except TlsException as v: raise PathocError(str(v)) self.sslinfo = SSLInfo( @@ -394,7 +395,7 @@ class Pathoc(tcp.TCPClient): Returns Response if we have a non-ignored response. - May raise http.HTTPError, tcp.NetLibError + May raise a NetlibException """ logger = log.ConnectionLogger( self.fp, @@ -414,7 +415,7 @@ class Pathoc(tcp.TCPClient): except HttpException as v: lg("Invalid server response: %s" % v) raise - except tcp.NetLibTimeout: + except TcpTimeout: if self.ignoretimeout: lg("Timeout (ignored)") return None @@ -438,7 +439,7 @@ class Pathoc(tcp.TCPClient): Returns Response if we have a non-ignored response. - May raise http.HTTPError, tcp.NetLibError + May raise a NetlibException """ if isinstance(r, basestring): r = language.parse_pathoc(r, self.use_http2).next() @@ -495,7 +496,7 @@ def main(args): # pragma: nocover trycount = 0 try: p.connect(args.connect_to, args.showssl) - except tcp.NetLibError as v: + except TcpException as v: print >> sys.stderr, str(v) continue except PathocError as v: @@ -523,7 +524,7 @@ def main(args): # pragma: nocover # We consume the queue when we can, so it doesn't build up. for i_ in p.wait(timeout=0, finish=False): pass - except (http.HttpError, tcp.NetLibError) as v: + except NetlibException: break for i_ in p.wait(timeout=0.01, finish=True): pass diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 6478fe4f3..7436b9b15 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -6,8 +6,8 @@ import threading import urllib from netlib import tcp, http, certutils, websockets -from netlib.exceptions import HttpException, HttpReadDisconnect -from netlib.http import ALPN_PROTO_HTTP1, ALPN_PROTO_H2 +from netlib.exceptions import HttpException, HttpReadDisconnect, TcpTimeout, TcpDisconnect, \ + TlsException from . import version, app, language, utils, log, protocols import language.http @@ -41,7 +41,7 @@ class SSLOptions(object): ssl_options=tcp.SSL_DEFAULT_OPTIONS, ciphers=None, certs=None, - alpn_select=ALPN_PROTO_H2, + alpn_select=http.ALPN_PROTO_H2, ): self.confdir = confdir self.cn = cn @@ -247,7 +247,7 @@ class PathodHandler(tcp.BaseHandler): options=self.server.ssloptions.ssl_options, alpn_select=self.server.ssloptions.alpn_select, ) - except tcp.NetLibError as v: + except TlsException as v: s = str(v) self.server.add_log( dict( @@ -259,7 +259,7 @@ class PathodHandler(tcp.BaseHandler): return alp = self.get_alpn_proto_negotiated() - if alp == ALPN_PROTO_H2: + if alp == http.ALPN_PROTO_H2: self.protocol = protocols.http2.HTTP2Protocol(self) self.use_http2 = True @@ -387,7 +387,7 @@ class Pathod(tcp.TCPServer): try: h.handle() h.finish() - except tcp.NetLibDisconnect: # pragma: no cover + except TcpDisconnect: # pragma: no cover log.write_raw(self.logfp, "Disconnect") self.add_log( dict( @@ -396,7 +396,7 @@ class Pathod(tcp.TCPServer): ) ) return - except tcp.NetLibTimeout: + except TcpTimeout: log.write_raw(self.logfp, "Timeout") self.add_log( dict( diff --git a/libpathod/protocols/http.py b/libpathod/protocols/http.py index 531854d6a..0ed6c01eb 100644 --- a/libpathod/protocols/http.py +++ b/libpathod/protocols/http.py @@ -1,5 +1,5 @@ from netlib import tcp, wsgi -from netlib.exceptions import HttpReadDisconnect +from netlib.exceptions import HttpReadDisconnect, TlsException from netlib.http import http1, Request from .. import version, language @@ -61,7 +61,7 @@ class HTTPProtocol(object): options=self.pathod_handler.server.ssloptions.ssl_options, alpn_select=self.pathod_handler.server.ssloptions.alpn_select, ) - except tcp.NetLibError as v: + except TlsException as v: s = str(v) lg(s) return None, dict(type="error", msg=s) diff --git a/test/test_log.py b/test/test_log.py index 2cac1e7c4..8f38c0400 100644 --- a/test/test_log.py +++ b/test/test_log.py @@ -1,5 +1,6 @@ import StringIO from libpathod import log +from netlib.exceptions import TcpDisconnect import netlib.tcp @@ -19,6 +20,6 @@ def test_disconnect(): try: with l.ctx() as lg: lg("Test") - except netlib.tcp.NetLibDisconnect: + except TcpDisconnect: pass assert "Test" in outf.getvalue() diff --git a/test/test_pathoc.py b/test/test_pathoc.py index 1e15c9eb1..6c0bf0395 100644 --- a/test/test_pathoc.py +++ b/test/test_pathoc.py @@ -5,7 +5,7 @@ import OpenSSL from mock import Mock from netlib import tcp, http, socks -from netlib.exceptions import HttpException +from netlib.exceptions import HttpException, TcpException, NetlibException from netlib.http import http1, http2 from libpathod import pathoc, test, version, pathod, language @@ -84,7 +84,7 @@ class _TestDaemon: r = r.freeze(language.Settings()) try: c.request(r) - except (HttpException, tcp.NetLibError): + except NetlibException: pass return s.getvalue() diff --git a/test/test_pathod.py b/test/test_pathod.py index ed37385d1..1da2633f3 100644 --- a/test/test_pathod.py +++ b/test/test_pathod.py @@ -4,7 +4,7 @@ import OpenSSL from libpathod import pathod, version from netlib import tcp, http -from netlib.exceptions import HttpException +from netlib.exceptions import HttpException, TlsException import tutils @@ -264,7 +264,7 @@ class TestDaemonSSL(CommonTests): c.wbufsize = 0 c.connect() c.wfile.write("\0\0\0\0") - tutils.raises(tcp.NetLibError, c.convert_to_ssl) + tutils.raises(TlsException, c.convert_to_ssl) l = self.d.last_log() assert l["type"] == "error" assert "SSL" in l["msg"]