aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/pathoc.py
diff options
context:
space:
mode:
Diffstat (limited to 'libpathod/pathoc.py')
-rw-r--r--libpathod/pathoc.py95
1 files changed, 75 insertions, 20 deletions
diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py
index 4efa0447..ba06b2f1 100644
--- a/libpathod/pathoc.py
+++ b/libpathod/pathoc.py
@@ -11,23 +11,32 @@ import threading
import OpenSSL.crypto
-from netlib import tcp, http, certutils, websockets
+from netlib import tcp, http, http2, certutils, websockets
import language.http
import language.websockets
from . import utils, log
+import logging
+logging.getLogger("hpack").setLevel(logging.WARNING)
+
class PathocError(Exception):
pass
class SSLInfo:
- def __init__(self, certchain, cipher):
- self.certchain, self.cipher = certchain, cipher
+ def __init__(self, certchain, cipher, alp):
+ self.certchain, self.cipher, self.alp = certchain, cipher, alp
def __str__(self):
+ if self.alp:
+ alp = self.alp
+ else:
+ alp = '<no protocol negotiated>'
+
parts = [
+ "Application Layer Protocol: %s" % alp,
"Cipher: %s, %s bit, %s" % self.cipher,
"SSL certificate chain:"
]
@@ -150,6 +159,10 @@ class Pathoc(tcp.TCPClient):
clientcert=None,
ciphers=None,
+ # HTTP/2
+ use_http2=False,
+ http2_skip_connection_preface=False,
+
# Websockets
ws_read_limit = None,
@@ -177,18 +190,16 @@ class Pathoc(tcp.TCPClient):
ignorecodes: Sequence of return codes to ignore
"""
tcp.TCPClient.__init__(self, address)
- self.settings = language.Settings(
- staticdir = os.getcwd(),
- unconstrained_file_access = True,
- request_host = self.address.host,
- is_client = True
- )
+
self.ssl, self.sni = ssl, sni
self.clientcert = clientcert
self.sslversion = utils.SSLVERSIONS[sslversion]
self.ciphers = ciphers
self.sslinfo = None
+ self.use_http2 = use_http2
+ self.http2_skip_connection_preface = http2_skip_connection_preface
+
self.ws_read_limit = ws_read_limit
self.timeout = timeout
@@ -204,6 +215,20 @@ class Pathoc(tcp.TCPClient):
self.ws_framereader = None
+ if self.use_http2:
+ self.protocol = http2.HTTP2Protocol(self)
+ else:
+ # TODO: create HTTP or Websockets protocol
+ self.protocol = None
+
+ self.settings = language.Settings(
+ is_client = True,
+ staticdir = os.getcwd(),
+ unconstrained_file_access = True,
+ request_host = self.address.host,
+ protocol = self.protocol,
+ )
+
def log(self):
return log.Log(
self.fp,
@@ -233,26 +258,44 @@ class Pathoc(tcp.TCPClient):
connect_to: A (host, port) tuple, which will be connected to with
an HTTP CONNECT request.
"""
+ if self.use_http2 and not self.ssl:
+ raise ValueError("HTTP2 without SSL is not supported.")
+
tcp.TCPClient.connect(self)
+
if connect_to:
self.http_connect(connect_to)
+
self.sslinfo = None
if self.ssl:
try:
+ alpn_protos = [b'http1.1'] # TODO: move to a new HTTP1 protocol
+ if self.use_http2:
+ alpn_protos.append(http2.HTTP2Protocol.ALPN_PROTO_H2)
+
self.convert_to_ssl(
sni=self.sni,
cert=self.clientcert,
method=self.sslversion,
- cipher_list = self.ciphers
+ cipher_list=self.ciphers,
+ alpn_protos=alpn_protos
)
except tcp.NetLibError as v:
raise PathocError(str(v))
+
self.sslinfo = SSLInfo(
self.connection.get_peer_cert_chain(),
- self.get_current_cipher()
+ self.get_current_cipher(),
+ self.get_alpn_proto_negotiated()
)
if showssl:
print >> fp, str(self.sslinfo)
+
+ if self.use_http2:
+ self.protocol.check_alpn()
+ if not self.http2_skip_connection_preface:
+ self.protocol.perform_connection_preface()
+
if self.timeout:
self.settimeout(self.timeout)
@@ -337,15 +380,20 @@ class Pathoc(tcp.TCPClient):
try:
req = language.serve(r, self.wfile, self.settings)
self.wfile.flush()
- resp = list(
- http.read_response(
- self.rfile,
- req["method"],
- None
+
+ if self.use_http2:
+ status_code, headers, body = self.protocol.read_response()
+ resp = Response("HTTP/2", status_code, "", headers, body, self.sslinfo)
+ else:
+ resp = list(
+ http.read_response(
+ self.rfile,
+ req["method"],
+ None
+ )
)
- )
- resp.append(self.sslinfo)
- resp = Response(*resp)
+ resp.append(self.sslinfo)
+ resp = Response(*resp)
except http.HttpError, v:
log("Invalid server response: %s" % v)
raise
@@ -374,7 +422,8 @@ class Pathoc(tcp.TCPClient):
May raise http.HTTPError, tcp.NetLibError
"""
if isinstance(r, basestring):
- r = language.parse_pathoc(r).next()
+ r = language.parse_pathoc(r, self.use_http2).next()
+
if isinstance(r, language.http.Request):
if r.ws:
return self.websocket_start(r)
@@ -382,6 +431,10 @@ class Pathoc(tcp.TCPClient):
return self.http(r)
elif isinstance(r, language.websockets.WebsocketFrame):
self.websocket_send_frame(r)
+ elif isinstance(r, language.http2.Request):
+ return self.http(r)
+ # elif isinstance(r, language.http2.Frame):
+ # TODO: do something
def main(args): # pragma: nocover
@@ -407,6 +460,8 @@ def main(args): # pragma: nocover
sslversion = args.sslversion,
clientcert = args.clientcert,
ciphers = args.ciphers,
+ use_http2 = args.use_http2,
+ http2_skip_connection_preface = args.http2_skip_connection_preface,
showreq = args.showreq,
showresp = args.showresp,
explain = args.explain,