http2: more coverage

This commit is contained in:
Thomas Kriechbaumer 2016-07-10 20:07:43 +02:00
parent b611997619
commit 7c09255298
2 changed files with 231 additions and 43 deletions

View File

@ -78,7 +78,7 @@ class SafeH2Connection(connection.H2Connection):
self.send_data(stream_id, frame_chunk) self.send_data(stream_id, frame_chunk)
try: try:
self.conn.send(self.data_to_send()) self.conn.send(self.data_to_send())
except Exception as e: except Exception as e: # pragma: no cover
raise e raise e
finally: finally:
self.lock.release() self.lock.release()
@ -142,9 +142,9 @@ class Http2Layer(base.Layer):
self.streams[eid].timestamp_start = time.time() self.streams[eid].timestamp_start = time.time()
self.streams[eid].no_body = (event.stream_ended is not None) self.streams[eid].no_body = (event.stream_ended is not None)
if event.priority_updated is not None: if event.priority_updated is not None:
self.streams[eid].priority_weight = event.priority_updated.weight
self.streams[eid].priority_depends_on = event.priority_updated.depends_on
self.streams[eid].priority_exclusive = event.priority_updated.exclusive self.streams[eid].priority_exclusive = event.priority_updated.exclusive
self.streams[eid].priority_depends_on = event.priority_updated.depends_on
self.streams[eid].priority_weight = event.priority_updated.weight
self.streams[eid].handled_priority_event = event.priority_updated self.streams[eid].handled_priority_event = event.priority_updated
self.streams[eid].start() self.streams[eid].start()
elif isinstance(event, events.ResponseReceived): elif isinstance(event, events.ResponseReceived):
@ -155,10 +155,13 @@ class Http2Layer(base.Layer):
self.streams[eid].response_arrived.set() self.streams[eid].response_arrived.set()
elif isinstance(event, events.DataReceived): elif isinstance(event, events.DataReceived):
if self.config.body_size_limit and self.streams[eid].queued_data_length > self.config.body_size_limit: if self.config.body_size_limit and self.streams[eid].queued_data_length > self.config.body_size_limit:
raise netlib.exceptions.HttpException("HTTP body too large. Limit is {}.".format(self.config.body_size_limit)) self.streams[eid].zombie = time.time()
self.streams[eid].data_queue.put(event.data) source_conn.h2.safe_reset_stream(event.stream_id, 0x7)
self.streams[eid].queued_data_length += len(event.data) self.log("HTTP body too large. Limit is {}.".format(self.config.body_size_limit), "info")
source_conn.h2.safe_increment_flow_control(event.stream_id, event.flow_controlled_length) else:
self.streams[eid].data_queue.put(event.data)
self.streams[eid].queued_data_length += len(event.data)
source_conn.h2.safe_increment_flow_control(event.stream_id, event.flow_controlled_length)
elif isinstance(event, events.StreamEnded): elif isinstance(event, events.StreamEnded):
self.streams[eid].timestamp_end = time.time() self.streams[eid].timestamp_end = time.time()
self.streams[eid].data_finished.set() self.streams[eid].data_finished.set()
@ -206,6 +209,11 @@ class Http2Layer(base.Layer):
self.streams[event.pushed_stream_id].request_data_finished.set() self.streams[event.pushed_stream_id].request_data_finished.set()
self.streams[event.pushed_stream_id].start() self.streams[event.pushed_stream_id].start()
elif isinstance(event, events.PriorityUpdated): elif isinstance(event, events.PriorityUpdated):
if eid in self.streams and self.streams[eid].handled_priority_event is event:
# this event was already handled during stream creation
# HeadersFrame + Priority information as RequestReceived
return True
mapped_stream_id = event.stream_id mapped_stream_id = event.stream_id
if mapped_stream_id in self.streams and self.streams[mapped_stream_id].server_stream_id: if mapped_stream_id in self.streams and self.streams[mapped_stream_id].server_stream_id:
# if the stream is already up and running and was sent to the server # if the stream is already up and running and was sent to the server
@ -213,13 +221,9 @@ class Http2Layer(base.Layer):
mapped_stream_id = self.streams[mapped_stream_id].server_stream_id mapped_stream_id = self.streams[mapped_stream_id].server_stream_id
if eid in self.streams: if eid in self.streams:
if self.streams[eid].handled_priority_event is event:
# this event was already handled during stream creation
# HeadersFrame + Priority information as RequestReceived
return True
self.streams[eid].priority_weight = event.weight
self.streams[eid].priority_depends_on = event.depends_on
self.streams[eid].priority_exclusive = event.exclusive self.streams[eid].priority_exclusive = event.exclusive
self.streams[eid].priority_depends_on = event.depends_on
self.streams[eid].priority_weight = event.weight
with self.server_conn.h2.lock: with self.server_conn.h2.lock:
self.server_conn.h2.prioritize( self.server_conn.h2.prioritize(
@ -248,10 +252,12 @@ class Http2Layer(base.Layer):
def _cleanup_streams(self): def _cleanup_streams(self):
death_time = time.time() - 10 death_time = time.time() - 10
for stream_id in self.streams.keys():
zombie = self.streams[stream_id].zombie zombie_streams = [(stream_id, stream) for stream_id, stream in list(self.streams.items()) if stream.zombie]
if zombie and zombie <= death_time: outdated_streams = [stream_id for stream_id, stream in zombie_streams if stream.zombie <= death_time]
self.streams.pop(stream_id, None)
for stream_id in outdated_streams: # pragma: no cover
self.streams.pop(stream_id, None)
def _kill_all_streams(self): def _kill_all_streams(self):
for stream in self.streams.values(): for stream in self.streams.values():
@ -296,7 +302,7 @@ class Http2Layer(base.Layer):
return return
self._cleanup_streams() self._cleanup_streams()
except Exception as e: except Exception as e: # pragma: no cover
self.log(repr(e), "info") self.log(repr(e), "info")
self.log(traceback.format_exc(), "debug") self.log(traceback.format_exc(), "debug")
self._kill_all_streams() self._kill_all_streams()
@ -326,9 +332,9 @@ class Http2SingleStreamLayer(http._HttpTransmissionLayer, basethread.BaseThread)
self.no_body = False self.no_body = False
self.priority_weight = None
self.priority_depends_on = None
self.priority_exclusive = None self.priority_exclusive = None
self.priority_depends_on = None
self.priority_weight = None
self.handled_priority_event = None self.handled_priority_event = None
@property @property
@ -428,11 +434,11 @@ class Http2SingleStreamLayer(http._HttpTransmissionLayer, basethread.BaseThread)
self.server_stream_id, self.server_stream_id,
headers, headers,
end_stream=self.no_body, end_stream=self.no_body,
priority_weight=self.priority_weight,
priority_depends_on=self._map_depends_on_stream_id(self.server_stream_id, self.priority_depends_on),
priority_exclusive=self.priority_exclusive, priority_exclusive=self.priority_exclusive,
priority_depends_on=self._map_depends_on_stream_id(self.server_stream_id, self.priority_depends_on),
priority_weight=self.priority_weight,
) )
except Exception as e: except Exception as e: # pragma: no cover
raise e raise e
finally: finally:
self.server_conn.h2.lock.release() self.server_conn.h2.lock.release()
@ -523,7 +529,7 @@ class Http2SingleStreamLayer(http._HttpTransmissionLayer, basethread.BaseThread)
try: try:
layer() layer()
except exceptions.ProtocolException as e: except exceptions.ProtocolException as e: # pragma: no cover
self.log(repr(e), "info") self.log(repr(e), "info")
self.log(traceback.format_exc(), "debug") self.log(traceback.format_exc(), "debug")

