http2: improve framereader

This commit is contained in:
Thomas Kriechbaumer 2016-08-23 19:29:24 +02:00
parent 043180a8fa
commit 5dda9505b6
6 changed files with 29 additions and 24 deletions

View File

@ -325,7 +325,7 @@ class Http2Layer(base.Layer):
with source_conn.h2.lock:
try:
raw_frame = b''.join(http2.framereader.http2_read_raw_frame(source_conn.rfile))
raw_frame = b''.join(http2.read_raw_frame(source_conn.rfile))
except:
# read frame failed: connection closed
self._kill_all_streams()

View File

@ -1,8 +1,10 @@
from __future__ import absolute_import, print_function, division
from netlib.http.http2 import framereader
from netlib.http.http2.framereader import read_raw_frame, parse_frame
from netlib.http.http2.utils import parse_headers
__all__ = [
"framereader",
"read_raw_frame",
"parse_frame",
"parse_headers",
]

View File

@ -4,7 +4,7 @@ import hyperframe
from ...exceptions import HttpException
def http2_read_raw_frame(rfile):
def read_raw_frame(rfile):
header = rfile.safe_read(9)
length = int(codecs.encode(header[:3], 'hex_codec'), 16)
@ -15,8 +15,11 @@ def http2_read_raw_frame(rfile):
return [header, body]
def http2_read_frame(rfile):
header, body = http2_read_raw_frame(rfile)
def parse_frame(header, body=None):
if body is None:
body = header[9:]
header = header[:9]
frame, length = hyperframe.frame.Frame.parse_frame_header(header)
frame.parse_body(memoryview(body))
return frame

View File

@ -254,7 +254,7 @@ class HTTP2StateProtocol(object):
def read_frame(self, hide=False):
while True:
frm = http2.framereader.http2_read_frame(self.tcp_handler.rfile)
frm = http2.parse_frame(*http2.read_raw_frame(self.tcp_handler.rfile))
if not hide and self.dump_frames: # pragma no cover
print(frm.human_readable("<<"))

View File

@ -15,7 +15,7 @@ from mitmproxy.proxy.config import ProxyConfig
import netlib
from ...netlib import tservers as netlib_tservers
from netlib.exceptions import HttpException
from netlib.http.http2 import framereader
from netlib.http import http2
from .. import tservers
@ -55,7 +55,7 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(self.rfile))
raw = b''.join(http2.read_raw_frame(self.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -243,7 +243,7 @@ class TestSimple(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -320,7 +320,7 @@ class TestRequestWithPriority(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -359,7 +359,7 @@ class TestRequestWithPriority(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -431,7 +431,7 @@ class TestPriority(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -508,7 +508,7 @@ class TestPriorityWithExistingStream(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -559,7 +559,7 @@ class TestStreamResetFromServer(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -609,7 +609,7 @@ class TestBodySizeLimit(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -694,7 +694,7 @@ class TestPushPromise(_Http2Test):
responses = 0
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -747,7 +747,7 @@ class TestPushPromise(_Http2Test):
responses = 0
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -807,7 +807,7 @@ class TestConnectionLost(_Http2Test):
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
@ -864,7 +864,7 @@ class TestMaxConcurrentStreams(_Http2Test):
ended_streams = 0
while ended_streams != len(new_streams):
try:
header, body = framereader.http2_read_raw_frame(client.rfile)
header, body = http2.read_raw_frame(client.rfile)
events = h2_conn.receive_data(b''.join([header, body]))
except:
break
@ -910,7 +910,7 @@ class TestConnectionTerminated(_Http2Test):
connection_terminated_event = None
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
raw = b''.join(http2.read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
for event in events:
if isinstance(event, h2.events.ConnectionTerminated):

View File

@ -5,7 +5,7 @@ import hyperframe
from netlib import tcp, http
from netlib.tutils import raises
from netlib.exceptions import TcpDisconnect
from netlib.http.http2 import framereader
from netlib.http import http2
from ..netlib import tservers as netlib_tservers
@ -112,11 +112,11 @@ class TestPerformServerConnectionPreface(netlib_tservers.ServerTestBase):
self.wfile.flush()
# check empty settings frame
raw = framereader.http2_read_raw_frame(self.rfile)
raw = http2.read_raw_frame(self.rfile)
assert raw == codecs.decode('00000c040000000000000200000000000300000001', 'hex_codec')
# check settings acknowledgement
raw = framereader.http2_read_raw_frame(self.rfile)
raw = http2.read_raw_frame(self.rfile)
assert raw == codecs.decode('000000040100000000', 'hex_codec')
# send settings acknowledgement