2015-06-08 14:54:19 +00:00
|
|
|
import sys
|
2015-05-25 10:10:21 +00:00
|
|
|
import struct
|
2015-05-27 15:53:06 +00:00
|
|
|
from hpack.hpack import Encoder, Decoder
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-05-27 09:25:33 +00:00
|
|
|
from .. import utils
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-06-04 17:44:48 +00:00
|
|
|
|
2015-06-04 12:28:09 +00:00
|
|
|
class FrameSizeError(Exception):
|
|
|
|
pass
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-06-04 17:44:48 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class Frame(object):
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
"""
|
|
|
|
Baseclass Frame
|
|
|
|
contains header
|
|
|
|
payload is defined in subclasses
|
|
|
|
"""
|
|
|
|
|
|
|
|
FLAG_NO_FLAGS = 0x0
|
|
|
|
FLAG_ACK = 0x1
|
|
|
|
FLAG_END_STREAM = 0x1
|
|
|
|
FLAG_END_HEADERS = 0x4
|
|
|
|
FLAG_PADDED = 0x8
|
|
|
|
FLAG_PRIORITY = 0x20
|
|
|
|
|
2015-06-01 10:34:50 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
state=None,
|
|
|
|
length=0,
|
|
|
|
flags=FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0):
|
2015-05-25 10:10:21 +00:00
|
|
|
valid_flags = reduce(lambda x, y: x | y, self.VALID_FLAGS, 0x0)
|
|
|
|
if flags | valid_flags != valid_flags:
|
|
|
|
raise ValueError('invalid flags detected.')
|
|
|
|
|
2015-06-01 13:17:50 +00:00
|
|
|
if state is None:
|
|
|
|
class State(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
state = State()
|
2015-06-06 00:26:48 +00:00
|
|
|
state.http2_settings = HTTP2_DEFAULT_SETTINGS.copy()
|
2015-06-01 13:17:50 +00:00
|
|
|
state.encoder = Encoder()
|
|
|
|
state.decoder = Decoder()
|
|
|
|
|
|
|
|
self.state = state
|
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
self.length = length
|
2015-05-27 10:58:55 +00:00
|
|
|
self.type = self.TYPE
|
2015-05-25 10:10:21 +00:00
|
|
|
self.flags = flags
|
|
|
|
self.stream_id = stream_id
|
|
|
|
|
2015-06-04 12:11:19 +00:00
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def _check_frame_size(cls, length, state):
|
2015-06-04 12:11:19 +00:00
|
|
|
if state:
|
|
|
|
settings = state.http2_settings
|
|
|
|
else:
|
2015-06-06 00:26:48 +00:00
|
|
|
settings = HTTP2_DEFAULT_SETTINGS.copy()
|
2015-06-04 12:11:19 +00:00
|
|
|
|
2015-06-04 12:28:09 +00:00
|
|
|
max_frame_size = settings[
|
|
|
|
SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE]
|
2015-06-04 12:11:19 +00:00
|
|
|
|
|
|
|
if length > max_frame_size:
|
2015-06-04 12:28:09 +00:00
|
|
|
raise FrameSizeError(
|
2015-06-01 10:34:50 +00:00
|
|
|
"Frame size exceeded: %d, but only %d allowed." % (
|
2015-06-04 12:11:19 +00:00
|
|
|
length, max_frame_size))
|
2015-06-01 10:34:50 +00:00
|
|
|
|
2015-05-27 15:53:45 +00:00
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_file(cls, fp, state=None):
|
2015-05-27 15:53:45 +00:00
|
|
|
"""
|
|
|
|
read a HTTP/2 frame sent by a server or client
|
|
|
|
fp is a "file like" object that could be backed by a network
|
|
|
|
stream or a disk or an in memory stream reader
|
|
|
|
"""
|
|
|
|
raw_header = fp.safe_read(9)
|
|
|
|
|
|
|
|
fields = struct.unpack("!HBBBL", raw_header)
|
|
|
|
length = (fields[0] << 8) + fields[1]
|
|
|
|
flags = fields[3]
|
|
|
|
stream_id = fields[4]
|
|
|
|
|
2015-06-08 14:54:19 +00:00
|
|
|
if raw_header[:4] == b'HTTP': # pragma no cover
|
|
|
|
print >> sys.stderr, "WARNING: This looks like an HTTP/1 connection!"
|
|
|
|
|
2015-06-17 11:10:27 +00:00
|
|
|
cls._check_frame_size(length, state)
|
2015-06-01 10:34:50 +00:00
|
|
|
|
2015-05-27 15:53:45 +00:00
|
|
|
payload = fp.safe_read(length)
|
2015-06-01 10:34:50 +00:00
|
|
|
return FRAMES[fields[2]].from_bytes(
|
|
|
|
state,
|
|
|
|
length,
|
|
|
|
flags,
|
|
|
|
stream_id,
|
|
|
|
payload)
|
2015-05-27 15:53:45 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
def to_bytes(self):
|
|
|
|
payload = self.payload_bytes()
|
|
|
|
self.length = len(payload)
|
|
|
|
|
2015-06-04 12:11:19 +00:00
|
|
|
self._check_frame_size(self.length, self.state)
|
2015-06-01 10:34:50 +00:00
|
|
|
|
2015-08-20 08:21:22 +00:00
|
|
|
b = struct.pack('!HB', (self.length & 0xFFFF00) >> 8, self.length & 0x0000FF)
|
2015-05-25 10:10:21 +00:00
|
|
|
b += struct.pack('!B', self.TYPE)
|
|
|
|
b += struct.pack('!B', self.flags)
|
|
|
|
b += struct.pack('!L', self.stream_id & 0x7FFFFFFF)
|
|
|
|
b += payload
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_bytes(self): # pragma: no cover
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def payload_human_readable(self): # pragma: no cover
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2015-06-15 11:15:06 +00:00
|
|
|
def human_readable(self, direction="-"):
|
2015-06-15 13:31:58 +00:00
|
|
|
self.length = len(self.payload_bytes())
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
return "\n".join([
|
2015-06-18 13:32:52 +00:00
|
|
|
"%s: %s | length: %d | flags: %#x | stream_id: %d" % (
|
2015-08-10 18:44:36 +00:00
|
|
|
direction, self.__class__.__name__, self.length, self.flags, self.stream_id),
|
2015-05-27 10:58:55 +00:00
|
|
|
self.payload_human_readable(),
|
2015-06-15 11:15:06 +00:00
|
|
|
"===============================================================",
|
2015-05-27 10:58:55 +00:00
|
|
|
])
|
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self.to_bytes() == other.to_bytes()
|
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class DataFrame(Frame):
|
|
|
|
TYPE = 0x0
|
|
|
|
VALID_FLAGS = [Frame.FLAG_END_STREAM, Frame.FLAG_PADDED]
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
payload=b'',
|
|
|
|
pad_length=0):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(DataFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.payload = payload
|
|
|
|
self.pad_length = pad_length
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-06-17 11:10:27 +00:00
|
|
|
if f.flags & Frame.FLAG_PADDED:
|
2015-05-25 10:10:21 +00:00
|
|
|
f.pad_length = struct.unpack('!B', payload[0])[0]
|
|
|
|
f.payload = payload[1:-f.pad_length]
|
|
|
|
else:
|
|
|
|
f.payload = payload
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id == 0x0:
|
|
|
|
raise ValueError('DATA frames MUST be associated with a stream.')
|
|
|
|
|
|
|
|
b = b''
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
b += struct.pack('!B', self.pad_length)
|
|
|
|
|
|
|
|
b += bytes(self.payload)
|
|
|
|
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
b += b'\0' * self.pad_length
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
return "payload: %s" % str(self.payload)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class HeadersFrame(Frame):
|
|
|
|
TYPE = 0x1
|
2015-05-30 00:02:58 +00:00
|
|
|
VALID_FLAGS = [
|
|
|
|
Frame.FLAG_END_STREAM,
|
|
|
|
Frame.FLAG_END_HEADERS,
|
|
|
|
Frame.FLAG_PADDED,
|
|
|
|
Frame.FLAG_PRIORITY]
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
2015-06-04 12:11:19 +00:00
|
|
|
header_block_fragment=b'',
|
2015-05-30 00:02:58 +00:00
|
|
|
pad_length=0,
|
|
|
|
exclusive=False,
|
|
|
|
stream_dependency=0x0,
|
|
|
|
weight=0):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(HeadersFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-06-04 12:11:19 +00:00
|
|
|
self.header_block_fragment = header_block_fragment
|
2015-05-25 10:10:21 +00:00
|
|
|
self.pad_length = pad_length
|
|
|
|
self.exclusive = exclusive
|
|
|
|
self.stream_dependency = stream_dependency
|
|
|
|
self.weight = weight
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-06-17 11:10:27 +00:00
|
|
|
if f.flags & Frame.FLAG_PADDED:
|
2015-05-25 10:10:21 +00:00
|
|
|
f.pad_length = struct.unpack('!B', payload[0])[0]
|
2015-06-01 10:34:50 +00:00
|
|
|
f.header_block_fragment = payload[1:-f.pad_length]
|
2015-05-25 10:10:21 +00:00
|
|
|
else:
|
2015-06-01 10:34:50 +00:00
|
|
|
f.header_block_fragment = payload[0:]
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-06-17 11:10:27 +00:00
|
|
|
if f.flags & Frame.FLAG_PRIORITY:
|
2015-05-30 00:02:58 +00:00
|
|
|
f.stream_dependency, f.weight = struct.unpack(
|
2015-06-04 12:11:19 +00:00
|
|
|
'!LB', f.header_block_fragment[:5])
|
2015-05-25 10:10:21 +00:00
|
|
|
f.exclusive = bool(f.stream_dependency >> 31)
|
|
|
|
f.stream_dependency &= 0x7FFFFFFF
|
2015-06-01 10:34:50 +00:00
|
|
|
f.header_block_fragment = f.header_block_fragment[5:]
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id == 0x0:
|
|
|
|
raise ValueError('HEADERS frames MUST be associated with a stream.')
|
|
|
|
|
|
|
|
b = b''
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
b += struct.pack('!B', self.pad_length)
|
|
|
|
|
|
|
|
if self.flags & self.FLAG_PRIORITY:
|
2015-05-30 00:02:58 +00:00
|
|
|
b += struct.pack('!LB',
|
|
|
|
(int(self.exclusive) << 31) | self.stream_dependency,
|
|
|
|
self.weight)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-06-04 12:11:19 +00:00
|
|
|
b += self.header_block_fragment
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
b += b'\0' * self.pad_length
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
s = []
|
|
|
|
|
|
|
|
if self.flags & self.FLAG_PRIORITY:
|
|
|
|
s.append("exclusive: %d" % self.exclusive)
|
|
|
|
s.append("stream dependency: %#x" % self.stream_dependency)
|
|
|
|
s.append("weight: %d" % self.weight)
|
|
|
|
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
s.append("padding: %d" % self.pad_length)
|
|
|
|
|
2015-06-04 12:28:09 +00:00
|
|
|
s.append(
|
|
|
|
"header_block_fragment: %s" %
|
|
|
|
self.header_block_fragment.encode('hex'))
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
return "\n".join(s)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class PriorityFrame(Frame):
|
|
|
|
TYPE = 0x2
|
|
|
|
VALID_FLAGS = []
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
exclusive=False,
|
|
|
|
stream_dependency=0x0,
|
|
|
|
weight=0):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(PriorityFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.exclusive = exclusive
|
|
|
|
self.stream_dependency = stream_dependency
|
|
|
|
self.weight = weight
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
f.stream_dependency, f.weight = struct.unpack('!LB', payload)
|
|
|
|
f.exclusive = bool(f.stream_dependency >> 31)
|
|
|
|
f.stream_dependency &= 0x7FFFFFFF
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id == 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'PRIORITY frames MUST be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
if self.stream_dependency == 0x0:
|
|
|
|
raise ValueError('stream dependency is invalid.')
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
return struct.pack(
|
|
|
|
'!LB',
|
|
|
|
(int(
|
|
|
|
self.exclusive) << 31) | self.stream_dependency,
|
|
|
|
self.weight)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
s = []
|
|
|
|
s.append("exclusive: %d" % self.exclusive)
|
|
|
|
s.append("stream dependency: %#x" % self.stream_dependency)
|
|
|
|
s.append("weight: %d" % self.weight)
|
|
|
|
return "\n".join(s)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class RstStreamFrame(Frame):
|
|
|
|
TYPE = 0x3
|
|
|
|
VALID_FLAGS = []
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
error_code=0x0):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(RstStreamFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.error_code = error_code
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
f.error_code = struct.unpack('!L', payload)[0]
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id == 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'RST_STREAM frames MUST be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
return struct.pack('!L', self.error_code)
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
return "error code: %#x" % self.error_code
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class SettingsFrame(Frame):
|
|
|
|
TYPE = 0x4
|
|
|
|
VALID_FLAGS = [Frame.FLAG_ACK]
|
|
|
|
|
|
|
|
SETTINGS = utils.BiDi(
|
2015-05-27 09:18:54 +00:00
|
|
|
SETTINGS_HEADER_TABLE_SIZE=0x1,
|
|
|
|
SETTINGS_ENABLE_PUSH=0x2,
|
|
|
|
SETTINGS_MAX_CONCURRENT_STREAMS=0x3,
|
|
|
|
SETTINGS_INITIAL_WINDOW_SIZE=0x4,
|
|
|
|
SETTINGS_MAX_FRAME_SIZE=0x5,
|
|
|
|
SETTINGS_MAX_HEADER_LIST_SIZE=0x6,
|
|
|
|
)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
settings=None):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(SettingsFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-27 15:31:18 +00:00
|
|
|
|
|
|
|
if settings is None:
|
|
|
|
settings = {}
|
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
self.settings = settings
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
for i in xrange(0, len(payload), 6):
|
2015-05-27 09:18:54 +00:00
|
|
|
identifier, value = struct.unpack("!HL", payload[i:i + 6])
|
2015-05-25 10:10:21 +00:00
|
|
|
f.settings[identifier] = value
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id != 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'SETTINGS frames MUST NOT be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
b = b''
|
|
|
|
for identifier, value in self.settings.items():
|
|
|
|
b += struct.pack("!HL", identifier & 0xFF, value)
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
s = []
|
|
|
|
|
|
|
|
for identifier, value in self.settings.items():
|
|
|
|
s.append("%s: %#x" % (self.SETTINGS.get_name(identifier), value))
|
|
|
|
|
|
|
|
if not s:
|
|
|
|
return "settings: None"
|
|
|
|
else:
|
|
|
|
return "\n".join(s)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class PushPromiseFrame(Frame):
|
|
|
|
TYPE = 0x5
|
|
|
|
VALID_FLAGS = [Frame.FLAG_END_HEADERS, Frame.FLAG_PADDED]
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
promised_stream=0x0,
|
|
|
|
header_block_fragment=b'',
|
|
|
|
pad_length=0):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(PushPromiseFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.pad_length = pad_length
|
|
|
|
self.promised_stream = promised_stream
|
|
|
|
self.header_block_fragment = header_block_fragment
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
2015-06-17 11:10:27 +00:00
|
|
|
if f.flags & Frame.FLAG_PADDED:
|
2015-05-25 10:10:21 +00:00
|
|
|
f.pad_length, f.promised_stream = struct.unpack('!BL', payload[:5])
|
|
|
|
f.header_block_fragment = payload[5:-f.pad_length]
|
|
|
|
else:
|
|
|
|
f.promised_stream = int(struct.unpack("!L", payload[:4])[0])
|
|
|
|
f.header_block_fragment = payload[4:]
|
|
|
|
|
|
|
|
f.promised_stream &= 0x7FFFFFFF
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id == 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'PUSH_PROMISE frames MUST be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
if self.promised_stream == 0x0:
|
|
|
|
raise ValueError('Promised stream id not valid.')
|
|
|
|
|
|
|
|
b = b''
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
b += struct.pack('!B', self.pad_length)
|
|
|
|
|
|
|
|
b += struct.pack('!L', self.promised_stream & 0x7FFFFFFF)
|
|
|
|
b += bytes(self.header_block_fragment)
|
|
|
|
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
b += b'\0' * self.pad_length
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
s = []
|
|
|
|
|
|
|
|
if self.flags & self.FLAG_PADDED:
|
|
|
|
s.append("padding: %d" % self.pad_length)
|
|
|
|
|
|
|
|
s.append("promised stream: %#x" % self.promised_stream)
|
2015-06-15 13:31:58 +00:00
|
|
|
s.append(
|
|
|
|
"header_block_fragment: %s" %
|
|
|
|
self.header_block_fragment.encode('hex'))
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
return "\n".join(s)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class PingFrame(Frame):
|
|
|
|
TYPE = 0x6
|
|
|
|
VALID_FLAGS = [Frame.FLAG_ACK]
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
payload=b''):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(PingFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.payload = payload
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
f.payload = payload
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id != 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'PING frames MUST NOT be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
b = self.payload[0:8]
|
|
|
|
b += b'\0' * (8 - len(b))
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
return "opaque data: %s" % str(self.payload)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class GoAwayFrame(Frame):
|
|
|
|
TYPE = 0x7
|
|
|
|
VALID_FLAGS = []
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
last_stream=0x0,
|
|
|
|
error_code=0x0,
|
|
|
|
data=b''):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(GoAwayFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.last_stream = last_stream
|
|
|
|
self.error_code = error_code
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
f.last_stream, f.error_code = struct.unpack("!LL", payload[:8])
|
|
|
|
f.last_stream &= 0x7FFFFFFF
|
|
|
|
f.data = payload[8:]
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id != 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'GOAWAY frames MUST NOT be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
b = struct.pack('!LL', self.last_stream & 0x7FFFFFFF, self.error_code)
|
|
|
|
b += bytes(self.data)
|
|
|
|
return b
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
s = []
|
|
|
|
s.append("last stream: %#x" % self.last_stream)
|
|
|
|
s.append("error code: %d" % self.error_code)
|
|
|
|
s.append("debug data: %s" % str(self.data))
|
|
|
|
return "\n".join(s)
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class WindowUpdateFrame(Frame):
|
|
|
|
TYPE = 0x8
|
|
|
|
VALID_FLAGS = []
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
window_size_increment=0x0):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(WindowUpdateFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.window_size_increment = window_size_increment
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
f.window_size_increment = struct.unpack("!L", payload)[0]
|
|
|
|
f.window_size_increment &= 0x7FFFFFFF
|
|
|
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
2015-05-27 09:18:54 +00:00
|
|
|
if self.window_size_increment <= 0 or self.window_size_increment >= 2 ** 31:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
2015-08-20 07:54:45 +00:00
|
|
|
'Window Size Increment MUST be greater than 0 and less than 2^31.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
return struct.pack('!L', self.window_size_increment & 0x7FFFFFFF)
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
|
|
|
return "window size increment: %#x" % self.window_size_increment
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-05-27 15:53:06 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
class ContinuationFrame(Frame):
|
|
|
|
TYPE = 0x9
|
|
|
|
VALID_FLAGS = [Frame.FLAG_END_HEADERS]
|
|
|
|
|
2015-05-30 00:02:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2015-06-01 13:17:50 +00:00
|
|
|
state=None,
|
2015-05-30 00:02:58 +00:00
|
|
|
length=0,
|
|
|
|
flags=Frame.FLAG_NO_FLAGS,
|
|
|
|
stream_id=0x0,
|
|
|
|
header_block_fragment=b''):
|
2015-06-01 13:17:50 +00:00
|
|
|
super(ContinuationFrame, self).__init__(state, length, flags, stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
self.header_block_fragment = header_block_fragment
|
|
|
|
|
|
|
|
@classmethod
|
2015-06-17 11:10:27 +00:00
|
|
|
def from_bytes(cls, state, length, flags, stream_id, payload):
|
|
|
|
f = cls(state=state, length=length, flags=flags, stream_id=stream_id)
|
2015-05-25 10:10:21 +00:00
|
|
|
f.header_block_fragment = payload
|
|
|
|
return f
|
|
|
|
|
|
|
|
def payload_bytes(self):
|
|
|
|
if self.stream_id == 0x0:
|
2015-05-30 00:02:58 +00:00
|
|
|
raise ValueError(
|
|
|
|
'CONTINUATION frames MUST be associated with a stream.')
|
2015-05-25 10:10:21 +00:00
|
|
|
|
|
|
|
return self.header_block_fragment
|
|
|
|
|
2015-05-27 10:58:55 +00:00
|
|
|
def payload_human_readable(self):
|
2015-06-15 13:31:58 +00:00
|
|
|
s = []
|
|
|
|
s.append(
|
|
|
|
"header_block_fragment: %s" %
|
|
|
|
self.header_block_fragment.encode('hex'))
|
|
|
|
return "\n".join(s)
|
2015-05-27 10:58:55 +00:00
|
|
|
|
2015-05-25 10:10:21 +00:00
|
|
|
_FRAME_CLASSES = [
|
|
|
|
DataFrame,
|
|
|
|
HeadersFrame,
|
|
|
|
PriorityFrame,
|
|
|
|
RstStreamFrame,
|
|
|
|
SettingsFrame,
|
|
|
|
PushPromiseFrame,
|
|
|
|
PingFrame,
|
|
|
|
GoAwayFrame,
|
|
|
|
WindowUpdateFrame,
|
|
|
|
ContinuationFrame
|
|
|
|
]
|
|
|
|
FRAMES = {cls.TYPE: cls for cls in _FRAME_CLASSES}
|
2015-06-06 00:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
HTTP2_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,
|
|
|
|
}
|