Debian Codegraph @

lib/Protocol/HTTP2/Stream.pm

raw
1 package Protocol::HTTP2::Stream;
2 use strict;
3 use warnings;
4 use Protocol::HTTP2::Constants qw(:states :endpoints :settings :frame_types
5 :limits :errors);
6 use Protocol::HTTP2::HeaderCompression qw( headers_decode );
7 use Protocol::HTTP2::Trace qw(tracer);
8
9 # Streams related part of Protocol::HTTP2::Conntection
10
11 # Autogen properties
12 {
13 no strict 'refs';
14 for my $prop (
15 qw(promised_sid headers pp_headers header_block trailer
16 trailer_headers length blocked_data weight end reset)
17 )
18 {
19 *{ __PACKAGE__ . '::stream_' . $prop } = sub {
20 return
21 !exists $_[0]->{streams}->{ $_[1] } ? undef
22 : @_ == 2 ? $_[0]->{streams}->{ $_[1] }->{$prop}
23 : ( $_[0]->{streams}->{ $_[1] }->{$prop} = $_[2] );
24 }
25 }
26 }
27
28 sub new_stream {
29 my $self = shift;
30 return undef if $self->goaway;
31
32 $self->{last_stream} += 2
33 if exists $self->{streams}->{ $self->{type} == CLIENT ? 1 : 2 };
34 $self->{streams}->{ $self->{last_stream} } = {
35 'state' => IDLE,
36 'weight' => DEFAULT_WEIGHT,
37 'stream_dep' => 0,
38 'fcw_recv' => $self->dec_setting(SETTINGS_INITIAL_WINDOW_SIZE),
39 'fcw_send' => $self->enc_setting(SETTINGS_INITIAL_WINDOW_SIZE),
40 };
41 return $self->{last_stream};
42 }
43
44 sub new_peer_stream {
45 my $self = shift;
46 my $stream_id = shift;
47 if ( $stream_id < $self->{last_peer_stream}
48 || ( $stream_id % 2 ) == ( $self->{type} == CLIENT ) ? 1 : 0
49 || $self->goaway )
50 {
51 tracer->error("Peer send invalid stream id: $stream_id\n");
52 $self->error(PROTOCOL_ERROR);
53 return undef;
54 }
55 $self->{last_peer_stream} = $stream_id;
56 if ( $self->dec_setting(SETTINGS_MAX_CONCURRENT_STREAMS) <=
57 $self->{active_peer_streams} )
58 {
59 tracer->warning("SETTINGS_MAX_CONCURRENT_STREAMS exceeded\n");
60 $self->stream_error( $stream_id, REFUSED_STREAM );
61 return undef;
62 }
63 $self->{active_peer_streams}++;
64 tracer->debug("Active streams: $self->{active_peer_streams}");
65 $self->{streams}->{$stream_id} = {
66 'state' => IDLE,
67 'weight' => DEFAULT_WEIGHT,
68 'stream_dep' => 0,
69 'fcw_recv' => $self->dec_setting(SETTINGS_INITIAL_WINDOW_SIZE),
70 'fcw_send' => $self->enc_setting(SETTINGS_INITIAL_WINDOW_SIZE),
71 };
72 $self->{on_new_peer_stream}->($stream_id)
73 if exists $self->{on_new_peer_stream};
74
75 return $self->{last_peer_stream};
76 }
77
78 sub stream {
79 my ( $self, $stream_id ) = @_;
80 return undef unless exists $self->{streams}->{$stream_id};
81
82 $self->{streams}->{$stream_id};
83 }
84
85 # stream_state ( $self, $stream_id, $new_state?, $pending? )
86
87 sub stream_state {
88 my $self = shift;
89 my $stream_id = shift;
90 return undef unless exists $self->{streams}->{$stream_id};
91 my $s = $self->{streams}->{$stream_id};
92
93 if (@_) {
94 my ( $new_state, $pending ) = @_;
95
96 if ($pending) {
97 $self->stream_pending_state( $stream_id, $new_state );
98 }
99 else {
100 $self->{on_change_state}->( $stream_id, $s->{state}, $new_state )
101 if exists $self->{on_change_state};
102
103 $s->{state} = $new_state;
104
105 # Exec callbacks for new state
106 if ( exists $s->{cb} && exists $s->{cb}->{ $s->{state} } ) {
107 for my $cb ( @{ $s->{cb}->{ $s->{state} } } ) {
108 $cb->();
109 }
110 }
111
112 # Cleanup
113 if ( $new_state == CLOSED ) {
114 $self->{active_peer_streams}--
115 if $self->{active_peer_streams}
116 && ( ( $stream_id % 2 ) ^ ( $self->{type} == CLIENT ) );
117 tracer->info(
118 "Active streams: $self->{active_peer_streams} $stream_id");
119 for my $key ( keys %$s ) {
120 next if grep { $key eq $_ } (
121 qw(state weight stream_dep
122 fcw_recv fcw_send reset)
123 );
124 delete $s->{$key};
125 }
126
127 # Prevent endless growth of closed streams structure
128 # Drop old closed streams (possible violation of the standard)
129 push @{ $self->{closed_streams} }, $stream_id;
130 delete $self->{streams}->{ shift @{ $self->{closed_streams} } }
131 if @{ $self->{closed_streams} } > $self->{max_closed_streams}
132 }
133 }
134 }
135
136 $s->{state};
137 }
138
139 sub stream_pending_state {
140 my $self = shift;
141 my $stream_id = shift;
142 return undef unless exists $self->{streams}->{$stream_id};
143 my $s = $self->{streams}->{$stream_id};
144 if (@_) {
145 $s->{pending_state} = shift;
146 $self->{pending_stream} =
147 defined $s->{pending_state} ? $stream_id : undef;
148 }
149 $s->{pending_state};
150 }
151
152 sub stream_cb {
153 my ( $self, $stream_id, $state, $cb ) = @_;
154
155 return undef unless exists $self->{streams}->{$stream_id};
156
157 push @{ $self->{streams}->{$stream_id}->{cb}->{$state} }, $cb;
158 }
159
160 sub stream_frame_cb {
161 my ( $self, $stream_id, $frame, $cb ) = @_;
162
163 return undef unless exists $self->{streams}->{$stream_id};
164
165 push @{ $self->{streams}->{$stream_id}->{frame_cb}->{$frame} }, $cb;
166 }
167
168 sub stream_data {
169 my $self = shift;
170 my $stream_id = shift;
171 return undef unless exists $self->{streams}->{$stream_id};
172 my $s = $self->{streams}->{$stream_id};
173
174 if (@_) {
175
176 # Exec callbacks for data
177 if ( exists $s->{frame_cb} && exists $s->{frame_cb}->{&DATA} ) {
178 for my $cb ( @{ $s->{frame_cb}->{&DATA} } ) {
179 $cb->( $_[0] );
180 }
181 }
182 else {
183 $s->{data} .= shift;
184 }
185 }
186
187 $s->{data};
188 }
189
190 sub stream_headers_done {
191 my $self = shift;
192 my $stream_id = shift;
193 return undef unless exists $self->{streams}->{$stream_id};
194 my $s = $self->{streams}->{$stream_id};
195
196 my $res =
197 headers_decode( $self, \$s->{header_block}, 0,
198 length $s->{header_block}, $stream_id );
199
200 tracer->debug("Headers done for stream $stream_id\n");
201
202 return undef unless defined $res;
203
204 # Clear header_block
205 $s->{header_block} = '';
206
207 my $eh = $self->decode_context->{emitted_headers};
208 my $is_response = $self->{type} == CLIENT && !$s->{promised_sid};
209 my $is_trailer = !!$self->stream_trailer($stream_id);
210
211 return undef
212 unless $self->validate_headers( $eh, $stream_id, $is_response );
213
214 if ( $s->{promised_sid} ) {
215 $self->{streams}->{ $s->{promised_sid} }->{pp_headers} = $eh;
216 }
217 elsif ($is_trailer) {
218 $self->stream_trailer_headers( $stream_id, $eh );
219 }
220 else {
221 $s->{headers} = $eh;
222 }
223
224 # Exec callbacks for headers
225 if ( exists $s->{frame_cb} && exists $s->{frame_cb}->{&HEADERS} ) {
226 for my $cb ( @{ $s->{frame_cb}->{&HEADERS} } ) {
227 $cb->($eh);
228 }
229 }
230
231 # Clear emitted headers
232 $self->decode_context->{emitted_headers} = [];
233
234 return 1;
235 }
236
237 sub validate_headers {
238 my ( $self, $headers, $stream_id, $is_response ) = @_;
239 my $pseudo_flag = 1;
240 my %pseudo_hash = ();
241 my @h = $is_response ? (qw(:status)) : (
242 qw(:method :scheme :authority
243 :path)
244 );
245
246 # Trailer headers ?
247 if ( my $t = $self->stream_trailer($stream_id) ) {
248 for my $i ( 0 .. @$headers / 2 - 1 ) {
249 my ( $h, $v ) = ( $headers->[ $i * 2 ], $headers->[ $i * 2 + 1 ] );
250 if ( !exists $t->{$h} ) {
251 tracer->warning(
252 "header <$h> doesn't listed in the trailer header");
253 $self->stream_error( $stream_id, PROTOCOL_ERROR );
254 return undef;
255 }
256 }
257 return 1;
258 }
259
260 for my $i ( 0 .. @$headers / 2 - 1 ) {
261 my ( $h, $v ) = ( $headers->[ $i * 2 ], $headers->[ $i * 2 + 1 ] );
262 if ( $h =~ /^\:/ ) {
263 if ( !$pseudo_flag ) {
264 tracer->warning(
265 "pseudo-header <$h> appears after a regular header");
266 $self->stream_error( $stream_id, PROTOCOL_ERROR );
267 return undef;
268 }
269 elsif ( !grep { $_ eq $h } @h ) {
270 tracer->warning("invalid pseudo-header <$h>");
271 $self->stream_error( $stream_id, PROTOCOL_ERROR );
272 return undef;
273 }
274 elsif ( exists $pseudo_hash{$h} ) {
275 tracer->warning("repeated pseudo-header <$h>");
276 $self->stream_error( $stream_id, PROTOCOL_ERROR );
277 return undef;
278 }
279
280 $pseudo_hash{$h} = $v;
281 next;
282 }
283
284 $pseudo_flag = 0 if $pseudo_flag;
285
286 if ( $h eq 'connection' ) {
287 tracer->warning("connection header is not valid in http/2");
288 $self->stream_error( $stream_id, PROTOCOL_ERROR );
289 return undef;
290 }
291 elsif ( $h eq 'te' && $v ne 'trailers' ) {
292 tracer->warning("TE header can contain only value 'trailers'");
293 $self->stream_error( $stream_id, PROTOCOL_ERROR );
294 return undef;
295 }
296 elsif ( $h eq 'content-length' ) {
297 $self->stream_length( $stream_id, $v );
298 }
299 elsif ( $h eq 'trailer' ) {
300 my %th = map { $_ => 1 } split /\s*,\s*/, lc($v);
301 if (
302 grep { exists $th{$_} } (
303 qw(transfer-encoding content-length host authentication
304 cache-control expect max-forwards pragma range te
305 content-encoding content-type content-range trailer)
306 )
307 )
308 {
309 tracer->warning("trailer header contain forbidden headers");
310 $self->stream_error( $stream_id, PROTOCOL_ERROR );
311 return undef;
312 }
313 $self->stream_trailer( $stream_id, {%th} );
314 }
315 }
316
317 for my $h (@h) {
318 next if exists $pseudo_hash{$h};
319
320 tracer->warning("missed mandatory pseudo-header $h");
321 $self->stream_error( $stream_id, PROTOCOL_ERROR );
322 return undef;
323 }
324
325 1;
326 }
327
328 # RST_STREAM for stream errors
329 sub stream_error {
330 my ( $self, $stream_id, $error ) = @_;
331 $self->enqueue( RST_STREAM, 0, $stream_id, $error );
332 }
333
334 # Flow control windown of stream
335 sub _stream_fcw {
336 my $dir = shift;
337 my $self = shift;
338 my $stream_id = shift;
339 return undef unless exists $self->{streams}->{$stream_id};
340 my $s = $self->{streams}->{$stream_id};
341
342 if (@_) {
343 $s->{$dir} += shift;
344 tracer->debug( "Stream $stream_id $dir now is " . $s->{$dir} . "\n" );
345 }
346 $s->{$dir};
347 }
348
349 sub stream_fcw_send {
350 _stream_fcw( 'fcw_send', @_ );
351 }
352
353 sub stream_fcw_recv {
354 _stream_fcw( 'fcw_recv', @_ );
355 }
356
357 sub stream_fcw_update {
358 my ( $self, $stream_id ) = @_;
359
360 # TODO: check size of data of stream in memory
361 my $size = $self->dec_setting(SETTINGS_INITIAL_WINDOW_SIZE);
362 tracer->debug("update fcw recv of stream $stream_id with $size b.\n");
363 $self->stream_fcw_recv( $stream_id, $size );
364 $self->enqueue( WINDOW_UPDATE, 0, $stream_id, $size );
365 }
366
367 sub stream_send_blocked {
368 my ( $self, $stream_id ) = @_;
369 my $s = $self->{streams}->{$stream_id} or return undef;
370
371 if ( defined( $s->{blocked_data} ) && length( $s->{blocked_data} )
372 && $self->stream_fcw_send($stream_id) > 0 )
373 {
374 $self->send_data($stream_id);
375 }
376 }
377
378 sub stream_reprio {
379 my ( $self, $stream_id, $exclusive, $stream_dep ) = @_;
380 return undef
381 unless exists $self->{streams}->{$stream_id}
382 && ( $stream_dep == 0 || exists $self->{streams}->{$stream_dep} )
383 && $stream_id != $stream_dep;
384 my $s = $self->{streams};
385
386 if ( $s->{$stream_id}->{stream_dep} != $stream_dep ) {
387
388 # check if new stream_dep is stream child
389 if ( $stream_dep != 0 ) {
390 my $sid = $stream_dep;
391 while ( $sid = $s->{$sid}->{stream_dep} ) {
392 next unless $sid == $stream_id;
393
394 # Child take my stream dep
395 $s->{$stream_dep}->{stream_dep} =
396 $s->{$stream_id}->{stream_dep};
397 last;
398 }
399 }
400
401 # Set new stream dep
402 $s->{$stream_id}->{stream_dep} = $stream_dep;
403 }
404
405 if ($exclusive) {
406
407 # move all siblings to children
408 for my $sid ( keys %$s ) {
409 next
410 if $s->{$sid}->{stream_dep} != $stream_dep
411 || $sid == $stream_id;
412
413 $s->{$sid}->{stream_dep} = $stream_id;
414 }
415 }
416
417 return 1;
418 }
419
420 sub stream_header_block_add {
421 my ( $self, $stream_id, $header_chunk ) = @_;
422 return undef
423 if !exists $self->{streams}->{$stream_id} || !defined $header_chunk;
424 $self->{streams}->{$stream_id}->{header_block} .= $header_chunk;
425 }
426
427 1;