Class: Raptor::Http2::ResponseStream
- Inherits:
-
Object
- Object
- Raptor::Http2::ResponseStream
- Defined in:
- lib/raptor/http2.rb,
sig/generated/raptor/http2.rbs
Overview
Adapts one HTTP/2 response stream to Rack's streaming body interface.
Instance Attribute Summary collapse
-
#bytes_written ⇒ Integer
readonly
Returns the number of response body bytes written to the stream.
Instance Method Summary collapse
-
#<<(chunk) ⇒ ResponseStream
Writes response body bytes and returns the stream.
-
#close ⇒ void
Finishes the stream.
-
#close_read ⇒ bot
Closes the unsupported read side of the stream.
-
#close_write ⇒ void
Finishes the writable side of the stream.
-
#closed? ⇒ Boolean
Returns whether the stream has finished.
-
#flush ⇒ ResponseStream
Flushes the stream.
-
#initialize(socket, writer, flow_control, stream_id, trailers) ⇒ ResponseStream
constructor
Creates a response stream for a Rack streaming body.
-
#read ⇒ bot
Rejects reads; streaming responses are write-only.
-
#write(chunk) ⇒ Integer
Writes response body bytes to the stream.
Constructor Details
#initialize(socket, writer, flow_control, stream_id, trailers) ⇒ ResponseStream
Creates a response stream for a Rack streaming body.
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/raptor/http2.rb', line 340 def initialize(socket, writer, flow_control, stream_id, trailers) @socket = socket @writer = writer @flow_control = flow_control @stream_id = stream_id @trailers = trailers @parser = Http2Parser.new @bytes_written = 0 @closed = false end |
Instance Attribute Details
#bytes_written ⇒ Integer (readonly)
Returns the number of response body bytes written to the stream.
328 329 330 |
# File 'lib/raptor/http2.rb', line 328 def bytes_written @bytes_written end |
Instance Method Details
#<<(chunk) ⇒ ResponseStream
Writes response body bytes and returns the stream.
391 392 393 394 |
# File 'lib/raptor/http2.rb', line 391 def <<(chunk) write(chunk) self end |
#close ⇒ void
This method returns an undefined value.
Finishes the stream.
412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/raptor/http2.rb', line 412 def close return if @closed @flow_control.check(@stream_id) @closed = true if @trailers.empty? frame = @parser.build_frame(:data, FLAG_END_STREAM, @stream_id, nil) else encoded = @parser.encode_response_trailers(@trailers) frame = @parser.build_frame(:headers, FLAG_END_STREAM | FLAG_END_HEADERS, @stream_id, encoded) end @writer.write_frames(@socket, [frame]) end |
#close_read ⇒ bot
Closes the unsupported read side of the stream.
431 432 433 |
# File 'lib/raptor/http2.rb', line 431 def close_read raise IOError, "closing non-duplex IO for reading" end |
#close_write ⇒ void
This method returns an undefined value.
Finishes the writable side of the stream.
440 441 442 |
# File 'lib/raptor/http2.rb', line 440 def close_write close end |
#closed? ⇒ Boolean
Returns whether the stream has finished.
449 450 451 |
# File 'lib/raptor/http2.rb', line 449 def closed? @closed end |
#flush ⇒ ResponseStream
Flushes the stream. Frames are already handed to the writer as they are produced.
402 403 404 |
# File 'lib/raptor/http2.rb', line 402 def flush self end |
#read ⇒ bot
Rejects reads; streaming responses are write-only.
356 357 358 |
# File 'lib/raptor/http2.rb', line 356 def read(*) raise IOError, "not opened for reading" end |
#write(chunk) ⇒ Integer
Writes response body bytes to the stream.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/raptor/http2.rb', line 369 def write(chunk) raise IOError, "closed stream" if @closed raise TypeError, "body must yield String values" unless chunk.is_a?(String) offset = 0 while offset < chunk.bytesize granted = @flow_control.acquire(@stream_id, chunk.bytesize - offset) slice = offset.zero? && granted == chunk.bytesize ? chunk : chunk.byteslice(offset, granted) offset += granted @writer.write_frames(@socket, [@parser.build_frame(:data, 0, @stream_id, slice)]) end @bytes_written += chunk.bytesize chunk.bytesize end |