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, body_or_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
32
33
34
35
36
37
# File 'lib/h2/server/push_promise.rb', line 12

def initialize path, body_or_headers = {}, body = nil
  @path = path
  if Hash === body_or_headers
    headers = body_or_headers.dup
    @body = body
  else
    headers = {}
    @body = body_or_headers
  end

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



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

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

#canceled?Boolean

Returns:

  • (Boolean)


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

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

#keep(size = nil) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/h2/server/push_promise.rb', line 60

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



54
55
56
# File 'lib/h2/server/push_promise.rb', line 54

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

#kept?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/h2/server/push_promise.rb', line 86

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

#log(level, msg) ⇒ Object



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

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/h2/server/push_promise.rb', line 42

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



106
107
108
109
# File 'lib/h2/server/push_promise.rb', line 106

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