Skip to content

Backport #476 to 2.6.X #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Release History
===============

2.6.1dev0
---------

Bugfixes
~~~~~~~~

- Allowed hyperframe v5 support while continuing to ignore unexpected frames.

2.6.0 (2017-02-28)
------------------

Expand Down
32 changes: 26 additions & 6 deletions h2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
from .exceptions import (
ProtocolError, NoSuchStreamError, FlowControlError, FrameTooLargeError,
TooManyStreamsError, StreamClosedError, StreamIDTooLowError,
NoAvailableStreamIDError, UnsupportedFrameError, RFC1122Error,
DenialOfServiceError
NoAvailableStreamIDError, RFC1122Error, DenialOfServiceError
)
from .frame_buffer import FrameBuffer
from .settings import Settings, SettingCodes
Expand All @@ -46,6 +45,15 @@ class OversizedHeaderListError(Exception):
pass


try:
from hyperframe.frame import ExtensionFrame
except ImportError: # Platform-specific: Hyperframe < 5.0.0
# If the frame doesn't exist, that's just fine: we'll define it ourselves
# and the method will just never be called.
class ExtensionFrame(object):
pass


class ConnectionState(Enum):
IDLE = 0
CLIENT_OPEN = 1
Expand Down Expand Up @@ -404,6 +412,7 @@ def __init__(self, client_side=True, header_encoding='utf-8', config=None):
GoAwayFrame: self._receive_goaway_frame,
ContinuationFrame: self._receive_naked_continuation,
AltSvcFrame: self._receive_alt_svc_frame,
ExtensionFrame: self._receive_unknown_frame
}

def _prepare_for_sending(self, frames):
Expand Down Expand Up @@ -1575,10 +1584,6 @@ def _receive_frame(self, frame):
# Closed implicitly, also a connection error, but of type
# PROTOCOL_ERROR.
raise
except KeyError as e: # pragma: no cover
# We don't have a function for handling this frame. Let's call this
# a PROTOCOL_ERROR and exit.
raise UnsupportedFrameError("Unexpected frame: %s" % frame)
else:
self._prepare_for_sending(frames)

Expand Down Expand Up @@ -1922,6 +1927,21 @@ def _receive_alt_svc_frame(self, frame):

return frames, events

def _receive_unknown_frame(self, frame):
"""
We have received a frame that we do not understand. This is almost
certainly an extension frame, though it's impossible to be entirely
sure.

RFC 7540 § 5.5 says that we MUST ignore unknown frame types: so we
do.
"""
# All we do here is log.
self.config.logger.debug(
"Received unknown extension frame (ID %d)", frame.stream_id
)
return [], []

def _local_settings_acked(self):
"""
Handle the local settings being ACKed, update internal state.
Expand Down
4 changes: 3 additions & 1 deletion h2/frame_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ def _parse_frame_header(self, data):
"""
try:
frame, length = Frame.parse_frame_header(data[:9])
except UnknownFrameError as e:
except UnknownFrameError as e: # Platform-specific: Hyperframe < 5.0
# Here we do something a bit odd. We want to consume the frame data
# as consistently as possible, but we also don't ever want to yield
# None. Instead, we make sure that, if there is no frame, we
# recurse into ourselves.
# This can only happen now on older versions of hyperframe.
# TODO: Remove in 3.0
length = e.length
frame = None
except ValueError as e:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
'Programming Language :: Python :: Implementation :: PyPy',
],
install_requires=[
'hyperframe>=3.1, <5, !=4.0.0',
'hyperframe>=3.1, <6, !=4.0.0',
'hpack>=2.2, <3',
],
extras_require={
Expand Down