@@ -82,6 +82,8 @@ static struct http2_stream_ctx *allocate_http_stream_context(
82
82
client -> streams [i ].stream_state = HTTP2_STREAM_OPEN ;
83
83
client -> streams [i ].window_size =
84
84
HTTP_SERVER_INITIAL_WINDOW_SIZE ;
85
+ client -> streams [i ].headers_sent = false;
86
+ client -> streams [i ].end_stream_sent = false;
85
87
return & client -> streams [i ];
86
88
}
87
89
}
@@ -339,6 +341,10 @@ static int handle_http2_static_resource(
339
341
return - ENOTSUP ;
340
342
}
341
343
344
+ if (client -> current_stream == NULL ) {
345
+ return - ENOENT ;
346
+ }
347
+
342
348
content_200 = static_detail -> static_data ;
343
349
content_len = static_detail -> static_data_len ;
344
350
@@ -349,7 +355,7 @@ static int handle_http2_static_resource(
349
355
goto out ;
350
356
}
351
357
352
- client -> headers_sent = true;
358
+ client -> current_stream -> headers_sent = true;
353
359
354
360
ret = send_data_frame (client , content_200 , content_len ,
355
361
frame -> stream_identifier ,
@@ -359,7 +365,7 @@ static int handle_http2_static_resource(
359
365
goto out ;
360
366
}
361
367
362
- client -> end_stream_sent = true;
368
+ client -> current_stream -> end_stream_sent = true;
363
369
364
370
out :
365
371
return ret ;
@@ -372,14 +378,18 @@ static int dynamic_get_req_v2(struct http_resource_detail_dynamic *dynamic_detai
372
378
int ret , remaining , offset = dynamic_detail -> common .path_len ;
373
379
char * ptr ;
374
380
381
+ if (client -> current_stream == NULL ) {
382
+ return - ENOENT ;
383
+ }
384
+
375
385
ret = send_headers_frame (client , HTTP_200_OK , frame -> stream_identifier ,
376
386
& dynamic_detail -> common , 0 );
377
387
if (ret < 0 ) {
378
388
LOG_DBG ("Cannot write to socket (%d)" , ret );
379
389
return ret ;
380
390
}
381
391
382
- client -> headers_sent = true;
392
+ client -> current_stream -> headers_sent = true;
383
393
384
394
remaining = strlen (& client -> url_buffer [dynamic_detail -> common .path_len ]);
385
395
@@ -428,7 +438,7 @@ static int dynamic_get_req_v2(struct http_resource_detail_dynamic *dynamic_detai
428
438
LOG_DBG ("Cannot send last frame (%d)" , ret );
429
439
}
430
440
431
- client -> end_stream_sent = true;
441
+ client -> current_stream -> end_stream_sent = true;
432
442
433
443
dynamic_detail -> holder = NULL ;
434
444
@@ -450,6 +460,10 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
450
460
return - ENOENT ;
451
461
}
452
462
463
+ if (client -> current_stream == NULL ) {
464
+ return - ENOENT ;
465
+ }
466
+
453
467
data_len = MIN (frame -> length , client -> data_len );
454
468
copy_len = MIN (data_len , dynamic_detail -> data_buffer_len );
455
469
@@ -481,7 +495,7 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
481
495
if (send_len > 0 ) {
482
496
uint8_t flags = 0 ;
483
497
484
- if (!client -> headers_sent ) {
498
+ if (!client -> current_stream -> headers_sent ) {
485
499
ret = send_headers_frame (
486
500
client , HTTP_200_OK , frame -> stream_identifier ,
487
501
& dynamic_detail -> common , 0 );
@@ -490,7 +504,7 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
490
504
return ret ;
491
505
}
492
506
493
- client -> headers_sent = true;
507
+ client -> current_stream -> headers_sent = true;
494
508
}
495
509
496
510
/* In case no more data is available, that was the last
@@ -499,7 +513,7 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
499
513
if (frame -> length == 0 &&
500
514
is_header_flag_set (frame -> flags , HTTP2_FLAG_END_STREAM )) {
501
515
flags = HTTP2_FLAG_END_STREAM ;
502
- client -> end_stream_sent = true;
516
+ client -> current_stream -> end_stream_sent = true;
503
517
}
504
518
505
519
ret = send_data_frame (client ,
@@ -518,7 +532,7 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
518
532
519
533
if (frame -> length == 0 &&
520
534
is_header_flag_set (frame -> flags , HTTP2_FLAG_END_STREAM )) {
521
- if (!client -> headers_sent ) {
535
+ if (!client -> current_stream -> headers_sent ) {
522
536
/* The callback did not report any data to send, therefore send
523
537
* headers frame now, including END_STREAM flag.
524
538
*/
@@ -531,8 +545,8 @@ static int dynamic_post_req_v2(struct http_resource_detail_dynamic *dynamic_deta
531
545
return ret ;
532
546
}
533
547
534
- client -> headers_sent = true;
535
- client -> end_stream_sent = true;
548
+ client -> current_stream -> headers_sent = true;
549
+ client -> current_stream -> end_stream_sent = true;
536
550
}
537
551
538
552
dynamic_detail -> holder = NULL ;
@@ -659,6 +673,7 @@ static int enter_http_frame_data_state(struct http_client_ctx *client)
659
673
stream -> window_size -= frame -> length ;
660
674
client -> window_size -= frame -> length ;
661
675
client -> server_state = HTTP_SERVER_FRAME_DATA_STATE ;
676
+ client -> current_stream = stream ;
662
677
663
678
return 0 ;
664
679
}
@@ -680,6 +695,8 @@ static int enter_http_frame_headers_state(struct http_client_ctx *client)
680
695
}
681
696
}
682
697
698
+ client -> current_stream = stream ;
699
+
683
700
if (!is_header_flag_set (frame -> flags , HTTP2_FLAG_END_HEADERS )) {
684
701
client -> expect_continuation = true;
685
702
} else {
@@ -756,6 +773,8 @@ int handle_http_frame_header(struct http_client_ctx *client)
756
773
return - EBADMSG ;
757
774
}
758
775
776
+ client -> current_stream = NULL ;
777
+
759
778
switch (client -> current_frame .type ) {
760
779
case HTTP2_DATA_FRAME :
761
780
return enter_http_frame_data_state (client );
@@ -792,6 +811,7 @@ int handle_http1_to_http2_upgrade(struct http_client_ctx *client)
792
811
"\r\n" ;
793
812
struct http2_frame * frame = & client -> current_frame ;
794
813
struct http_resource_detail * detail ;
814
+ struct http2_stream_ctx * stream ;
795
815
int path_len ;
796
816
int ret ;
797
817
@@ -808,6 +828,18 @@ int handle_http1_to_http2_upgrade(struct http_client_ctx *client)
808
828
frame -> flags = 0 ;
809
829
}
810
830
831
+ /* Allocate stream. */
832
+ stream = find_http_stream_context (client , frame -> stream_identifier );
833
+ if (stream == NULL ) {
834
+ stream = allocate_http_stream_context (client , frame -> stream_identifier );
835
+ if (!stream ) {
836
+ LOG_DBG ("No available stream slots. Connection closed." );
837
+ return - ENOMEM ;
838
+ }
839
+ }
840
+
841
+ client -> current_stream = stream ;
842
+
811
843
if (!client -> preface_sent ) {
812
844
ret = http_server_sendall (client , switching_protocols ,
813
845
sizeof (switching_protocols ) - 1 );
@@ -866,6 +898,7 @@ int handle_http1_to_http2_upgrade(struct http_client_ctx *client)
866
898
* to HTTP2.
867
899
*/
868
900
if (client -> parser_state == HTTP1_MESSAGE_COMPLETE_STATE ) {
901
+ release_http_stream_context (client , frame -> stream_identifier );
869
902
client -> current_detail = NULL ;
870
903
client -> server_state = HTTP_SERVER_PREFACE_STATE ;
871
904
client -> cursor += client -> data_len ;
@@ -1121,6 +1154,10 @@ static int handle_http_frame_headers_end_stream(struct http_client_ctx *client)
1121
1154
goto out ;
1122
1155
}
1123
1156
1157
+ if (client -> current_stream == NULL ) {
1158
+ return - ENOENT ;
1159
+ }
1160
+
1124
1161
if (client -> current_detail -> type == HTTP_RESOURCE_TYPE_DYNAMIC ) {
1125
1162
struct http_resource_detail_dynamic * dynamic_detail =
1126
1163
(struct http_resource_detail_dynamic * )client -> current_detail ;
@@ -1130,7 +1167,7 @@ static int handle_http_frame_headers_end_stream(struct http_client_ctx *client)
1130
1167
dynamic_detail -> data_buffer , 0 ,
1131
1168
dynamic_detail -> user_data );
1132
1169
if (send_len > 0 ) {
1133
- if (!client -> headers_sent ) {
1170
+ if (!client -> current_stream -> headers_sent ) {
1134
1171
ret = send_headers_frame (
1135
1172
client , HTTP_200_OK , frame -> stream_identifier ,
1136
1173
client -> current_detail , 0 );
@@ -1139,7 +1176,7 @@ static int handle_http_frame_headers_end_stream(struct http_client_ctx *client)
1139
1176
goto out ;
1140
1177
}
1141
1178
1142
- client -> headers_sent = true;
1179
+ client -> current_stream -> headers_sent = true;
1143
1180
}
1144
1181
1145
1182
ret = send_data_frame (client ,
@@ -1151,21 +1188,21 @@ static int handle_http_frame_headers_end_stream(struct http_client_ctx *client)
1151
1188
goto out ;
1152
1189
}
1153
1190
1154
- client -> end_stream_sent = true;
1191
+ client -> current_stream -> end_stream_sent = true;
1155
1192
}
1156
1193
1157
1194
dynamic_detail -> holder = NULL ;
1158
1195
}
1159
1196
1160
- if (!client -> headers_sent ) {
1197
+ if (!client -> current_stream -> headers_sent ) {
1161
1198
ret = send_headers_frame (
1162
1199
client , HTTP_200_OK , frame -> stream_identifier ,
1163
1200
client -> current_detail , HTTP2_FLAG_END_STREAM );
1164
1201
if (ret < 0 ) {
1165
1202
LOG_DBG ("Cannot write to socket (%d)" , ret );
1166
1203
goto out ;
1167
1204
}
1168
- } else if (!client -> end_stream_sent ) {
1205
+ } else if (!client -> current_stream -> end_stream_sent ) {
1169
1206
ret = send_data_frame (client , NULL , 0 , frame -> stream_identifier ,
1170
1207
HTTP2_FLAG_END_STREAM );
1171
1208
if (ret < 0 ) {
0 commit comments