-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathh2_stream.lua
1416 lines (1306 loc) · 47.2 KB
/
h2_stream.lua
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
local cqueues = require "cqueues"
local monotime = cqueues.monotime
local cc = require "cqueues.condition"
local ce = require "cqueues.errno"
local new_fifo = require "fifo"
local band = require "http.bit".band
local bor = require "http.bit".bor
local h2_error = require "http.h2_error"
local h2_errors = h2_error.errors
local stream_common = require "http.stream_common"
local spack = string.pack or require "compat53.string".pack -- luacheck: ignore 143
local sunpack = string.unpack or require "compat53.string".unpack -- luacheck: ignore 143
local unpack = table.unpack or unpack -- luacheck: ignore 113 143
local assert = assert
if _VERSION:match("%d+%.?%d*") < "5.3" then
assert = require "compat53.module".assert
end
local MAX_HEADER_BUFFER_SIZE = 400*1024 -- 400 KB is max size in h2o
local known_settings = {}
for i, s in pairs({
[0x1] = "HEADER_TABLE_SIZE";
[0x2] = "ENABLE_PUSH";
[0x3] = "MAX_CONCURRENT_STREAMS";
[0x4] = "INITIAL_WINDOW_SIZE";
[0x5] = "MAX_FRAME_SIZE";
[0x6] = "MAX_HEADER_LIST_SIZE";
[0x8] = "SETTINGS_ENABLE_CONNECT_PROTOCOL";
[0x10] = "TLS_RENEG_PERMITTED";
}) do
known_settings[i] = s
known_settings[s] = i
end
local frame_types = {
[0x0] = "DATA";
[0x1] = "HEADERS";
[0x2] = "PRIORITY";
[0x3] = "RST_STREAM";
[0x4] = "SETTING";
[0x5] = "PUSH_PROMISE";
[0x6] = "PING";
[0x7] = "GOAWAY";
[0x8] = "WINDOW_UPDATE";
[0x9] = "CONTINUATION";
[0xa] = "ALTSVC";
[0xc] = "ORIGIN";
}
for i=0x0, 0x9 do
frame_types[frame_types[i]] = i
end
local frame_handlers = {}
local stream_methods = {}
for k, v in pairs(stream_common.methods) do
stream_methods[k] = v
end
local stream_mt = {
__name = "http.h2_stream";
__index = stream_methods;
}
function stream_mt:__tostring()
local dependee_list = {}
for s in pairs(self.dependees) do
dependee_list[#dependee_list+1] = string.format("%d", s.id)
end
table.sort(dependee_list)
dependee_list = table.concat(dependee_list, ",")
return string.format("http.h2_stream{connection=%s;id=%s;state=%q;parent=%s;dependees={%s}}",
tostring(self.connection), tostring(self.id), self.state,
(self.parent and tostring(self.parent.id) or "nil"), dependee_list)
end
local function new_stream(connection)
local self = setmetatable({
connection = connection;
type = connection.type;
state = "idle";
id = nil;
peer_flow_credits = 0;
peer_flow_credits_change = cc.new();
parent = nil;
dependees = setmetatable({}, {__mode="kv"});
weight = 16; -- http2 spec, section 5.3.5
rst_stream_error = nil;
stats_sent_headers = 0; -- number of header blocks sent
stats_recv_headers = 0; -- number of header blocks received
stats_sent = 0; -- #bytes sent in DATA blocks
stats_recv = 0; -- #bytes received in DATA blocks
recv_headers_fifo = new_fifo();
recv_headers_cond = cc.new();
chunk_fifo = new_fifo();
chunk_cond = cc.new();
end_stream_after_continuation = nil;
content_length = nil;
}, stream_mt)
return self
end
function stream_methods:pick_id(id)
assert(self.id == nil)
if id == nil then
if self.connection.recv_goaway_lowest then
h2_error.errors.PROTOCOL_ERROR("Receivers of a GOAWAY frame MUST NOT open additional streams on the connection")
end
if self.type == "client" then
-- Pick next free odd number
id = self.connection.highest_odd_stream + 2
self.connection.highest_odd_stream = id
else
-- Pick next free even number
id = self.connection.highest_even_stream + 2
self.connection.highest_even_stream = id
end
self.id = id
else
assert(type(id) == "number" and id >= 0 and id <= 0x7fffffff and id % 1 == 0, "invalid stream id")
assert(self.connection.streams[id] == nil)
self.id = id
if id % 2 == 0 then
if id > self.connection.highest_even_stream then
self.connection.highest_even_stream = id
end
-- stream 'already' existed but was possibly collected. see http2 spec 5.1.1
if id <= self.connection.highest_even_non_idle_stream then
self:set_state("closed")
end
else
if id > self.connection.highest_odd_stream then
self.connection.highest_odd_stream = id
end
-- stream 'already' existed but was possibly collected. see http2 spec 5.1.1
if id <= self.connection.highest_odd_non_idle_stream then
self:set_state("closed")
end
end
end
-- TODO: check MAX_CONCURRENT_STREAMS
self.connection.streams[id] = self
if id == 0 then
self.connection.stream0 = self
else
self.peer_flow_credits = self.connection.peer_settings[known_settings.INITIAL_WINDOW_SIZE]
self.peer_flow_credits_change:signal()
-- Add dependency on stream 0. http2 spec, 5.3.1
self.connection.stream0:reprioritise(self)
end
return true
end
local valid_states = {
["idle"] = 1; -- initial
["open"] = 2; -- have sent or received headers; haven't sent body yet
["reserved (local)"] = 2; -- have sent a PUSH_PROMISE
["reserved (remote)"] = 2; -- have received a PUSH_PROMISE
["half closed (local)"] = 3; -- have sent whole body
["half closed (remote)"] = 3; -- have received whole body
["closed"] = 4; -- complete
}
function stream_methods:set_state(new)
local new_order = assert(valid_states[new])
local old = self.state
if new_order <= valid_states[old] then
error("invalid state progression ('"..old.."' to '"..new.."')")
end
if new ~= "closed" then
assert(self.id)
end
self.state = new
if new == "closed" or new == "half closed (remote)" then
self.recv_headers_cond:signal()
self.chunk_cond:signal()
end
if old == "idle" then
if self.id % 2 == 0 then
if self.id > self.connection.highest_even_non_idle_stream then
self.connection.highest_even_non_idle_stream = self.id
end
else
if self.id > self.connection.highest_odd_non_idle_stream then
self.connection.highest_odd_non_idle_stream = self.id
end
end
end
if old == "idle" and new ~= "closed" then
self.connection.n_active_streams = self.connection.n_active_streams + 1
elseif old ~= "idle" and new == "closed" then
local n_active_streams = self.connection.n_active_streams - 1
self.connection.n_active_streams = n_active_streams
if n_active_streams == 0 then
self.connection:onidle()(self.connection)
end
end
end
function stream_methods:write_http2_frame(typ, flags, payload, timeout, flush)
local stream_id = assert(self.id, "stream has unset id")
return self.connection:write_http2_frame(typ, flags, stream_id, payload, timeout, flush)
end
function stream_methods:reprioritise(child, exclusive)
assert(child)
assert(child.id)
assert(child.id ~= 0) -- cannot reprioritise stream 0
if self == child then
-- http2 spec, section 5.3.1
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("A stream cannot depend on itself", true), ce.EILSEQ
end
do -- Check if the child is an ancestor
local ancestor = self.parent
while ancestor do
if ancestor == child then
-- Break the loop. http spec, section 5.3.3
local ok, err, errno = child.parent:reprioritise(self, false)
if not ok then
return nil, err, errno
end
break
end
ancestor = ancestor.parent
end
end
-- Remove old parent
if child.parent then
child.parent.dependees[child] = nil
end
-- We are now the parent
child.parent = self
if exclusive then
-- All the parent's deps are now the child's
for s, v in pairs(self.dependees) do
s.parent = child
child.dependees[s] = v
self.dependees[s] = nil
end
else
self.dependees[child] = true
end
return true
end
local chunk_methods = {}
local chunk_mt = {
__name = "http.h2_stream.chunk";
__index = chunk_methods;
}
local function new_chunk(original_length, data)
return setmetatable({
original_length = original_length;
acked = false;
data = data;
}, chunk_mt)
end
function chunk_methods:ack()
if self.acked then
return 0
else
self.acked = true
return self.original_length
end
end
frame_handlers[frame_types.DATA] = function(stream, flags, payload, deadline) -- luacheck: ignore 212
if stream.id == 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'DATA' framess MUST be associated with a stream"), ce.EILSEQ
end
if stream.state == "idle" or stream.state == "reserved (remote)" then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'DATA' frames not allowed in 'idle' state"), ce.EILSEQ
elseif stream.state ~= "open" and stream.state ~= "half closed (local)" then
return nil, h2_errors.STREAM_CLOSED:new_traceback("'DATA' frames not allowed in '" .. stream.state .. "' state"), ce.EILSEQ
end
local end_stream = band(flags, 0x1) ~= 0
local padded = band(flags, 0x8) ~= 0
local original_length = #payload
if padded then
local pad_len = sunpack("> B", payload)
if pad_len >= #payload then -- >= will take care of the pad_len itself
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("length of the padding is the length of the frame payload or greater"), ce.EILSEQ
elseif payload:match("[^%z]", -pad_len) then
-- 6.1: A receiver is not obligated to verify padding but MAY treat non-zero padding as a connection error of type PROTOCOL_ERROR.
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("padding not null bytes"), ce.EILSEQ
end
payload = payload:sub(2, -pad_len-1)
end
local stats_recv = stream.stats_recv + #payload
if stream.content_length and stats_recv > stream.content_length then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("content-length exceeded", true), ce.EILSEQ
end
local chunk = new_chunk(original_length, payload)
stream.chunk_fifo:push(chunk)
stream.stats_recv = stats_recv
if end_stream then
stream.chunk_fifo:push(nil)
-- chunk_cond gets signaled by :set_state
if stream.state == "half closed (local)" then
stream:set_state("closed")
else
stream:set_state("half closed (remote)")
end
else
stream.chunk_cond:signal()
end
return true
end
function stream_methods:write_data_frame(payload, end_stream, padded, timeout, flush)
if self.id == 0 then
h2_errors.PROTOCOL_ERROR("'DATA' frames MUST be associated with a stream")
end
if self.state ~= "open" and self.state ~= "half closed (remote)" then
h2_errors.STREAM_CLOSED("'DATA' frame not allowed in '" .. self.state .. "' state")
end
local pad_len, padding = "", ""
local flags = 0
if end_stream then
flags = bor(flags, 0x1)
end
if padded then
flags = bor(flags, 0x8)
pad_len = spack("> B", padded)
padding = ("\0"):rep(padded)
end
payload = pad_len .. payload .. padding
-- The entire DATA frame payload is included in flow control,
-- including Pad Length and Padding fields if present
local new_stream_peer_flow_credits = self.peer_flow_credits - #payload
local new_connection_peer_flow_credits = self.connection.peer_flow_credits - #payload
if new_stream_peer_flow_credits < 0 or new_connection_peer_flow_credits < 0 then
h2_errors.FLOW_CONTROL_ERROR("not enough flow credits")
end
local ok, err, errno = self:write_http2_frame(frame_types.DATA, flags, payload, timeout, flush)
if not ok then return nil, err, errno end
self.peer_flow_credits = new_stream_peer_flow_credits
self.connection.peer_flow_credits = new_connection_peer_flow_credits
self.stats_sent = self.stats_sent + #payload
if end_stream then
if self.state == "half closed (remote)" then
self:set_state("closed")
else
self:set_state("half closed (local)")
end
end
return ok
end
-- Map from header name to whether it belongs in a request (vs a response)
local valid_pseudo_headers = {
[":method"] = true;
[":scheme"] = true;
[":path"] = true;
[":authority"] = true;
[":status"] = false;
}
local function validate_headers(headers, is_request, nth_header, ended_stream)
-- Section 8.1.2: A request or response containing uppercase header field names MUST be treated as malformed
for name in headers:each() do
if name:lower() ~= name then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("header field names MUST be lowercase", true), ce.EINVAL
end
end
do -- Section 8.1.2.1: Validate that all colon fields are before other ones
local seen_non_colon = false
for name, value in headers:each() do
if name:sub(1,1) == ":" then
--[[ Pseudo-header fields are only valid in the context in
which they are defined. Pseudo-header fields defined for
requests MUST NOT appear in responses; pseudo-header fields
defined for responses MUST NOT appear in requests.
Pseudo-header fields MUST NOT appear in trailers.
Endpoints MUST treat a request or response that contains
undefined or invalid pseudo-header fields as malformed]]
if (is_request and nth_header ~= 1) or valid_pseudo_headers[name] ~= is_request then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("Pseudo-header fields are only valid in the context in which they are defined", true), ce.EILSEQ
end
if seen_non_colon then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("All pseudo-header fields MUST appear in the header block before regular header fields", true), ce.EILSEQ
end
else
seen_non_colon = true
end
if type(value) ~= "string" then
return nil, "invalid header field", ce.EINVAL
end
end
end
if headers:has("connection") then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("An endpoint MUST NOT generate an HTTP/2 message containing connection-specific header fields", true), ce.EILSEQ
end
local te = headers:get_as_sequence("te")
if te.n > 0 and (te[1] ~= "trailers" or te.n ~= 1) then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback([[The TE header field, which MAY be present in an HTTP/2 request; when it is, it MUST NOT contain any value other than "trailers"]], true), ce.EILSEQ
end
if is_request then
if nth_header == 1 then
--[[ All HTTP/2 requests MUST include exactly one valid value for the :method, :scheme,
and :path pseudo-header fields, unless it is a CONNECT request (Section 8.3).
An HTTP request that omits mandatory pseudo-header fields is malformed (Section 8.1.2.6).]]
local methods = headers:get_as_sequence(":method")
if methods.n ~= 1 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("requests MUST include exactly one valid value for the :method, :scheme, and :path pseudo-header fields, unless it is a CONNECT request", true), ce.EILSEQ
elseif methods[1] ~= "CONNECT" then
local scheme = headers:get_as_sequence(":scheme")
local path = headers:get_as_sequence(":path")
if scheme.n ~= 1 or path.n ~= 1 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("requests MUST include exactly one valid value for the :method, :scheme, and :path pseudo-header fields, unless it is a CONNECT request", true), ce.EILSEQ
end
if path[1] == "" and (scheme[1] == "http" or scheme[1] == "https") then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("The :path pseudo-header field MUST NOT be empty for http or https URIs", true), ce.EILSEQ
end
else -- is CONNECT method
-- Section 8.3
if headers:has(":scheme") or headers:has(":path") then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("For a CONNECT request, the :scheme and :path pseudo-header fields MUST be omitted", true), ce.EILSEQ
end
end
elseif nth_header == 2 then
if not ended_stream then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("Trailers MUST be at end of stream", true), ce.EILSEQ
end
elseif nth_header > 2 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("An HTTP request consists of maximum 2 HEADER blocks", true), ce.EILSEQ
end
else
--[[ For HTTP/2 responses, a single :status pseudo-header field is
defined that carries the HTTP status code field (RFC7231, Section 6).
This pseudo-header field MUST be included in all responses; otherwise,
the response is malformed (Section 8.1.2.6)]]
if not headers:has(":status") then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback(":status pseudo-header field MUST be included in all responses", true), ce.EILSEQ
end
end
return true
end
local function process_end_headers(stream, end_stream, pad_len, pos, promised_stream, payload)
if pad_len > 0 then
if pad_len + pos - 1 > #payload then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("length of the padding is the length of the frame payload or greater"), ce.EILSEQ
elseif payload:match("[^%z]", -pad_len) then
-- 6.2: Padding fields and flags are identical to those defined for DATA frames
-- 6.1: A receiver is not obligated to verify padding but MAY treat non-zero padding as a connection error of type PROTOCOL_ERROR.
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("padding not null bytes"), ce.EILSEQ
end
payload = payload:sub(1, -pad_len-1)
end
local headers, newpos, errno = stream.connection.decoding_context:decode_headers(payload, nil, pos)
if not headers then
return nil, newpos, errno
end
if newpos ~= #payload + 1 then
return nil, h2_errors.COMPRESSION_ERROR:new_traceback("incomplete header fragment"), ce.EILSEQ
end
if not promised_stream then
stream.stats_recv_headers = stream.stats_recv_headers + 1
local validate_ok, validate_err, errno2 = validate_headers(headers, stream.type ~= "client", stream.stats_recv_headers, end_stream)
if not validate_ok then
return nil, validate_err, errno2
end
if headers:has("content-length") then
stream.content_length = tonumber(headers:get("content-length"), 10)
end
stream.recv_headers_fifo:push(headers)
if end_stream then
stream.chunk_fifo:push(nil)
-- recv_headers_cond and chunk_cond get signaled by :set_state
if stream.state == "half closed (local)" then
stream:set_state("closed")
else
stream:set_state("half closed (remote)")
end
else
stream.recv_headers_cond:signal()
if stream.state == "idle" then
stream:set_state("open")
end
end
else
local validate_ok, validate_err, errno2 = validate_headers(headers, true, 1, false)
if not validate_ok then
return nil, validate_err, errno2
end
promised_stream:set_state("reserved (remote)")
promised_stream.recv_headers_fifo:push(headers)
promised_stream.recv_headers_cond:signal()
-- If we have sent a haven't seen this stream before, and we should be discarding frames from it,
-- then don't push it into the new_streams fifo
if stream.connection.send_goaway_lowest == nil or promised_stream.id <= stream.connection.send_goaway_lowest then
stream.connection.new_streams:push(promised_stream)
stream.connection.new_streams_cond:signal(1)
end
end
return true
end
frame_handlers[frame_types.HEADERS] = function(stream, flags, payload, deadline) -- luacheck: ignore 212
if stream.id == 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'HEADERS' frames MUST be associated with a stream"), ce.EILSEQ
end
if stream.state ~= "idle" and stream.state ~= "open" and stream.state ~= "half closed (local)" and stream.state ~= "reserved (remote)" then
return nil, h2_errors.STREAM_CLOSED:new_traceback("'HEADERS' frame not allowed in '" .. stream.state .. "' state"), ce.EILSEQ
end
local end_stream = band(flags, 0x1) ~= 0
local end_headers = band(flags, 0x04) ~= 0
local padded = band(flags, 0x8) ~= 0
local priority = band(flags, 0x20) ~= 0
-- index where payload body starts
local pos = 1
local pad_len
if padded then
pad_len = sunpack("> B", payload, pos)
pos = 2
else
pad_len = 0
end
if priority then
local exclusive, stream_dep, weight
local tmp
tmp, weight = sunpack(">I4 B", payload, pos)
exclusive = band(tmp, 0x80000000) ~= 0
stream_dep = band(tmp, 0x7fffffff)
weight = weight + 1
pos = pos + 5
local new_parent = stream.connection.streams[stream_dep]
-- 5.3.1. Stream Dependencies
-- A dependency on a stream that is not currently in the tree
-- results in that stream being given a default priority
if new_parent then
local ok, err, errno = new_parent:reprioritise(stream, exclusive)
if not ok then
return nil, err, errno
end
stream.weight = weight
end
end
local len = #payload - pos + 1 -- TODO: minus pad_len?
if len > MAX_HEADER_BUFFER_SIZE then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("headers too large"), ce.E2BIG
end
if end_headers then
return process_end_headers(stream, end_stream, pad_len, pos, nil, payload)
else
stream.connection.need_continuation = stream
stream.connection.recv_headers_end_stream = end_stream
stream.connection.recv_headers_buffer = { payload }
stream.connection.recv_headers_buffer_pos = pos
stream.connection.recv_headers_buffer_pad_len = pad_len
stream.connection.recv_headers_buffer_items = 1
stream.connection.recv_headers_buffer_length = len
return true
end
end
function stream_methods:write_headers_frame(payload, end_stream, end_headers, padded, exclusive, stream_dep, weight, timeout, flush)
assert(self.state ~= "closed" and self.state ~= "half closed (local)")
if self.id == nil then
self:pick_id()
end
local pad_len, pri, padding = "", "", ""
local flags = 0
if end_stream then
flags = bor(flags, 0x1)
end
if end_headers then
flags = bor(flags, 0x4)
end
if padded then
flags = bor(flags, 0x8)
pad_len = spack("> B", padded)
padding = ("\0"):rep(padded)
end
if weight or stream_dep then
flags = bor(flags, 0x20)
assert(stream_dep <= 0x7fffffff)
local tmp = stream_dep
if exclusive then
tmp = bor(tmp, 0x80000000)
end
weight = weight and weight - 1 or 0
pri = spack("> I4 B", tmp, weight)
end
payload = pad_len .. pri .. payload .. padding
local ok, err, errno = self:write_http2_frame(frame_types.HEADERS, flags, payload, timeout, flush)
if ok == nil then
return nil, err, errno
end
self.stats_sent_headers = self.stats_sent_headers + 1
if end_headers then
if end_stream then
if self.state == "half closed (remote)" or self.state == "reserved (local)" then
self:set_state("closed")
else
self:set_state("half closed (local)")
end
else
if self.state == "idle" then
self:set_state("open")
elseif self.state == "reserved (local)" then
self:set_state("half closed (remote)")
end
end
else
self.end_stream_after_continuation = end_stream
end
return ok
end
frame_handlers[frame_types.PRIORITY] = function(stream, flags, payload) -- luacheck: ignore 212
if stream.id == 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'PRIORITY' frames MUST be associated with a stream"), ce.EILSEQ
end
if #payload ~= 5 then
return nil, h2_errors.FRAME_SIZE_ERROR:new_traceback("'PRIORITY' frames must be 5 bytes", true), ce.EILSEQ
end
local exclusive, stream_dep, weight
local tmp
tmp, weight = sunpack(">I4 B", payload)
weight = weight + 1
exclusive = band(tmp, 0x80000000) ~= 0
stream_dep = band(tmp, 0x7fffffff)
-- 5.3.1. Stream Dependencies
-- A dependency on a stream that is not currently in the tree
-- results in that stream being given a default priority
local new_parent = stream.connection.streams[stream_dep]
if new_parent then
local ok, err, errno = new_parent:reprioritise(stream, exclusive)
if not ok then
return nil, err, errno
end
stream.weight = weight
end
return true
end
function stream_methods:write_priority_frame(exclusive, stream_dep, weight, timeout, flush)
assert(stream_dep <= 0x7fffffff)
if self.id == nil then
self:pick_id()
end
local tmp = stream_dep
if exclusive then
tmp = bor(tmp, 0x80000000)
end
weight = weight and weight - 1 or 0
local payload = spack("> I4 B", tmp, weight)
return self:write_http2_frame(frame_types.PRIORITY, 0, payload, timeout, flush)
end
frame_handlers[frame_types.RST_STREAM] = function(stream, flags, payload, deadline) -- luacheck: ignore 212
if stream.id == 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'RST_STREAM' frames MUST be associated with a stream"), ce.EILSEQ
end
if #payload ~= 4 then
return nil, h2_errors.FRAME_SIZE_ERROR:new_traceback("'RST_STREAM' frames must be 4 bytes"), ce.EILSEQ
end
if stream.state == "idle" then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'RST_STREAM' frames MUST NOT be sent for a stream in the 'idle' state"), ce.EILSEQ
elseif stream.state == "closed" then
-- probably a delayed RST_STREAM, ignore
return true
end
local err_code = sunpack(">I4", payload)
stream.rst_stream_error = (h2_errors[err_code] or h2_errors.INTERNAL_ERROR):new {
message = string.format("'RST_STREAM' on stream #%d (code=0x%x)", stream.id, err_code);
stream_error = true;
}
stream:set_state("closed")
return true
end
function stream_methods:write_rst_stream_frame(err_code, timeout, flush)
if self.id == 0 then
h2_errors.PROTOCOL_ERROR("'RST_STREAM' frames MUST be associated with a stream")
end
if self.state == "idle" then
h2_errors.PROTOCOL_ERROR([['RST_STREAM' frames MUST NOT be sent for a stream in the "idle" state]])
end
local flags = 0
local payload = spack(">I4", err_code)
local ok, err, errno = self:write_http2_frame(frame_types.RST_STREAM, flags, payload, timeout, flush)
if not ok then return nil, err, errno end
if self.state ~= "closed" then
self:set_state("closed")
end
self:shutdown()
return ok
end
function stream_methods:rst_stream(err, timeout)
local code
if err == nil then
code = 0
elseif h2_error.is(err) then
code = err.code
else
err = h2_errors.INTERNAL_ERROR:new {
message = tostring(err);
stream_error = true;
}
code = err.code
end
if self.rst_stream_error == nil then
self.rst_stream_error = err
end
return self:write_rst_stream_frame(code, timeout)
end
frame_handlers[frame_types.SETTING] = function(stream, flags, payload, deadline)
if stream.id ~= 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("stream identifier for a 'SETTINGS' frame MUST be zero"), ce.EILSEQ
end
local ack = band(flags, 0x1) ~= 0
if ack then -- server is ACK-ing our settings
if #payload ~= 0 then
return nil, h2_errors.FRAME_SIZE_ERROR:new_traceback("Receipt of a 'SETTINGS' frame with the ACK flag set and a length field value other than 0"), ce.EILSEQ
end
stream.connection:ack_settings()
return true
else -- settings from server
if #payload % 6 ~= 0 then
return nil, h2_errors.FRAME_SIZE_ERROR:new_traceback("'SETTINGS' frame with a length other than a multiple of 6 octets"), ce.EILSEQ
end
local peer_settings = {}
for i=1, #payload, 6 do
local id, val = sunpack(">I2 I4", payload, i)
if id == known_settings.HEADER_TABLE_SIZE then
stream.connection.encoding_context:set_max_dynamic_table_size(val)
-- Add a 'max size' element to the next outgoing header
stream.connection.encoding_context:encode_max_size(val)
elseif id == known_settings.ENABLE_PUSH then
-- Convert to boolean
if val == 0 then
val = false
elseif val == 1 then
val = true
else
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("invalid value for boolean"), ce.EILSEQ
end
if val and stream.type == "client" then
-- Clients MUST reject any attempt to change the SETTINGS_ENABLE_PUSH
-- setting to a value other than 0 by treating the message as a connection
-- error of type PROTOCOL_ERROR.
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("SETTINGS_ENABLE_PUSH not allowed for clients"), ce.EILSEQ
end
elseif id == known_settings.INITIAL_WINDOW_SIZE then
if val >= 2^31 then
return nil, h2_errors.FLOW_CONTROL_ERROR:new_traceback("SETTINGS_INITIAL_WINDOW_SIZE must be less than 2^31"), ce.EILSEQ
end
elseif id == known_settings.MAX_FRAME_SIZE then
if val < 16384 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("SETTINGS_MAX_FRAME_SIZE must be greater than or equal to 16384"), ce.EILSEQ
elseif val >= 2^24 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("SETTINGS_MAX_FRAME_SIZE must be less than 2^24"), ce.EILSEQ
end
end
peer_settings[id] = val
end
stream.connection:set_peer_settings(peer_settings)
-- Ack server's settings
local ok, err, errno = stream:write_settings_frame(true, nil, 0, "f")
if not ok then
return ok, err, errno
end
-- ignore :flush failure
stream.connection:flush(deadline and deadline-monotime())
return true
end
end
local function pack_settings_payload(settings)
local i = 0
local a = {}
local function append(k, v)
a[i*2+1] = k
a[i*2+2] = v
i = i + 1
end
local HEADER_TABLE_SIZE = settings[0x1]
if HEADER_TABLE_SIZE == nil then
HEADER_TABLE_SIZE = settings.HEADER_TABLE_SIZE
end
if HEADER_TABLE_SIZE ~= nil then
append(0x1, HEADER_TABLE_SIZE)
end
local ENABLE_PUSH = settings[0x2]
if ENABLE_PUSH == nil then
ENABLE_PUSH = settings.ENABLE_PUSH
end
if ENABLE_PUSH ~= nil then
if type(ENABLE_PUSH) == "boolean" then
ENABLE_PUSH = ENABLE_PUSH and 1 or 0
end
append(0x2, ENABLE_PUSH)
ENABLE_PUSH = ENABLE_PUSH ~= 0
end
local MAX_CONCURRENT_STREAMS = settings[0x3]
if MAX_CONCURRENT_STREAMS == nil then
MAX_CONCURRENT_STREAMS = settings.MAX_CONCURRENT_STREAMS
end
if MAX_CONCURRENT_STREAMS ~= nil then
append(0x3, MAX_CONCURRENT_STREAMS)
end
local INITIAL_WINDOW_SIZE = settings[0x4]
if INITIAL_WINDOW_SIZE == nil then
INITIAL_WINDOW_SIZE = settings.INITIAL_WINDOW_SIZE
end
if INITIAL_WINDOW_SIZE ~= nil then
if INITIAL_WINDOW_SIZE >= 2^31 then
h2_errors.FLOW_CONTROL_ERROR("SETTINGS_INITIAL_WINDOW_SIZE must be less than 2^31")
end
append(0x4, INITIAL_WINDOW_SIZE)
end
local MAX_FRAME_SIZE = settings[0x5]
if MAX_FRAME_SIZE == nil then
MAX_FRAME_SIZE = settings.MAX_FRAME_SIZE
end
if MAX_FRAME_SIZE ~= nil then
if MAX_FRAME_SIZE < 16384 then
h2_errors.PROTOCOL_ERROR("SETTINGS_MAX_FRAME_SIZE must be greater than or equal to 16384")
elseif MAX_FRAME_SIZE >= 2^24 then
h2_errors.PROTOCOL_ERROR("SETTINGS_MAX_FRAME_SIZE must be less than 2^24")
end
append(0x5, MAX_FRAME_SIZE)
end
local MAX_HEADER_LIST_SIZE = settings[0x6]
if MAX_HEADER_LIST_SIZE == nil then
MAX_HEADER_LIST_SIZE = settings.MAX_HEADER_LIST_SIZE
end
if MAX_HEADER_LIST_SIZE ~= nil then
append(0x6, MAX_HEADER_LIST_SIZE)
end
local settings_to_merge = {
HEADER_TABLE_SIZE;
ENABLE_PUSH;
MAX_CONCURRENT_STREAMS;
INITIAL_WINDOW_SIZE;
MAX_FRAME_SIZE;
MAX_HEADER_LIST_SIZE;
}
return spack(">" .. ("I2 I4"):rep(i), unpack(a, 1, i*2)), settings_to_merge
end
function stream_methods:write_settings_frame(ACK, settings, timeout, flush)
if self.id ~= 0 then
h2_errors.PROTOCOL_ERROR("'SETTINGS' frames must be on stream id 0")
end
local flags, payload, settings_to_merge
if ACK then
if settings ~= nil then
h2_errors.PROTOCOL_ERROR("'SETTINGS' ACK cannot have new settings")
end
flags = 0x1
payload = ""
else
flags = 0
payload, settings_to_merge = pack_settings_payload(settings)
end
local ok, err, errno = self:write_http2_frame(frame_types.SETTING, flags, payload, timeout, flush)
if ok and not ACK then
local n = self.connection.send_settings.n + 1
self.connection.send_settings.n = n
self.connection.send_settings[n] = settings_to_merge
ok = n
end
return ok, err, errno
end
frame_handlers[frame_types.PUSH_PROMISE] = function(stream, flags, payload, deadline) -- luacheck: ignore 212
if not stream.connection.acked_settings[known_settings.ENABLE_PUSH] then
-- An endpoint that has both set this parameter to 0 and had it acknowledged MUST
-- treat the receipt of a PUSH_PROMISE frame as a connection error of type PROTOCOL_ERROR.
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("SETTINGS_ENABLE_PUSH is 0"), ce.EILSEQ
elseif stream.type == "server" then
-- A client cannot push. Thus, servers MUST treat the receipt of a PUSH_PROMISE
-- frame as a connection error of type PROTOCOL_ERROR.
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("A client cannot push"), ce.EILSEQ
end
if stream.id == 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'PUSH_PROMISE' frames MUST be associated with a stream"), ce.EILSEQ
end
local end_headers = band(flags, 0x04) ~= 0
local padded = band(flags, 0x8) ~= 0
-- index where payload body starts
local pos = 1
local pad_len
if padded then
pad_len = sunpack("> B", payload, pos)
pos = 2
else
pad_len = 0
end
local tmp = sunpack(">I4", payload, pos)
local promised_stream_id = band(tmp, 0x7fffffff)
pos = pos + 4
local len = #payload - pos + 1 -- TODO: minus pad_len?
if len > MAX_HEADER_BUFFER_SIZE then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("headers too large"), ce.EILSEQ
end
local promised_stream = stream.connection:new_stream(promised_stream_id)
stream:reprioritise(promised_stream)
if end_headers then
return process_end_headers(stream, false, pad_len, pos, promised_stream, payload)
else
stream.connection.need_continuation = stream
stream.connection.promised_stream = promised_stream
stream.connection.recv_headers_end_stream = false
stream.connection.recv_headers_buffer = { payload }
stream.connection.recv_headers_buffer_pos = pos
stream.connection.recv_headers_buffer_pad_len = pad_len
stream.connection.recv_headers_buffer_items = 1
stream.connection.recv_headers_buffer_length = len
return true
end
end
function stream_methods:write_push_promise_frame(promised_stream_id, payload, end_headers, padded, timeout, flush)
assert(self.state == "open" or self.state == "half closed (remote)")
assert(self.id ~= 0)
local promised_stream = self.connection.streams[promised_stream_id]
assert(promised_stream and promised_stream.state == "idle")
-- 8.2.1: PUSH_PROMISE frames MUST NOT be sent by the client.
assert(self.type == "server" and promised_stream.id % 2 == 0)
local pad_len, padding = "", ""
local flags = 0
if end_headers then
flags = bor(flags, 0x4)
end
if padded then
flags = bor(flags, 0x8)
pad_len = spack("> B", padded)
padding = ("\0"):rep(padded)
end
promised_stream_id = spack(">I4", promised_stream_id)
payload = pad_len .. promised_stream_id .. payload .. padding
local ok, err, errno = self:write_http2_frame(frame_types.PUSH_PROMISE, flags, payload, 0, "f")
if ok == nil then
return nil, err, errno
end
if end_headers then
promised_stream:set_state("reserved (local)")
else
promised_stream.end_stream_after_continuation = false
end
if flush ~= "f" then
return self.connection:flush(timeout)
else
return true
end
end
frame_handlers[frame_types.PING] = function(stream, flags, payload, deadline)
if stream.id ~= 0 then
return nil, h2_errors.PROTOCOL_ERROR:new_traceback("'PING' must be on stream id 0"), ce.EILSEQ