Class: H2::Server::Stream::PushPromise
- Inherits:
-
Object
- Object
- H2::Server::Stream::PushPromise
- Includes:
- HeaderStringifier, ContentEncoder
- Defined in:
- lib/h2/server/stream/push_promise.rb
Defined Under Namespace
Classes: FSM
Constant Summary collapse
- GET =
'GET'
- STATUS =
'200'
Instance Attribute Summary collapse
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#push_stream ⇒ Object
readonly
Returns the value of attribute push_stream.
Instance Method Summary collapse
-
#cancel! ⇒ Object
cancel this promise, most likely due to a RST_STREAM frame from the client (already in cache, etc…).
- #canceled? ⇒ Boolean
-
#initialize(path:, headers: {}, body: nil) ⇒ PushPromise
constructor
build a new
PushPromise
for the path, with the headers and body given. -
#keep(size = nil) ⇒ Object
deliver the body for this promise, optionally splicing into
size
chunks. - #keep_async ⇒ Object
- #kept? ⇒ Boolean
- #log(level, msg) ⇒ Object
-
#make_on(stream) ⇒ Object
create a new promise stream from
stream
, send the headers and set @push_stream from the callback. - #to_s ⇒ Object (also: #to_str)
Methods included from ContentEncoder
Constructor Details
#initialize(path:, headers: {}, body: nil) ⇒ PushPromise
build a new PushPromise
for the path, with the headers and body given
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/h2/server/stream/push_promise.rb', line 15 def initialize path:, headers: {}, body: nil @path = path @body = body @headers = headers @promise_headers = { METHOD_KEY => GET, AUTHORITY_KEY => headers.delete(AUTHORITY_KEY), PATH_KEY => @path, SCHEME_KEY => headers.delete(SCHEME_KEY) } @fsm = FSM.new end |
Instance Attribute Details
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
11 12 13 |
# File 'lib/h2/server/stream/push_promise.rb', line 11 def content_length @content_length end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/h2/server/stream/push_promise.rb', line 11 def path @path end |
#push_stream ⇒ Object (readonly)
Returns the value of attribute push_stream.
11 12 13 |
# File 'lib/h2/server/stream/push_promise.rb', line 11 def push_stream @push_stream end |
Instance Method Details
#cancel! ⇒ Object
cancel this promise, most likely due to a RST_STREAM frame from the client (already in cache, etc…)
94 95 96 97 |
# File 'lib/h2/server/stream/push_promise.rb', line 94 def cancel! @fsm.transition :canceled @stream.on_complete end |
#canceled? ⇒ Boolean
87 88 89 |
# File 'lib/h2/server/stream/push_promise.rb', line 87 def canceled? @fsm.state == :canceled end |
#keep(size = nil) ⇒ Object
deliver the body for this promise, optionally splicing into size
chunks
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/h2/server/stream/push_promise.rb', line 57 def keep size = nil return false unless @fsm.state == :made if size.nil? @push_stream.data @body else body = @body if body.bytesize > size body = @body.dup while body.bytesize > size @push_stream.data body.slice!(0, size), end_stream: false yield if block_given? end else yield if block_given? end @push_stream.data body end @fsm.transition :kept log :info, self @stream.on_complete end |
#keep_async ⇒ Object
51 52 53 |
# File 'lib/h2/server/stream/push_promise.rb', line 51 def keep_async @stream.connection.server.async.handle_push_promise self end |
#kept? ⇒ Boolean
83 84 85 |
# File 'lib/h2/server/stream/push_promise.rb', line 83 def kept? @fsm.state == :kept end |
#log(level, msg) ⇒ Object
99 100 101 |
# File 'lib/h2/server/stream/push_promise.rb', line 99 def log level, msg Logger.__send__ level, "[stream #{@push_stream.id}] #{msg}" end |
#make_on(stream) ⇒ Object
create a new promise stream from stream
, send the headers and set @push_stream from the callback
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/h2/server/stream/push_promise.rb', line 33 def make_on stream return unless @fsm.state == :init @stream = stream @headers = {STATUS_KEY => STATUS}.merge stringify_headers(@headers) check_accept_encoding @content_length = @body.bytesize.to_s @headers.merge! CONTENT_LENGTH_KEY => @content_length @stream.stream.promise(@promise_headers, end_headers: false) do |push| push.headers @headers @push_stream = push @push_stream.on(:close){|err| cancel! if err == :cancel} end @fsm.transition :made self end |
#to_s ⇒ Object Also known as: to_str
103 104 105 106 |
# File 'lib/h2/server/stream/push_promise.rb', line 103 def to_s request = @stream.request %{#{request.addr} "push #{@path} HTTP/2" #{STATUS} #{@content_length}} end |