forked from tempesta-tech/tempesta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.c
6901 lines (6160 loc) · 194 KB
/
http.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
*
* Core HTTP-layer processing, including HTTP/2 and HTTP/1.1 versions.
*
* Notes about the design of HTTP/2 implementation.
*
* In order to perform internal HTTP-specific analysis (HTTP-tables,
* HTTP-limits, Sessions module, Scheduling etc.) - for both HTTP/2 and
* HTTP/1.1 processing - we need representation of HTTP-message content.
* In case of HTTP/1.1 we have such representation right from the message's
* source skb - those are ASCII strings, which don't need to be decrypted
* and allow quick processing with SIMD instructions. However, content of
* HTTP/2-message (i.e. headers) could be encrypted with special HPACK
* methods (static/dynamic indexing and Huffman coding), which doesn't
* allow to analyze the message content directly. To solve this issue for
* HTTP/2-requests processing, current implementation of HTTP-layer creates
* HTTP/1.1-representation of HTTP/2 headers (during HPACK-decoding and
* HTTP/2-parsing) and places that representation into the special pool
* @TfwHttpReq.pit.pool.
*
* Actually, we don't need to create separate HTTP/1.1-representation
* for all HTTP/2 headers: some of them could be not encoded (not in Huffman
* form - in ASCII instead) and some - could be just indexed (we can keep static
* and dynamic indexes during internal request analysis and convert them into
* ASCII strings in-place - on demand); thus, in current implementation we save
* memory/processor resources and create additional HTTP/1.1-representation only
* for Huffman encoded headers. On the final stage of request processing (in
* @tfw_h2_adjust_req() procedure, before re-sending request to backend) the
* obtained @TfwHttpReq.pit.pool with HTTP/1.1-representation is used for
* request assembling in case of HTTP/2 => HTTP/1.1 transformation.
*
* Described above approach was chosen instead of immediate HTTP/2 => HTTP/1.1
* transformation on HTTP/2-message decoding, because the latter one is not
* scales for the case of HTTP/2 => HTTP/2 proxying, since during request
* receiving/processing we do not know what backend (HTTP/2 or HTTP/1.1)
* the request attended for. Thus, until the backend determination moment,
* we need to leave HTTP/2-representation in the source skb.
* Yet another choice (instead of HTTP/1.1-representation creation for HTTP/2
* message) could be an implementation HTTP/2-specific functionality set with
* custom flags in @TfwHttpReq.h_tbl - to perform HTTP-specific analysis right
* over source HTTP/2-representation of the message. However, there are several
* problems with HTTP/2-representation analysis:
* 1. HTTP/2 still allows ASCII strings, and even the same HTTP message may
* contain the same header in ASCII and in static table index. Thus, even
* with pure HTTP/2 operation we still must care about binary headers
* representation and plain ASCII representation. That means that the whole
* headers analyzing logic (mentioned above Schedulers, HTTPTables etc.) must
* either encode/decode strings on the fly or keep the two representations
* for the headers;
* 2. Huffman decoding is extremely slow: it operates on units less than a byte,
* while we use SIMD operations for ASCII processing. The bytes-crossing
* logic doesn't allow to efficiently encode Huffman decoding in SIMD;
* 3. Huffman encoding, also due to byte boundary crossing, mangles string
* information representation, so efficient strings matching algorithms
* required for #496 and #732 can not be applied.
* Thus, the points specified above (and #732) leave us only one possibility:
* since we must process ASCII-headers along with Huffman-encoded, we have to
* decode Huffman on HTTP-message reception.
*
* Regarding of internal HTTP/2-responses (error and cached) generation - dual
* logic design is used; i.e. along with existing functionality for
* HTTP/1.1-responses creation, separate logic implemented for HTTP/2-responses
* generation, in order to get performance benefits of creating a message right
* away in HTTP/2-format instead of transforming into HTTP/2 from already
* created HTTP/1.1-message.
*
* Copyright (C) 2014 NatSys Lab. (info@natsys-lab.com).
* Copyright (C) 2015-2022 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/string.h>
#include <linux/vmalloc.h>
#include <linux/sort.h>
#include <linux/bsearch.h>
#include "lib/hash.h"
#include "lib/str.h"
#include "cache.h"
#include "hash.h"
#include "http_limits.h"
#include "http_tbl.h"
#include "http_parser.h"
#include "http_frame.h"
#include "client.h"
#include "http_msg.h"
#include "http_sess.h"
#include "log.h"
#include "procfs.h"
#include "server.h"
#include "tls.h"
#include "apm.h"
#include "access_log.h"
#include "sync_socket.h"
#include "lib/common.h"
#define S_H2_METHOD ":method"
#define S_H2_SCHEME ":scheme"
#define S_H2_AUTH ":authority"
#define S_H2_PATH ":path"
#define S_H2_STAT ":status"
#define RESP_BUF_LEN 128
static DEFINE_PER_CPU(char[RESP_BUF_LEN], g_buf);
#define TFW_CFG_BLK_DEF (TFW_BLK_ERR_REPLY)
unsigned short tfw_blk_flags = TFW_CFG_BLK_DEF;
/* Array of whitelist marks for request's skb. */
static struct {
unsigned int *mrks;
unsigned int sz;
} tfw_wl_marks;
#define S_CRLFCRLF "\r\n\r\n"
#define S_HTTP "http://"
#define S_HTTPS "https://"
#define S_200 "HTTP/1.1 200 OK"
#define S_302 "HTTP/1.1 302 Found"
#define S_304 "HTTP/1.1 304 Not Modified"
#define S_400 "HTTP/1.1 400 Bad Request"
#define S_403 "HTTP/1.1 403 Forbidden"
#define S_404 "HTTP/1.1 404 Not Found"
#define S_412 "HTTP/1.1 412 Precondition Failed"
#define S_500 "HTTP/1.1 500 Internal Server Error"
#define S_502 "HTTP/1.1 502 Bad Gateway"
#define S_503 "HTTP/1.1 503 Service Unavailable"
#define S_504 "HTTP/1.1 504 Gateway Timeout"
#define S_XFF "x-forwarded-for"
#define S_WARN "warning"
#define S_F_HOST "host: "
#define S_F_DATE "date: "
#define S_F_CONTENT_LENGTH "content-length: "
#define S_F_CONTENT_TYPE "content-type: "
#define S_F_CONNECTION "connection: "
#define S_F_ETAG "etag: "
#define S_F_RETRY_AFTER "retry-after: "
#define S_F_SERVER "server: "
#define S_V_DATE "Sun, 06 Nov 1994 08:49:37 GMT"
#define S_V_CONTENT_LENGTH "9999"
#define S_V_CONN_CLOSE "close"
#define S_V_CONN_KA "keep-alive"
#define S_V_RETRY_AFTER "10"
#define S_V_MULTIPART "multipart/form-data; boundary="
#define S_V_WARN "110 - Response is stale"
#define S_H_CONN_KA S_F_CONNECTION S_V_CONN_KA S_CRLFCRLF
#define S_H_CONN_CLOSE S_F_CONNECTION S_V_CONN_CLOSE S_CRLFCRLF
#define S_200_PART_01 S_200 S_CRLF S_F_DATE
#define S_200_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_400_PART_01 S_400 S_CRLF S_F_DATE
#define S_400_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_403_PART_01 S_403 S_CRLF S_F_DATE
#define S_403_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_404_PART_01 S_404 S_CRLF S_F_DATE
#define S_404_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_412_PART_01 S_412 S_CRLF S_F_DATE
#define S_412_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_500_PART_01 S_500 S_CRLF S_F_DATE
#define S_500_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_502_PART_01 S_502 S_CRLF S_F_DATE
#define S_502_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_503_PART_01 S_503 S_CRLF S_F_DATE
#define S_503_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF \
S_F_RETRY_AFTER S_V_RETRY_AFTER S_CRLF
#define S_504_PART_01 S_504 S_CRLF S_F_DATE
#define S_504_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_DEF_PART_02 S_CRLF S_F_CONTENT_LENGTH "0" S_CRLF
#define S_DEF_PART_03 S_F_SERVER TFW_NAME "/" TFW_VERSION S_CRLF
/*
* Array with predefined response data
*/
static TfwStr http_predef_resps[RESP_NUM] = {
[RESP_200] = {
.chunks = (TfwStr []){
{ .data = S_200_PART_01, .len = SLEN(S_200_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_200_PART_02, .len = SLEN(S_200_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_200_PART_01 S_V_DATE S_200_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/* Response has invalid syntax, client shouldn't repeat it. */
[RESP_400] = {
.chunks = (TfwStr []){
{ .data = S_400_PART_01, .len = SLEN(S_400_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_400_PART_02, .len = SLEN(S_400_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_400_PART_01 S_V_DATE S_400_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/* Response is syntactically valid, but refuse to authorize it. */
[RESP_403] = {
.chunks = (TfwStr []){
{ .data = S_403_PART_01, .len = SLEN(S_403_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_403_PART_02, .len = SLEN(S_403_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_403_PART_01 S_V_DATE S_403_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/* Can't find the requested resource. */
[RESP_404] = {
.chunks = (TfwStr []){
{ .data = S_404_PART_01, .len = SLEN(S_404_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_404_PART_02, .len = SLEN(S_404_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_404_PART_01 S_V_DATE S_404_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
[RESP_412] = {
.chunks = (TfwStr []){
{ .data = S_412_PART_01, .len = SLEN(S_412_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_412_PART_02, .len = SLEN(S_412_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_412_PART_01 S_V_DATE S_412_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/* Internal error in TempestaFW. */
[RESP_500] = {
.chunks = (TfwStr []){
{ .data = S_500_PART_01, .len = SLEN(S_500_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_500_PART_02, .len = SLEN(S_500_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_500_PART_01 S_V_DATE S_500_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/* Error (syntax or network) while receiving request from backend. */
[RESP_502] = {
.chunks = (TfwStr []){
{ .data = S_502_PART_01, .len = SLEN(S_502_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_502_PART_02, .len = SLEN(S_502_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_502_PART_01 S_V_DATE S_502_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/*
* Sticky cookie or JS challenge failed, refuse to serve the client.
* Add Retry-After header, normal browser will repeat the request
* after given time, 10s by default.
*/
[RESP_503] = {
.chunks = (TfwStr []){
{ .data = S_503_PART_01, .len = SLEN(S_503_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_503_PART_02, .len = SLEN(S_503_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_503_PART_01 S_V_DATE S_503_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
},
/* Can't get a response in time. */
[RESP_504] = {
.chunks = (TfwStr []){
{ .data = S_504_PART_01, .len = SLEN(S_504_PART_01) },
{ .data = NULL, .len = SLEN(S_V_DATE) },
{ .data = S_504_PART_02, .len = SLEN(S_504_PART_02) },
{ .data = S_DEF_PART_03, .len = SLEN(S_DEF_PART_03) },
{ .data = S_CRLF, .len = SLEN(S_CRLF) },
{ .data = NULL, .len = 0 },
},
.len = SLEN(S_504_PART_01 S_V_DATE S_504_PART_02 S_DEF_PART_03
S_CRLF),
.nchunks = 6
}
};
/*
* Chunks for various message parts in @http_predef_resps array
* have predefined positions:
* 1: Start line,
* 2: Date,
* 3: Content-Length header,
* 4: Server header,
* 5: CRLF,
* 6: Message body.
* Message body is empty by default but can be overridden by 'response_body'
* directive.
*
* Some position-dependent macros specific to @http_predef_resps
* are defined below.
*/
#define TFW_STR_START_CH(msg) __TFW_STR_CH(msg, 0)
#define TFW_STR_DATE_CH(msg) __TFW_STR_CH(msg, 1)
#define TFW_STR_CLEN_CH(msg) __TFW_STR_CH(msg, 2)
#define TFW_STR_SRV_CH(msg) __TFW_STR_CH(msg, 3)
#define TFW_STR_CRLF_CH(msg) __TFW_STR_CH(msg, 4)
#define TFW_STR_BODY_CH(msg) __TFW_STR_CH(msg, 5)
/*
* Two static TfwStr structures are needed due to have the opportunity
* to set separately one page body, e.g. for 500 answer, and another
* page body - for the remaining 5xx answers.
*/
static TfwStr http_4xx_resp_body = {
.chunks = (TfwStr []){
{ .data = NULL, .len = 0 },
{ .data = NULL, .len = 0 },
},
.len = 0,
};
static TfwStr http_5xx_resp_body = {
.chunks = (TfwStr []){
{ .data = NULL, .len = 0 },
{ .data = NULL, .len = 0 },
},
.len = 0,
};
/*
* Prepare current date in the format required for HTTP "Date:"
* header field. See RFC 2616 section 3.3.
*/
static void
tfw_http_prep_date_from(char *buf, long date)
{
struct tm tm;
char *ptr = buf;
static const char * const wday[] =
{ "Sun, ", "Mon, ", "Tue, ",
"Wed, ", "Thu, ", "Fri, ", "Sat, " };
static const char * const month[] =
{ " Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ",
" Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec " };
/*
* If you see the function in perf top, then replace the naive
* printer by https://github.com/jeaiii/itoa.git
*/
#define PRINT_2DIGIT(p, n) \
*p++ = (n <= 9) ? '0' : '0' + n / 10; \
*p++ = '0' + n % 10;
time64_to_tm(date, 0, &tm);
memcpy(ptr, wday[tm.tm_wday], 5);
ptr += 5;
PRINT_2DIGIT(ptr, tm.tm_mday);
memcpy(ptr, month[tm.tm_mon], 5);
ptr += 5;
PRINT_2DIGIT(ptr, (tm.tm_year + 1900) / 100);
PRINT_2DIGIT(ptr, (tm.tm_year + 1900) % 100);
*ptr++ = ' ';
PRINT_2DIGIT(ptr, tm.tm_hour);
*ptr++ = ':';
PRINT_2DIGIT(ptr, tm.tm_min);
*ptr++ = ':';
PRINT_2DIGIT(ptr, tm.tm_sec);
memcpy(ptr, " GMT", 4);
#undef PRINT_2DIGIT
}
static inline void
tfw_http_prep_date(char *buf)
{
tfw_http_prep_date_from(buf, tfw_current_timestamp());
}
int
tfw_h2_prep_redirect(TfwHttpResp *resp, unsigned short status, TfwStr *rmark,
TfwStr *cookie, TfwStr *body)
{
int r;
TfwHPackInt vlen;
unsigned int stream_id;
unsigned long hdrs_len, loc_val_len;
TfwHttpReq *req = resp->req;
TfwHttpTransIter *mit = &resp->mit;
TfwMsgIter *iter = &mit->iter;
struct sk_buff **skb_head = &resp->msg.skb_head;
static TfwStr h_loc = TFW_STR_STRING(S_LOCATION);
static TfwStr proto = TFW_STR_STRING(S_HTTPS);
static TfwStr h_sc = TFW_STR_STRING(S_SET_COOKIE);
TfwStr host, *host_ptr = &host, s_vlen = {};
stream_id = tfw_h2_stream_id_close(req, HTTP2_HEADERS,
HTTP2_F_END_STREAM);
if (unlikely(!stream_id))
return -ENOENT;
/* Set HTTP/2 ':status' pseudo-header. */
mit->start_off = FRAME_HEADER_SIZE;
r = tfw_h2_resp_status_write(resp, status, TFW_H2_TRANS_EXPAND, false);
if (unlikely(r))
return r;
/* Add 'date' header. */
r = tfw_h2_add_hdr_date(resp, TFW_H2_TRANS_EXPAND, false);
if (unlikely(r))
return r;
/* Add 'location' header (possibly, with redirection mark). */
h_loc.hpack_idx = 46;
r = tfw_hpack_encode(resp, &h_loc, TFW_H2_TRANS_EXPAND, false);
if (unlikely(r))
return r;
if (req->host.len)
host_ptr = &req->host;
else
__h2_msg_hdr_val(&req->h_tbl->tbl[TFW_HTTP_HDR_HOST], &host);
loc_val_len = req->uri_path.len;
loc_val_len += host_ptr->len ? host_ptr->len + proto.len : 0;
loc_val_len += rmark->len;
write_int(loc_val_len, 0x7F, 0, &vlen);
s_vlen.data = vlen.buf;
s_vlen.len = vlen.sz;
r = tfw_http_msg_expand_data(iter, skb_head, &s_vlen, NULL);
if (unlikely(r))
return r;
if (host_ptr->len) {
r = tfw_http_msg_expand_data(iter, skb_head, &proto, NULL);
if (unlikely(r))
return r;
r = tfw_http_msg_expand_data(iter, skb_head, host_ptr, NULL);
if (unlikely(r))
return r;
}
if (rmark->len) {
r = tfw_http_msg_expand_data(iter, skb_head, rmark, NULL);
if (unlikely(r))
return r;
}
r = tfw_http_msg_expand_data(iter, skb_head, &req->uri_path, NULL);
if (unlikely(r))
return r;
hdrs_len = s_vlen.len + loc_val_len;
/* Add 'set-cookie' header. */
h_sc.hpack_idx = 55;
r = tfw_hpack_encode(resp, &h_sc, TFW_H2_TRANS_EXPAND, false);
if (unlikely(r))
return r;
write_int(cookie->len, 0x7F, 0, &vlen);
s_vlen.data = vlen.buf;
s_vlen.len = vlen.sz;
r = tfw_http_msg_expand_data(iter, skb_head, &s_vlen, NULL);
if (unlikely(r))
return r;
r = tfw_http_msg_expand_data(iter, skb_head, cookie, NULL);
if (unlikely(r))
return r;
hdrs_len += s_vlen.len + cookie->len;
hdrs_len += mit->acc_len;
return tfw_h2_frame_local_resp(resp, stream_id, hdrs_len, body);
}
#define S_REDIR_302 S_302 S_CRLF
#define S_REDIR_503 S_503 S_CRLF
#define S_REDIR_GEN " Redirection" S_CRLF
#define S_REDIR_P_01 S_F_DATE
#define S_REDIR_P_02 S_CRLF S_F_LOCATION
#define S_REDIR_P_03 S_CRLF S_F_SET_COOKIE
#define S_REDIR_KEEP S_CRLF S_F_CONNECTION S_V_CONN_KA S_CRLF
#define S_REDIR_CLOSE S_CRLF S_F_CONNECTION S_V_CONN_CLOSE S_CRLF
/**
* The response redirects the client to the same URI as the original request,
* but it includes 'Set-Cookie:' header field that sets Tempesta sticky cookie.
* If JS challenge is enabled, then body contained JS challenge is provided.
* Body string contains the 'Content-Length' header, CRLF and body itself.
*/
int
tfw_h1_prep_redirect(TfwHttpResp *resp, unsigned short status, TfwStr *rmark,
TfwStr *cookie, TfwStr *body)
{
TfwHttpReq *req = resp->req;
size_t data_len;
int ret = 0;
TfwMsgIter it;
static TfwStr rh_302 = {
.data = S_REDIR_302, .len = SLEN(S_REDIR_302) };
static TfwStr rh_503 = {
.data = S_REDIR_503, .len = SLEN(S_REDIR_503) };
TfwStr rh_gen = {
.chunks = (TfwStr []){
{ .data = S_0, .len = SLEN(S_0) },
{ .data = (*this_cpu_ptr(&g_buf) + RESP_BUF_LEN / 2),
.len = 3 },
{ .data = S_REDIR_GEN, .len = SLEN(S_REDIR_GEN) }
},
.len = SLEN(S_0 S_REDIR_GEN) + 3,
.nchunks = 3
};
TfwStr h_common_1 = {
.chunks = (TfwStr []){
{ .data = S_REDIR_P_01, .len = SLEN(S_REDIR_P_01) },
{ .data = *this_cpu_ptr(&g_buf), .len = SLEN(S_V_DATE) },
{ .data = S_REDIR_P_02, .len = SLEN(S_REDIR_P_02) }
},
.len = SLEN(S_REDIR_P_01 S_V_DATE S_REDIR_P_02),
.nchunks = 3
};
static TfwStr h_common_2 = {
.data = S_REDIR_P_03, .len = SLEN(S_REDIR_P_03) };
static TfwStr crlf = {
.data = S_CRLF, .len = SLEN(S_CRLF) };
static TfwStr crlf_keep = {
.data = S_REDIR_KEEP, .len = SLEN(S_REDIR_KEEP) };
static TfwStr crlf_close = {
.data = S_REDIR_CLOSE, .len = SLEN(S_REDIR_CLOSE) };
TfwStr c_len_crlf = {
.chunks = (TfwStr []){
{ .data = S_F_CONTENT_LENGTH,
.len = SLEN(S_F_CONTENT_LENGTH) },
{ .data = (*this_cpu_ptr(&g_buf) + RESP_BUF_LEN / 2 + 3),
.len = 0 },
{ .data = S_CRLFCRLF, .len = SLEN(S_CRLFCRLF) }
},
.len = SLEN(S_F_CONTENT_LENGTH S_CRLFCRLF),
.nchunks = 3
};
static TfwStr protos[] = {
{ .data = S_HTTP, .len = SLEN(S_HTTP) },
{ .data = S_HTTPS, .len = SLEN(S_HTTPS) },
};
TfwStr *proto = &protos[TFW_CONN_PROTO(req->conn) == TFW_FSM_HTTPS];
TfwStr host, *rh, *cookie_crlf = &crlf;
if (status == 302) {
rh = &rh_302;
} else if (status == 503) {
rh = &rh_503;
} else {
tfw_ultoa(status, __TFW_STR_CH(&rh_gen, 1)->data, 3);
rh = &rh_gen;
}
__TFW_STR_CH(&c_len_crlf, 1)->len += tfw_ultoa(
body ? body->len : 0, __TFW_STR_CH(&c_len_crlf, 1)->data,
RESP_BUF_LEN / 2 - 3);
c_len_crlf.len += __TFW_STR_CH(&c_len_crlf, 1)->len;
if (req->host.len)
host = req->host;
else
tfw_http_msg_clnthdr_val(req,
&req->h_tbl->tbl[TFW_HTTP_HDR_HOST],
TFW_HTTP_HDR_HOST, &host);
/* Set "Connection:" header field if needed. */
if (test_bit(TFW_HTTP_B_CONN_CLOSE, req->flags))
cookie_crlf = &crlf_close;
else if (test_bit(TFW_HTTP_B_CONN_KA, req->flags))
cookie_crlf = &crlf_keep;
/* Add variable part of data length to get the total */
data_len = rh->len + h_common_1.len;
data_len += host.len ? host.len + proto->len : 0;
data_len += rmark->len;
data_len += req->uri_path.len + h_common_2.len + cookie->len;
data_len += cookie_crlf->len + c_len_crlf.len;
data_len += body ? body->len : 0;
if (tfw_http_msg_setup((TfwHttpMsg *)resp, &it, data_len, 0))
return TFW_BLOCK;
tfw_http_prep_date(__TFW_STR_CH(&h_common_1, 1)->data);
ret = tfw_msg_write(&it, rh);
ret |= tfw_msg_write(&it, &h_common_1);
/*
* HTTP/1.0 may have no host part, so we create relative URI.
* See RFC 1945 9.3 and RFC 7231 7.1.2.
*/
if (host.len) {
ret |= tfw_msg_write(&it, proto);
ret |= tfw_msg_write(&it, &host);
}
if (rmark->len)
ret |= tfw_msg_write(&it, rmark);
ret |= tfw_msg_write(&it, &req->uri_path);
ret |= tfw_msg_write(&it, &h_common_2);
ret |= tfw_msg_write(&it, cookie);
ret |= tfw_msg_write(&it, cookie_crlf);
ret |= tfw_msg_write(&it, &c_len_crlf);
if (body)
ret |= tfw_msg_write(&it, body);
return ret;
}
#define S_304_PART_01 S_304 S_CRLF
#define S_304_KEEP S_F_CONNECTION S_V_CONN_KA S_CRLF
#define S_304_CLOSE S_F_CONNECTION S_V_CONN_CLOSE S_CRLF
/*
* Preparing 304 response (Not Modified) for HTTP/1.1-client.
*/
int
tfw_http_prep_304(TfwHttpReq *req, struct sk_buff **skb_head, TfwMsgIter *it)
{
int ret = 0;
static TfwStr rh = {
.data = S_304_PART_01, .len = SLEN(S_304_PART_01) };
static TfwStr crlf_keep = {
.data = S_304_KEEP, .len = SLEN(S_304_KEEP) };
static TfwStr crlf_close = {
.data = S_304_CLOSE, .len = SLEN(S_304_CLOSE) };
TfwStr *end = NULL;
/* Set "Connection:" header field if needed. */
if (test_bit(TFW_HTTP_B_CONN_CLOSE, req->flags))
end = &crlf_close;
else if (test_bit(TFW_HTTP_B_CONN_KA, req->flags))
end = &crlf_keep;
ret = tfw_http_msg_expand_data(it, skb_head, &rh, NULL);
if (unlikely(ret))
return ret;
if (end) {
ret = tfw_http_msg_expand_data(it, skb_head, end, NULL);
if (unlikely(ret))
return ret;
}
T_DBG("Send HTTP 304 response\n");
return 0;
}
/*
* Free an HTTP message.
* Also, free the connection instance if there's no more references.
*
* This function should be used anytime when there's a chance that
* a connection instance may belong to multiple messages, which is
* almost always. If a connection is suddenly closed then it still
* can be safely dereferenced and used in the code.
* In rare cases we're sure that a connection instance in a message
* doesn't have multiple users. For example, when an error response
* is prepared and sent by Tempesta, that HTTP message does not need
* a connection instance. The message is then immediately destroyed,
* and a simpler tfw_http_msg_free() can be used for that.
*
* NOTE: @hm->conn may be NULL if @hm is the response that was served
* from cache.
*/
void
tfw_http_conn_msg_free(TfwHttpMsg *hm)
{
if (unlikely(!hm))
return;
if (hm->conn) {
/*
* Check that the paired response has been destroyed before
* the request.
*/
WARN_ON_ONCE((TFW_CONN_TYPE(hm->conn) & Conn_Clnt) && hm->pair);
/*
* Unlink the connection while there is at least one
* reference. Use atomic exchange to avoid races with
* new messages arriving on the connection.
*
* NOTE: currently this unlink operation is not needed
* for HTTP/2 mode; it is left here as is since it does
* nothing in HTTP/2 mode, because default general stream
* @conn->stream is not used and @conn->stream.msg must
* be always NULL during HTTP/2 processing.
*/
__cmpxchg((unsigned long *)&hm->conn->stream.msg,
(unsigned long)hm, 0UL, sizeof(long));
tfw_connection_put(hm->conn);
}
tfw_http_msg_free(hm);
}
/*
* Free request after removing it from seq_queue (or after closing the
* corresponding stream - in case of HTTP/2 processing). This function
* is needed in cases when response is not sent to client for some reasons.
*/
static inline void
tfw_http_conn_req_clean(TfwHttpReq *req)
{
if (TFW_MSG_H2(req)) {
tfw_h2_stream_id_close(req, _HTTP2_UNDEFINED, 0);
} else {
spin_lock_bh(&((TfwCliConn *)req->conn)->seq_qlock);
if (likely(!list_empty(&req->msg.seq_list)))
list_del_init(&req->msg.seq_list);
spin_unlock_bh(&((TfwCliConn *)req->conn)->seq_qlock);
}
tfw_http_conn_msg_free((TfwHttpMsg *)req);
}
/*
* Free the request and the paired response.
*/
static inline void
tfw_http_resp_pair_free(TfwHttpReq *req)
{
tfw_http_conn_msg_free(req->pair);
tfw_http_conn_msg_free((TfwHttpMsg *)req);
}
/*
* Close the client connection and free unpaired request. This function
* is needed for cases when we cannot prepare response for this request.
* As soon as request is not linked with any response, sending to that
* client stops starting with that request, because that creates
* a "hole" in the chain of requests -- a request without a response.
* Subsequent responses cannot be sent to the client until that
* hole is closed, which at this point will never happen. To solve
* this situation there is no choice but to close client connection.
*
* Note: As a consequence of closing a client connection on error of
* preparing a response, it's possible that some already prepared
* responses will not be sent to the client. That depends on the
* order in which CPUs close the connection and call tfw_http_resp_fwd().
* This is the intended behaviour. The goal is to free some memory
* at the cost of dropping a few clients, so that Tempesta can
* continue working.
*/
void
tfw_http_resp_build_error(TfwHttpReq *req)
{
tfw_connection_close(req->conn, true);
tfw_http_conn_req_clean(req);
TFW_INC_STAT_BH(clnt.msgs_otherr);
}
static inline resp_code_t
tfw_http_enum_resp_code(int status)
{
switch(status) {
case 200:
return RESP_200;
case 400:
return RESP_400;
case 403:
return RESP_403;
case 404:
return RESP_404;
case 412:
return RESP_412;
case 500:
return RESP_500;
case 502:
return RESP_502;
case 503:
return RESP_503;
case 504:
return RESP_504;
default:
return RESP_NUM;
}
}
/**
* Write HTTP/2 ':status' pseudo-header. The ':status' is only defined
* pseudo-header for the response and all HTTP/2 responses must contain it.
* https://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2.4.
*/
int
tfw_h2_resp_status_write(TfwHttpResp *resp, unsigned short status,
TfwH2TransOp op, bool cache)
{
int ret;
unsigned short index = tfw_h2_pseudo_index(status);
char buf[H2_STAT_VAL_LEN];
TfwStr s_hdr = {
.chunks = (TfwStr []){
{ .data = S_H2_STAT, .len = SLEN(S_H2_STAT) },
{ .data = buf, .len = H2_STAT_VAL_LEN }
},
.len = SLEN(S_H2_STAT) + H2_STAT_VAL_LEN,
.nchunks = 2,
.hpack_idx = index ? index : 8
};
WARN_ON_ONCE(op != TFW_H2_TRANS_EXPAND && op != TFW_H2_TRANS_SUB);
/*
* If the status code is not in the static table, set the default
* static index just for the ':status' name.
*/
if (index) {
s_hdr.flags |= TFW_STR_FULL_INDEX;
}
if (!tfw_ultoa(status, __TFW_STR_CH(&s_hdr, 1)->data, H2_STAT_VAL_LEN))
return -E2BIG;
if ((ret = tfw_hpack_encode(resp, &s_hdr, op, !cache)))
return ret;
/* set status on response for access logging */
resp->status = status;
return 0;
}
void
tfw_h2_resp_fwd(TfwHttpResp *resp)
{
TfwHttpReq *req = resp->req;
TfwH2Ctx *ctx = tfw_h2_context(req->conn);
tfw_connection_get(req->conn);
do_access_log(resp);
if (tfw_cli_conn_send((TfwCliConn *)req->conn, (TfwMsg *)resp)) {
T_DBG("%s: cannot send data to client via HTTP/2\n", __func__);
TFW_INC_STAT_BH(serv.msgs_otherr);
tfw_connection_close(req->conn, true);
}
else {
TFW_INC_STAT_BH(serv.msgs_forwarded);
}
tfw_connection_put(req->conn);
tfw_hpack_enc_release(&ctx->hpack, resp->flags);
tfw_http_resp_pair_free(req);
}
static void
tfw_h2_send_resp(TfwHttpReq *req, int status, unsigned int stream_id)
{
TfwStr *msg;
resp_code_t code;
TfwHttpResp *resp;
struct sk_buff **skb_head;
TfwHttpTransIter *mit;
char *date_val, *data_ptr;
unsigned long nlen, vlen;
TfwStr *start, *date, *clen, *srv, *body;
TfwH2Ctx *ctx = tfw_h2_context(req->conn);
TfwStr hdr = {
.chunks = (TfwStr []){ {}, {} },
.nchunks = 2
};
if (!stream_id) {
stream_id = tfw_h2_stream_id_close(req, HTTP2_HEADERS,
HTTP2_F_END_STREAM);
if (unlikely(!stream_id)) {
tfw_http_conn_msg_free((TfwHttpMsg *)req);
return;
}
}
code = tfw_http_enum_resp_code(status);
if (code == RESP_NUM) {
T_WARN("Unexpected response error code: [%d]\n", status);
code = RESP_500;
}
msg = &http_predef_resps[code];
resp = tfw_http_msg_alloc_resp_light(req);
if (unlikely(!resp))
goto err;
mit = &resp->mit;
skb_head = &resp->msg.skb_head;
body = TFW_STR_BODY_CH(msg);
/* Set HTTP/2 ':status' pseudo-header. */
mit->start_off = FRAME_HEADER_SIZE;
if (tfw_h2_resp_status_write(resp, status, TFW_H2_TRANS_EXPAND, false))
goto err_setup;
/*
* Form and write HTTP/2 response headers excluding "\r\n", ':'
* separators and OWS.
*/
start = TFW_STR_START_CH(msg);
date = TFW_STR_DATE_CH(msg);
__TFW_STR_CH(&hdr, 0)->data = start->data + start->len - SLEN(S_F_DATE);
__TFW_STR_CH(&hdr, 0)->len = nlen = SLEN(S_F_DATE) - 2;
date_val = *this_cpu_ptr(&g_buf);
tfw_http_prep_date(date_val);
__TFW_STR_CH(&hdr, 1)->data = date_val;
__TFW_STR_CH(&hdr, 1)->len = vlen = date->len;
hdr.len = nlen + vlen;
hdr.hpack_idx = 33;
if (tfw_hpack_encode(resp, &hdr, TFW_H2_TRANS_EXPAND, true))
goto err_setup;
clen = TFW_STR_CLEN_CH(msg);
__TFW_STR_CH(&hdr, 0)->data = data_ptr = clen->data + SLEN(S_CRLF);
__TFW_STR_CH(&hdr, 0)->len = nlen = SLEN(S_F_CONTENT_LENGTH) - 2;
__TFW_STR_CH(&hdr, 1)->data = data_ptr = data_ptr + nlen + 2;
__TFW_STR_CH(&hdr, 1)->len = vlen = clen->data + clen->len
- data_ptr - SLEN(S_CRLF);
hdr.len = nlen + vlen;
hdr.hpack_idx = 28;
if (tfw_hpack_encode(resp, &hdr, TFW_H2_TRANS_EXPAND, true))
goto err_setup;
srv = TFW_STR_SRV_CH(msg);
__TFW_STR_CH(&hdr, 0)->data = data_ptr = srv->data;
__TFW_STR_CH(&hdr, 0)->len = nlen = SLEN(S_F_SERVER) - 2;
__TFW_STR_CH(&hdr, 1)->data = data_ptr = data_ptr + nlen + 2;
__TFW_STR_CH(&hdr, 1)->len = vlen = srv->data + srv->len
- data_ptr - SLEN(S_CRLF);
hdr.len = nlen + vlen;
hdr.hpack_idx = 54;
if (tfw_hpack_encode(resp, &hdr, TFW_H2_TRANS_EXPAND, true))
goto err_setup;
if (WARN_ON_ONCE(!mit->acc_len))
goto err_setup;
if (tfw_h2_frame_local_resp(resp, stream_id, mit->acc_len, body))
goto err_setup;
/* Send resulting HTTP/2 response and release HPACK encoder index. */
tfw_h2_resp_fwd(resp);
return;
err_setup:
T_DBG("%s: HTTP/2 response message transformation error: conn=[%p]\n",
__func__, req->conn);
tfw_hpack_enc_release(&ctx->hpack, resp->flags);
tfw_http_msg_free((TfwHttpMsg *)resp);
err:
tfw_http_resp_build_error(req);