View File

@ -60,7 +60,10 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase):
except HttpException: except HttpException:
print(traceback.format_exc()) print(traceback.format_exc())
assert False assert False
except netlib.exceptions.TcpDisconnect:
break
except: except:
print(traceback.format_exc())
break break
self.wfile.write(h2_conn.data_to_send()) self.wfile.write(h2_conn.data_to_send())
self.wfile.flush() self.wfile.flush()
@ -70,8 +73,11 @@ class _Http2ServerBase(netlib_tservers.ServerTestBase):
if not self.server.handle_server_event(event, h2_conn, self.rfile, self.wfile): if not self.server.handle_server_event(event, h2_conn, self.rfile, self.wfile):
done = True done = True
break break
except netlib.exceptions.TcpDisconnect:
done = True
except: except:
done = True done = True
print(traceback.format_exc())
break break
def handle_server_event(self, h2_conn, rfile, wfile): def handle_server_event(self, h2_conn, rfile, wfile):
@ -138,11 +144,22 @@ class _Http2TestBase(object):
return client, h2_conn return client, h2_conn
def _send_request(self, wfile, h2_conn, stream_id=1, headers=[], body=b''): def _send_request(self,
wfile,
h2_conn,
stream_id=1,
headers=[],
body=b'',
priority_exclusive=None,
priority_depends_on=None,
priority_weight=None):
h2_conn.send_headers( h2_conn.send_headers(
stream_id=stream_id, stream_id=stream_id,
headers=headers, headers=headers,
end_stream=(len(body) == 0), end_stream=(len(body) == 0),
priority_exclusive=priority_exclusive,
priority_depends_on=priority_depends_on,
priority_weight=priority_weight,
) )
if body: if body:
h2_conn.send_data(stream_id, body) h2_conn.send_data(stream_id, body)
@ -166,6 +183,7 @@ class _Http2Test(_Http2TestBase, _Http2ServerBase):
@requires_alpn @requires_alpn
class TestSimple(_Http2Test): class TestSimple(_Http2Test):
request_body_buffer = b''
@classmethod @classmethod
def handle_server_event(self, event, h2_conn, rfile, wfile): def handle_server_event(self, event, h2_conn, rfile, wfile):
@ -190,13 +208,16 @@ class TestSimple(_Http2Test):
('föo', 'bär'), ('föo', 'bär'),
('X-Stream-ID', str(event.stream_id)), ('X-Stream-ID', str(event.stream_id)),
]) ])
h2_conn.send_data(event.stream_id, b'foobar') h2_conn.send_data(event.stream_id, b'response body')
h2_conn.end_stream(event.stream_id) h2_conn.end_stream(event.stream_id)
wfile.write(h2_conn.data_to_send()) wfile.write(h2_conn.data_to_send())
wfile.flush() wfile.flush()
elif isinstance(event, h2.events.DataReceived):
self.request_body_buffer += event.data
return True return True
def test_simple(self): def test_simple(self):
response_body_buffer = b''
client, h2_conn = self._setup_connection() client, h2_conn = self._setup_connection()
self._send_request( self._send_request(
@ -210,7 +231,7 @@ class TestSimple(_Http2Test):
('ClIeNt-FoO', 'client-bar-1'), ('ClIeNt-FoO', 'client-bar-1'),
('ClIeNt-FoO', 'client-bar-2'), ('ClIeNt-FoO', 'client-bar-2'),
], ],
body=b'my request body echoed back to me') body=b'request body')
done = False done = False
while not done: while not done:
@ -225,7 +246,9 @@ class TestSimple(_Http2Test):
client.wfile.flush() client.wfile.flush()
for event in events: for event in events:
if isinstance(event, h2.events.StreamEnded): if isinstance(event, h2.events.DataReceived):
response_body_buffer += event.data
elif isinstance(event, h2.events.StreamEnded):
done = True done = True
h2_conn.close_connection() h2_conn.close_connection()
@ -236,31 +259,41 @@ class TestSimple(_Http2Test):
assert self.master.state.flows[0].response.status_code == 200 assert self.master.state.flows[0].response.status_code == 200
assert self.master.state.flows[0].response.headers['server-foo'] == 'server-bar' assert self.master.state.flows[0].response.headers['server-foo'] == 'server-bar'
assert self.master.state.flows[0].response.headers['föo'] == 'bär' assert self.master.state.flows[0].response.headers['föo'] == 'bär'
assert self.master.state.flows[0].response.body == b'foobar' assert self.master.state.flows[0].response.body == b'response body'
assert self.request_body_buffer == b'request body'
assert response_body_buffer == b'response body'
@requires_alpn @requires_alpn
class TestWithBodies(_Http2Test): class TestRequestWithPriority(_Http2Test):
tmp_data_buffer_foobar = b''
@classmethod @classmethod
def handle_server_event(self, event, h2_conn, rfile, wfile): def handle_server_event(self, event, h2_conn, rfile, wfile):
if isinstance(event, h2.events.ConnectionTerminated): if isinstance(event, h2.events.ConnectionTerminated):
return False return False
if isinstance(event, h2.events.DataReceived): elif isinstance(event, h2.events.RequestReceived):
self.tmp_data_buffer_foobar += event.data import warnings
elif isinstance(event, h2.events.StreamEnded): with warnings.catch_warnings():
h2_conn.send_headers(1, [ # Ignore UnicodeWarning:
(':status', '200'), # h2/utilities.py:64: UnicodeWarning: Unicode equal comparison
]) # failed to convert both arguments to Unicode - interpreting
h2_conn.send_data(1, self.tmp_data_buffer_foobar) # them as being unequal.
h2_conn.end_stream(1) # elif header[0] in (b'cookie', u'cookie') and len(header[1]) < 20:
warnings.simplefilter("ignore")
headers = [(':status', '200')]
if event.priority_updated:
headers.append(('priority_exclusive', event.priority_updated.exclusive))
headers.append(('priority_depends_on', event.priority_updated.depends_on))
headers.append(('priority_weight', event.priority_updated.weight))
h2_conn.send_headers(event.stream_id, headers)
h2_conn.end_stream(event.stream_id)
wfile.write(h2_conn.data_to_send()) wfile.write(h2_conn.data_to_send())
wfile.flush() wfile.flush()
return True return True
def test_with_bodies(self): def test_request_with_priority(self):
client, h2_conn = self._setup_connection() client, h2_conn = self._setup_connection()
self._send_request( self._send_request(
@ -272,7 +305,9 @@ class TestWithBodies(_Http2Test):
(':scheme', 'https'), (':scheme', 'https'),
(':path', '/'), (':path', '/'),
], ],
body=b'foobar with request body', priority_exclusive = True,
priority_depends_on = 42424242,
priority_weight = 42,
) )
done = False done = False
@ -295,7 +330,149 @@ class TestWithBodies(_Http2Test):
client.wfile.write(h2_conn.data_to_send()) client.wfile.write(h2_conn.data_to_send())
client.wfile.flush() client.wfile.flush()
assert self.master.state.flows[0].response.body == b'foobar with request body' assert len(self.master.state.flows) == 1
assert self.master.state.flows[0].response.headers['priority_exclusive'] == 'True'
assert self.master.state.flows[0].response.headers['priority_depends_on'] == '42424242'
assert self.master.state.flows[0].response.headers['priority_weight'] == '42'
def test_request_without_priority(self):
client, h2_conn = self._setup_connection()
self._send_request(
client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:%s" % self.server.server.address.port),
(':method', 'GET'),
(':scheme', 'https'),
(':path', '/'),
],
)
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
assert False
client.wfile.write(h2_conn.data_to_send())
client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamEnded):
done = True
h2_conn.close_connection()
client.wfile.write(h2_conn.data_to_send())
client.wfile.flush()
assert len(self.master.state.flows) == 1
assert 'priority_exclusive' not in self.master.state.flows[0].response.headers
assert 'priority_depends_on' not in self.master.state.flows[0].response.headers
assert 'priority_weight' not in self.master.state.flows[0].response.headers
@requires_alpn
class TestStreamResetFromServer(_Http2Test):
@classmethod
def handle_server_event(self, event, h2_conn, rfile, wfile):
if isinstance(event, h2.events.ConnectionTerminated):
return False
elif isinstance(event, h2.events.RequestReceived):
h2_conn.reset_stream(event.stream_id, 0x8)
wfile.write(h2_conn.data_to_send())
wfile.flush()
return True
def test_request_with_priority(self):
client, h2_conn = self._setup_connection()
self._send_request(
client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:%s" % self.server.server.address.port),
(':method', 'GET'),
(':scheme', 'https'),
(':path', '/'),
],
)
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
assert False
client.wfile.write(h2_conn.data_to_send())
client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamReset):
done = True
h2_conn.close_connection()
client.wfile.write(h2_conn.data_to_send())
client.wfile.flush()
assert len(self.master.state.flows) == 1
assert self.master.state.flows[0].response is None
@requires_alpn
class TestBodySizeLimit(_Http2Test):
@classmethod
def handle_server_event(self, event, h2_conn, rfile, wfile):
if isinstance(event, h2.events.ConnectionTerminated):
return False
return True
def test_body_size_limit(self):
self.config.body_size_limit = 20
client, h2_conn = self._setup_connection()
self._send_request(
client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:%s" % self.server.server.address.port),
(':method', 'GET'),
(':scheme', 'https'),
(':path', '/'),
],
body=b'very long body over 20 characters long',
)
done = False
while not done:
try:
raw = b''.join(framereader.http2_read_raw_frame(client.rfile))
events = h2_conn.receive_data(raw)
except HttpException:
print(traceback.format_exc())
assert False
client.wfile.write(h2_conn.data_to_send())
client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamReset):
done = True
h2_conn.close_connection()
client.wfile.write(h2_conn.data_to_send())
client.wfile.flush()
assert len(self.master.state.flows) == 0
@requires_alpn @requires_alpn
@ -496,6 +673,11 @@ class TestConnectionLost(_Http2Test):
@requires_alpn @requires_alpn
class TestMaxConcurrentStreams(_Http2Test): class TestMaxConcurrentStreams(_Http2Test):
@classmethod
def setup_class(self):
_Http2TestBase.setup_class()
_Http2ServerBase.setup_class(h2_server_settings={h2.settings.MAX_CONCURRENT_STREAMS: 2})
@classmethod @classmethod
def handle_server_event(self, event, h2_conn, rfile, wfile): def handle_server_event(self, event, h2_conn, rfile, wfile):
if isinstance(event, h2.events.ConnectionTerminated): if isinstance(event, h2.events.ConnectionTerminated):