Skip to content

Commit

Permalink
Merge pull request #240 from python-hyper/related-events-2
Browse files Browse the repository at this point in the history
[RFC] Alternative approach to "related events".
  • Loading branch information
Lukasa authored Jun 28, 2016
2 parents 67c07f4 + 7fe127f commit ab91f88
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 2 deletions.
1 change: 1 addition & 0 deletions h2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ def _receive_headers_frame(self, frame):

if 'PRIORITY' in frame.flags:
p_frames, p_events = self._receive_priority_frame(frame)
stream_events[0].priority_updated = p_events[0]
stream_events.extend(p_events)
assert not p_frames

Expand Down
67 changes: 67 additions & 0 deletions h2/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class RequestReceived(object):
.. versionchanged:: 2.3.0
Changed the type of ``headers`` to :class:`HeaderTuple
<hpack:hpack.HeaderTuple>`. This has no effect on current users.
.. versionchanged:: 2.4.0
Added ``stream_ended`` and ``priority_updated`` properties.
"""
def __init__(self):
#: The Stream ID for the stream this request was made on.
Expand All @@ -31,6 +34,20 @@ def __init__(self):
#: The request headers.
self.headers = None

#: If this request also ended the stream, the associated
#: :class:`StreamEnded <h2.events.StreamEnded>` event will be available
#: here.
#:
#: .. versionadded:: 2.4.0
self.stream_ended = None

#: If this request also had associated priority information, the
#: associated :class:`PriorityUpdated <h2.events.PriorityUpdated>`
#: event will be available here.
#:
#: .. versionadded:: 2.4.0
self.priority_updated = None

def __repr__(self):
return "<RequestReceived stream_id:%s, headers:%s>" % (
self.stream_id, self.headers
Expand All @@ -46,6 +63,9 @@ class ResponseReceived(object):
.. versionchanged:: 2.3.0
Changed the type of ``headers`` to :class:`HeaderTuple
<hpack:hpack.HeaderTuple>`. This has no effect on current users.
.. versionchanged:: 2.4.0
Added ``stream_ended`` and ``priority_updated`` properties.
"""
def __init__(self):
#: The Stream ID for the stream this response was made on.
Expand All @@ -54,6 +74,20 @@ def __init__(self):
#: The response headers.
self.headers = None

#: If this response also ended the stream, the associated
#: :class:`StreamEnded <h2.events.StreamEnded>` event will be available
#: here.
#:
#: .. versionadded:: 2.4.0
self.stream_ended = None

#: If this response also had associated priority information, the
#: associated :class:`PriorityUpdated <h2.events.PriorityUpdated>`
#: event will be available here.
#:
#: .. versionadded:: 2.4.0
self.priority_updated = None

def __repr__(self):
return "<ResponseReceived stream_id:%s, headers:%s>" % (
self.stream_id, self.headers
Expand All @@ -72,6 +106,9 @@ class TrailersReceived(object):
.. versionchanged:: 2.3.0
Changed the type of ``headers`` to :class:`HeaderTuple
<hpack:hpack.HeaderTuple>`. This has no effect on current users.
.. versionchanged:: 2.4.0
Added ``stream_ended`` and ``priority_updated`` properties.
"""
def __init__(self):
#: The Stream ID for the stream on which these trailers were received.
Expand All @@ -80,6 +117,19 @@ def __init__(self):
#: The trailers themselves.
self.headers = None

#: Trailers always end streams. This property has the associated
#: :class:`StreamEnded <h2.events.StreamEnded>` in it.
#:
#: .. versionadded:: 2.4.0
self.stream_ended = None

#: If the trailers also set associated priority information, the
#: associated :class:`PriorityUpdated <h2.events.PriorityUpdated>`
#: event will be available here.
#:
#: .. versionadded:: 2.4.0
self.priority_updated = None

def __repr__(self):
return "<TrailersReceived stream_id:%s, headers:%s>" % (
self.stream_id, self.headers
Expand All @@ -104,6 +154,9 @@ class InformationalResponseReceived(object):
.. versionchanged:: 2.3.0
Changed the type of ``headers`` to :class:`HeaderTuple
<hpack:hpack.HeaderTuple>`. This has no effect on current users.
.. versionchanged:: 2.4.0
Added ``priority_updated`` property.
"""
def __init__(self):
#: The Stream ID for the stream this informational response was made
Expand All @@ -113,6 +166,13 @@ def __init__(self):
#: The headers for this informational response.
self.headers = None

#: If this response also had associated priority information, the
#: associated :class:`PriorityUpdated <h2.events.PriorityUpdated>`
#: event will be available here.
#:
#: .. versionadded:: 2.4.0
self.priority_updated = None

def __repr__(self):
return "<InformationalResponseReceived stream_id:%s, headers:%s>" % (
self.stream_id, self.headers
Expand All @@ -138,6 +198,13 @@ def __init__(self):
#: than ``len(data)``.
self.flow_controlled_length = None

#: If this data chunk also completed the stream, the associated
#: :class:`StreamEnded <h2.events.StreamEnded>` event will be available
#: here.
#:
#: .. versionadded:: 2.4.0
self.stream_ended = None

def __repr__(self):
return (
"<DataReceived stream_id:%s, "
Expand Down
8 changes: 6 additions & 2 deletions h2/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,11 @@ def receive_headers(self, headers, end_stream, header_encoding):
events = self.state_machine.process_input(input_)

if end_stream:
events += self.state_machine.process_input(
es_events = self.state_machine.process_input(
StreamInputs.RECV_END_STREAM
)
events[0].stream_ended = es_events[0]
events += es_events

self._initialize_content_length(headers)

Expand All @@ -926,9 +928,11 @@ def receive_data(self, data, end_stream, flow_control_len):
self._track_content_length(len(data), end_stream)

if end_stream:
events += self.state_machine.process_input(
es_events = self.state_machine.process_input(
StreamInputs.RECV_END_STREAM
)
events[0].stream_ended = es_events[0]
events.extend(es_events)

events[0].data = data
events[0].flow_controlled_length = flow_control_len
Expand Down
Loading

0 comments on commit ab91f88

Please sign in to comment.