From f32258e8f7eb2f4b43a704a003f00c32efbb898c Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 26 Jun 2012 10:15:11 +1200 Subject: [PATCH] Port pathoc to new netlib API. Add SNI. --- libpathod/pathoc.py | 4 ++-- libpathod/pathod.py | 5 +++++ pathoc | 14 ++++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 5105decfc..7b49d0b19 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -15,9 +15,9 @@ def print_full(fp, httpversion, code, msg, headers, content): class Pathoc(tcp.TCPClient): - def __init__(self, ssl, host, port, clientcert): + def __init__(self, host, port): try: - tcp.TCPClient.__init__(self, ssl, host, port, clientcert) + tcp.TCPClient.__init__(self, host, port) except tcp.NetLibError, v: raise PathocError(v) diff --git a/libpathod/pathod.py b/libpathod/pathod.py index db7f37f19..2ef3b0bf2 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -7,6 +7,10 @@ class PathodError(Exception): pass class PathodHandler(tcp.BaseHandler): + sni = None + def handle_sni(self, connection): + self.sni = connection.get_servername() + def handle(self): if self.server.ssloptions: self.convert_to_ssl( @@ -50,6 +54,7 @@ class PathodHandler(tcp.BaseHandler): path = path, method = method, headers = headers.lst, + sni = self.sni, #remote_address = self.request.connection.address, #full_url = self.request.full_url(), #query = self.request.query, diff --git a/pathoc b/pathoc index e4fee2db6..9dc87c030 100755 --- a/pathoc +++ b/pathoc @@ -1,11 +1,12 @@ #!/usr/bin/env python import argparse, sys -from libpathod import pathoc, version +from libpathod import pathoc, version, rparse if __name__ == "__main__": parser = argparse.ArgumentParser(description='A perverse HTTP client.') parser.add_argument('--port', "-p", type=int, default=None, help="Port. Defaults to 80, or 443 if SSL is active.") parser.add_argument('--ssl', "-s", action="store_true", default=False, help="Connect with SSL.") + parser.add_argument('--sni', "-n", type=str, default=False, help="SSL Server Name Indication.") parser.add_argument('--verbose', '-v', action='count') parser.add_argument('host', type=str, help='Host to connect to') parser.add_argument('request', type=str, nargs="+", help='Request specification') @@ -17,16 +18,21 @@ if __name__ == "__main__": else: port = args.port - try: - p = pathoc.Pathoc(args.ssl, args.host, port, None) + p = pathoc.Pathoc(args.host, port) + p.connect() + if args.ssl: + p.convert_to_ssl(sni=args.sni) for i in args.request: ret = p.request(i) if args.verbose: pathoc.print_full(sys.stdout, *ret) else: pathoc.print_short(sys.stdout, *ret) - except pathoc.PathocError, v: print >> sys.stderr, str(v) sys.exit(1) + except rparse.ParseException, v: + print >> sys.stderr, "Error parsing request spec: %s"%v.msg + print >> sys.stderr, v.marked() + sys.exit(1)