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

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

Direct Known Subclasses

Request::Stream, Response::Stream

Defined Under Namespace

Classes: Buffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStream

Returns a new instance of Stream.



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

def initialize(*)
	super
	
	@headers = nil
	@trailers = nil
	
	# Input buffer (receive_data):
	@length = nil
	@input = nil
	
	# Output buffer (window_updated):
	@output = nil
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



124
125
126
# File 'lib/async/http/protocol/http2/stream.rb', line 124

def headers
  @headers
end

Instance Method Details

#add_header(key, value) ⇒ Object



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

def add_header(key, value)
	if key == CONNECTION
		raise ::Protocol::HTTP2::HeaderError, "Connection header is not allowed!"
	elsif key.start_with? ':'
		raise ::Protocol::HTTP2::HeaderError, "Invalid pseudo-header #{key}!"
	elsif key =~ /[A-Z]/
		raise ::Protocol::HTTP2::HeaderError, "Invalid upper-case characters in header #{key}!"
	else
		@headers.add(key, value)
	end
end

#add_trailer(key, value) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/async/http/protocol/http2/stream.rb', line 138

def add_trailer(key, value)
	if @trailers.include(key)
		add_header(key, value)
	else
		raise ::Protocol::HTTP2::HeaderError, "Cannot add trailer #{key} as it was not specified in trailers!"
	end
end

#close(error = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/async/http/protocol/http2/stream.rb', line 200

def close(error = nil)
	super
	
	if @input
		@input.close(error)
		@input = nil
	end
	
	if @output
		@output.close(error)
		@output = nil
	end
end

#process_data(frame) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/async/http/protocol/http2/stream.rb', line 168

def process_data(frame)
	data = frame.unpack
	
	if @input
		unless data.empty?
			@input.write(data)
		end
		
		if frame.end_stream?
			@input.close
			@input = nil
		end
	end
	
	return data
rescue ::Protocol::HTTP2::ProtocolError
	raise
rescue # Anything else...
	send_reset_stream(::Protocol::HTTP2::Error::INTERNAL_ERROR)
end

#receive_headers(frame) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/async/http/protocol/http2/stream.rb', line 152

def receive_headers(frame)
	if @headers.nil?
		@headers = ::Protocol::HTTP::Headers.new
		self.receive_initial_headers(super, frame.end_stream?)
		@trailers = @headers[TRAILERS]
	elsif @trailers and frame.end_stream?
		self.receive_trailing_headers(super, frame.end_stream?)
	else
		raise ::Protocol::HTTP2::HeaderError, "Unable to process headers!"
	end
rescue ::Protocol::HTTP2::HeaderError => error
	Async.logger.error(self, error)
	
	send_reset_stream(error.code)
end

#receive_trailing_headers(headers, end_stream) ⇒ Object



146
147
148
149
150
# File 'lib/async/http/protocol/http2/stream.rb', line 146

def receive_trailing_headers(headers, end_stream)
	headers.each do |key, value|
		add_trailer(key, value)
	end
end

#send_body(body) ⇒ Object

Set the body and begin sending it.



190
191
192
# File 'lib/async/http/protocol/http2/stream.rb', line 190

def send_body(body)
	@output = Buffer.new(self, body)
end

#window_updated(size) ⇒ Object



194
195
196
197
198
# File 'lib/async/http/protocol/http2/stream.rb', line 194

def window_updated(size)
	super
	
	@output&.window_updated(size)
end