-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
ngx_http_v2.c
4903 lines (3615 loc) · 132 KB
/
ngx_http_v2.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
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_http_v2_module.h>
/* errors */
#define NGX_HTTP_V2_NO_ERROR 0x0
#define NGX_HTTP_V2_PROTOCOL_ERROR 0x1
#define NGX_HTTP_V2_INTERNAL_ERROR 0x2
#define NGX_HTTP_V2_FLOW_CTRL_ERROR 0x3
#define NGX_HTTP_V2_SETTINGS_TIMEOUT 0x4
#define NGX_HTTP_V2_STREAM_CLOSED 0x5
#define NGX_HTTP_V2_SIZE_ERROR 0x6
#define NGX_HTTP_V2_REFUSED_STREAM 0x7
#define NGX_HTTP_V2_CANCEL 0x8
#define NGX_HTTP_V2_COMP_ERROR 0x9
#define NGX_HTTP_V2_CONNECT_ERROR 0xa
#define NGX_HTTP_V2_ENHANCE_YOUR_CALM 0xb
#define NGX_HTTP_V2_INADEQUATE_SECURITY 0xc
#define NGX_HTTP_V2_HTTP_1_1_REQUIRED 0xd
/* frame sizes */
#define NGX_HTTP_V2_SETTINGS_ACK_SIZE 0
#define NGX_HTTP_V2_RST_STREAM_SIZE 4
#define NGX_HTTP_V2_PRIORITY_SIZE 5
#define NGX_HTTP_V2_PING_SIZE 8
#define NGX_HTTP_V2_GOAWAY_SIZE 8
#define NGX_HTTP_V2_WINDOW_UPDATE_SIZE 4
#define NGX_HTTP_V2_SETTINGS_PARAM_SIZE 6
/* settings fields */
#define NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING 0x1
#define NGX_HTTP_V2_ENABLE_PUSH_SETTING 0x2
#define NGX_HTTP_V2_MAX_STREAMS_SETTING 0x3
#define NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING 0x4
#define NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING 0x5
#define NGX_HTTP_V2_FRAME_BUFFER_SIZE 24
#define NGX_HTTP_V2_ROOT (void *) -1
static void ngx_http_v2_read_handler(ngx_event_t *rev);
static void ngx_http_v2_write_handler(ngx_event_t *wev);
static void ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c);
static void ngx_http_v2_lingering_close(ngx_connection_t *c);
static void ngx_http_v2_lingering_close_handler(ngx_event_t *rev);
static u_char *ngx_http_v2_state_preface(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_preface_end(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_head(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_read_data(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_header_block(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_len(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_raw(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_skip(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_handle_continuation(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
static u_char *ngx_http_v2_state_priority(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_rst_stream(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
static u_char *ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
static u_char *ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
ngx_uint_t err);
static ngx_int_t ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c,
u_char **pos, u_char *end, ngx_uint_t prefix);
static ngx_http_v2_stream_t *ngx_http_v2_create_stream(
ngx_http_v2_connection_t *h2c);
static ngx_http_v2_node_t *ngx_http_v2_get_node_by_id(
ngx_http_v2_connection_t *h2c, ngx_uint_t sid, ngx_uint_t alloc);
static ngx_http_v2_node_t *ngx_http_v2_get_closed_node(
ngx_http_v2_connection_t *h2c);
#define ngx_http_v2_index_size(h2scf) (h2scf->streams_index_mask + 1)
#define ngx_http_v2_index(h2scf, sid) ((sid >> 1) & h2scf->streams_index_mask)
static ngx_int_t ngx_http_v2_send_settings(ngx_http_v2_connection_t *h2c);
static ngx_int_t ngx_http_v2_settings_frame_handler(
ngx_http_v2_connection_t *h2c, ngx_http_v2_out_frame_t *frame);
static ngx_int_t ngx_http_v2_send_window_update(ngx_http_v2_connection_t *h2c,
ngx_uint_t sid, size_t window);
static ngx_int_t ngx_http_v2_send_rst_stream(ngx_http_v2_connection_t *h2c,
ngx_uint_t sid, ngx_uint_t status);
static ngx_int_t ngx_http_v2_send_goaway(ngx_http_v2_connection_t *h2c,
ngx_uint_t status);
static ngx_http_v2_out_frame_t *ngx_http_v2_get_frame(
ngx_http_v2_connection_t *h2c, size_t length, ngx_uint_t type,
u_char flags, ngx_uint_t sid);
static ngx_int_t ngx_http_v2_frame_handler(ngx_http_v2_connection_t *h2c,
ngx_http_v2_out_frame_t *frame);
static ngx_int_t ngx_http_v2_validate_header(ngx_http_request_t *r,
ngx_http_v2_header_t *header);
static ngx_int_t ngx_http_v2_pseudo_header(ngx_http_request_t *r,
ngx_http_v2_header_t *header);
static ngx_int_t ngx_http_v2_parse_path(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_method(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_scheme(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_authority(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_construct_request_line(ngx_http_request_t *r);
static ngx_int_t ngx_http_v2_cookie(ngx_http_request_t *r,
ngx_http_v2_header_t *header);
static ngx_int_t ngx_http_v2_construct_cookie_header(ngx_http_request_t *r);
static void ngx_http_v2_run_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_v2_process_request_body(ngx_http_request_t *r,
u_char *pos, size_t size, ngx_uint_t last, ngx_uint_t flush);
static ngx_int_t ngx_http_v2_filter_request_body(ngx_http_request_t *r);
static void ngx_http_v2_read_client_request_body_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
ngx_http_v2_stream_t *stream, ngx_uint_t status);
static void ngx_http_v2_close_stream_handler(ngx_event_t *ev);
static void ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev);
static void ngx_http_v2_handle_connection_handler(ngx_event_t *rev);
static void ngx_http_v2_idle_handler(ngx_event_t *rev);
static void ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
ngx_uint_t status);
static ngx_int_t ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c,
ssize_t delta);
static void ngx_http_v2_set_dependency(ngx_http_v2_connection_t *h2c,
ngx_http_v2_node_t *node, ngx_uint_t depend, ngx_uint_t exclusive);
static void ngx_http_v2_node_children_update(ngx_http_v2_node_t *node);
static void ngx_http_v2_pool_cleanup(void *data);
static ngx_http_v2_handler_pt ngx_http_v2_frame_states[] = {
ngx_http_v2_state_data, /* NGX_HTTP_V2_DATA_FRAME */
ngx_http_v2_state_headers, /* NGX_HTTP_V2_HEADERS_FRAME */
ngx_http_v2_state_priority, /* NGX_HTTP_V2_PRIORITY_FRAME */
ngx_http_v2_state_rst_stream, /* NGX_HTTP_V2_RST_STREAM_FRAME */
ngx_http_v2_state_settings, /* NGX_HTTP_V2_SETTINGS_FRAME */
ngx_http_v2_state_push_promise, /* NGX_HTTP_V2_PUSH_PROMISE_FRAME */
ngx_http_v2_state_ping, /* NGX_HTTP_V2_PING_FRAME */
ngx_http_v2_state_goaway, /* NGX_HTTP_V2_GOAWAY_FRAME */
ngx_http_v2_state_window_update, /* NGX_HTTP_V2_WINDOW_UPDATE_FRAME */
ngx_http_v2_state_continuation /* NGX_HTTP_V2_CONTINUATION_FRAME */
};
#define NGX_HTTP_V2_FRAME_STATES \
(sizeof(ngx_http_v2_frame_states) / sizeof(ngx_http_v2_handler_pt))
void
ngx_http_v2_init(ngx_event_t *rev)
{
u_char *p, *end;
ngx_connection_t *c;
ngx_pool_cleanup_t *cln;
ngx_http_connection_t *hc;
ngx_http_v2_srv_conf_t *h2scf;
ngx_http_v2_main_conf_t *h2mcf;
ngx_http_v2_connection_t *h2c;
ngx_http_core_srv_conf_t *cscf;
c = rev->data;
hc = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "init http2 connection");
c->log->action = "processing HTTP/2 connection";
h2mcf = ngx_http_get_module_main_conf(hc->conf_ctx, ngx_http_v2_module);
if (h2mcf->recv_buffer == NULL) {
h2mcf->recv_buffer = ngx_palloc(ngx_cycle->pool,
h2mcf->recv_buffer_size);
if (h2mcf->recv_buffer == NULL) {
ngx_http_close_connection(c);
return;
}
}
h2c = ngx_pcalloc(c->pool, sizeof(ngx_http_v2_connection_t));
if (h2c == NULL) {
ngx_http_close_connection(c);
return;
}
h2c->connection = c;
h2c->http_connection = hc;
h2c->send_window = NGX_HTTP_V2_DEFAULT_WINDOW;
h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;
h2c->init_window = NGX_HTTP_V2_DEFAULT_WINDOW;
h2c->frame_size = NGX_HTTP_V2_DEFAULT_FRAME_SIZE;
h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);
h2c->priority_limit = ngx_max(h2scf->concurrent_streams, 100);
h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
if (h2c->pool == NULL) {
ngx_http_close_connection(c);
return;
}
cln = ngx_pool_cleanup_add(c->pool, 0);
if (cln == NULL) {
ngx_http_close_connection(c);
return;
}
cln->handler = ngx_http_v2_pool_cleanup;
cln->data = h2c;
h2c->streams_index = ngx_pcalloc(c->pool, ngx_http_v2_index_size(h2scf)
* sizeof(ngx_http_v2_node_t *));
if (h2c->streams_index == NULL) {
ngx_http_close_connection(c);
return;
}
if (ngx_http_v2_send_settings(h2c) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
if (ngx_http_v2_send_window_update(h2c, 0, NGX_HTTP_V2_MAX_WINDOW
- NGX_HTTP_V2_DEFAULT_WINDOW)
== NGX_ERROR)
{
ngx_http_close_connection(c);
return;
}
h2c->state.handler = ngx_http_v2_state_preface;
ngx_queue_init(&h2c->waiting);
ngx_queue_init(&h2c->dependencies);
ngx_queue_init(&h2c->closed);
c->data = h2c;
rev->handler = ngx_http_v2_read_handler;
c->write->handler = ngx_http_v2_write_handler;
if (!rev->timer_set) {
cscf = ngx_http_get_module_srv_conf(hc->conf_ctx,
ngx_http_core_module);
ngx_add_timer(rev, cscf->client_header_timeout);
}
c->idle = 1;
ngx_reusable_connection(c, 0);
if (c->buffer) {
p = c->buffer->pos;
end = c->buffer->last;
do {
p = h2c->state.handler(h2c, p, end);
if (p == NULL) {
return;
}
} while (p != end);
h2c->total_bytes += p - c->buffer->pos;
c->buffer->pos = p;
}
ngx_http_v2_read_handler(rev);
}
static void
ngx_http_v2_read_handler(ngx_event_t *rev)
{
u_char *p, *end;
size_t available;
ssize_t n;
ngx_connection_t *c;
ngx_http_v2_main_conf_t *h2mcf;
ngx_http_v2_connection_t *h2c;
c = rev->data;
h2c = c->data;
if (rev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 read handler");
h2c->blocked = 1;
if (c->close) {
c->close = 0;
if (c->error) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
if (!h2c->processing) {
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
return;
}
if (!h2c->goaway) {
h2c->goaway = 1;
if (ngx_http_v2_send_goaway(h2c, NGX_HTTP_V2_NO_ERROR)
== NGX_ERROR)
{
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
}
h2c->blocked = 0;
return;
}
h2mcf = ngx_http_get_module_main_conf(h2c->http_connection->conf_ctx,
ngx_http_v2_module);
available = h2mcf->recv_buffer_size - 2 * NGX_HTTP_V2_STATE_BUFFER_SIZE;
do {
p = h2mcf->recv_buffer;
ngx_memcpy(p, h2c->state.buffer, NGX_HTTP_V2_STATE_BUFFER_SIZE);
end = p + h2c->state.buffer_used;
n = c->recv(c, end, available);
if (n == NGX_AGAIN) {
break;
}
if (n == 0 && (h2c->state.incomplete || h2c->processing)) {
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"client prematurely closed connection");
}
if (n == 0 || n == NGX_ERROR) {
c->error = 1;
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
end += n;
h2c->state.buffer_used = 0;
h2c->state.incomplete = 0;
do {
p = h2c->state.handler(h2c, p, end);
if (p == NULL) {
return;
}
} while (p != end);
h2c->total_bytes += n;
if (h2c->total_bytes / 8 > h2c->payload_bytes + 1048576) {
ngx_log_error(NGX_LOG_INFO, c->log, 0, "http2 flood detected");
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
return;
}
} while (rev->ready);
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
return;
}
if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
h2c->blocked = 0;
ngx_http_v2_handle_connection(h2c);
}
static void
ngx_http_v2_write_handler(ngx_event_t *wev)
{
ngx_int_t rc;
ngx_connection_t *c;
ngx_http_v2_connection_t *h2c;
c = wev->data;
h2c = c->data;
if (wev->timedout) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 write event timed out");
c->error = 1;
c->timedout = 1;
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 write handler");
if (h2c->last_out == NULL && !c->buffered) {
if (wev->timer_set) {
ngx_del_timer(wev);
}
ngx_http_v2_handle_connection(h2c);
return;
}
h2c->blocked = 1;
rc = ngx_http_v2_send_output_queue(h2c);
if (rc == NGX_ERROR) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
h2c->blocked = 0;
if (rc == NGX_AGAIN) {
return;
}
ngx_http_v2_handle_connection(h2c);
}
ngx_int_t
ngx_http_v2_send_output_queue(ngx_http_v2_connection_t *h2c)
{
int tcp_nodelay;
ngx_chain_t *cl;
ngx_event_t *wev;
ngx_connection_t *c;
ngx_http_v2_out_frame_t *out, *frame, *fn;
ngx_http_core_loc_conf_t *clcf;
c = h2c->connection;
wev = c->write;
if (c->error) {
goto error;
}
if (!wev->ready) {
return NGX_AGAIN;
}
cl = NULL;
out = NULL;
for (frame = h2c->last_out; frame; frame = fn) {
frame->last->next = cl;
cl = frame->first;
fn = frame->next;
frame->next = out;
out = frame;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 frame out: %p sid:%ui bl:%d len:%uz",
out, out->stream ? out->stream->node->id : 0,
out->blocked, out->length);
}
cl = c->send_chain(c, cl, 0);
if (cl == NGX_CHAIN_ERROR) {
goto error;
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
goto error;
}
if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
if (ngx_tcp_push(c->fd) == -1) {
ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
goto error;
}
c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
} else {
tcp_nodelay = 1;
}
if (tcp_nodelay && clcf->tcp_nodelay && ngx_tcp_nodelay(c) != NGX_OK) {
goto error;
}
for ( /* void */ ; out; out = fn) {
fn = out->next;
if (out->handler(h2c, out) != NGX_OK) {
out->blocked = 1;
break;
}
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 frame sent: %p sid:%ui bl:%d len:%uz",
out, out->stream ? out->stream->node->id : 0,
out->blocked, out->length);
}
frame = NULL;
for ( /* void */ ; out; out = fn) {
fn = out->next;
out->next = frame;
frame = out;
}
h2c->last_out = frame;
if (!wev->ready) {
ngx_add_timer(wev, clcf->send_timeout);
return NGX_AGAIN;
}
if (wev->timer_set) {
ngx_del_timer(wev);
}
return NGX_OK;
error:
c->error = 1;
if (!h2c->blocked) {
ngx_post_event(wev, &ngx_posted_events);
}
return NGX_ERROR;
}
static void
ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c)
{
ngx_int_t rc;
ngx_connection_t *c;
ngx_http_core_loc_conf_t *clcf;
if (h2c->last_out || h2c->processing) {
return;
}
c = h2c->connection;
if (c->error) {
ngx_http_close_connection(c);
return;
}
if (c->buffered) {
h2c->blocked = 1;
rc = ngx_http_v2_send_output_queue(h2c);
h2c->blocked = 0;
if (rc == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
if (rc == NGX_AGAIN) {
return;
}
/* rc == NGX_OK */
}
if (h2c->goaway) {
ngx_http_v2_lingering_close(c);
return;
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (!c->read->timer_set) {
ngx_add_timer(c->read, clcf->keepalive_timeout);
}
ngx_reusable_connection(c, 1);
if (h2c->state.incomplete) {
return;
}
ngx_destroy_pool(h2c->pool);
h2c->pool = NULL;
h2c->free_frames = NULL;
h2c->frames = 0;
h2c->free_fake_connections = NULL;
#if (NGX_HTTP_SSL)
if (c->ssl) {
ngx_ssl_free_buffer(c);
}
#endif
c->destroyed = 1;
c->write->handler = ngx_http_empty_handler;
c->read->handler = ngx_http_v2_idle_handler;
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
}
static void
ngx_http_v2_lingering_close(ngx_connection_t *c)
{
ngx_event_t *rev, *wev;
ngx_http_v2_connection_t *h2c;
ngx_http_core_loc_conf_t *clcf;
h2c = c->data;
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (clcf->lingering_close == NGX_HTTP_LINGERING_OFF) {
ngx_http_close_connection(c);
return;
}
if (h2c->lingering_time == 0) {
h2c->lingering_time = ngx_time()
+ (time_t) (clcf->lingering_time / 1000);
}
#if (NGX_HTTP_SSL)
if (c->ssl) {
ngx_int_t rc;
rc = ngx_ssl_shutdown(c);
if (rc == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
if (rc == NGX_AGAIN) {
c->ssl->handler = ngx_http_v2_lingering_close;
return;
}
}
#endif
rev = c->read;
rev->handler = ngx_http_v2_lingering_close_handler;
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
wev = c->write;
wev->handler = ngx_http_empty_handler;
if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
}
if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
ngx_connection_error(c, ngx_socket_errno,
ngx_shutdown_socket_n " failed");
ngx_http_close_connection(c);
return;
}
c->close = 0;
ngx_reusable_connection(c, 1);
ngx_add_timer(rev, clcf->lingering_timeout);
if (rev->ready) {
ngx_http_v2_lingering_close_handler(rev);
}
}
static void
ngx_http_v2_lingering_close_handler(ngx_event_t *rev)
{
ssize_t n;
ngx_msec_t timer;
ngx_connection_t *c;
ngx_http_core_loc_conf_t *clcf;
ngx_http_v2_connection_t *h2c;
u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
c = rev->data;
h2c = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 lingering close handler");
if (rev->timedout || c->close) {
ngx_http_close_connection(c);
return;
}
timer = (ngx_msec_t) h2c->lingering_time - (ngx_msec_t) ngx_time();
if ((ngx_msec_int_t) timer <= 0) {
ngx_http_close_connection(c);
return;
}
do {
n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %z", n);
if (n == NGX_AGAIN) {
break;
}
if (n == NGX_ERROR || n == 0) {
ngx_http_close_connection(c);
return;
}
} while (rev->ready);
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
timer *= 1000;
if (timer > clcf->lingering_timeout) {
timer = clcf->lingering_timeout;
}
ngx_add_timer(rev, timer);
}
static u_char *
ngx_http_v2_state_preface(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
static const u_char preface[] = NGX_HTTP_V2_PREFACE_START;
if ((size_t) (end - pos) < sizeof(preface) - 1) {
return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_preface);
}
if (ngx_memcmp(pos, preface, sizeof(preface) - 1) != 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"invalid connection preface");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
return ngx_http_v2_state_preface_end(h2c, pos + sizeof(preface) - 1, end);
}
static u_char *
ngx_http_v2_state_preface_end(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
static const u_char preface[] = NGX_HTTP_V2_PREFACE_END;
if ((size_t) (end - pos) < sizeof(preface) - 1) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_preface_end);
}
if (ngx_memcmp(pos, preface, sizeof(preface) - 1) != 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"invalid connection preface");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 preface verified");
return ngx_http_v2_state_head(h2c, pos + sizeof(preface) - 1, end);
}
static u_char *
ngx_http_v2_state_head(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
{
uint32_t head;
ngx_uint_t type;
if (end - pos < NGX_HTTP_V2_FRAME_HEADER_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_head);
}
head = ngx_http_v2_parse_uint32(pos);
h2c->state.length = ngx_http_v2_parse_length(head);
h2c->state.flags = pos[4];
h2c->state.sid = ngx_http_v2_parse_sid(&pos[5]);
pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;
type = ngx_http_v2_parse_type(head);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 frame type:%ui f:%Xd l:%uz sid:%ui",
type, h2c->state.flags, h2c->state.length, h2c->state.sid);
if (type >= NGX_HTTP_V2_FRAME_STATES) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent frame with unknown type %ui", type);
return ngx_http_v2_state_skip(h2c, pos, end);
}
return ngx_http_v2_frame_states[type](h2c, pos, end);
}
static u_char *
ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
{
size_t size;
ngx_http_v2_node_t *node;
ngx_http_v2_stream_t *stream;
size = h2c->state.length;
if (h2c->state.flags & NGX_HTTP_V2_PADDED_FLAG) {
if (h2c->state.length == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent padded DATA frame "
"with incorrect length: 0");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos == 0) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_data);
}
h2c->state.padding = *pos++;
if (h2c->state.padding >= size) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent padded DATA frame "
"with incorrect length: %uz, padding: %uz",
size, h2c->state.padding);
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->state.length -= 1 + h2c->state.padding;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 DATA frame");
if (h2c->state.sid == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent DATA frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (size > h2c->recv_window) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated connection flow control: "
"received DATA frame length %uz, available window %uz",
size, h2c->recv_window);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
}
h2c->recv_window -= size;
if (h2c->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4) {
if (ngx_http_v2_send_window_update(h2c, 0, NGX_HTTP_V2_MAX_WINDOW
- h2c->recv_window)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;
}
node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);
if (node == NULL || node->stream == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"unknown http2 stream");
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
stream = node->stream;
if (size > stream->recv_window) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated flow control for stream %ui: "