Class: H2::Server::PushPromise

Inherits:
Object
  • Object
show all
Defined in:
lib/h2/server/push_promise.rb

Defined Under Namespace

Classes: FSM

Constant Summary collapse

GET =
'GET'
STATUS =
'200'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, headers: {}, body: nil) ⇒ PushPromise

build a new PushPromise for the path, with the headers and body given



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/h2/server/push_promise.rb', line 12

def initialize path:, headers: {}, body: nil
  @path = path
  @body = body

  @promise_headers = {
    METHOD_KEY    => GET,
    AUTHORITY_KEY => headers.delete(AUTHORITY_KEY),
    PATH_KEY      => @path,
    SCHEME_KEY    => headers.delete(SCHEME_KEY)
  }

  @content_length = @body.bytesize.to_s

  @push_headers = {
    STATUS_KEY         => STATUS,
    CONTENT_LENGTH_KEY => @content_length
  }.merge headers

  @fsm = FSM.new
end

Instance Attribute Details

#content_lengthObject (readonly)

Returns the value of attribute content_length.



8
9
10
# File 'lib/h2/server/push_promise.rb', line 8

def content_length
  @content_length
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/h2/server/push_promise.rb', line 8

def path
  @path
end

#push_streamObject (readonly)

Returns the value of attribute push_stream.



8
9
10
# File 'lib/h2/server/push_promise.rb', line 8

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…)



91
92
93
94
# File 'lib/h2/server/push_promise.rb', line 91

def cancel!
  @fsm.transition :canceled
  @stream.on_complete
end

#canceled?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/h2/server/push_promise.rb', line 84

def canceled?
  @fsm.state == :canceled
end

#keep(size = nil) ⇒ Object

deliver the body for this promise, optionally splicing into size chunks



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/h2/server/push_promise.rb', line 54

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_asyncObject



48
49
50
# File 'lib/h2/server/push_promise.rb', line 48

def keep_async
  @stream.connection.server.async.handle_push_promise self
end

#kept?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/h2/server/push_promise.rb', line 80

def kept?
  @fsm.state == :kept
end

#log(level, msg) ⇒ Object



96
97
98
# File 'lib/h2/server/push_promise.rb', line 96

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/h2/server/push_promise.rb', line 36

def make_on stream
  return unless @fsm.state == :init
  @stream = stream
  @stream.stream.promise(@promise_headers, end_headers: false) do |push|
    push.headers @push_headers
    @push_stream = push
    @push_stream.on(:close){|err| cancel! if err == :cancel}
  end
  @fsm.transition :made
  self
end

#to_sObject Also known as: to_str



100
101
102
103
# File 'lib/h2/server/push_promise.rb', line 100

def to_s
  request = @stream.request
  %{#{request.addr} "push #{@path} HTTP/2" #{STATUS} #{@content_length}}
end