Class: Async::HTTP::Protocol::HTTP2::Response::Stream

Inherits:
Stream
  • Object
show all
Defined in:
lib/async/http/protocol/http2/response.rb

Instance Attribute Summary collapse

Attributes inherited from Stream

#headers, #input

Instance Method Summary collapse

Methods inherited from Stream

#add_header, #add_trailer, #finish_output, #prepare_input, #process_data, #process_headers, #receive_trailing_headers, #send_body, #update_local_window, #window_updated

Constructor Details

#initializeStream

Returns a new instance of Stream.



33
34
35
36
37
38
39
40
# File 'lib/async/http/protocol/http2/response.rb', line 33

def initialize(*)
  super
  
  @response = Response.new(self)
  
  @notification = Async::Notification.new
  @exception = nil
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



42
43
44
# File 'lib/async/http/protocol/http2/response.rb', line 42

def response
  @response
end

Instance Method Details

#accept_push_promise_stream(promised_stream_id, headers) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/async/http/protocol/http2/response.rb', line 52

def accept_push_promise_stream(promised_stream_id, headers)
  stream = @connection.accept_push_promise_stream(promised_stream_id, &Stream.method(:create))
  
  stream.response.build_request(headers)
  
  @response.promises.enqueue(stream.response)
  
  return stream
end

#closed(error) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/async/http/protocol/http2/response.rb', line 112

def closed(error)
  super
  
  if @response
    @response.promises.enqueue nil
    @response = nil
  end
  
  @exception = error
  
  notify!
end

#notify!Object

Notify anyone waiting on the response headers to be received (or failure).



95
96
97
98
99
100
# File 'lib/async/http/protocol/http2/response.rb', line 95

def notify!
  if notification = @notification
    @notification = nil
    notification.signal
  end
end

#receive_initial_headers(headers, end_stream) ⇒ Object

This should be invoked from the background reader, and notifies the task waiting for the headers that we are done.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/async/http/protocol/http2/response.rb', line 63

def receive_initial_headers(headers, end_stream)
  headers.each do |key, value|
    if key == STATUS
      @response.status = Integer(value)
    elsif key == PROTOCOL
      @response.protocol = value
    elsif key == CONTENT_LENGTH
      @length = Integer(value)
    else
      add_header(key, value)
    end
  end
  
  @response.headers = @headers
  
  if @response.valid?
    if !end_stream
      # We only construct the input/body if data is coming.
      @response.body = prepare_input(@length)
    elsif @response.head?
      @response.body = ::Protocol::HTTP::Body::Head.new(@length)
    end
  else
    send_reset_stream(::Protocol::HTTP2::Error::PROTOCOL_ERROR)
  end
  
  self.notify!
  
  return headers
end

#waitObject

Wait for the headers to be received or for stream reset.



103
104
105
106
107
108
109
110
# File 'lib/async/http/protocol/http2/response.rb', line 103

def wait
  # If you call wait after the headers were already received, it should return immediately:
  @notification&.wait
  
  if @exception
    raise @exception
  end
end

#wait_for_inputObject



44
45
46
47
48
49
50
# File 'lib/async/http/protocol/http2/response.rb', line 44

def wait_for_input
  # The input isn't ready until the response headers have been received:
  @response.wait
  
  # There is a possible race condition if you try to access @input - it might already be closed and nil.
  return @response.body
end