10
10
import collections
11
11
import struct
12
12
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.
14
14
FRAME_MAX_LEN = (2 ** 14 ) - 1
15
15
16
+ # The maximum allowed length of a frame.
17
+ FRAME_MAX_ALLOWED_LEN = (2 ** 24 ) - 1
16
18
17
19
class Frame (object ):
18
20
"""
@@ -31,6 +33,7 @@ class Frame(object):
31
33
def __init__ (self , stream_id ):
32
34
self .stream_id = stream_id
33
35
self .flags = set ()
36
+ self .body_len = 0
34
37
35
38
if self .stream_association == 'has-stream' and not self .stream_id :
36
39
raise ValueError ('Stream ID must be non-zero' )
@@ -40,7 +43,7 @@ def __init__(self, stream_id):
40
43
@staticmethod
41
44
def parse_frame_header (header ):
42
45
"""
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
44
47
Frame object and the length that needs to be read from the socket.
45
48
"""
46
49
fields = struct .unpack ("!HBBBL" , header )
@@ -63,7 +66,7 @@ def parse_flags(self, flag_byte):
63
66
64
67
def serialize (self ):
65
68
body = self .serialize_body ()
66
- body_len = len (body )
69
+ self . body_len = len (body )
67
70
68
71
# Build the common frame header.
69
72
# First, get the flags.
@@ -75,8 +78,8 @@ def serialize(self):
75
78
76
79
header = struct .pack (
77
80
"!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 ,
80
83
self .type ,
81
84
flags ,
82
85
self .stream_id & 0x7FFFFFFF # Stream ID is 32 bits.
@@ -408,7 +411,7 @@ class HeadersFrame(Padding, Priority, Frame):
408
411
(remote)" states.
409
412
410
413
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.
412
415
A header block fragment that doesn't fit in an entire HEADERS frame needs
413
416
to be followed with CONTINUATION frames. From the perspective of the frame
414
417
building code the header block is an opaque data segment.
0 commit comments