2016-05-31 05:16:31 +00:00
|
|
|
import codecs
|
|
|
|
|
|
|
|
import hyperframe
|
2016-06-22 20:09:57 +00:00
|
|
|
from ...exceptions import HttpException
|
2016-05-31 05:16:31 +00:00
|
|
|
|
|
|
|
|
2016-08-23 17:29:24 +00:00
|
|
|
def read_raw_frame(rfile):
|
2016-05-31 05:16:31 +00:00
|
|
|
header = rfile.safe_read(9)
|
|
|
|
length = int(codecs.encode(header[:3], 'hex_codec'), 16)
|
|
|
|
|
|
|
|
if length == 4740180:
|
2016-06-22 20:09:57 +00:00
|
|
|
raise HttpException("Length field looks more like HTTP/1.1:\n{}".format(rfile.read(-1)))
|
2016-05-31 05:16:31 +00:00
|
|
|
|
|
|
|
body = rfile.safe_read(length)
|
|
|
|
return [header, body]
|
|
|
|
|
|
|
|
|
2016-08-23 17:29:24 +00:00
|
|
|
def parse_frame(header, body=None):
|
|
|
|
if body is None:
|
|
|
|
body = header[9:]
|
|
|
|
header = header[:9]
|
|
|
|
|
2016-05-31 05:16:31 +00:00
|
|
|
frame, length = hyperframe.frame.Frame.parse_frame_header(header)
|
|
|
|
frame.parse_body(memoryview(body))
|
|
|
|
return frame
|