-
Notifications
You must be signed in to change notification settings - Fork 4
/
nghttp2_session.c
7428 lines (6115 loc) · 222 KB
/
nghttp2_session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "nghttp2_session.h"
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include "nghttp2_helper.h"
#include "nghttp2_net.h"
#include "nghttp2_priority_spec.h"
#include "nghttp2_option.h"
#include "nghttp2_http.h"
#include "nghttp2_pq.h"
/*
* Returns non-zero if the number of outgoing opened streams is larger
* than or equal to
* remote_settings.max_concurrent_streams.
*/
static int
session_is_outgoing_concurrent_streams_max(nghttp2_session *session) {
return session->remote_settings.max_concurrent_streams <=
session->num_outgoing_streams;
}
/*
* Returns non-zero if the number of incoming opened streams is larger
* than or equal to
* local_settings.max_concurrent_streams.
*/
static int
session_is_incoming_concurrent_streams_max(nghttp2_session *session) {
return session->local_settings.max_concurrent_streams <=
session->num_incoming_streams;
}
/*
* Returns non-zero if the number of incoming opened streams is larger
* than or equal to
* session->pending_local_max_concurrent_stream.
*/
static int
session_is_incoming_concurrent_streams_pending_max(nghttp2_session *session) {
return session->pending_local_max_concurrent_stream <=
session->num_incoming_streams;
}
/*
* Returns non-zero if |lib_error| is non-fatal error.
*/
static int is_non_fatal(int lib_error_code) {
return lib_error_code < 0 && lib_error_code > NGHTTP2_ERR_FATAL;
}
int nghttp2_is_fatal(int lib_error_code) {
return lib_error_code < NGHTTP2_ERR_FATAL;
}
static int session_enforce_http_messaging(nghttp2_session *session) {
return (session->opt_flags & NGHTTP2_OPTMASK_NO_HTTP_MESSAGING) == 0;
}
/*
* Returns nonzero if |frame| is trailer headers.
*/
static int session_trailer_headers(nghttp2_session *session,
nghttp2_stream *stream,
nghttp2_frame *frame) {
if (!stream || frame->hd.type != NGHTTP2_HEADERS) {
return 0;
}
if (session->server) {
return frame->headers.cat == NGHTTP2_HCAT_HEADERS;
}
return frame->headers.cat == NGHTTP2_HCAT_HEADERS &&
(stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) == 0;
}
/* Returns nonzero if the |stream| is in reserved(remote) state */
static int state_reserved_remote(nghttp2_session *session,
nghttp2_stream *stream) {
return stream->state == NGHTTP2_STREAM_RESERVED &&
!nghttp2_session_is_my_stream_id(session, stream->stream_id);
}
/* Returns nonzero if the |stream| is in reserved(local) state */
static int state_reserved_local(nghttp2_session *session,
nghttp2_stream *stream) {
return stream->state == NGHTTP2_STREAM_RESERVED &&
nghttp2_session_is_my_stream_id(session, stream->stream_id);
}
/*
* Checks whether received stream_id is valid. This function returns
* 1 if it succeeds, or 0.
*/
static int session_is_new_peer_stream_id(nghttp2_session *session,
int32_t stream_id) {
return stream_id != 0 &&
!nghttp2_session_is_my_stream_id(session, stream_id) &&
session->last_recv_stream_id < stream_id;
}
static int session_detect_idle_stream(nghttp2_session *session,
int32_t stream_id) {
/* Assume that stream object with stream_id does not exist */
if (nghttp2_session_is_my_stream_id(session, stream_id)) {
if (session->last_sent_stream_id < stream_id) {
return 1;
}
return 0;
}
if (session_is_new_peer_stream_id(session, stream_id)) {
return 1;
}
return 0;
}
static int check_ext_type_set(const uint8_t *ext_types, uint8_t type) {
return (ext_types[type / 8] & (1 << (type & 0x7))) > 0;
}
static int session_call_error_callback(nghttp2_session *session,
const char *fmt, ...) {
size_t bufsize;
va_list ap;
char *buf;
int rv;
nghttp2_mem *mem;
if (!session->callbacks.error_callback) {
return 0;
}
mem = &session->mem;
va_start(ap, fmt);
rv = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (rv < 0) {
return NGHTTP2_ERR_NOMEM;
}
bufsize = (size_t)(rv + 1);
buf = nghttp2_mem_malloc(mem, bufsize);
if (buf == NULL) {
return NGHTTP2_ERR_NOMEM;
}
va_start(ap, fmt);
rv = vsnprintf(buf, bufsize, fmt, ap);
va_end(ap);
if (rv < 0) {
nghttp2_mem_free(mem, buf);
/* vsnprintf may return error because of various things we can
imagine, but typically we don't want to drop session just for
debug callback. */
DEBUGF(fprintf(stderr,
"error_callback: vsnprintf failed. The template was %s\n",
fmt));
return 0;
}
rv = session->callbacks.error_callback(session, buf, (size_t)rv,
session->user_data);
nghttp2_mem_free(mem, buf);
if (rv != 0) {
return NGHTTP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
static int session_terminate_session(nghttp2_session *session,
int32_t last_stream_id,
uint32_t error_code, const char *reason) {
int rv;
const uint8_t *debug_data;
size_t debug_datalen;
if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
return 0;
}
if (reason == NULL) {
debug_data = NULL;
debug_datalen = 0;
} else {
debug_data = (const uint8_t *)reason;
debug_datalen = strlen(reason);
}
rv = nghttp2_session_add_goaway(session, last_stream_id, error_code,
debug_data, debug_datalen,
NGHTTP2_GOAWAY_AUX_TERM_ON_SEND);
if (rv != 0) {
return rv;
}
session->goaway_flags |= NGHTTP2_GOAWAY_TERM_ON_SEND;
return 0;
}
int nghttp2_session_terminate_session(nghttp2_session *session,
uint32_t error_code) {
return session_terminate_session(session, session->last_proc_stream_id,
error_code, NULL);
}
int nghttp2_session_terminate_session2(nghttp2_session *session,
int32_t last_stream_id,
uint32_t error_code) {
return session_terminate_session(session, last_stream_id, error_code, NULL);
}
int nghttp2_session_terminate_session_with_reason(nghttp2_session *session,
uint32_t error_code,
const char *reason) {
return session_terminate_session(session, session->last_proc_stream_id,
error_code, reason);
}
int nghttp2_session_is_my_stream_id(nghttp2_session *session,
int32_t stream_id) {
int rem;
if (stream_id == 0) {
return 0;
}
rem = stream_id & 0x1;
if (session->server) {
return rem == 0;
}
return rem == 1;
}
nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session,
int32_t stream_id) {
nghttp2_stream *stream;
stream = (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
if (stream == NULL || (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) ||
stream->state == NGHTTP2_STREAM_IDLE) {
return NULL;
}
return stream;
}
nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session,
int32_t stream_id) {
return (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
}
static void session_inbound_frame_reset(nghttp2_session *session) {
nghttp2_inbound_frame *iframe = &session->iframe;
nghttp2_mem *mem = &session->mem;
/* A bit risky code, since if this function is called from
nghttp2_session_new(), we rely on the fact that
iframe->frame.hd.type is 0, so that no free is performed. */
switch (iframe->frame.hd.type) {
case NGHTTP2_DATA:
break;
case NGHTTP2_HEADERS:
nghttp2_frame_headers_free(&iframe->frame.headers, mem);
break;
case NGHTTP2_PRIORITY:
nghttp2_frame_priority_free(&iframe->frame.priority);
break;
case NGHTTP2_RST_STREAM:
nghttp2_frame_rst_stream_free(&iframe->frame.rst_stream);
break;
case NGHTTP2_SETTINGS:
nghttp2_frame_settings_free(&iframe->frame.settings, mem);
nghttp2_mem_free(mem, iframe->iv);
iframe->iv = NULL;
iframe->niv = 0;
iframe->max_niv = 0;
break;
case NGHTTP2_PUSH_PROMISE:
nghttp2_frame_push_promise_free(&iframe->frame.push_promise, mem);
break;
case NGHTTP2_PING:
nghttp2_frame_ping_free(&iframe->frame.ping);
break;
case NGHTTP2_GOAWAY:
nghttp2_frame_goaway_free(&iframe->frame.goaway, mem);
break;
case NGHTTP2_WINDOW_UPDATE:
nghttp2_frame_window_update_free(&iframe->frame.window_update);
break;
default:
/* extension frame */
if (check_ext_type_set(session->user_recv_ext_types,
iframe->frame.hd.type)) {
nghttp2_frame_extension_free(&iframe->frame.ext);
} else {
switch (iframe->frame.hd.type) {
case NGHTTP2_ALTSVC:
if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) == 0) {
break;
}
nghttp2_frame_altsvc_free(&iframe->frame.ext, mem);
break;
}
}
break;
}
memset(&iframe->frame, 0, sizeof(nghttp2_frame));
memset(&iframe->ext_frame_payload, 0, sizeof(nghttp2_ext_frame_payload));
iframe->state = NGHTTP2_IB_READ_HEAD;
nghttp2_buf_wrap_init(&iframe->sbuf, iframe->raw_sbuf,
sizeof(iframe->raw_sbuf));
iframe->sbuf.mark += NGHTTP2_FRAME_HDLEN;
nghttp2_buf_free(&iframe->lbuf, mem);
nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
iframe->raw_lbuf = NULL;
iframe->payloadleft = 0;
iframe->padlen = 0;
}
static void init_settings(nghttp2_settings_storage *settings) {
settings->header_table_size = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
settings->enable_push = 1;
settings->max_concurrent_streams = NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS;
settings->initial_window_size = NGHTTP2_INITIAL_WINDOW_SIZE;
settings->max_frame_size = NGHTTP2_MAX_FRAME_SIZE_MIN;
settings->max_header_list_size = UINT32_MAX;
}
static void active_outbound_item_reset(nghttp2_active_outbound_item *aob,
nghttp2_mem *mem) {
DEBUGF(fprintf(stderr, "send: reset nghttp2_active_outbound_item\n"));
DEBUGF(fprintf(stderr, "send: aob->item = %p\n", aob->item));
nghttp2_outbound_item_free(aob->item, mem);
nghttp2_mem_free(mem, aob->item);
aob->item = NULL;
nghttp2_bufs_reset(&aob->framebufs);
aob->state = NGHTTP2_OB_POP_ITEM;
}
int nghttp2_enable_strict_preface = 1;
static int session_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data, int server,
const nghttp2_option *option, nghttp2_mem *mem) {
int rv;
size_t nbuffer;
if (mem == NULL) {
mem = nghttp2_mem_default();
}
*session_ptr = nghttp2_mem_calloc(mem, 1, sizeof(nghttp2_session));
if (*session_ptr == NULL) {
rv = NGHTTP2_ERR_NOMEM;
goto fail_session;
}
(*session_ptr)->mem = *mem;
mem = &(*session_ptr)->mem;
/* next_stream_id is initialized in either
nghttp2_session_client_new2 or nghttp2_session_server_new2 */
rv = nghttp2_hd_deflate_init(&(*session_ptr)->hd_deflater, mem);
if (rv != 0) {
goto fail_hd_deflater;
}
rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, mem);
if (rv != 0) {
goto fail_hd_inflater;
}
rv = nghttp2_map_init(&(*session_ptr)->streams, mem);
if (rv != 0) {
goto fail_map;
}
nghttp2_stream_init(&(*session_ptr)->root, 0, NGHTTP2_STREAM_FLAG_NONE,
NGHTTP2_STREAM_IDLE, NGHTTP2_DEFAULT_WEIGHT, 0, 0, NULL,
mem);
(*session_ptr)->remote_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
(*session_ptr)->recv_window_size = 0;
(*session_ptr)->consumed_size = 0;
(*session_ptr)->recv_reduction = 0;
(*session_ptr)->local_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
(*session_ptr)->goaway_flags = NGHTTP2_GOAWAY_NONE;
(*session_ptr)->local_last_stream_id = (1u << 31) - 1;
(*session_ptr)->remote_last_stream_id = (1u << 31) - 1;
(*session_ptr)->pending_local_max_concurrent_stream =
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS;
(*session_ptr)->pending_enable_push = 1;
if (server) {
(*session_ptr)->server = 1;
}
init_settings(&(*session_ptr)->remote_settings);
init_settings(&(*session_ptr)->local_settings);
(*session_ptr)->max_incoming_reserved_streams =
NGHTTP2_MAX_INCOMING_RESERVED_STREAMS;
/* Limit max outgoing concurrent streams to sensible value */
(*session_ptr)->remote_settings.max_concurrent_streams = 100;
(*session_ptr)->max_send_header_block_length = NGHTTP2_MAX_HEADERSLEN;
if (option) {
if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE) &&
option->no_auto_window_update) {
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE;
}
if (option->opt_set_mask & NGHTTP2_OPT_PEER_MAX_CONCURRENT_STREAMS) {
(*session_ptr)->remote_settings.max_concurrent_streams =
option->peer_max_concurrent_streams;
}
if (option->opt_set_mask & NGHTTP2_OPT_MAX_RESERVED_REMOTE_STREAMS) {
(*session_ptr)->max_incoming_reserved_streams =
option->max_reserved_remote_streams;
}
if ((option->opt_set_mask & NGHTTP2_OPT_NO_RECV_CLIENT_MAGIC) &&
option->no_recv_client_magic) {
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC;
}
if ((option->opt_set_mask & NGHTTP2_OPT_NO_HTTP_MESSAGING) &&
option->no_http_messaging) {
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_HTTP_MESSAGING;
}
if (option->opt_set_mask & NGHTTP2_OPT_USER_RECV_EXT_TYPES) {
memcpy((*session_ptr)->user_recv_ext_types, option->user_recv_ext_types,
sizeof((*session_ptr)->user_recv_ext_types));
}
if (option->opt_set_mask & NGHTTP2_OPT_BUILTIN_RECV_EXT_TYPES) {
(*session_ptr)->builtin_recv_ext_types = option->builtin_recv_ext_types;
}
if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_PING_ACK) &&
option->no_auto_ping_ack) {
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_PING_ACK;
}
if (option->opt_set_mask & NGHTTP2_OPT_MAX_SEND_HEADER_BLOCK_LENGTH) {
(*session_ptr)->max_send_header_block_length =
option->max_send_header_block_length;
}
}
nbuffer = ((*session_ptr)->max_send_header_block_length +
NGHTTP2_FRAMEBUF_CHUNKLEN - 1) /
NGHTTP2_FRAMEBUF_CHUNKLEN;
if (nbuffer == 0) {
nbuffer = 1;
}
/* 1 for Pad Field. */
rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs,
NGHTTP2_FRAMEBUF_CHUNKLEN, nbuffer, 1,
NGHTTP2_FRAME_HDLEN + 1, mem);
if (rv != 0) {
goto fail_aob_framebuf;
}
active_outbound_item_reset(&(*session_ptr)->aob, mem);
(*session_ptr)->callbacks = *callbacks;
(*session_ptr)->user_data = user_data;
session_inbound_frame_reset(*session_ptr);
if (nghttp2_enable_strict_preface) {
nghttp2_inbound_frame *iframe = &(*session_ptr)->iframe;
if (server &&
((*session_ptr)->opt_flags & NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC) ==
0) {
iframe->state = NGHTTP2_IB_READ_CLIENT_MAGIC;
iframe->payloadleft = NGHTTP2_CLIENT_MAGIC_LEN;
} else {
iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
}
if (!server) {
(*session_ptr)->aob.state = NGHTTP2_OB_SEND_CLIENT_MAGIC;
nghttp2_bufs_add(&(*session_ptr)->aob.framebufs, NGHTTP2_CLIENT_MAGIC,
NGHTTP2_CLIENT_MAGIC_LEN);
}
}
return 0;
fail_aob_framebuf:
nghttp2_map_free(&(*session_ptr)->streams);
fail_map:
nghttp2_hd_inflate_free(&(*session_ptr)->hd_inflater);
fail_hd_inflater:
nghttp2_hd_deflate_free(&(*session_ptr)->hd_deflater);
fail_hd_deflater:
nghttp2_mem_free(mem, *session_ptr);
fail_session:
return rv;
}
int nghttp2_session_client_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data) {
return nghttp2_session_client_new3(session_ptr, callbacks, user_data, NULL,
NULL);
}
int nghttp2_session_client_new2(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option) {
return nghttp2_session_client_new3(session_ptr, callbacks, user_data, option,
NULL);
}
int nghttp2_session_client_new3(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option,
nghttp2_mem *mem) {
int rv;
nghttp2_session *session;
rv = session_new(&session, callbacks, user_data, 0, option, mem);
if (rv != 0) {
return rv;
}
/* IDs for use in client */
session->next_stream_id = 1;
*session_ptr = session;
return 0;
}
int nghttp2_session_server_new(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data) {
return nghttp2_session_server_new3(session_ptr, callbacks, user_data, NULL,
NULL);
}
int nghttp2_session_server_new2(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option) {
return nghttp2_session_server_new3(session_ptr, callbacks, user_data, option,
NULL);
}
int nghttp2_session_server_new3(nghttp2_session **session_ptr,
const nghttp2_session_callbacks *callbacks,
void *user_data, const nghttp2_option *option,
nghttp2_mem *mem) {
int rv;
nghttp2_session *session;
rv = session_new(&session, callbacks, user_data, 1, option, mem);
if (rv != 0) {
return rv;
}
/* IDs for use in client */
session->next_stream_id = 2;
*session_ptr = session;
return 0;
}
static int free_streams(nghttp2_map_entry *entry, void *ptr) {
nghttp2_session *session;
nghttp2_stream *stream;
nghttp2_outbound_item *item;
nghttp2_mem *mem;
session = (nghttp2_session *)ptr;
mem = &session->mem;
stream = (nghttp2_stream *)entry;
item = stream->item;
if (item && !item->queued && item != session->aob.item) {
nghttp2_outbound_item_free(item, mem);
nghttp2_mem_free(mem, item);
}
nghttp2_stream_free(stream);
nghttp2_mem_free(mem, stream);
return 0;
}
static void ob_q_free(nghttp2_outbound_queue *q, nghttp2_mem *mem) {
nghttp2_outbound_item *item, *next;
for (item = q->head; item;) {
next = item->qnext;
nghttp2_outbound_item_free(item, mem);
nghttp2_mem_free(mem, item);
item = next;
}
}
static int inflight_settings_new(nghttp2_inflight_settings **settings_ptr,
const nghttp2_settings_entry *iv, size_t niv,
nghttp2_mem *mem) {
*settings_ptr = nghttp2_mem_malloc(mem, sizeof(nghttp2_inflight_settings));
if (!*settings_ptr) {
return NGHTTP2_ERR_NOMEM;
}
if (niv > 0) {
(*settings_ptr)->iv = nghttp2_frame_iv_copy(iv, niv, mem);
if (!(*settings_ptr)->iv) {
return NGHTTP2_ERR_NOMEM;
}
} else {
(*settings_ptr)->iv = NULL;
}
(*settings_ptr)->niv = niv;
(*settings_ptr)->next = NULL;
return 0;
}
static void inflight_settings_del(nghttp2_inflight_settings *settings,
nghttp2_mem *mem) {
if (!settings) {
return;
}
nghttp2_mem_free(mem, settings->iv);
nghttp2_mem_free(mem, settings);
}
void nghttp2_session_del(nghttp2_session *session) {
nghttp2_mem *mem;
nghttp2_inflight_settings *settings;
if (session == NULL) {
return;
}
mem = &session->mem;
for (settings = session->inflight_settings_head; settings;) {
nghttp2_inflight_settings *next = settings->next;
inflight_settings_del(settings, mem);
settings = next;
}
nghttp2_stream_free(&session->root);
/* Have to free streams first, so that we can check
stream->item->queued */
nghttp2_map_each_free(&session->streams, free_streams, session);
nghttp2_map_free(&session->streams);
ob_q_free(&session->ob_urgent, mem);
ob_q_free(&session->ob_reg, mem);
ob_q_free(&session->ob_syn, mem);
active_outbound_item_reset(&session->aob, mem);
session_inbound_frame_reset(session);
nghttp2_hd_deflate_free(&session->hd_deflater);
nghttp2_hd_inflate_free(&session->hd_inflater);
nghttp2_bufs_free(&session->aob.framebufs);
nghttp2_mem_free(mem, session);
}
int nghttp2_session_reprioritize_stream(
nghttp2_session *session, nghttp2_stream *stream,
const nghttp2_priority_spec *pri_spec_in) {
int rv;
nghttp2_stream *dep_stream = NULL;
nghttp2_priority_spec pri_spec_default;
const nghttp2_priority_spec *pri_spec = pri_spec_in;
//assert(pri_spec->stream_id != stream->stream_id);
if (!nghttp2_stream_in_dep_tree(stream)) {
return 0;
}
if (pri_spec->stream_id != 0) {
dep_stream = nghttp2_session_get_stream_raw(session, pri_spec->stream_id);
if (!dep_stream &&
session_detect_idle_stream(session, pri_spec->stream_id)) {
nghttp2_priority_spec_default_init(&pri_spec_default);
dep_stream = nghttp2_session_open_stream(
session, pri_spec->stream_id, NGHTTP2_FLAG_NONE, &pri_spec_default,
NGHTTP2_STREAM_IDLE, NULL);
if (dep_stream == NULL) {
return NGHTTP2_ERR_NOMEM;
}
} else if (!dep_stream || !nghttp2_stream_in_dep_tree(dep_stream)) {
nghttp2_priority_spec_default_init(&pri_spec_default);
pri_spec = &pri_spec_default;
}
}
if (pri_spec->stream_id == 0) {
dep_stream = &session->root;
} else if (nghttp2_stream_dep_find_ancestor(dep_stream, stream)) {
DEBUGF(fprintf(stderr, "stream: cycle detected, dep_stream(%p)=%d "
"stream(%p)=%d\n",
dep_stream, dep_stream->stream_id, stream,
stream->stream_id));
nghttp2_stream_dep_remove_subtree(dep_stream);
rv = nghttp2_stream_dep_add_subtree(&session->root, dep_stream);
if (rv != 0) {
return rv;
}
}
assert(dep_stream);
if (dep_stream == stream->dep_prev && !pri_spec->exclusive) {
/* This is minor optimization when just weight is changed. */
nghttp2_stream_change_weight(stream, pri_spec->weight);
return 0;
}
nghttp2_stream_dep_remove_subtree(stream);
/* We have to update weight after removing stream from tree */
stream->weight = pri_spec->weight;
if (pri_spec->exclusive) {
rv = nghttp2_stream_dep_insert_subtree(dep_stream, stream);
} else {
rv = nghttp2_stream_dep_add_subtree(dep_stream, stream);
}
if (rv != 0) {
return rv;
}
return 0;
}
int nghttp2_session_add_item(nghttp2_session *session,
nghttp2_outbound_item *item) {
/* TODO Return error if stream is not found for the frame requiring
stream presence. */
int rv = 0;
nghttp2_stream *stream;
nghttp2_frame *frame;
frame = &item->frame;
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
if (frame->hd.type != NGHTTP2_DATA) {
switch (frame->hd.type) {
case NGHTTP2_HEADERS:
/* We push request HEADERS and push response HEADERS to
dedicated queue because their transmission is affected by
SETTINGS_MAX_CONCURRENT_STREAMS */
/* TODO If 2 HEADERS are submitted for reserved stream, then
both of them are queued into ob_syn, which is not
desirable. */
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
(stream && stream->state == NGHTTP2_STREAM_RESERVED)) {
nghttp2_outbound_queue_push(&session->ob_syn, item);
item->queued = 1;
break;
}
nghttp2_outbound_queue_push(&session->ob_reg, item);
item->queued = 1;
break;
case NGHTTP2_SETTINGS:
case NGHTTP2_PING:
nghttp2_outbound_queue_push(&session->ob_urgent, item);
item->queued = 1;
break;
case NGHTTP2_RST_STREAM:
if (stream) {
stream->state = NGHTTP2_STREAM_CLOSING;
}
nghttp2_outbound_queue_push(&session->ob_reg, item);
item->queued = 1;
break;
case NGHTTP2_PUSH_PROMISE: {
nghttp2_headers_aux_data *aux_data;
nghttp2_priority_spec pri_spec;
aux_data = &item->aux_data.headers;
if (!stream) {
return NGHTTP2_ERR_STREAM_CLOSED;
}
nghttp2_priority_spec_init(&pri_spec, stream->stream_id,
NGHTTP2_DEFAULT_WEIGHT, 0);
if (!nghttp2_session_open_stream(
session, frame->push_promise.promised_stream_id,
NGHTTP2_STREAM_FLAG_NONE, &pri_spec, NGHTTP2_STREAM_RESERVED,
aux_data->stream_user_data)) {
return NGHTTP2_ERR_NOMEM;
}
/* We don't have to call nghttp2_session_adjust_closed_stream()
here, since stream->stream_id is local stream_id, and it does
not affect closed stream count. */
nghttp2_outbound_queue_push(&session->ob_reg, item);
item->queued = 1;
break;
}
case NGHTTP2_WINDOW_UPDATE:
if (stream) {
stream->window_update_queued = 1;
} else if (frame->hd.stream_id == 0) {
session->window_update_queued = 1;
}
nghttp2_outbound_queue_push(&session->ob_reg, item);
item->queued = 1;
break;
default:
nghttp2_outbound_queue_push(&session->ob_reg, item);
item->queued = 1;
}
return 0;
}
if (!stream) {
return NGHTTP2_ERR_STREAM_CLOSED;
}
if (stream->item) {
return NGHTTP2_ERR_DATA_EXIST;
}
rv = nghttp2_stream_attach_item(stream, item);
if (rv != 0) {
return rv;
}
return 0;
}
int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
uint32_t error_code) {
int rv;
nghttp2_outbound_item *item;
nghttp2_frame *frame;
nghttp2_stream *stream;
nghttp2_mem *mem;
mem = &session->mem;
stream = nghttp2_session_get_stream(session, stream_id);
if (stream && stream->state == NGHTTP2_STREAM_CLOSING) {
return 0;
}
/* Cancel pending request HEADERS in ob_syn if this RST_STREAM
refers to that stream. */
if (!session->server && nghttp2_session_is_my_stream_id(session, stream_id) &&
nghttp2_outbound_queue_top(&session->ob_syn)) {
nghttp2_headers_aux_data *aux_data;
nghttp2_frame *headers_frame;
headers_frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
assert(headers_frame->hd.type == NGHTTP2_HEADERS);
if (headers_frame->hd.stream_id <= stream_id &&
(uint32_t)stream_id < session->next_stream_id) {
for (item = session->ob_syn.head; item; item = item->qnext) {
aux_data = &item->aux_data.headers;
if (item->frame.hd.stream_id < stream_id) {
continue;
}
/* stream_id in ob_syn queue must be strictly increasing. If
we found larger ID, then we can break here. */
if (item->frame.hd.stream_id > stream_id || aux_data->canceled) {
break;
}
aux_data->error_code = error_code;
aux_data->canceled = 1;
return 0;
}
}
}
item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
if (item == NULL) {
return NGHTTP2_ERR_NOMEM;
}
nghttp2_outbound_item_init(item);
frame = &item->frame;
nghttp2_frame_rst_stream_init(&frame->rst_stream, stream_id, error_code);
rv = nghttp2_session_add_item(session, item);
if (rv != 0) {
nghttp2_frame_rst_stream_free(&frame->rst_stream);
nghttp2_mem_free(mem, item);
return rv;
}
return 0;
}
nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
int32_t stream_id, uint8_t flags,
nghttp2_priority_spec *pri_spec_in,
nghttp2_stream_state initial_state,
void *stream_user_data) {
int rv;
nghttp2_stream *stream;
nghttp2_stream *dep_stream = NULL;
int stream_alloc = 0;
nghttp2_priority_spec pri_spec_default;
nghttp2_priority_spec *pri_spec = pri_spec_in;
nghttp2_mem *mem;
mem = &session->mem;
stream = nghttp2_session_get_stream_raw(session, stream_id);
if (stream) {
assert(stream->state == NGHTTP2_STREAM_IDLE);
assert(nghttp2_stream_in_dep_tree(stream));
nghttp2_session_detach_idle_stream(session, stream);