-
Notifications
You must be signed in to change notification settings - Fork 859
Expand file tree
/
Copy pathHTTP.cc
More file actions
2400 lines (2050 loc) · 76.2 KB
/
HTTP.cc
File metadata and controls
2400 lines (2050 loc) · 76.2 KB
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
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "tscore/ink_defs.h"
#include "tscore/ink_platform.h"
#include "tscore/ink_inet.h"
#include <cassert>
#include <cstdio>
#include <cstring>
#include <string_view>
#include "proxy/hdrs/HTTP.h"
#include "proxy/hdrs/HdrToken.h"
#include "tscore/Diags.h"
using namespace std::literals;
/***********************************************************************
* *
* C O M P I L E O P T I O N S *
* *
***********************************************************************/
#define ENABLE_PARSER_FAST_PATHS 1
/***********************************************************************
* *
* C O N S T A N T S *
* *
***********************************************************************/
c_str_view HTTP_METHOD_CONNECT;
c_str_view HTTP_METHOD_DELETE;
c_str_view HTTP_METHOD_GET;
c_str_view HTTP_METHOD_HEAD;
c_str_view HTTP_METHOD_OPTIONS;
c_str_view HTTP_METHOD_POST;
c_str_view HTTP_METHOD_PURGE;
c_str_view HTTP_METHOD_PUT;
c_str_view HTTP_METHOD_TRACE;
c_str_view HTTP_METHOD_PUSH;
int HTTP_WKSIDX_CONNECT;
int HTTP_WKSIDX_DELETE;
int HTTP_WKSIDX_GET;
int HTTP_WKSIDX_HEAD;
int HTTP_WKSIDX_OPTIONS;
int HTTP_WKSIDX_POST;
int HTTP_WKSIDX_PURGE;
int HTTP_WKSIDX_PUT;
int HTTP_WKSIDX_TRACE;
int HTTP_WKSIDX_PUSH;
int HTTP_WKSIDX_METHODS_CNT = 0;
c_str_view HTTP_VALUE_BYTES;
c_str_view HTTP_VALUE_CHUNKED;
c_str_view HTTP_VALUE_CLOSE;
c_str_view HTTP_VALUE_COMPRESS;
c_str_view HTTP_VALUE_DEFLATE;
c_str_view HTTP_VALUE_GZIP;
c_str_view HTTP_VALUE_BROTLI;
c_str_view HTTP_VALUE_ZSTD;
c_str_view HTTP_VALUE_IDENTITY;
c_str_view HTTP_VALUE_KEEP_ALIVE;
c_str_view HTTP_VALUE_MAX_AGE;
c_str_view HTTP_VALUE_MAX_STALE;
c_str_view HTTP_VALUE_MIN_FRESH;
c_str_view HTTP_VALUE_MUST_REVALIDATE;
c_str_view HTTP_VALUE_NONE;
c_str_view HTTP_VALUE_NO_CACHE;
c_str_view HTTP_VALUE_NO_STORE;
c_str_view HTTP_VALUE_NO_TRANSFORM;
c_str_view HTTP_VALUE_ONLY_IF_CACHED;
c_str_view HTTP_VALUE_PRIVATE;
c_str_view HTTP_VALUE_PROXY_REVALIDATE;
c_str_view HTTP_VALUE_PUBLIC;
c_str_view HTTP_VALUE_S_MAXAGE;
c_str_view HTTP_VALUE_NEED_REVALIDATE_ONCE;
c_str_view HTTP_VALUE_100_CONTINUE;
// Cache-control: extension "need-revalidate-once" is used internally by T.S.
// to invalidate a document, and it is not returned/forwarded.
// If a cached document has this extension set (ie, is invalidated),
// then the T.S. needs to revalidate the document once before returning it.
// After a successful revalidation, the extension will be removed by T.S.
// To set or unset this directive should be done via the following two
// function:
// set_cooked_cc_need_revalidate_once()
// unset_cooked_cc_need_revalidate_once()
// To test, use regular Cache-control testing functions, eg,
// is_cache_control_set(HTTP_VALUE_NEED_REVALIDATE_ONCE)
Arena *const HTTPHdr::USE_HDR_HEAP_MAGIC = reinterpret_cast<Arena *>(1);
namespace
{
DbgCtl dbg_ctl_http{"http"};
} // end anonymous namespace
/***********************************************************************
* *
* M A I N C O D E *
* *
***********************************************************************/
void
http_hdr_adjust(HTTPHdrImpl * /* hdrp ATS_UNUSED */, int32_t /* offset ATS_UNUSED */, int32_t /* length ATS_UNUSED */,
int32_t /* delta ATS_UNUSED */)
{
ink_release_assert(!"http_hdr_adjust not implemented");
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_init()
{
static int init = 1;
if (init) {
init = 0;
mime_init();
url_init();
HTTP_METHOD_CONNECT = hdrtoken_string_to_wks_sv("CONNECT");
HTTP_METHOD_DELETE = hdrtoken_string_to_wks_sv("DELETE");
HTTP_METHOD_GET = hdrtoken_string_to_wks_sv("GET");
HTTP_METHOD_HEAD = hdrtoken_string_to_wks_sv("HEAD");
HTTP_METHOD_OPTIONS = hdrtoken_string_to_wks_sv("OPTIONS");
HTTP_METHOD_POST = hdrtoken_string_to_wks_sv("POST");
HTTP_METHOD_PURGE = hdrtoken_string_to_wks_sv("PURGE");
HTTP_METHOD_PUT = hdrtoken_string_to_wks_sv("PUT");
HTTP_METHOD_TRACE = hdrtoken_string_to_wks_sv("TRACE");
HTTP_METHOD_PUSH = hdrtoken_string_to_wks_sv("PUSH");
// HTTP methods index calculation. Don't forget to count them!
// Don't change the order of calculation! Each index has related bitmask (see http quick filter)
HTTP_WKSIDX_CONNECT = hdrtoken_wks_to_index(HTTP_METHOD_CONNECT.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_DELETE = hdrtoken_wks_to_index(HTTP_METHOD_DELETE.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_GET = hdrtoken_wks_to_index(HTTP_METHOD_GET.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_HEAD = hdrtoken_wks_to_index(HTTP_METHOD_HEAD.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_OPTIONS = hdrtoken_wks_to_index(HTTP_METHOD_OPTIONS.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_POST = hdrtoken_wks_to_index(HTTP_METHOD_POST.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_PURGE = hdrtoken_wks_to_index(HTTP_METHOD_PURGE.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_PUT = hdrtoken_wks_to_index(HTTP_METHOD_PUT.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_TRACE = hdrtoken_wks_to_index(HTTP_METHOD_TRACE.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_WKSIDX_PUSH = hdrtoken_wks_to_index(HTTP_METHOD_PUSH.c_str());
HTTP_WKSIDX_METHODS_CNT++;
HTTP_VALUE_BYTES = hdrtoken_string_to_wks_sv("bytes");
HTTP_VALUE_CHUNKED = hdrtoken_string_to_wks_sv("chunked");
HTTP_VALUE_CLOSE = hdrtoken_string_to_wks_sv("close");
HTTP_VALUE_COMPRESS = hdrtoken_string_to_wks_sv("compress");
HTTP_VALUE_DEFLATE = hdrtoken_string_to_wks_sv("deflate");
HTTP_VALUE_GZIP = hdrtoken_string_to_wks_sv("gzip");
HTTP_VALUE_BROTLI = hdrtoken_string_to_wks_sv("br");
HTTP_VALUE_ZSTD = hdrtoken_string_to_wks_sv("zstd");
HTTP_VALUE_IDENTITY = hdrtoken_string_to_wks_sv("identity");
HTTP_VALUE_KEEP_ALIVE = hdrtoken_string_to_wks_sv("keep-alive");
HTTP_VALUE_MAX_AGE = hdrtoken_string_to_wks_sv("max-age");
HTTP_VALUE_MAX_STALE = hdrtoken_string_to_wks_sv("max-stale");
HTTP_VALUE_MIN_FRESH = hdrtoken_string_to_wks_sv("min-fresh");
HTTP_VALUE_MUST_REVALIDATE = hdrtoken_string_to_wks_sv("must-revalidate");
HTTP_VALUE_NONE = hdrtoken_string_to_wks_sv("none");
HTTP_VALUE_NO_CACHE = hdrtoken_string_to_wks_sv("no-cache");
HTTP_VALUE_NO_STORE = hdrtoken_string_to_wks_sv("no-store");
HTTP_VALUE_NO_TRANSFORM = hdrtoken_string_to_wks_sv("no-transform");
HTTP_VALUE_ONLY_IF_CACHED = hdrtoken_string_to_wks_sv("only-if-cached");
HTTP_VALUE_PRIVATE = hdrtoken_string_to_wks_sv("private");
HTTP_VALUE_PROXY_REVALIDATE = hdrtoken_string_to_wks_sv("proxy-revalidate");
HTTP_VALUE_PUBLIC = hdrtoken_string_to_wks_sv("public");
HTTP_VALUE_S_MAXAGE = hdrtoken_string_to_wks_sv("s-maxage");
HTTP_VALUE_NEED_REVALIDATE_ONCE = hdrtoken_string_to_wks_sv("need-revalidate-once");
HTTP_VALUE_100_CONTINUE = hdrtoken_string_to_wks_sv("100-continue");
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
HTTPHdrImpl *
http_hdr_create(HdrHeap *heap, HTTPType polarity, HTTPVersion version)
{
HTTPHdrImpl *hh;
hh = (HTTPHdrImpl *)heap->allocate_obj(sizeof(HTTPHdrImpl), HdrHeapObjType::HTTP_HEADER);
http_hdr_init(heap, hh, polarity, version);
return (hh);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_init(HdrHeap *heap, HTTPHdrImpl *hh, HTTPType polarity, HTTPVersion version)
{
memset(&(hh->u), 0, sizeof(hh->u));
hh->m_polarity = polarity;
hh->m_version = HTTP_1_0;
hh->m_fields_impl = mime_hdr_create(heap);
if (polarity == HTTPType::REQUEST) {
hh->u.req.m_url_impl = url_create(heap);
hh->u.req.m_method_wks_idx = -1;
}
if (version == HTTP_2_0 || version == HTTP_3_0) {
MIMEField *field;
switch (polarity) {
case HTTPType::REQUEST:
field = mime_field_create_named(heap, hh->m_fields_impl, PSEUDO_HEADER_METHOD);
mime_hdr_field_attach(hh->m_fields_impl, field, false, nullptr);
field = mime_field_create_named(heap, hh->m_fields_impl, PSEUDO_HEADER_SCHEME);
mime_hdr_field_attach(hh->m_fields_impl, field, false, nullptr);
field = mime_field_create_named(heap, hh->m_fields_impl, PSEUDO_HEADER_AUTHORITY);
mime_hdr_field_attach(hh->m_fields_impl, field, false, nullptr);
field = mime_field_create_named(heap, hh->m_fields_impl, PSEUDO_HEADER_PATH);
mime_hdr_field_attach(hh->m_fields_impl, field, false, nullptr);
break;
case HTTPType::RESPONSE:
field = mime_field_create_named(heap, hh->m_fields_impl, PSEUDO_HEADER_STATUS);
mime_hdr_field_attach(hh->m_fields_impl, field, false, nullptr);
break;
default:
ink_abort("HTTPType::UNKNOWN");
}
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_copy_onto(HTTPHdrImpl *s_hh, HdrHeap *s_heap, HTTPHdrImpl *d_hh, HdrHeap *d_heap, bool inherit_strs)
{
MIMEHdrImpl *s_mh, *d_mh;
URLImpl *s_url, *d_url;
HTTPType d_polarity;
s_mh = s_hh->m_fields_impl;
s_url = s_hh->u.req.m_url_impl;
d_mh = d_hh->m_fields_impl;
d_url = d_hh->u.req.m_url_impl;
d_polarity = d_hh->m_polarity;
ink_assert(s_hh->m_polarity != HTTPType::UNKNOWN);
ink_assert(s_mh != nullptr);
ink_assert(d_mh != nullptr);
memcpy(d_hh, s_hh, sizeof(HTTPHdrImpl));
d_hh->m_fields_impl = d_mh; // restore pre-memcpy mime impl
if (s_hh->m_polarity == HTTPType::REQUEST) {
if (d_polarity == HTTPType::REQUEST) {
d_hh->u.req.m_url_impl = d_url; // restore pre-memcpy url impl
} else {
d_url = d_hh->u.req.m_url_impl = url_create(d_heap); // create url
}
url_copy_onto(s_url, s_heap, d_url, d_heap, false);
} else if (d_polarity == HTTPType::REQUEST) {
// gender bender. Need to kill off old url
url_clear(d_url);
}
mime_hdr_copy_onto(s_mh, s_heap, d_mh, d_heap, false);
if (inherit_strs) {
d_heap->inherit_string_heaps(s_heap);
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
HTTPHdrImpl *
http_hdr_clone(HTTPHdrImpl *s_hh, HdrHeap *s_heap, HdrHeap *d_heap)
{
HTTPHdrImpl *d_hh;
// FIX: A future optimization is to copy contiguous objects with
// one single memcpy. For this first optimization, we just
// copy each object separately.
d_hh = http_hdr_create(d_heap, s_hh->m_polarity, s_hh->m_version);
http_hdr_copy_onto(s_hh, s_heap, d_hh, d_heap, ((s_heap != d_heap) ? true : false));
return (d_hh);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
static inline char *
http_hdr_version_to_string(const HTTPVersion &version, char *buf9)
{
ink_assert(version.get_major() < 10);
ink_assert(version.get_minor() < 10);
buf9[0] = 'H';
buf9[1] = 'T';
buf9[2] = 'T';
buf9[3] = 'P';
buf9[4] = '/';
buf9[5] = '0' + version.get_major();
buf9[6] = '.';
buf9[7] = '0' + version.get_minor();
buf9[8] = '\0';
return (buf9);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
int
http_version_print(const HTTPVersion &version, char *buf, int bufsize, int *bufindex, int *dumpoffset)
{
#define TRY(x) \
if (!x) \
return 0
char tmpbuf[16];
http_hdr_version_to_string(version, tmpbuf);
TRY(mime_mem_print(std::string_view{tmpbuf, 8}, buf, bufsize, bufindex, dumpoffset));
return 1;
#undef TRY
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
int
http_hdr_print(HTTPHdrImpl const *hdr, char *buf, int bufsize, int *bufindex, int *dumpoffset)
{
#define TRY(x) \
if (!x) \
return 0
int tmplen;
char tmpbuf[32];
char *p;
ink_assert((hdr->m_polarity == HTTPType::REQUEST) || (hdr->m_polarity == HTTPType::RESPONSE));
if (hdr->m_polarity == HTTPType::REQUEST) {
if (hdr->u.req.m_ptr_method == nullptr) {
return 1;
}
if ((buf != nullptr) && (*dumpoffset == 0) && (bufsize - *bufindex >= hdr->u.req.m_len_method + 1)) { // fastpath
p = buf + *bufindex;
memcpy(p, hdr->u.req.m_ptr_method, hdr->u.req.m_len_method);
p += hdr->u.req.m_len_method;
*p++ = ' ';
*bufindex += hdr->u.req.m_len_method + 1;
if (hdr->u.req.m_url_impl) {
TRY(url_print(hdr->u.req.m_url_impl, buf, bufsize, bufindex, dumpoffset));
if (bufsize - *bufindex >= 1) {
if (hdr->u.req.m_method_wks_idx == HTTP_WKSIDX_CONNECT) {
*bufindex -= 1; // remove trailing slash for CONNECT request
}
p = buf + *bufindex;
*p++ = ' ';
*bufindex += 1;
} else {
return 0;
}
}
if (bufsize - *bufindex >= 9) {
http_hdr_version_to_string(hdr->m_version, p);
*bufindex += 9 - 1; // overwrite '\0';
} else {
TRY(http_version_print(hdr->m_version, buf, bufsize, bufindex, dumpoffset));
}
if (bufsize - *bufindex >= 2) {
p = buf + *bufindex;
*p++ = '\r';
*p++ = '\n';
*bufindex += 2;
} else {
TRY(mime_mem_print("\r\n"sv, buf, bufsize, bufindex, dumpoffset));
}
TRY(mime_hdr_print(hdr->m_fields_impl, buf, bufsize, bufindex, dumpoffset));
} else {
TRY(
mime_mem_print(std::string_view{hdr->u.req.m_ptr_method, static_cast<std::string_view::size_type>(hdr->u.req.m_len_method)},
buf, bufsize, bufindex, dumpoffset));
TRY(mime_mem_print(" "sv, buf, bufsize, bufindex, dumpoffset));
if (hdr->u.req.m_url_impl) {
TRY(url_print(hdr->u.req.m_url_impl, buf, bufsize, bufindex, dumpoffset));
TRY(mime_mem_print(" "sv, buf, bufsize, bufindex, dumpoffset));
}
TRY(http_version_print(hdr->m_version, buf, bufsize, bufindex, dumpoffset));
TRY(mime_mem_print("\r\n"sv, buf, bufsize, bufindex, dumpoffset));
TRY(mime_hdr_print(hdr->m_fields_impl, buf, bufsize, bufindex, dumpoffset));
}
} else { // hdr->m_polarity == HTTPType::RESPONSE
if ((buf != nullptr) && (*dumpoffset == 0) && (bufsize - *bufindex >= 9 + 6 + 1)) { // fastpath
p = buf + *bufindex;
http_hdr_version_to_string(hdr->m_version, p);
p += 8; // overwrite '\0' with space
*p++ = ' ';
*bufindex += 9;
if (auto hdrstat{static_cast<int32_t>(http_hdr_status_get(hdr))}; hdrstat == 200) {
*p++ = '2';
*p++ = '0';
*p++ = '0';
tmplen = 3;
} else {
tmplen = mime_format_int(p, hdrstat, (bufsize - (p - buf)));
ink_assert(tmplen <= 6);
p += tmplen;
}
*p++ = ' ';
*bufindex += tmplen + 1;
if (hdr->u.resp.m_ptr_reason) {
TRY(mime_mem_print(
std::string_view{hdr->u.resp.m_ptr_reason, static_cast<std::string_view::size_type>(hdr->u.resp.m_len_reason)}, buf,
bufsize, bufindex, dumpoffset));
}
if (bufsize - *bufindex >= 2) {
p = buf + *bufindex;
*p++ = '\r';
*p++ = '\n';
*bufindex += 2;
} else {
TRY(mime_mem_print("\r\n"sv, buf, bufsize, bufindex, dumpoffset));
}
TRY(mime_hdr_print(hdr->m_fields_impl, buf, bufsize, bufindex, dumpoffset));
} else {
TRY(http_version_print(hdr->m_version, buf, bufsize, bufindex, dumpoffset));
TRY(mime_mem_print(" "sv, buf, bufsize, bufindex, dumpoffset));
tmplen = mime_format_int(tmpbuf, static_cast<int32_t>(http_hdr_status_get(hdr)), sizeof(tmpbuf));
TRY(mime_mem_print(std::string_view{tmpbuf, static_cast<std::string_view::size_type>(tmplen)}, buf, bufsize, bufindex,
dumpoffset));
TRY(mime_mem_print(" "sv, buf, bufsize, bufindex, dumpoffset));
if (hdr->u.resp.m_ptr_reason) {
TRY(mime_mem_print(
std::string_view{hdr->u.resp.m_ptr_reason, static_cast<std::string_view::size_type>(hdr->u.resp.m_len_reason)}, buf,
bufsize, bufindex, dumpoffset));
}
TRY(mime_mem_print("\r\n"sv, buf, bufsize, bufindex, dumpoffset));
TRY(mime_hdr_print(hdr->m_fields_impl, buf, bufsize, bufindex, dumpoffset));
}
}
return 1;
#undef TRY
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_describe(HdrHeapObjImpl *raw, bool recurse)
{
HTTPHdrImpl *obj = (HTTPHdrImpl *)raw;
if (obj->m_polarity == HTTPType::REQUEST) {
Dbg(dbg_ctl_http, "[TYPE: REQ, V: %04X, URL: %p, METHOD: \"%.*s\", METHOD_LEN: %d, FIELDS: %p]",
obj->m_version.get_flat_version(), obj->u.req.m_url_impl, obj->u.req.m_len_method,
(obj->u.req.m_ptr_method ? obj->u.req.m_ptr_method : "NULL"), obj->u.req.m_len_method, obj->m_fields_impl);
if (recurse) {
if (obj->u.req.m_url_impl) {
obj_describe(obj->u.req.m_url_impl, recurse);
}
if (obj->m_fields_impl) {
obj_describe(obj->m_fields_impl, recurse);
}
}
} else {
Dbg(dbg_ctl_http, "[TYPE: RSP, V: %04X, STATUS: %d, REASON: \"%.*s\", REASON_LEN: %d, FIELDS: %p]",
obj->m_version.get_flat_version(), obj->u.resp.m_status, obj->u.resp.m_len_reason,
(obj->u.resp.m_ptr_reason ? obj->u.resp.m_ptr_reason : "NULL"), obj->u.resp.m_len_reason, obj->m_fields_impl);
if (recurse) {
if (obj->m_fields_impl) {
obj_describe(obj->m_fields_impl, recurse);
}
}
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
int
HTTPHdr::length_get() const
{
int length = 0;
if (m_http->m_polarity == HTTPType::REQUEST) {
if (m_http->u.req.m_ptr_method) {
length = m_http->u.req.m_len_method;
} else {
length = 0;
}
length += 1; // " "
if (m_http->u.req.m_url_impl) {
length += url_length_get(m_http->u.req.m_url_impl);
}
length += 1; // " "
length += 8; // HTTP/%d.%d
length += 2; // "\r\n"
} else if (m_http->m_polarity == HTTPType::RESPONSE) {
if (m_http->u.resp.m_ptr_reason) {
length = m_http->u.resp.m_len_reason;
} else {
length = 0;
}
length += 8; // HTTP/%d.%d
length += 1; // " "
length += 3; // status
length += 1; // " "
length += 2; // "\r\n"
}
length += mime_hdr_length_get(m_http->m_fields_impl);
return length;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_type_set(HTTPHdrImpl *hh, HTTPType type)
{
hh->m_polarity = type;
}
/*-------------------------------------------------------------------------
RFC2616 specifies that HTTP version is of the format <major>.<minor>
in the request line. However, the features supported and in use are
for versions 1.0, 1.1 and 2.0 (with HTTP/3.0 being developed). HTTP/2.0
and HTTP/3.0 are both negotiated using ALPN over TLS and not via the HTTP
request line thus leaving the versions supported on the request line to be
HTTP/1.0 and HTTP/1.1 alone. This utility checks if the HTTP Version
received in the request line is one of these and returns false otherwise
-------------------------------------------------------------------------*/
bool
is_http1_version(const uint8_t major, const uint8_t minor)
{
// Return true if 1.1 or 1.0
return (major == 1) && (minor == 1 || minor == 0);
}
bool
is_http1_hdr_version_supported(const HTTPVersion &http_version)
{
return is_http1_version(http_version.get_major(), http_version.get_minor());
}
bool
http_hdr_version_set(HTTPHdrImpl *hh, const HTTPVersion &ver)
{
hh->m_version = ver;
return is_http1_version(ver.get_major(), ver.get_minor());
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
std::string_view
http_hdr_method_get(HTTPHdrImpl *hh)
{
const char *str;
int length;
ink_assert(hh->m_polarity == HTTPType::REQUEST);
if (hh->u.req.m_method_wks_idx >= 0) {
str = hdrtoken_index_to_wks(hh->u.req.m_method_wks_idx);
length = hdrtoken_index_to_length(hh->u.req.m_method_wks_idx);
} else {
str = hh->u.req.m_ptr_method;
length = hh->u.req.m_len_method;
}
return std::string_view{str, static_cast<std::string_view::size_type>(length)};
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_method_set(HdrHeap *heap, HTTPHdrImpl *hh, std::string_view method, int16_t method_wks_idx, bool must_copy)
{
ink_assert(hh->m_polarity == HTTPType::REQUEST);
hh->u.req.m_method_wks_idx = method_wks_idx;
mime_str_u16_set(heap, method, &(hh->u.req.m_ptr_method), &(hh->u.req.m_len_method), must_copy);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_url_set(HdrHeap *heap, HTTPHdrImpl *hh, URLImpl *url)
{
ink_assert(hh->m_polarity == HTTPType::REQUEST);
if (hh->u.req.m_url_impl != url) {
if (hh->u.req.m_url_impl != nullptr) {
heap->deallocate_obj(hh->u.req.m_url_impl);
}
// Clone into new heap if the URL was allocated against a different heap
if (reinterpret_cast<char *>(url) < heap->m_data_start || reinterpret_cast<char *>(url) >= heap->m_free_start) {
hh->u.req.m_url_impl = static_cast<URLImpl *>(heap->allocate_obj(url->m_length, static_cast<HdrHeapObjType>(url->m_type)));
memcpy(hh->u.req.m_url_impl, url, url->m_length);
// Make sure there is a read_write heap
if (heap->m_read_write_heap.get() == nullptr) {
int url_string_length = url->strings_length();
heap->m_read_write_heap = HdrStrHeap::alloc(url_string_length);
}
hh->u.req.m_url_impl->rehome_strings(heap);
} else {
hh->u.req.m_url_impl = url;
}
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_status_set(HTTPHdrImpl *hh, HTTPStatus status)
{
ink_release_assert(hh->m_polarity == HTTPType::RESPONSE);
hh->u.resp.m_status = static_cast<int16_t>(status);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
std::string_view
http_hdr_reason_get(HTTPHdrImpl *hh)
{
ink_assert(hh->m_polarity == HTTPType::RESPONSE);
return std::string_view{hh->u.resp.m_ptr_reason, static_cast<std::string_view::size_type>(hh->u.resp.m_len_reason)};
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void
http_hdr_reason_set(HdrHeap *heap, HTTPHdrImpl *hh, std::string_view value, bool must_copy)
{
ink_release_assert(hh->m_polarity == HTTPType::RESPONSE);
mime_str_u16_set(heap, value, &(hh->u.resp.m_ptr_reason), &(hh->u.resp.m_len_reason), must_copy);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
const char *
http_hdr_reason_lookup(HTTPStatus status)
{
#define HTTP_STATUS_ENTRY(value, reason) \
case value: \
return #reason
switch (static_cast<int>(status)) {
HTTP_STATUS_ENTRY(0, None); // TS_HTTP_STATUS_NONE
HTTP_STATUS_ENTRY(100, Continue); // [RFC2616]
HTTP_STATUS_ENTRY(101, Switching Protocols); // [RFC2616]
HTTP_STATUS_ENTRY(102, Processing); // [RFC2518]
HTTP_STATUS_ENTRY(103, Early Hints); // TODO: add RFC number
// 103-199 Unassigned
HTTP_STATUS_ENTRY(200, OK); // [RFC2616]
HTTP_STATUS_ENTRY(201, Created); // [RFC2616]
HTTP_STATUS_ENTRY(202, Accepted); // [RFC2616]
HTTP_STATUS_ENTRY(203, Non - Authoritative Information); // [RFC2616]
HTTP_STATUS_ENTRY(204, No Content); // [RFC2616]
HTTP_STATUS_ENTRY(205, Reset Content); // [RFC2616]
HTTP_STATUS_ENTRY(206, Partial Content); // [RFC2616]
HTTP_STATUS_ENTRY(207, Multi - Status); // [RFC4918]
HTTP_STATUS_ENTRY(208, Already Reported); // [RFC5842]
// 209-225 Unassigned
HTTP_STATUS_ENTRY(226, IM Used); // [RFC3229]
// 227-299 Unassigned
HTTP_STATUS_ENTRY(300, Multiple Choices); // [RFC2616]
HTTP_STATUS_ENTRY(301, Moved Permanently); // [RFC2616]
HTTP_STATUS_ENTRY(302, Found); // [RFC2616]
HTTP_STATUS_ENTRY(303, See Other); // [RFC2616]
HTTP_STATUS_ENTRY(304, Not Modified); // [RFC2616]
HTTP_STATUS_ENTRY(305, Use Proxy); // [RFC2616]
// 306 Reserved // [RFC2616]
HTTP_STATUS_ENTRY(307, Temporary Redirect); // [RFC2616]
HTTP_STATUS_ENTRY(308, Permanent Redirect); // [RFC-reschke-http-status-308-07]
// 309-399 Unassigned
HTTP_STATUS_ENTRY(400, Bad Request); // [RFC2616]
HTTP_STATUS_ENTRY(401, Unauthorized); // [RFC2616]
HTTP_STATUS_ENTRY(402, Payment Required); // [RFC2616]
HTTP_STATUS_ENTRY(403, Forbidden); // [RFC2616]
HTTP_STATUS_ENTRY(404, Not Found); // [RFC2616]
HTTP_STATUS_ENTRY(405, Method Not Allowed); // [RFC2616]
HTTP_STATUS_ENTRY(406, Not Acceptable); // [RFC2616]
HTTP_STATUS_ENTRY(407, Proxy Authentication Required); // [RFC2616]
HTTP_STATUS_ENTRY(408, Request Timeout); // [RFC2616]
HTTP_STATUS_ENTRY(409, Conflict); // [RFC2616]
HTTP_STATUS_ENTRY(410, Gone); // [RFC2616]
HTTP_STATUS_ENTRY(411, Length Required); // [RFC2616]
HTTP_STATUS_ENTRY(412, Precondition Failed); // [RFC2616]
HTTP_STATUS_ENTRY(413, Request Entity Too Large); // [RFC2616]
HTTP_STATUS_ENTRY(414, Request - URI Too Long); // [RFC2616]
HTTP_STATUS_ENTRY(415, Unsupported Media Type); // [RFC2616]
HTTP_STATUS_ENTRY(416, Requested Range Not Satisfiable); // [RFC2616]
HTTP_STATUS_ENTRY(417, Expectation Failed); // [RFC2616]
HTTP_STATUS_ENTRY(422, Unprocessable Entity); // [RFC4918]
HTTP_STATUS_ENTRY(423, Locked); // [RFC4918]
HTTP_STATUS_ENTRY(424, Failed Dependency); // [RFC4918]
// 425 Reserved // [RFC2817]
HTTP_STATUS_ENTRY(426, Upgrade Required); // [RFC2817]
// 427 Unassigned
HTTP_STATUS_ENTRY(428, Precondition Required); // [RFC6585]
HTTP_STATUS_ENTRY(429, Too Many Requests); // [RFC6585]
// 430 Unassigned
HTTP_STATUS_ENTRY(431, Request Header Fields Too Large); // [RFC6585]
// 432-499 Unassigned
HTTP_STATUS_ENTRY(500, Internal Server Error); // [RFC2616]
HTTP_STATUS_ENTRY(501, Not Implemented); // [RFC2616]
HTTP_STATUS_ENTRY(502, Bad Gateway); // [RFC2616]
HTTP_STATUS_ENTRY(503, Service Unavailable); // [RFC2616]
HTTP_STATUS_ENTRY(504, Gateway Timeout); // [RFC2616]
HTTP_STATUS_ENTRY(505, HTTP Version Not Supported); // [RFC2616]
HTTP_STATUS_ENTRY(506, Variant Also Negotiates); // [RFC2295]
HTTP_STATUS_ENTRY(507, Insufficient Storage); // [RFC4918]
HTTP_STATUS_ENTRY(508, Loop Detected); // [RFC5842]
// 509 Unassigned
HTTP_STATUS_ENTRY(510, Not Extended); // [RFC2774]
HTTP_STATUS_ENTRY(511, Network Authentication Required); // [RFC6585]
// 512-599 Unassigned
}
#undef HTTP_STATUS_ENTRY
return nullptr;
}
//////////////////////////////////////////////////////
// init first time structure setup //
// clear resets an already-initialized structure //
//////////////////////////////////////////////////////
void
http_parser_init(HTTPParser *parser)
{
parser->m_parsing_http = true;
mime_parser_init(&parser->m_mime_parser);
}
void
http_parser_clear(HTTPParser *parser)
{
parser->m_parsing_http = true;
mime_parser_clear(&parser->m_mime_parser);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
#define GETNEXT(label) \
{ \
cur += 1; \
if (cur >= end) { \
goto label; \
} \
}
#define GETPREV(label) \
{ \
cur -= 1; \
if (cur < line_start) { \
goto label; \
} \
}
// NOTE: end is ONE CHARACTER PAST end of string!
ParseResult
http_parser_parse_req(HTTPParser *parser, HdrHeap *heap, HTTPHdrImpl *hh, const char **start, const char *end,
bool must_copy_strings, bool eof, int strict_uri_parsing, size_t max_request_line_size,
size_t max_hdr_field_size)
{
if (parser->m_parsing_http) {
MIMEScanner *scanner = &parser->m_mime_parser.m_scanner;
URLImpl *url;
ParseResult err;
bool line_is_real;
const char *cur;
const char *line_start;
const char *real_end;
const char *method_start;
const char *method_end;
const char *url_start;
const char *url_end;
const char *version_start;
const char *version_end;
swoc::TextView text, parsed;
real_end = end;
start:
hh->m_polarity = HTTPType::REQUEST;
// Make sure the line is not longer than max_request_line_size
if (scanner->get_buffered_line_size() > max_request_line_size) {
return ParseResult::ERROR;
}
text.assign(*start, real_end);
err = scanner->get(text, parsed, line_is_real, eof, MIMEScanner::ScanType::LINE);
*start = text.data();
if (static_cast<int>(err) < 0) {
return err;
}
// We have to get a request line. If we get parse done here,
// that meas we got an empty request
if (err == ParseResult::DONE) {
return ParseResult::ERROR;
}
if (err == ParseResult::CONT) {
return err;
}
ink_assert(parsed.size() < UINT16_MAX);
line_start = cur = parsed.data();
end = parsed.data_end();
if (static_cast<unsigned>(end - line_start) > max_request_line_size) {
return ParseResult::ERROR;
}
must_copy_strings = (must_copy_strings || (!line_is_real));
#if (ENABLE_PARSER_FAST_PATHS)
// first try fast path
if (end - cur >= 16) {
if (((cur[0] ^ 'G') | (cur[1] ^ 'E') | (cur[2] ^ 'T')) != 0) {
goto slow_case;
}
if (((end[-10] ^ 'H') | (end[-9] ^ 'T') | (end[-8] ^ 'T') | (end[-7] ^ 'P') | (end[-6] ^ '/') | (end[-4] ^ '.') |
(end[-2] ^ '\r') | (end[-1] ^ '\n')) != 0) {
goto slow_case;
}
if (!(isdigit(end[-5]) && isdigit(end[-3]))) {
goto slow_case;
}
if (!(ParseRules::is_space(cur[3]) && (!ParseRules::is_space(cur[4])) && (!ParseRules::is_space(end[-12])) &&
ParseRules::is_space(end[-11]))) {
goto slow_case;
}
if (&(cur[4]) >= &(end[-11])) {
goto slow_case;
}
HTTPVersion version{static_cast<uint8_t>(end[-5] - '0'), static_cast<uint8_t>(end[-3] - '0')};
http_hdr_method_set(heap, hh, {&(cur[0]), 3}, HTTP_WKSIDX_GET, must_copy_strings);
ink_assert(hh->u.req.m_url_impl != nullptr);
url = hh->u.req.m_url_impl;
url_start = &(cur[4]);
err = ::url_parse(heap, url, &url_start, &(end[-11]), must_copy_strings, strict_uri_parsing);
if (static_cast<int>(err) < 0) {
return err;
}
if (!http_hdr_version_set(hh, version)) {
return ParseResult::ERROR;
}
end = real_end;
parser->m_parsing_http = false;
ParseResult ret = mime_parser_parse(&parser->m_mime_parser, heap, hh->m_fields_impl, start, end, must_copy_strings, eof,
false, max_hdr_field_size);
// If we're done with the main parse do some validation
if (ret == ParseResult::DONE) {
ret = validate_hdr_request_target(HTTP_WKSIDX_GET, url);
}
if (ret == ParseResult::DONE) {
ret = validate_hdr_host(hh); // check HOST header
}
if (ret == ParseResult::DONE) {
ret = validate_hdr_content_length(heap, hh);
}
return ret;
}
#endif
slow_case:
method_start = nullptr;
method_end = nullptr;
url_start = nullptr;
url_end = nullptr;
version_start = nullptr;
version_end = nullptr;
url = nullptr;
if (ParseRules::is_cr(*cur))
GETNEXT(done);
if (ParseRules::is_lf(*cur)) {
goto start;
}
parse_method1:
if (ParseRules::is_ws(*cur)) {
GETNEXT(done);
goto parse_method1;
}
if (!ParseRules::is_token(*cur)) {
goto done;
}
method_start = cur;
GETNEXT(done);
parse_method2:
if (ParseRules::is_ws(*cur)) {
method_end = cur;
goto parse_version1;
}
if (!ParseRules::is_token(*cur)) {
goto done;
}
GETNEXT(done);
goto parse_method2;
parse_version1:
cur = end - 1;
if (ParseRules::is_lf(*cur) && (cur >= line_start)) {