Class: HTTP2::Stream
- Inherits:
-
Object
- Object
- HTTP2::Stream
- Includes:
- Emitter, Error, FlowBuffer
- Defined in:
- lib/http/2/stream.rb,
sig/stream.rbs
Overview
A single HTTP 2.0 connection can multiplex multiple streams in parallel: multiple requests and responses can be in flight simultaneously and stream data can be interleaved and prioritized.
This class encapsulates all of the state, transition, flow-control, and error management as defined by the HTTP 2.0 specification. All you have to do is subscribe to appropriate events (marked with ":" prefix in diagram below) and provide your application logic to handle request and response processing.
+--------+
PP | | PP
,--------| idle |--------.
/ | | \
v +--------+ v
+----------+ | +----------+
| | | H | |
,---|:reserved | | |:reserved |---.
| | (local) | v | (remote) | |
| +----------+ +--------+ +----------+ |
| | :active | | :active | |
| | ,-------|:active |-------. | |
| | H / ES | | ES \ H | |
| v v +--------+ v v |
| +-----------+ | +-----------+ |
| |:half_close| | |:half_close| |
| | (remote) | | | (local) | |
| +-----------+ | +-----------+ |
| | v | |
| | ES/R +--------+ ES/R | |
| `----------->| |<-----------' |
| R | :close | R |
`-------------------->| |<--------------------'
+--------+
Constant Summary collapse
- STREAM_OPEN_STATES =
%i[open half_closed_local half_closing closing].freeze
Constants included from FlowBuffer
Instance Attribute Summary collapse
-
#closed ⇒ Symbol?
readonly
Reason why connection was closed.
-
#dependency ⇒ Integer
readonly
Returns the value of attribute dependency.
-
#id ⇒ Integer
readonly
Stream ID (odd for client initiated streams, even otherwise).
-
#local_window ⇒ Integer
(also: #window)
readonly
Size of current stream flow control window.
-
#parent ⇒ Stream?
readonly
Request parent stream of push stream.
-
#remote_window ⇒ Integer
readonly
Returns the value of attribute remote_window.
-
#state ⇒ Symbol
readonly
Stream state as defined by HTTP 2.0.
-
#weight ⇒ Integer
readonly
Stream priority as set by initiator.
Attributes included from FlowBuffer
Instance Method Summary collapse
-
#activate_stream_in_conn ⇒ void
Streams that are in the "open" state, or either of the "half closed" states count toward the maximum number of streams that an endpoint is permitted to open.
- #calculate_content_length(data_length) ⇒ void
-
#cancel ⇒ void
Sends a RST_STREAM indicating that the stream is no longer needed.
-
#chunk_data(payload, max_size) {|arg0| ... } ⇒ String
Chunk data into max_size, yield each chunk, then return final chunk.
-
#close(error = :stream_closed) ⇒ Object
Sends a RST_STREAM frame which closes current stream - this does not close the underlying connection.
- #close_stream_in_conn(*args) ⇒ void
- #closed? ⇒ Boolean
- #complete_transition(frame) ⇒ void
-
#data(payload, end_stream: true) ⇒ void
Sends DATA frame containing response payload.
- #end_stream?(frame) ⇒ Boolean
- #event(newstate) ⇒ void
-
#headers(headers, end_headers: true, end_stream: false) ⇒ void
Sends a HEADERS frame containing HTTP response headers.
-
#initialize(connection:, id:, weight: 16, dependency: 0, exclusive: false, parent: nil, state: :idle) ⇒ Stream
constructor
Initializes new stream.
- #manage_state(frame) { ... } ⇒ void
- #process_priority(frame) ⇒ void
- #promise(headers, end_headers: true) {|arg0| ... } ⇒ void
-
#receive(frame) ⇒ void
(also: #<<)
Processes incoming HTTP 2.0 frames.
-
#refuse ⇒ void
Sends a RST_STREAM indicating that the stream has been refused prior to performing any application processing.
-
#reprioritize(weight: 16, dependency: 0, exclusive: false) ⇒ void
Sends a PRIORITY frame with new stream priority value (can only be performed by the client).
-
#send(frame) ⇒ void
Processes outgoing HTTP 2.0 frames.
- #stream_error(error = :internal_error, msg: nil) ⇒ void (also: #error)
-
#transition(frame, sending) ⇒ void
HTTP 2.0 Stream States - http://tools.ietf.org/html/draft-ietf-httpbis-http2-16#section-5.1.
- #verify_trailers(frame) ⇒ void
-
#window_update(increment) ⇒ void
Sends a WINDOW_UPDATE frame to the peer.
Methods included from Error
Methods included from Emitter
Methods included from FlowBuffer
#buffered_amount, #calculate_window_update, #flush, #process_window_update, #send_data, #send_frame, #update_local_window
Constructor Details
#initialize(connection:, id:, weight: 16, dependency: 0, exclusive: false, parent: nil, state: :idle) ⇒ Stream
Initializes new stream.
Note that you should never have to call this directly. To create a new client initiated stream, use Connection#new_stream. Similarly, Connection will emit new stream objects, when new stream frames are received.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/http/2/stream.rb', line 78 def initialize(connection:, id:, weight: 16, dependency: 0, exclusive: false, parent: nil, state: :idle) stream_error(:protocol_error, msg: "stream can't depend on itself") if id == dependency @connection = connection @id = id @weight = weight @dependency = dependency # from mixins @listeners = Hash.new { |hash, key| hash[key] = [] } @send_buffer = FrameBuffer.new process_priority(weight: weight, dependency: dependency, exclusive: exclusive) @local_window_max_size = connection.local_settings[:settings_initial_window_size] @local_window = connection.local_settings[:settings_initial_window_size] @remote_window = connection.remote_settings[:settings_initial_window_size] @parent = parent @state = state @error = false @closed = false @_method = @_content_length = @_status_code = @_trailers = nil @_waiting_on_trailers = false @received_data = false @activated = false on(:window) { |v| @remote_window = v } on(:local_window) { |v| @local_window_max_size = @local_window = v } end |
Instance Attribute Details
#closed ⇒ Symbol? (readonly)
Reason why connection was closed.
63 64 65 |
# File 'lib/http/2/stream.rb', line 63 def closed @closed end |
#dependency ⇒ Integer (readonly)
Returns the value of attribute dependency.
56 57 58 |
# File 'lib/http/2/stream.rb', line 56 def dependency @dependency end |
#id ⇒ Integer (readonly)
Stream ID (odd for client initiated streams, even otherwise).
46 47 48 |
# File 'lib/http/2/stream.rb', line 46 def id @id end |
#local_window ⇒ Integer (readonly) Also known as: window
Size of current stream flow control window.
59 60 61 |
# File 'lib/http/2/stream.rb', line 59 def local_window @local_window end |
#parent ⇒ Stream? (readonly)
Request parent stream of push stream.
52 53 54 |
# File 'lib/http/2/stream.rb', line 52 def parent @parent end |
#remote_window ⇒ Integer (readonly)
Returns the value of attribute remote_window.
56 57 58 |
# File 'lib/http/2/stream.rb', line 56 def remote_window @remote_window end |
#state ⇒ Symbol (readonly)
Stream state as defined by HTTP 2.0.
49 50 51 |
# File 'lib/http/2/stream.rb', line 49 def state @state end |
#weight ⇒ Integer (readonly)
Stream priority as set by initiator.
55 56 57 |
# File 'lib/http/2/stream.rb', line 55 def weight @weight end |
Instance Method Details
#activate_stream_in_conn ⇒ void
This method returns an undefined value.
Streams that are in the "open" state, or either of the "half closed" states count toward the maximum number of streams that an endpoint is permitted to open.
631 632 633 634 635 |
# File 'lib/http/2/stream.rb', line 631 def activate_stream_in_conn @connection.active_stream_count += 1 @activated = true emit(:active) end |
#calculate_content_length(data_length) ⇒ void
This method returns an undefined value.
189 190 191 192 193 194 195 196 |
# File 'lib/http/2/stream.rb', line 189 def calculate_content_length(data_length) return unless @_content_length && data_length @_content_length -= data_length return if @_content_length >= 0 stream_error(:protocol_error, msg: "received more data than what was defined in content-length") end |
#cancel ⇒ void
This method returns an undefined value.
Sends a RST_STREAM indicating that the stream is no longer needed.
293 294 295 |
# File 'lib/http/2/stream.rb', line 293 def cancel send(type: :rst_stream, error: :cancel) end |
#chunk_data(payload, max_size) {|arg0| ... } ⇒ String
Chunk data into max_size, yield each chunk, then return final chunk
274 275 276 277 278 279 280 281 282 |
# File 'lib/http/2/stream.rb', line 274 def chunk_data(payload, max_size) total = payload.bytesize cursor = 0 while (total - cursor) > max_size yield payload.byteslice(cursor, max_size) cursor += max_size end payload.byteslice(cursor, total - cursor) end |
#close(error) ⇒ void #close ⇒ void
Sends a RST_STREAM frame which closes current stream - this does not close the underlying connection.
288 289 290 |
# File 'lib/http/2/stream.rb', line 288 def close(error = :stream_closed) send(type: :rst_stream, error: error) end |
#close_stream_in_conn(*args) ⇒ void
This method returns an undefined value.
637 638 639 640 |
# File 'lib/http/2/stream.rb', line 637 def close_stream_in_conn(*args) @connection.active_stream_count -= 1 if @activated emit(:close, *args) end |
#closed? ⇒ Boolean
107 108 109 |
# File 'lib/http/2/stream.rb', line 107 def closed? @state == :closed end |
#complete_transition(frame) ⇒ void
This method returns an undefined value.
642 643 644 645 646 647 648 649 650 651 |
# File 'lib/http/2/stream.rb', line 642 def complete_transition(frame) case @state when :closing @state = :closed close_stream_in_conn(frame[:error]) when :half_closing @state = @closed emit(:half_close) end end |
#data(payload, end_stream: true) ⇒ void
This method returns an undefined value.
Sends DATA frame containing response payload.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/http/2/stream.rb', line 256 def data(payload, end_stream: true) # Split data according to each frame is smaller enough # TODO: consider padding? max_size = @connection.remote_settings[:settings_max_frame_size] if payload.bytesize > max_size payload = chunk_data(payload, max_size) do |chunk| send(type: :data, flags: 0, payload: chunk) end end flags = 0 flags |= END_STREAM if end_stream send(type: :data, flags: flags, payload: payload) end |
#end_stream?(frame) ⇒ Boolean
669 670 671 672 673 674 675 676 |
# File 'lib/http/2/stream.rb', line 669 def end_stream?(frame) case frame[:type] when :data, :headers, :continuation # @type var frame: data_frame | headers_frame | continuation_frame frame[:flags] && frame[:flags].anybits?(END_STREAM) else false end end |
#event(newstate) ⇒ void
This method returns an undefined value.
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/http/2/stream.rb', line 605 def event(newstate) case newstate when :open @state = newstate activate_stream_in_conn when :reserved_local, :reserved_remote @state = newstate emit(:reserved) when :half_closed_local, :half_closed_remote @closed = newstate activate_stream_in_conn unless @state == :open @state = :half_closing when :local_closed, :remote_closed, :local_rst, :remote_rst @closed = newstate @state = :closing end @state end |
#headers(headers, end_headers: true, end_stream: false) ⇒ void
This method returns an undefined value.
Sends a HEADERS frame containing HTTP response headers. All pseudo-header fields MUST appear in the header block before regular header fields.
228 229 230 231 232 233 |
# File 'lib/http/2/stream.rb', line 228 def headers(headers, end_headers: true, end_stream: false) flags = end_headers ? END_HEADERS : 0 flags |= END_STREAM if end_stream || @_method == "HEAD" send(type: :headers, flags: flags, payload: headers) end |
#manage_state(frame) { ... } ⇒ void
This method returns an undefined value.
688 689 690 691 692 693 |
# File 'lib/http/2/stream.rb', line 688 def manage_state(frame) transition(frame, true) frame[:stream] ||= @id yield complete_transition(frame) end |
#process_priority(frame) ⇒ void
This method returns an undefined value.
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/http/2/stream.rb', line 653 def process_priority(frame) @weight = frame[:weight] @dependency = frame[:dependency] emit( :priority, weight: frame[:weight], dependency: frame[:dependency], exclusive: frame[:exclusive] ) # TODO: implement dependency tree housekeeping # Latest draft defines a fairly complex priority control. # See https://tools.ietf.org/html/draft-ietf-httpbis-http2-16#section-5.3 # We currently have no prioritization among streams. # We should add code here. end |
#promise(headers, end_headers: true) {|arg0| ... } ⇒ void
This method returns an undefined value.
235 236 237 238 239 240 |
# File 'lib/http/2/stream.rb', line 235 def promise(headers, end_headers: true, &block) raise ArgumentError, "must provide callback" unless block flags = end_headers ? END_HEADERS : 0 emit(:promise, self, headers, flags, &block) end |
#receive(frame) ⇒ void Also known as: <<
This method returns an undefined value.
Processes incoming HTTP 2.0 frames. The frames must be decoded upstream.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/http/2/stream.rb', line 114 def receive(frame) transition(frame, false) frame_type = frame[:type] #: Symbol case frame_type when :data # @type var frame: data_frame # 6.1. DATA # If a DATA frame is received whose stream is not in "open" or # "half closed (local)" state, the recipient MUST respond with a # stream error (Section 5.4.2) of type STREAM_CLOSED. stream_error(:stream_closed) unless STREAM_OPEN_STATES.include?(@state) || (@state == :closed && @closed == :local_rst) @received_data = true calculate_content_length(frame[:length]) update_local_window(frame) # Emit DATA frame emit(:data, frame[:payload]) unless frame[:ignore] calculate_window_update(@local_window_max_size) when :headers # @type var frame: headers_frame stream_error(:stream_closed) if (@state == :closed && @closed != :local_rst) || @state == :remote_closed @_method ||= frame[:method] @_status_code ||= frame[:status] @_content_length ||= frame[:content_length] @_trailers ||= frame[:trailer] if @_waiting_on_trailers || (@received_data && (!@_status_code || @_status_code >= 200)) # An endpoint that receives a HEADERS frame without the END_STREAM flag set after receiving a final # (non-informational) status code MUST treat the corresponding request or response as malformed. verify_trailers(frame) end emit(:headers, frame[:payload]) unless frame[:ignore] @_waiting_on_trailers = !@_trailers.nil? when :push_promise emit(:promise_headers, frame[:payload]) unless frame[:ignore] when :continuation stream_error(:stream_closed) if (@state == :closed && @closed != :local_rst) || @state == :remote_closed stream_error(:protocol_error) if @received_data when :priority # @type var frame: priority_frame process_priority(frame) when :window_update # @type var frame: window_update_frame process_window_update(frame: frame) when :altsvc # @type var frame: origin_frame # 4. The ALTSVC HTTP/2 Frame # An ALTSVC frame on a # stream other than stream 0 containing non-empty "Origin" information # is invalid and MUST be ignored. emit(frame_type, frame) if !frame[:origin] || frame[:origin].empty? end complete_transition(frame) end |
#refuse ⇒ void
This method returns an undefined value.
Sends a RST_STREAM indicating that the stream has been refused prior to performing any application processing.
299 300 301 |
# File 'lib/http/2/stream.rb', line 299 def refuse send(type: :rst_stream, error: :refused_stream) end |
#reprioritize(weight: 16, dependency: 0, exclusive: false) ⇒ void
This method returns an undefined value.
Sends a PRIORITY frame with new stream priority value (can only be performed by the client).
247 248 249 250 |
# File 'lib/http/2/stream.rb', line 247 def reprioritize(weight: 16, dependency: 0, exclusive: false) stream_error if @id.even? send(type: :priority, weight: weight, dependency: dependency, exclusive: exclusive) end |
#send(frame) ⇒ void
This method returns an undefined value.
Processes outgoing HTTP 2.0 frames. Data frames may be automatically split and buffered based on maximum frame size and current stream flow control window size.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/http/2/stream.rb', line 203 def send(frame) case frame[:type] when :data # @type var frame: data_frame # stream state management is maintained in send_data return send_data(frame) when :window_update # @type var frame: window_update_frame @local_window += frame[:increment] when :priority # @type var frame: priority_frame process_priority(frame) end manage_state(frame) do emit(:frame, frame) end end |
#stream_error(error = :internal_error, msg: nil) ⇒ void Also known as: error
This method returns an undefined value.
678 679 680 681 682 683 684 685 |
# File 'lib/http/2/stream.rb', line 678 def stream_error(error = :internal_error, msg: nil) # if the stream already broke with an error, ignore subsequent @error = error close(error) if @state != :closed raise Error.types[error], msg end |
#transition(frame, sending) ⇒ void
This method returns an undefined value.
HTTP 2.0 Stream States
-
http://tools.ietf.org/html/draft-ietf-httpbis-http2-16#section-5.1
+--------+ send PP | | recv PP ,--------| idle |--------. / | | \ v +--------+ v +----------+ | +----------+ | | | send H/ | |
,-----| reserved | | recv H | reserved |-----.
| | (local) | | | (remote) | |
| ---------- v ---------- |
| | -------- | |
| | recv ES | | send ES | |
| send H | ,-------| open |-------. | recv H |
| | / | | \ | |
| v v -------- v v |
| ---------- | ---------- |
| | half | | | half | |
| | closed | | send R/ | closed | |
| | (remote) | | recv R | (local) | |
| ---------- | ---------- |
| | | | |
| | send ES/ | recv ES/ | |
| | send R/ v send R/ | |
| | recv R -------- recv R | |
| send R/ ----------->| |<-----------' send R/ | | recv R | closed | recv R | ---------------------->| |<----------------------'
--------
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/http/2/stream.rb', line 347 def transition(frame, sending) case @state # All streams start in the "idle" state. In this state, no frames # have been exchanged. # The following transitions are valid from this state: # * Sending or receiving a HEADERS frame causes the stream to # become "open". The stream identifier is selected as described # in Section 5.1.1. The same HEADERS frame can also cause a # stream to immediately become "half closed". # * Sending a PUSH_PROMISE frame reserves an idle stream for later # use. The stream state for the reserved stream transitions to # "reserved (local)". # * Receiving a PUSH_PROMISE frame reserves an idle stream for # later use. The stream state for the reserved stream # transitions to "reserved (remote)". # Receiving any frames other than HEADERS, PUSH_PROMISE or PRIORITY # on a stream in this state MUST be treated as a connection error # (Section 5.4.1) of type PROTOCOL_ERROR. when :idle if sending case frame[:type] when :push_promise then event(:reserved_local) when :headers if end_stream?(frame) event(:half_closed_local) else event(:open) end when :rst_stream then event(:local_rst) when :priority then process_priority(frame) else stream_error end else case frame[:type] when :push_promise then event(:reserved_remote) when :headers if end_stream?(frame) event(:half_closed_remote) else event(:open) end when :priority else stream_error(:protocol_error) end end # A stream in the "reserved (local)" state is one that has been # promised by sending a PUSH_PROMISE frame. A PUSH_PROMISE frame # reserves an idle stream by associating the stream with an open # stream that was initiated by the remote peer (see Section 8.2). # In this state, only the following transitions are possible: # * The endpoint can send a HEADERS frame. This causes the stream # to open in a "half closed (remote)" state. # * Either endpoint can send a RST_STREAM frame to cause the stream # to become "closed". This releases the stream reservation. # An endpoint MUST NOT send any type of frame other than HEADERS, # RST_STREAM, or PRIORITY in this state. # A PRIORITY or WINDOW_UPDATE frame MAY be received in this state. # Receiving any type of frame other than RST_STREAM, PRIORITY or # WINDOW_UPDATE on a stream in this state MUST be treated as a # connection error (Section 5.4.1) of type PROTOCOL_ERROR. when :reserved_local if sending case frame[:type] when :headers then event(:half_closed_remote) when :rst_stream then event(:local_rst) when :priority else stream_error end else case frame[:type] when :rst_stream then event(:remote_rst) when :priority, :window_update else stream_error end end # A stream in the "reserved (remote)" state has been reserved by a # remote peer. # In this state, only the following transitions are possible: # * Receiving a HEADERS frame causes the stream to transition to # "half closed (local)". # * Either endpoint can send a RST_STREAM frame to cause the stream # to become "closed". This releases the stream reservation. # An endpoint MAY send a PRIORITY frame in this state to # reprioritize the reserved stream. An endpoint MUST NOT send any # type of frame other than RST_STREAM, WINDOW_UPDATE, or PRIORITY in # this state. # Receiving any type of frame other than HEADERS, RST_STREAM or # PRIORITY on a stream in this state MUST be treated as a connection # error (Section 5.4.1) of type PROTOCOL_ERROR. when :reserved_remote if sending case frame[:type] when :rst_stream then event(:local_rst) when :priority, :window_update else stream_error end else case frame[:type] when :headers then event(:half_closed_local) when :rst_stream then event(:remote_rst) when :priority else stream_error end end # A stream in the "open" state may be used by both peers to send # frames of any type. In this state, sending peers observe # advertised stream level flow control limits (Section 5.2). # From this state either endpoint can send a frame with an # END_STREAM flag set, which causes the stream to transition into # one of the "half closed" states: an endpoint sending an END_STREAM # flag causes the stream state to become "half closed (local)"; an # endpoint receiving an END_STREAM flag causes the stream state to # become "half closed (remote)". # Either endpoint can send a RST_STREAM frame from this state, # causing it to transition immediately to "closed". when :open if sending case frame[:type] when :data, :headers, :continuation event(:half_closed_local) if end_stream?(frame) when :rst_stream then event(:local_rst) when :priority end else case frame[:type] when :data, :headers, :continuation event(:half_closed_remote) if end_stream?(frame) when :rst_stream then event(:remote_rst) when :priority end end # A stream that is in the "half closed (local)" state cannot be used # for sending frames. Only WINDOW_UPDATE, PRIORITY and RST_STREAM # frames can be sent in this state. # A stream transitions from this state to "closed" when a frame that # contains an END_STREAM flag is received, or when either peer sends # a RST_STREAM frame. # A receiver can ignore WINDOW_UPDATE frames in this state, which # might arrive for a short period after a frame bearing the # END_STREAM flag is sent. # PRIORITY frames received in this state are used to reprioritize # streams that depend on the current stream. when :half_closed_local if sending case frame[:type] when :rst_stream event(:local_rst) when :priority, :window_update else stream_error end else case frame[:type] when :data, :headers, :continuation event(:remote_closed) if end_stream?(frame) when :rst_stream then event(:remote_rst) when :priority, :window_update end end # A stream that is "half closed (remote)" is no longer being used by # the peer to send frames. In this state, an endpoint is no longer # obligated to maintain a receiver flow control window if it # performs flow control. # If an endpoint receives additional frames for a stream that is in # this state, other than WINDOW_UPDATE, PRIORITY or RST_STREAM, it # MUST respond with a stream error (Section 5.4.2) of type # STREAM_CLOSED. # A stream that is "half closed (remote)" can be used by the # endpoint to send frames of any type. In this state, the endpoint # continues to observe advertised stream level flow control limits # (Section 5.2). # A stream can transition from this state to "closed" by sending a # frame that contains an END_STREAM flag, or when either peer sends # a RST_STREAM frame. when :half_closed_remote if sending case frame[:type] when :data, :headers, :continuation event(:local_closed) if end_stream?(frame) when :rst_stream then event(:local_rst) end else case frame[:type] when :rst_stream then event(:remote_rst) when :priority, :window_update # nop else stream_error(:stream_closed) end end # The "closed" state is the terminal state. # An endpoint MUST NOT send frames other than PRIORITY on a closed # stream. An endpoint that receives any frame other than PRIORITY # after receiving a RST_STREAM MUST treat that as a stream error # (Section 5.4.2) of type STREAM_CLOSED. Similarly, an endpoint # that receives any frames after receiving a frame with the # END_STREAM flag set MUST treat that as a connection error # (Section 5.4.1) of type STREAM_CLOSED, unless the frame is # permitted as described below. # WINDOW_UPDATE or RST_STREAM frames can be received in this state # for a short period after a DATA or HEADERS frame containing an # END_STREAM flag is sent. Until the remote peer receives and # processes RST_STREAM or the frame bearing the END_STREAM flag, it # might send frames of these types. Endpoints MUST ignore # WINDOW_UPDATE or RST_STREAM frames received in this state, though # endpoints MAY choose to treat frames that arrive a significant # time after sending END_STREAM as a connection error # (Section 5.4.1) of type PROTOCOL_ERROR. # PRIORITY frames can be sent on closed streams to prioritize # streams that are dependent on the closed stream. Endpoints SHOULD # process PRIORITY frames, though they can be ignored if the stream # has been removed from the dependency tree (see Section 5.3.4). # If this state is reached as a result of sending a RST_STREAM # frame, the peer that receives the RST_STREAM might have already # sent - or enqueued for sending - frames on the stream that cannot # be withdrawn. An endpoint MUST ignore frames that it receives on # closed streams after it has sent a RST_STREAM frame. An endpoint # MAY choose to limit the period over which it ignores frames and # treat frames that arrive after this time as being in error. # Flow controlled frames (i.e., DATA) received after sending # RST_STREAM are counted toward the connection flow control window. # Even though these frames might be ignored, because they are sent # before the sender receives the RST_STREAM, the sender will # consider the frames to count against the flow control window. # An endpoint might receive a PUSH_PROMISE frame after it sends # RST_STREAM. PUSH_PROMISE causes a stream to become "reserved" # even if the associated stream has been reset. Therefore, a # RST_STREAM is needed to close an unwanted promised stream. when :closed if sending case frame[:type] when :rst_stream, :priority else stream_error(:stream_closed) unless frame[:type] == :rst_stream end else case @closed when :remote_rst, :remote_closed case frame[:type] when :priority, :rst_stream, :window_update # nop here else stream_error(:stream_closed) end when :local_rst, :local_closed frame[:ignore] = true if frame[:type] != :window_update end end end end |
#verify_trailers(frame) ⇒ void
This method returns an undefined value.
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/http/2/stream.rb', line 175 def verify_trailers(frame) stream_error(:protocol_error, msg: "trailer headers frame must close the stream") unless end_stream?(frame) return unless @_trailers trailers = frame[:payload] return unless trailers.respond_to?(:each) trailers.each do |field, _| # rubocop:disable Style/HashEachMethods @_trailers.delete(field) break if @_trailers.empty? end stream_error(:protocol_error, msg: "didn't receive all expected trailer headers") unless @_trailers.empty? end |
#window_update(increment) ⇒ void
This method returns an undefined value.
Sends a WINDOW_UPDATE frame to the peer.
306 307 308 309 310 311 |
# File 'lib/http/2/stream.rb', line 306 def window_update(increment) # emit stream-level WINDOW_UPDATE unless stream is closed return if @state == :closed || @state == :remote_closed send(type: :window_update, increment: increment) end |