add HTTP/2-capable client

This commit is contained in:
Thomas Kriechbaumer 2015-05-28 17:46:30 +02:00
parent 780836b182
commit e2de49596d
2 changed files with 85 additions and 0 deletions

View File

@ -1,3 +1,5 @@
from .. import utils, odict, tcp
from frame import *
# "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
CLIENT_CONNECTION_PREFACE = '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'
@ -18,3 +20,66 @@ ERROR_CODES = utils.BiDi(
INADEQUATE_SECURITY=0xc,
HTTP_1_1_REQUIRED=0xd
)
class H2Client(tcp.TCPClient):
ALPN_PROTO_H2 = b'h2'
DEFAULT_SETTINGS = {
SettingsFrame.SETTINGS.SETTINGS_HEADER_TABLE_SIZE: 4096,
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1,
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: None,
SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 2 ^ 16 - 1,
SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE: 2 ^ 14,
SettingsFrame.SETTINGS.SETTINGS_MAX_HEADER_LIST_SIZE: None,
}
def __init__(self, address, source_address=None):
super(H2Client, self).__init__(address, source_address)
self.settings = self.DEFAULT_SETTINGS.copy()
def connect(self, send_preface=True):
super(H2Client, self).connect()
self.convert_to_ssl(alpn_protos=[self.ALPN_PROTO_H2])
alp = self.get_alpn_proto_negotiated()
if alp != b'h2':
raise NotImplementedError("H2Client can not handle unknown protocol: %s" % alp)
print "-> Successfully negotiated 'h2' application layer protocol."
if send_preface:
self.wfile.write(bytes(CLIENT_CONNECTION_PREFACE.decode('hex')))
self.send_frame(SettingsFrame())
frame = Frame.from_file(self.rfile)
print frame.human_readable()
assert isinstance(frame, SettingsFrame)
self.apply_settings(frame.settings)
print "-> Connection Preface completed."
print "-> H2Client is ready..."
def send_frame(self, frame):
self.wfile.write(frame.to_bytes())
self.wfile.flush()
def read_frame(self):
frame = Frame.from_file(self.rfile)
if isinstance(frame, SettingsFrame):
self.apply_settings(frame.settings)
return frame
def apply_settings(self, settings):
for setting, value in settings.items():
old_value = self.settings[setting]
if not old_value:
old_value = '-'
self.settings[setting] = value
print "-> Setting changed: %s to %d (was %s)" %
(SettingsFrame.SETTINGS.get_name(setting), value, str(old_value))
self.send_frame(SettingsFrame(flags=Frame.FLAG_ACK))
print "-> New settings acknowledged."

20
test/h2/example.py Normal file
View File

@ -0,0 +1,20 @@
from netlib import tcp
from netlib.h2.frame import *
from netlib.h2.h2 import *
from hpack.hpack import Encoder, Decoder
c = H2Client(("127.0.0.1", 443))
c.connect()
c.send_frame(HeadersFrame(
flags=(Frame.FLAG_END_HEADERS | Frame.FLAG_END_STREAM),
stream_id=0x1,
headers=[
(b':method', 'GET'),
(b':path', b'/index.html'),
(b':scheme', b'https'),
(b':authority', b'localhost'),
]))
while True:
print c.read_frame().human_readable()