forked from tempesta-tech/tempesta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_msg.c
1497 lines (1298 loc) · 38.8 KB
/
http_msg.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
/**
* Tempesta FW
*
* HTTP message manipulation helpers for the protocol processing.
*
* Copyright (C) 2014 NatSys Lab. (info@natsys-lab.com).
* Copyright (C) 2015-2021 Tempesta Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <linux/ctype.h>
#undef DEBUG
#if DBG_HTTP_PARSER > 0
#define DEBUG DBG_HTTP_PARSER
#endif
#include "lib/str.h"
#include "gfsm.h"
#include "http_msg.h"
#include "http_parser.h"
#include "ss_skb.h"
/**
* Build TfwStr representing HTTP header.
* @name - header name without ':';
* @value - header value;
*/
TfwStr *
tfw_http_msg_make_hdr(TfwPool *pool, const char *name, const char *val)
{
size_t n_len = strlen(name);
size_t v_len = val ? strlen(val) : 0;
size_t n;
TfwStr *hdr, *h_c;
char *data;
n = ((val ? 2 : 1) + 1) * sizeof(TfwStr) + n_len + v_len;
hdr = (TfwStr *)tfw_pool_alloc(pool, n);
if (!hdr)
return NULL;
TFW_STR_INIT(hdr);
hdr->len = n_len + v_len;
hdr->eolen = 2;
hdr->nchunks = (val ? 2 : 1);
hdr->chunks = hdr + 1;
data = (char *)(TFW_STR_LAST(hdr) + 1);
h_c = TFW_STR_CHUNK(hdr, 0);
TFW_STR_INIT(h_c);
h_c->data = data;
h_c->len = n_len;
tfw_cstrtolower_wo_avx2(data, name, n_len);
if (val) {
data += h_c->len;
++h_c;
TFW_STR_INIT(h_c);
h_c->data = data;
h_c->len = v_len;
h_c->flags = TFW_STR_HDR_VALUE;
memcpy(data, val, v_len);
}
return hdr;
}
/**
* Find @hdr in @array. @array must be sorted in alphabetical
* order. Similar to bsearch().
*/
const void *
__tfw_http_msg_find_hdr(const TfwStr *hdr, const void *array, size_t n,
size_t member_sz)
{
size_t start = 0, end = n;
int result, fc;
const TfwStr *h;
if (!TFW_STR_DUP(hdr))
h = hdr;
else
h = hdr->chunks;
fc = tolower(*(unsigned char *)(TFW_STR_CHUNK(h, 0)->data));
while (start < end) {
size_t mid = start + (end - start) / 2;
const TfwStr *sh = array + mid * member_sz;
int sc = *(unsigned char *)sh->data;
result = fc - sc;
if (!result)
result = tfw_stricmpspn(h, sh, ':');
if (result < 0)
end = mid;
else if (result > 0)
start = mid + 1;
else
return sh;
}
return NULL;
}
typedef struct {
TfwStr hdr; /* Header name. */
unsigned int id; /* id in TfwHttpHdrTbl */
} TfwHdrDef;
#define TfwStrDefV(v, id) {{ .data = (v), NULL, sizeof(v) - 1, 0 }, (id) }
static inline unsigned int
__tfw_http_msg_spec_hid(const TfwStr *hdr, const TfwHdrDef array[])
{
const TfwHdrDef *def;
size_t size = TFW_HTTP_HDR_RAW - TFW_HTTP_HDR_REGULAR;
/* TODO: return error if @hdr can't be applied to response or client. */
def = (TfwHdrDef *)__tfw_http_msg_find_hdr(hdr, array, size,
sizeof(TfwHdrDef));
return def ? def->id : TFW_HTTP_HDR_RAW;
}
/**
* Get header id in response header table for header @hdr.
*/
unsigned int
tfw_http_msg_resp_spec_hid(const TfwStr *hdr)
{
static const TfwHdrDef resp_hdrs[] = {
TfwStrDefV("connection:", TFW_HTTP_HDR_CONNECTION),
TfwStrDefV("content-length:", TFW_HTTP_HDR_CONTENT_LENGTH),
TfwStrDefV("content-type:", TFW_HTTP_HDR_CONTENT_TYPE),
TfwStrDefV("etag:", TFW_HTTP_HDR_ETAG),
TfwStrDefV("host:", TFW_HTTP_HDR_HOST),
TfwStrDefV("keep-alive:", TFW_HTTP_HDR_KEEP_ALIVE),
TfwStrDefV("referer:", TFW_HTTP_HDR_REFERER),
TfwStrDefV("server:", TFW_HTTP_HDR_SERVER),
TfwStrDefV("set-cookie:", TFW_HTTP_HDR_SET_COOKIE),
TfwStrDefV("transfer-encoding:",TFW_HTTP_HDR_TRANSFER_ENCODING),
TfwStrDefV("x-forwarded-for:", TFW_HTTP_HDR_X_FORWARDED_FOR),
TfwStrDefV("x-tempesta-cache:", TFW_HTTP_HDR_X_TEMPESTA_CACHE),
TfwStrDefV("upgrade:", TFW_HTTP_HDR_UPGRADE),
};
BUILD_BUG_ON(ARRAY_SIZE(resp_hdrs) !=
TFW_HTTP_HDR_RAW - TFW_HTTP_HDR_REGULAR);
return __tfw_http_msg_spec_hid(hdr, resp_hdrs);
}
/**
* Get header id in request header table for header @hdr.
*/
unsigned int
tfw_http_msg_req_spec_hid(const TfwStr *hdr)
{
static const TfwHdrDef req_hdrs[] = {
TfwStrDefV("connection:", TFW_HTTP_HDR_CONNECTION),
TfwStrDefV("content-length:", TFW_HTTP_HDR_CONTENT_LENGTH),
TfwStrDefV("content-type:", TFW_HTTP_HDR_CONTENT_TYPE),
TfwStrDefV("cookie:", TFW_HTTP_HDR_COOKIE),
TfwStrDefV("host:", TFW_HTTP_HDR_HOST),
TfwStrDefV("if-none-match:", TFW_HTTP_HDR_IF_NONE_MATCH),
TfwStrDefV("keep-alive:", TFW_HTTP_HDR_KEEP_ALIVE),
TfwStrDefV("referer:", TFW_HTTP_HDR_REFERER),
TfwStrDefV("transfer-encoding:",TFW_HTTP_HDR_TRANSFER_ENCODING),
TfwStrDefV("user-agent:", TFW_HTTP_HDR_USER_AGENT),
TfwStrDefV("x-forwarded-for:", TFW_HTTP_HDR_X_FORWARDED_FOR),
TfwStrDefV("x-tempesta-cache:", TFW_HTTP_HDR_X_TEMPESTA_CACHE),
TfwStrDefV("upgrade:", TFW_HTTP_HDR_UPGRADE),
};
BUILD_BUG_ON(ARRAY_SIZE(req_hdrs) !=
TFW_HTTP_HDR_RAW - TFW_HTTP_HDR_REGULAR);
return __tfw_http_msg_spec_hid(hdr, req_hdrs);
}
/**
* Fills @val with second part of special HTTP/1.1 header containing the
* header value.
*
* TODO: with the current HTTP-parser implementation (parsing header name,
* colon, LWS and value into different chunks) this procedure can be
* simplified to avoid the usage of predefined header arrays.
*/
void
__http_msg_hdr_val(TfwStr *hdr, unsigned id, TfwStr *val, bool client)
{
static const unsigned char hdr_lens[2][TFW_HTTP_HDR_RAW] = {
(unsigned char []) {
[TFW_HTTP_HDR_HOST] = SLEN("Host:"),
[TFW_HTTP_HDR_CONTENT_LENGTH] = SLEN("Content-Length:"),
[TFW_HTTP_HDR_CONTENT_TYPE] = SLEN("Content-Type:"),
[TFW_HTTP_HDR_CONNECTION] = SLEN("Connection:"),
[TFW_HTTP_HDR_X_FORWARDED_FOR] = SLEN("X-Forwarded-For:"),
[TFW_HTTP_HDR_X_TEMPESTA_CACHE] = SLEN("X-Tempesta-Cache:"),
[TFW_HTTP_HDR_KEEP_ALIVE] = SLEN("Keep-Alive:"),
[TFW_HTTP_HDR_TRANSFER_ENCODING]= SLEN("Transfer-Encoding:"),
[TFW_HTTP_HDR_SERVER] = SLEN("Server:"),
[TFW_HTTP_HDR_SET_COOKIE] = SLEN("Set-Cookie:"),
[TFW_HTTP_HDR_ETAG] = SLEN("ETag:"),
[TFW_HTTP_HDR_REFERER] = SLEN("Referer:"),
[TFW_HTTP_HDR_UPGRADE] = SLEN("Upgrade:"),
},
(unsigned char []) {
[TFW_HTTP_HDR_HOST] = SLEN("Host:"),
[TFW_HTTP_HDR_CONTENT_LENGTH] = SLEN("Content-Length:"),
[TFW_HTTP_HDR_CONTENT_TYPE] = SLEN("Content-Type:"),
[TFW_HTTP_HDR_CONNECTION] = SLEN("Connection:"),
[TFW_HTTP_HDR_X_FORWARDED_FOR] = SLEN("X-Forwarded-For:"),
[TFW_HTTP_HDR_X_TEMPESTA_CACHE] = SLEN("X-Tempesta-Cache:"),
[TFW_HTTP_HDR_KEEP_ALIVE] = SLEN("Keep-Alive:"),
[TFW_HTTP_HDR_TRANSFER_ENCODING]= SLEN("Transfer-Encoding:"),
[TFW_HTTP_HDR_USER_AGENT] = SLEN("User-Agent:"),
[TFW_HTTP_HDR_COOKIE] = SLEN("Cookie:"),
[TFW_HTTP_HDR_IF_NONE_MATCH] = SLEN("If-None-Match:"),
[TFW_HTTP_HDR_REFERER] = SLEN("Referer:"),
[TFW_HTTP_HDR_UPGRADE] = SLEN("Upgrade:"),
},
};
TfwStr *c, *end;
int nlen;
/* Empty and plain strings don't have header value part. */
if (unlikely(TFW_STR_PLAIN(hdr))) {
TFW_STR_INIT(val);
return;
}
BUG_ON(TFW_STR_DUP(hdr));
BUG_ON(id >= TFW_HTTP_HDR_RAW);
nlen = hdr_lens[client][id];
/*
* Only Host header is allowed to be empty.
* If header string is plain, it is always empty header.
* Not empty headers are compound strings.
*/
BUG_ON(id == TFW_HTTP_HDR_HOST ? nlen > hdr->len : nlen >= hdr->len);
*val = *hdr;
/* Field value, if it exist, lies in the separate chunk.
* So we skip several first chunks, containing field name,
* to get the field value. If we have field with empty value,
* we get an empty string with val->len = 0 and val->data from the
* last name's chunk, but it is unimportant.
*/
for (c = hdr->chunks, end = hdr->chunks + hdr->nchunks;
c < end; ++c)
{
BUG_ON(!c->len);
if (nlen > 0) {
nlen -= c->len;
val->len -= c->len;
}
else if (unlikely((c->data)[0] == ' '
|| (c->data)[0] == '\t'))
{
/*
* RFC 7230: skip OWS before header field.
* In most cases OWS is on the same chunk with
* the header name.
* Header field-value always begins at new chunk.
*/
val->len -= c->len;
}
else {
val->chunks = c;
return;
}
BUG_ON(val->nchunks < 1);
val->nchunks--;
}
/* Empty header value part. */
TFW_STR_INIT(val);
}
void
__h2_msg_hdr_val(TfwStr *hdr, TfwStr *out_val)
{
TfwStr *c, *end;
if (unlikely(TFW_STR_PLAIN(hdr))) {
TFW_STR_INIT(out_val);
return;
}
BUG_ON(TFW_STR_DUP(hdr));
*out_val = *hdr;
TFW_STR_FOR_EACH_CHUNK(c, hdr, end) {
if (c->flags & TFW_STR_HDR_VALUE) {
out_val->chunks = c;
return;
}
out_val->len -= c->len;
out_val->nchunks--;
}
/* Empty header value part. */
TFW_STR_INIT(out_val);
}
/**
* Slow check of generic (raw) header for singularity.
* Some of the header should be special and moved to tfw_http_hdr_t enum,
* so linear search is Ok here.
* @return true for headers which must never have duplicates.
*/
static inline bool
__hdr_is_singular(const TfwStr *hdr)
{
static const TfwStr hdr_singular[] = {
TFW_STR_STRING("authorization:"),
TFW_STR_STRING("from:"),
TFW_STR_STRING("if-unmodified-since:"),
TFW_STR_STRING("location:"),
TFW_STR_STRING("max-forwards:"),
TFW_STR_STRING("proxy-authorization:"),
TFW_STR_STRING("referer:"),
};
return tfw_http_msg_find_hdr(hdr, hdr_singular);
}
/**
* Lookup for the header @hdr in already collected headers table @ht,
* i.e. check whether the header is duplicate.
* The lookup is performed until ':', so header name only is enough in @hdr.
* @return the header id.
*/
unsigned int
tfw_http_msg_hdr_lookup(TfwHttpMsg *hm, const TfwStr *hdr)
{
unsigned int id;
TfwHttpHdrTbl *ht = hm->h_tbl;
for (id = TFW_HTTP_HDR_RAW; id < ht->off; ++id) {
TfwStr *h = &ht->tbl[id];
/* There is no sense to compare against all duplicates. */
if (h->flags & TFW_STR_DUPLICATE)
h = TFW_STR_CHUNK(h, 0);
if (!tfw_stricmpspn(hdr, h, ':'))
break;
}
return id;
}
/**
* Certain header fields are strictly singular and may not be repeated in
* an HTTP message. Duplicate of a singular header fields is a bug worth
* blocking the whole HTTP message.
*
* TODO: with the current HTTP-parser implementation (parsing header name,
* colon, LWS and value into different chunks) we can avoid slow string
* matcher, which is used in @tfw_http_msg_hdr_lookup(), and can compare
* strings just by chunks (including searching the stop character) for both
* HTTP/2 and HTTP/1.1 formatted headers (see @__hdr_name_cmp() below).
* Thus, @__h1_hdr_lookup() and @tfw_http_msg_hdr_lookup() procedures should
* be unified to @__hdr_name_cmp() and @__http_hdr_lookup() in order to
* substitute current mess of multiple partially duplicated procedures with
* one simple interface.
*/
static inline unsigned int
__h1_hdr_lookup(TfwHttpMsg *hm, const TfwStr *hdr)
{
unsigned int id = tfw_http_msg_hdr_lookup(hm, hdr);
if ((id < hm->h_tbl->off) && __hdr_is_singular(hdr))
__set_bit(TFW_HTTP_B_FIELD_DUPENTRY, hm->flags);
return id;
}
/**
* Special procedure comparing the name or HPACK static index of @cmp_hdr (can
* be in HTTP/2 or HTTP/1.1 format) against the header @hdr which also can be
* in HTTP/2 or HTTP/1.1 format.
*/
int
__hdr_name_cmp(const TfwStr *hdr, const TfwStr *cmp_hdr)
{
long n;
int i1, i2, off1, off2;
const TfwStr *c1, *c2;
BUG_ON(hdr->flags & TFW_STR_DUPLICATE);
BUG_ON(!cmp_hdr->len);
if (cmp_hdr->hpack_idx && cmp_hdr->hpack_idx == hdr->hpack_idx)
return 0;
if (unlikely(!hdr->len))
return 1;
i1 = i2 = 0;
off1 = off2 = 0;
n = min(hdr->len, cmp_hdr->len);
c1 = TFW_STR_CHUNK(hdr, 0);
c2 = TFW_STR_CHUNK(cmp_hdr, 0);
while (n) {
int cn = min(c1->len - off1, c2->len - off2);
if (tfw_cstricmp(c1->data + off1, c2->data + off2, cn))
return 1;
n -= cn;
if (cn == c1->len - off1) {
off1 = 0;
++i1;
c1 = TFW_STR_CHUNK(hdr, i1);
} else {
off1 += cn;
}
if (cn == c2->len - off2) {
off2 = 0;
++i2;
c2 = TFW_STR_CHUNK(cmp_hdr, i2);
} else {
off2 += cn;
}
BUG_ON(n && (!c1 || !c2));
/*
* Regardless of the header format (HTTP/2 or HTTP/1.1), the end
* of the name must match the end of the chunk, and the following
* chunk must contain value with appropriate flag (or it must
* contain just a single colon in case of HTTP/1.1-header).
*/
if (!off2) {
const TfwStr *prev_c1;
/*
* If @c2 or @c1 is NULL, then only name is contained in
* the @cmp_hdr or @hdr respectively.
*/
if (c2
&& !(c2->flags & TFW_STR_HDR_VALUE)
&& *c2->data != ':')
continue;
prev_c1 = TFW_STR_CHUNK(hdr, i1 - 1);
if (!off1
&& !(prev_c1->flags & TFW_STR_HDR_VALUE)
&& (!c1
|| c1->flags & TFW_STR_HDR_VALUE
|| *c1->data == ':'))
return 0;
return 1;
}
}
return 1;
}
/**
* As @__hdr_lookup(), but intended for search during HTTP/1.1=>HTTP/2
* and HTTP/2=>HTTP/1.1 transformations, comparing the specified name
* headers in HTTP/2 or HTTP/1.1 format.
*/
int
__http_hdr_lookup(TfwHttpMsg *hm, const TfwStr *hdr)
{
unsigned int id;
TfwHttpHdrTbl *ht = hm->h_tbl;
for (id = TFW_HTTP_HDR_RAW; id < ht->off; ++id) {
TfwStr *h = &ht->tbl[id];
/*
* We are looking only for the header's name matching,
* thus, there is no sense to compare against all duplicates.
*/
if (h->flags & TFW_STR_DUPLICATE)
h = TFW_STR_CHUNK(h, 0);
if (!__hdr_name_cmp(h, hdr))
break;
}
return id;
}
/**
* Open currently parsed header.
*/
void
tfw_http_msg_hdr_open(TfwHttpMsg *hm, unsigned char *hdr_start)
{
TfwStr *hdr = &hm->stream->parser.hdr;
BUG_ON(!TFW_STR_EMPTY(hdr));
hdr->data = hdr_start;
hdr->skb = ss_skb_peek_tail(&hm->msg.skb_head);
BUG_ON(!hdr->skb);
T_DBG3("open header at %p (char=[%c]), skb=%p\n",
hdr_start, *hdr_start, hdr->skb);
}
/**
* Store fully parsed, probably compound, header (i.e. close it) to
* HTTP message headers list.
*/
int
tfw_http_msg_hdr_close(TfwHttpMsg *hm)
{
TfwStr *hdr, *h;
TfwHttpHdrTbl *ht = hm->h_tbl;
TfwHttpParser *parser = &hm->stream->parser;
unsigned int id = parser->_hdr_tag;
BUG_ON(parser->hdr.flags & TFW_STR_DUPLICATE);
BUG_ON(id > TFW_HTTP_HDR_RAW);
/* Close just parsed header. */
parser->hdr.flags |= TFW_STR_COMPLETE;
/* Quick path for special headers. */
if (likely(id < TFW_HTTP_HDR_RAW)) {
hdr = h = &ht->tbl[id];
if (TFW_STR_EMPTY(hdr))
/* Just store the special header in empty slot. */
goto done;
/*
* Process duplicate header.
*
* RFC 7230 3.2.2: all duplicates of special singular
* headers must be blocked as early as possible,
* just when parser reads them.
*/
if (id < TFW_HTTP_HDR_NONSINGULAR) {
if (WARN_ON_ONCE(!TFW_MSG_H2(hm)
|| id != TFW_HTTP_HDR_COOKIE))
return TFW_BLOCK;
} else if (id != TFW_HTTP_HDR_X_FORWARDED_FOR) {
/*
* RFC 7230 3.2.2: duplicate of non-singular special
* header - leave the decision to classification layer.
*/
__set_bit(TFW_HTTP_B_FIELD_DUPENTRY, hm->flags);
}
goto duplicate;
}
/*
* A new raw header is to be stored, but it can be a duplicate of some
* existing header and we must find appropriate index for it.
* Both the headers, the new one and existing one, can already be
* compound.
*/
id = __http_hdr_lookup(hm, &parser->hdr);
/* Allocate some more room if not enough to store the header. */
if (unlikely(id == ht->size)) {
if (tfw_http_msg_grow_hdr_tbl(hm))
return TFW_BLOCK;
ht = hm->h_tbl;
}
hdr = h = &ht->tbl[id];
if (TFW_STR_EMPTY(hdr))
/* Add the new header. */
goto done;
duplicate:
h = tfw_str_add_duplicate(hm->pool, hdr);
if (unlikely(!h)) {
T_WARN("Cannot close header %p id=%d\n", &parser->hdr, id);
return TFW_BLOCK;
}
done:
/*
* During response HTTP/1.1=>HTTP/2 transformation we need only regular
* headers to be transformed, and status-line must not be present in the
* resulting HTTP/2 response at all; thus, we do not need status-line in
* the indirection map.
*/
if (TFW_RESP_TO_H2(hm)
&& id > TFW_HTTP_STATUS_LINE
&& tfw_h2_hdr_map((TfwHttpResp *)hm, hdr, id))
return TFW_BLOCK;
*h = parser->hdr;
TFW_STR_INIT(&parser->hdr);
T_DBG3("store header w/ ptr=%p len=%lu eolen=%u flags=%x id=%d\n",
h->data, h->len, h->eolen, h->flags, id);
/* Move the offset forward if current header is fully read. */
if (id == ht->off)
ht->off++;
return TFW_PASS;
}
/**
* Fixup the new data chunk starting at @data with length @len to @str.
*
* If @str is an empty string, then @len may not be zero. Please use
* other means for making TfwStr{} strings with such special properties.
*
* If @str doesn't have the length set yet, then @len is more like
* an offset from @data which is the current position in the string.
* The actual length is set relative to the start of @str.
*
* @len might be 0 if the field was fully read, but we have realized
* that just now by facing CRLF at the start of the current data chunk.
*/
int
__tfw_http_msg_add_str_data(TfwHttpMsg *hm, TfwStr *str, void *data,
size_t len, struct sk_buff *skb)
{
if (WARN_ON_ONCE(str->flags & (TFW_STR_DUPLICATE | TFW_STR_COMPLETE)))
return -EINVAL;
T_DBG3("store field chunk len=%lu data=%p(%c) field=<%#x,%lu,%p>\n",
len, data, isprint(*(char *)data) ? *(char *)data : '.',
str->flags, str->len, str->data);
if (TFW_STR_EMPTY(str)) {
if (!str->data)
__tfw_http_msg_set_str_data(str, data, skb);
str->len = data + len - (void *)str->data;
BUG_ON(!str->len);
}
else if (likely(len)) {
TfwStr *sn = tfw_str_add_compound(hm->pool, str);
if (!sn) {
T_WARN("Cannot grow HTTP data string\n");
return -ENOMEM;
}
__tfw_http_msg_set_str_data(sn, data, skb);
tfw_str_updlen(str, data + len);
}
return 0;
}
int
tfw_http_msg_grow_hdr_tbl(TfwHttpMsg *hm)
{
TfwHttpHdrTbl *ht = hm->h_tbl;
size_t order = ht->size / TFW_HTTP_HDR_NUM, new_order = order << 1;
ht = tfw_pool_realloc(hm->pool, ht, TFW_HHTBL_SZ(order),
TFW_HHTBL_SZ(new_order));
if (!ht)
return -ENOMEM;
ht->size = __HHTBL_SZ(new_order);
ht->off = hm->h_tbl->off;
bzero_fast(ht->tbl + __HHTBL_SZ(order),
__HHTBL_SZ(order) * sizeof(TfwStr));
hm->h_tbl = ht;
T_DBG3("grow http headers table to %d items\n", ht->size);
return 0;
}
/**
* Add new header @hdr to the message @hm just before CRLF.
*/
static int
__hdr_add(TfwHttpMsg *hm, const TfwStr *hdr, unsigned int hid)
{
int r;
TfwStr it = {};
TfwStr *h = TFW_STR_CHUNK(&hm->crlf, 0);
r = ss_skb_get_room(hm->msg.skb_head, hm->crlf.skb, h->data,
tfw_str_total_len(hdr), &it);
if (r)
return r;
tfw_str_fixup_eol(&it, tfw_str_eolen(hdr));
if (tfw_strcpy(&it, hdr))
return TFW_BLOCK;
/*
* Initialize the header table item by the iterator chunks.
* While the data references in the item are valid, some conventions
* (e.g. header name and value are placed in different chunks) aren't
* satisfied. So don't consider the header for normal HTTP processing.
*/
hm->h_tbl->tbl[hid] = it;
return 0;
}
/**
* Expand @orig_hdr by appending or replacing with the @hdr.
* (CRLF is not accounted in TfwStr representation of HTTP headers).
*
* Expand the first duplicate header, do not produce more duplicates.
*/
static int
__hdr_expand(TfwHttpMsg *hm, TfwStr *orig_hdr, const TfwStr *hdr, bool append)
{
int r;
TfwStr *h, it = {};
if (TFW_STR_DUP(orig_hdr))
orig_hdr = __TFW_STR_CH(orig_hdr, 0);
BUG_ON(!append && (hdr->len < orig_hdr->len));
h = TFW_STR_LAST(orig_hdr);
r = ss_skb_get_room(hm->msg.skb_head, h->skb, h->data + h->len,
append ? hdr->len : hdr->len - orig_hdr->len, &it);
if (r)
return r;
if (tfw_strcat(hm->pool, orig_hdr, &it)) {
T_WARN("Cannot concatenate hdr %.*s with %.*s\n",
PR_TFW_STR(orig_hdr), PR_TFW_STR(hdr));
return TFW_BLOCK;
}
return tfw_strcpy(append ? &it : orig_hdr, hdr) ? TFW_BLOCK : 0;
}
/**
* Delete header with identifier @hid from skb data and header table.
*/
static int
__hdr_del(TfwHttpMsg *hm, unsigned int hid)
{
TfwHttpHdrTbl *ht = hm->h_tbl;
TfwStr *dup, *end, *hdr = &ht->tbl[hid];
/* Delete the underlying data. */
TFW_STR_FOR_EACH_DUP(dup, hdr, end) {
if (ss_skb_cutoff_data(hm->msg.skb_head, dup, 0,
tfw_str_eolen(dup)))
return TFW_BLOCK;
};
/* Delete the header from header table. */
if (hid < TFW_HTTP_HDR_RAW) {
TFW_STR_INIT(&ht->tbl[hid]);
} else {
if (hid < ht->off - 1)
memmove(&ht->tbl[hid], &ht->tbl[hid + 1],
(ht->off - hid - 1) * sizeof(TfwStr));
--ht->off;
}
return 0;
}
/**
* Substitute header value.
*
* The original header may have LF or CRLF as it's EOL and such bytes are
* not a part of a header field string in Tempesta (at the moment). While
* substitution, we may want to follow the EOL pattern of the original. So,
* if the substitute string without the EOL fits into original header, then
* the fast path can be used. Otherwise, original header is expanded to fit
* substitute.
*/
static int
__hdr_sub(TfwHttpMsg *hm, const TfwStr *hdr, unsigned int hid)
{
TfwHttpHdrTbl *ht = hm->h_tbl;
TfwStr *dst, *tmp, *end, *orig_hdr = &ht->tbl[hid];
TFW_STR_FOR_EACH_DUP(dst, orig_hdr, end) {
if (dst->len < hdr->len)
continue;
/*
* Adjust @dst to have no more than @hdr.len bytes and rewrite
* the header in-place. Do not call @ss_skb_cutoff_data if no
* adjustment is needed.
*/
if (dst->len != hdr->len
&& ss_skb_cutoff_data(hm->msg.skb_head, dst, hdr->len, 0))
return TFW_BLOCK;
if (tfw_strcpy(dst, hdr))
return TFW_BLOCK;
goto cleanup;
}
if (__hdr_expand(hm, orig_hdr, hdr, false))
return TFW_BLOCK;
dst = TFW_STR_DUP(orig_hdr) ? __TFW_STR_CH(orig_hdr, 0) : orig_hdr;
cleanup:
TFW_STR_FOR_EACH_DUP(tmp, orig_hdr, end) {
if (tmp != dst
&& ss_skb_cutoff_data(hm->msg.skb_head, tmp, 0,
tfw_str_eolen(tmp)))
return TFW_BLOCK;
}
*orig_hdr = *dst;
return TFW_PASS;
}
/**
* Transform HTTP message @hm header with identifier @hid.
* @hdr must be compound string and contain two or three parts:
* header name, colon and header value. If @hdr value is empty,
* then the header will be deleted from @hm.
* If @hm already has the header it will be replaced by the new header
* unless @append.
* If @append is true, then @val will be concatenated to current
* header with @hid and @name, otherwise a new header will be created
* if the message has no the header.
*
* Note: The substitute string @hdr should have CRLF as EOL. The original
* string @orig_hdr may have a single LF as EOL. We may want to follow
* the EOL pattern of the original. For that, the EOL of @hdr needs
* to be made the same as in the original header field string.
*
* Note: In case of response transformation from HTTP/1.1 to HTTP/2, for
* optimization purposes, we use special add/replace procedures to adjust
* headers and create HTTP/2 representation at once; for headers deletion
* procedure there is no sense to use special HTTP/2 handling (the header
* must not exist in the resulting response); in case of headers appending
* we at first create the usual HTTP/1.1 representation of the final header
* and then transform it into HTTP/2 form at the common stage of response
* HTTP/2 transformation - we have no other choice, since we need a full
* header for going through the HTTP/2 transformation (i.e. search in the
* HPACK encoder dynamic index).
*/
int
tfw_http_msg_hdr_xfrm_str(TfwHttpMsg *hm, const TfwStr *hdr, unsigned int hid,
bool append)
{
TfwHttpHdrTbl *ht = hm->h_tbl;
TfwStr *orig_hdr = NULL;
const TfwStr *s_val = TFW_STR_CHUNK(hdr, 2);
if (unlikely(!ht)) {
T_WARN("Try to adjust lightweight response.");
return -EINVAL;
}
/* Firstly, get original message header to transform. */
if (hid < TFW_HTTP_HDR_RAW) {
orig_hdr = &ht->tbl[hid];
if (TFW_STR_EMPTY(orig_hdr) && !s_val)
/* Not found, nothing to delete. */
return 0;
} else {
hid = __h1_hdr_lookup(hm, hdr);
if (hid == ht->off && !s_val)
/* Not found, nothing to delete. */
return 0;
if (hid == ht->size) {
if (tfw_http_msg_grow_hdr_tbl(hm))
return -ENOMEM;
ht = hm->h_tbl;
}
if (hid == ht->off)
++ht->off;
else
orig_hdr = &ht->tbl[hid];
}
if (unlikely(append && hid < TFW_HTTP_HDR_NONSINGULAR)) {
T_WARN("Appending to nonsingular header %d\n", hid);
return -ENOENT;
}
if (!orig_hdr || TFW_STR_EMPTY(orig_hdr)) {
if (unlikely(!s_val))
return 0;
return __hdr_add(hm, hdr, hid);
}
if (!s_val)
return __hdr_del(hm, hid);
if (append) {
TfwStr hdr_app = {
.chunks = (TfwStr []){
{ .data = ", ", .len = 2 },
{ .data = s_val->data, .len = s_val->len }
},
.len = s_val->len + 2,
.nchunks = 2
};
return __hdr_expand(hm, orig_hdr, &hdr_app, true);
}
return __hdr_sub(hm, hdr, hid);
}
/**
* Same as @tfw_http_msg_hdr_xfrm_str() but use c-strings as argument.
*/
int
tfw_http_msg_hdr_xfrm(TfwHttpMsg *hm, char *name, size_t n_len,
char *val, size_t v_len, unsigned int hid, bool append)
{
TfwStr new_hdr = {
.chunks = (TfwStr []){
{ .data = name, .len = n_len },
{ .data = S_DLM, .len = SLEN(S_DLM) },
{ .data = val, .len = v_len },
},
.len = n_len + SLEN(S_DLM) + v_len,
.eolen = 2,
.nchunks = (val ? 3 : 2)
};
BUG_ON(!val && v_len);
return tfw_http_msg_hdr_xfrm_str(hm, &new_hdr, hid, append);
}
/**
* Delete @str (any parsed part of HTTP message) from skb data and
* init @str.
*/
int
tfw_http_msg_del_str(TfwHttpMsg *hm, TfwStr *str)
{
int r;
BUG_ON(TFW_STR_DUP(str));
if ((r = ss_skb_cutoff_data(hm->msg.skb_head, str, 0, 0)))
return r;
TFW_STR_INIT(str);
return 0;
}
/**
* Remove hop-by-hop headers in the message
*
* Connection header should not be removed, tfw_http_set_hdr_connection()
* optimize removal of the header.
*/
int
tfw_http_msg_del_hbh_hdrs(TfwHttpMsg *hm)
{
TfwHttpHdrTbl *ht = hm->h_tbl;
unsigned int hid = ht->off;
int r = 0;
do {
hid--;
if (hid == TFW_HTTP_HDR_CONNECTION)
continue;
if (ht->tbl[hid].flags & TFW_STR_HBH_HDR)
if ((r = __hdr_del(hm, hid)))
return r;
} while (hid);
return 0;
}
/**
* Modify message body, add string @data to the end of the body (if @append) or
* to its beginning.
*/
static int
tfw_http_msg_body_xfrm(TfwHttpMsg *hm, const TfwStr *data, bool append)
{
int r;
TfwStr it = {};
char *st = append
? (TFW_STR_CURR(&hm->body)->data + TFW_STR_CURR(&hm->body)->len)
: (TFW_STR_CHUNK(&hm->body, 0)->data);
/*
* Last skb in the message skb list is the last skb of the body,
* it's safe to add more chunks there. There are no trailer headers
* since trailer may appear only after chunked body, but the message
* wasn't in chunked encoding before.