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

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

Overview

Represents the HTTP/2 stream associated with an outgoing client-side response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStream

Initialize the response stream.



20
21
22
23
24
25
26
# File 'lib/async/http/protocol/http2/response.rb', line 20

def initialize(*)
	super
	
	@response = Response.new(self)
	
	@ready = Async::Promise.new
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



28
29
30
# File 'lib/async/http/protocol/http2/response.rb', line 28

def response
  @response
end

Instance Method Details

#accept_push_promise_stream(promised_stream_id, headers) ⇒ Object

Handle a push promise stream from the server.

Raises:

  • (ProtocolError)


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

def accept_push_promise_stream(promised_stream_id, headers)
	raise ProtocolError, "Cannot accept push promise stream!"
end

#closed(error) ⇒ Object

Called when the stream is closed.



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/async/http/protocol/http2/response.rb', line 125

def closed(error)
	super
	
	if @response
		@response = nil
	end
	
	unless @ready.resolved?
		error ||= EOFError.new("Stream closed before response headers were received!")
		@ready.reject(error)
	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.



50
51
52
53
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/async/http/protocol/http2/response.rb', line 50

def receive_initial_headers(headers, end_stream)
	# While in theory, the response pseudo-headers may be extended in the future, currently they only response pseudo-header is :status, so we can assume it is always the first header.
	status_header = headers.shift
	
	if status_header.first != ":status"
		raise ProtocolError, "Invalid response headers: #{headers.inspect}"
	end
	
	status = Integer(status_header.last)
	
	if status >= 100 && status < 200
		return receive_interim_headers(status, headers)
	end
	
	@response.status = status
	@headers = ::Protocol::HTTP::Headers.new
	
	# If the protocol request was successful, ensure the response protocol matches:
	if status == 200 and protocol = @response.request.protocol
		@response.protocol = Array(protocol).first
	end
	
	headers.each do |key, value|
		# It's guaranteed that this should be the first header:
		if 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
	
	@ready.resolve(nil)
	
	return headers
end

#receive_interim_headers(status, headers) ⇒ Object

Process interim (1xx) response headers.



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

def receive_interim_headers(status, headers)
	if headers.any?
		headers = ::Protocol::HTTP::Headers[headers]
	else
		headers = nil
	end
	
	@response.request.send_interim_response(status, headers)
end

#waitObject

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



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

def wait
	@ready.wait
rescue ::Protocol::HTTP2::StreamError => error
	if error.code == ::Protocol::HTTP2::Error::INTERNAL_ERROR
		raise ::Protocol::HTTP::RemoteError, error.message
	end
	
	raise
end

#wait_for_inputObject

Wait for the response headers and return the response body.



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

def wait_for_input
	response = @response
	
	# 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