1010import collections
1111import struct
1212
13- # The maximum length of a frame. Some frames have shorter maximum lengths.
13+ # The maximum initial length of a frame. Some frames have shorter maximum lengths.
1414FRAME_MAX_LEN = (2 ** 14 ) - 1
1515
16+ # The maximum allowed length of a frame.
17+ FRAME_MAX_ALLOWED_LEN = (2 ** 24 ) - 1
1618
1719class Frame (object ):
1820 """
@@ -31,6 +33,7 @@ class Frame(object):
3133 def __init__ (self , stream_id ):
3234 self .stream_id = stream_id
3335 self .flags = set ()
36+ self .body_len = 0
3437
3538 if self .stream_association == 'has-stream' and not self .stream_id :
3639 raise ValueError ('Stream ID must be non-zero' )
@@ -40,7 +43,7 @@ def __init__(self, stream_id):
4043 @staticmethod
4144 def parse_frame_header (header ):
4245 """
43- Takes an 9-byte frame header and returns a tuple of the appropriate
46+ Takes a 9-byte frame header and returns a tuple of the appropriate
4447 Frame object and the length that needs to be read from the socket.
4548 """
4649 fields = struct .unpack ("!HBBBL" , header )
@@ -63,7 +66,7 @@ def parse_flags(self, flag_byte):
6366
6467 def serialize (self ):
6568 body = self .serialize_body ()
66- body_len = len (body )
69+ self . body_len = len (body )
6770
6871 # Build the common frame header.
6972 # First, get the flags.
@@ -75,8 +78,8 @@ def serialize(self):
7578
7679 header = struct .pack (
7780 "!HBBBL" ,
78- body_len & 0xFFFF00 , # Length is spread over top 24 bits
79- body_len & 0x0000FF ,
81+ ( self . body_len & 0xFFFF00 ) >> 8 , # Length is spread over top 24 bits
82+ self . body_len & 0x0000FF ,
8083 self .type ,
8184 flags ,
8285 self .stream_id & 0x7FFFFFFF # Stream ID is 32 bits.
@@ -408,7 +411,7 @@ class HeadersFrame(Padding, Priority, Frame):
408411 (remote)" states.
409412
410413 The HeadersFrame class is actually basically a data frame in this
411- implementation, becuase of the requirement to control the sizes of frames.
414+ implementation, because of the requirement to control the sizes of frames.
412415 A header block fragment that doesn't fit in an entire HEADERS frame needs
413416 to be followed with CONTINUATION frames. From the perspective of the frame
414417 building code the header block is an opaque data segment.
0 commit comments