- Notifications
You must be signed in to change notification settings - Fork 826
/
Copy pathoperators.cc
1519 lines (1317 loc) · 47.7 KB
/
operators.cc
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
/*
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.
*/
//////////////////////////////////////////////////////////////////////////////////////////////
// operators.cc: implementation of the operator classes
//
//
#include<arpa/inet.h>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include"records/RecCore.h"
#include"ts/ts.h"
#include"swoc/swoc_file.h"
#include"operators.h"
#include"ts/apidefs.h"
namespace
{
constunsignedint LOCAL_IP_ADDRESS = 0x0100007f;
constunsignedint MAX_SIZE = 256;
constint LOCAL_PORT = 8080;
int
handleFetchEvents(TSCont cont, TSEvent event, void *edata)
{
TSHttpTxn http_txn = static_cast<TSHttpTxn>(TSContDataGet(cont));
switch (static_cast<int>(event)) {
case OperatorSetBodyFrom::TS_EVENT_FETCHSM_SUCCESS: {
TSHttpTxn fetchsm_txn = static_cast<TSHttpTxn>(edata);
int data_len;
constchar *data_start = TSFetchRespGet(fetchsm_txn, &data_len);
if (data_start && (data_len > 0)) {
constchar *data_end = data_start + data_len;
TSHttpParser parser = TSHttpParserCreate();
TSMBuffer hdr_buf = TSMBufferCreate();
TSMLoc hdr_loc = TSHttpHdrCreate(hdr_buf);
TSHttpHdrTypeSet(hdr_buf, hdr_loc, TS_HTTP_TYPE_RESPONSE);
if (TSHttpHdrParseResp(parser, hdr_buf, hdr_loc, &data_start, data_end) == TS_PARSE_DONE) {
TSHttpTxnErrorBodySet(http_txn, TSstrdup(data_start), (data_end - data_start), nullptr);
} else {
TSWarning("[%s] Unable to parse set-custom-body fetch response", __FUNCTION__);
}
TSHttpParserDestroy(parser);
TSHandleMLocRelease(hdr_buf, nullptr, hdr_loc);
TSMBufferDestroy(hdr_buf);
} else {
TSWarning("[%s] Successful set-custom-body fetch did not result in any content", __FUNCTION__);
}
TSHttpTxnReenable(http_txn, TS_EVENT_HTTP_ERROR);
} break;
case OperatorSetBodyFrom::TS_EVENT_FETCHSM_FAILURE: {
Dbg(pi_dbg_ctl, "OperatorSetBodyFrom: Error getting custom body");
TSHttpTxnReenable(http_txn, TS_EVENT_HTTP_CONTINUE);
} break;
case OperatorSetBodyFrom::TS_EVENT_FETCHSM_TIMEOUT: {
Dbg(pi_dbg_ctl, "OperatorSetBodyFrom: Timeout getting custom body");
TSHttpTxnReenable(http_txn, TS_EVENT_HTTP_CONTINUE);
} break;
case TS_EVENT_HTTP_TXN_CLOSE: {
TSContDestroy(cont);
TSHttpTxnReenable(http_txn, TS_EVENT_HTTP_CONTINUE);
} break;
default:
TSError("[%s] handleFetchEvents got unknown event: %d", PLUGIN_NAME, event);
break;
}
return0;
}
TSReturnCode
createRequestString(const std::string_view &value, char (&req_buf)[MAX_SIZE], int *req_buf_size)
{
constchar *start = value.data();
constchar *end = start + value.size();
TSMLoc url_loc;
TSMBuffer url_buf = TSMBufferCreate();
int host_len, url_len = 0;
if (TSUrlCreate(url_buf, &url_loc) == TS_SUCCESS && TSUrlParse(url_buf, url_loc, &start, end) == TS_PARSE_DONE) {
constchar *host = TSUrlHostGet(url_buf, url_loc, &host_len);
constchar *url = TSUrlStringGet(url_buf, url_loc, &url_len);
*req_buf_size = snprintf(req_buf, MAX_SIZE, "GET %.*s HTTP/1.1\r\nHost: %.*s\r\n\r\n", url_len, url, host_len, host);
TSMBufferDestroy(url_buf);
return TS_SUCCESS;
} else {
Dbg(pi_dbg_ctl, "Failed to parse url %s", start);
TSMBufferDestroy(url_buf);
return TS_ERROR;
}
}
} // namespace
// OperatorConfig
void
OperatorSetConfig::initialize(Parser &p)
{
Operator::initialize(p);
_config = p.get_arg();
if (TS_SUCCESS == TSHttpTxnConfigFind(_config.c_str(), _config.size(), &_key, &_type)) {
_value.set_value(p.get_value());
} else {
_key = TS_CONFIG_NULL;
TSError("[%s] no such records config: %s", PLUGIN_NAME, _config.c_str());
}
}
bool
OperatorSetConfig::exec(const Resources &res) const
{
if (TS_CONFIG_NULL != _key) {
switch (_type) {
case TS_RECORDDATATYPE_INT:
if (TS_SUCCESS == TSHttpTxnConfigIntSet(res.txnp, _key, _value.get_int_value())) {
Dbg(pi_dbg_ctl, "OperatorSetConfig::exec() invoked on %s=%d", _config.c_str(), _value.get_int_value());
} else {
Dbg(pi_dbg_ctl, "OperatorSetConfig::exec() invocation failed on %s=%d", _config.c_str(), _value.get_int_value());
}
break;
case TS_RECORDDATATYPE_FLOAT:
if (TS_SUCCESS == TSHttpTxnConfigFloatSet(res.txnp, _key, _value.get_float_value())) {
Dbg(pi_dbg_ctl, "OperatorSetConfig::exec() invoked on %s=%f", _config.c_str(), _value.get_float_value());
} else {
Dbg(pi_dbg_ctl, "OperatorSetConfig::exec() invocation failed on %s=%f", _config.c_str(), _value.get_float_value());
}
break;
case TS_RECORDDATATYPE_STRING:
if (TS_SUCCESS == TSHttpTxnConfigStringSet(res.txnp, _key, _value.get_value().c_str(), _value.size())) {
Dbg(pi_dbg_ctl, "OperatorSetConfig::exec() invoked on %s=%s", _config.c_str(), _value.get_value().c_str());
} else {
Dbg(pi_dbg_ctl, "OperatorSetConfig::exec() invocation failed on %s=%s", _config.c_str(), _value.get_value().c_str());
}
break;
default:
TSError("[%s] unknown data type, whut?", PLUGIN_NAME);
break;
}
}
returntrue;
}
// OperatorSetStatus
void
OperatorSetStatus::initialize(Parser &p)
{
Operator::initialize(p);
_status.set_value(p.get_arg());
if (nullptr == (_reason = TSHttpHdrReasonLookup(static_cast<TSHttpStatus>(_status.get_int_value())))) {
TSError("[%s] unknown status %d", PLUGIN_NAME, _status.get_int_value());
_reason_len = 0;
} else {
_reason_len = strlen(_reason);
}
require_resources(RSRC_SERVER_RESPONSE_HEADERS);
require_resources(RSRC_CLIENT_RESPONSE_HEADERS);
require_resources(RSRC_RESPONSE_STATUS);
}
void
OperatorSetStatus::initialize_hooks()
{
add_allowed_hook(TS_HTTP_READ_RESPONSE_HDR_HOOK);
add_allowed_hook(TS_HTTP_SEND_RESPONSE_HDR_HOOK);
add_allowed_hook(TS_HTTP_READ_REQUEST_HDR_HOOK);
add_allowed_hook(TS_HTTP_PRE_REMAP_HOOK);
add_allowed_hook(TS_REMAP_PSEUDO_HOOK);
}
bool
OperatorSetStatus::exec(const Resources &res) const
{
switch (get_hook()) {
case TS_HTTP_READ_RESPONSE_HDR_HOOK:
case TS_HTTP_SEND_RESPONSE_HDR_HOOK:
if (res.bufp && res.hdr_loc) {
TSHttpHdrStatusSet(res.bufp, res.hdr_loc, static_cast<TSHttpStatus>(_status.get_int_value()));
if (_reason && _reason_len > 0) {
TSHttpHdrReasonSet(res.bufp, res.hdr_loc, _reason, _reason_len);
}
}
break;
default:
TSHttpTxnStatusSet(res.txnp, static_cast<TSHttpStatus>(_status.get_int_value()));
break;
}
Dbg(pi_dbg_ctl, "OperatorSetStatus::exec() invoked with status=%d", _status.get_int_value());
returntrue;
}
// OperatorSetStatusReason
void
OperatorSetStatusReason::initialize(Parser &p)
{
Operator::initialize(p);
_reason.set_value(p.get_arg());
require_resources(RSRC_CLIENT_RESPONSE_HEADERS);
require_resources(RSRC_SERVER_RESPONSE_HEADERS);
}
void
OperatorSetStatusReason::initialize_hooks()
{
add_allowed_hook(TS_HTTP_READ_RESPONSE_HDR_HOOK);
add_allowed_hook(TS_HTTP_SEND_RESPONSE_HDR_HOOK);
}
bool
OperatorSetStatusReason::exec(const Resources &res) const
{
if (res.bufp && res.hdr_loc) {
std::string reason;
_reason.append_value(reason, res);
if (reason.size() > 0) {
Dbg(pi_dbg_ctl, "Setting Status Reason to %s", reason.c_str());
TSHttpHdrReasonSet(res.bufp, res.hdr_loc, reason.c_str(), reason.size());
}
}
returntrue;
}
// OperatorSetDestination
void
OperatorSetDestination::initialize(Parser &p)
{
Operator::initialize(p);
_url_qual = parse_url_qualifier(p.get_arg());
_value.set_value(p.get_value());
require_resources(RSRC_CLIENT_REQUEST_HEADERS);
require_resources(RSRC_SERVER_REQUEST_HEADERS);
}
bool
OperatorSetDestination::exec(const Resources &res) const
{
if (res._rri || (res.bufp && res.hdr_loc)) {
std::string value;
// Determine which TSMBuffer and TSMLoc to use
TSMBuffer bufp;
TSMLoc url_m_loc;
if (res._rri) {
bufp = res._rri->requestBufp;
url_m_loc = res._rri->requestUrl;
} else {
bufp = res.bufp;
if (TSHttpHdrUrlGet(res.bufp, res.hdr_loc, &url_m_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "TSHttpHdrUrlGet was unable to return the url m_loc");
returntrue;
}
}
// Never set an empty destination value (I don't think that ever makes sense?)
switch (_url_qual) {
case URL_QUAL_HOST:
_value.append_value(value, res);
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set destination HOST to an empty value, skipping");
} else {
const_cast<Resources &>(res).changed_url = true;
TSUrlHostSet(bufp, url_m_loc, value.c_str(), value.size());
Dbg(pi_dbg_ctl, "OperatorSetDestination::exec() invoked with HOST: %s", value.c_str());
}
break;
case URL_QUAL_PATH:
_value.append_value(value, res);
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set destination PATH to an empty value, skipping");
} else {
const_cast<Resources &>(res).changed_url = true;
TSUrlPathSet(bufp, url_m_loc, value.c_str(), value.size());
Dbg(pi_dbg_ctl, "OperatorSetDestination::exec() invoked with PATH: %s", value.c_str());
}
break;
case URL_QUAL_QUERY:
_value.append_value(value, res);
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set destination QUERY to an empty value, skipping");
} else {
// 1.6.4--Support for preserving QSA in case of set-destination
if (get_oper_modifiers() & OPER_QSA) {
int query_len = 0;
constchar *query = TSUrlHttpQueryGet(bufp, url_m_loc, &query_len);
Dbg(pi_dbg_ctl, "QSA mode, append original query string: %.*s", query_len, query);
// std::string connector = (value.find("?") == std::string::npos)? "?" : "&";
value.append("&");
value.append(query, query_len);
}
const_cast<Resources &>(res).changed_url = true;
TSUrlHttpQuerySet(bufp, url_m_loc, value.c_str(), value.size());
Dbg(pi_dbg_ctl, "OperatorSetDestination::exec() invoked with QUERY: %s", value.c_str());
}
break;
case URL_QUAL_PORT:
if (_value.get_int_value() <= 0 || _value.get_int_value() > 0xFFFF) {
Dbg(pi_dbg_ctl, "Would set destination PORT to an invalid range, skipping");
} else {
const_cast<Resources &>(res).changed_url = true;
TSUrlPortSet(bufp, url_m_loc, _value.get_int_value());
Dbg(pi_dbg_ctl, "OperatorSetDestination::exec() invoked with PORT: %d", _value.get_int_value());
}
break;
case URL_QUAL_URL:
_value.append_value(value, res);
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set destination URL to an empty value, skipping");
} else {
constchar *start = value.c_str();
constchar *end = start + value.size();
TSMLoc new_url_loc;
if (TSUrlCreate(bufp, &new_url_loc) == TS_SUCCESS && TSUrlParse(bufp, new_url_loc, &start, end) == TS_PARSE_DONE &&
TSHttpHdrUrlSet(bufp, res.hdr_loc, new_url_loc) == TS_SUCCESS) {
Dbg(pi_dbg_ctl, "Set destination URL to %s", value.c_str());
} else {
Dbg(pi_dbg_ctl, "Failed to set URL %s", value.c_str());
}
}
break;
case URL_QUAL_SCHEME:
_value.append_value(value, res);
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set destination SCHEME to an empty value, skipping");
} else {
TSUrlSchemeSet(bufp, url_m_loc, value.c_str(), value.length());
Dbg(pi_dbg_ctl, "OperatorSetDestination::exec() invoked with SCHEME: %s", value.c_str());
}
break;
default:
Dbg(pi_dbg_ctl, "Set destination %i has no handler", _url_qual);
break;
}
} else {
Dbg(pi_dbg_ctl, "OperatorSetDestination::exec() unable to continue due to missing bufp=%p or hdr_loc=%p, rri=%p!", res.bufp,
res.hdr_loc, res._rri);
}
returntrue;
}
#include<iostream>
// OperatorRMDestination
static std::vector<std::string_view>
_tokenize(swoc::TextView text, char delimiter)
{
std::vector<std::string_view> tokens;
while (text) {
tokens.push_back(text.take_prefix_at(delimiter));
}
return tokens;
}
void
OperatorRMDestination::initialize(Parser &p)
{
Operator::initialize(p);
_url_qual = parse_url_qualifier(p.get_arg());
_stop = p.get_value();
if (!_stop.empty()) {
if (get_oper_modifiers() & OPER_INV) {
_keep = true;
}
_stop_list = _tokenize(_stop, ',');
}
require_resources(RSRC_CLIENT_REQUEST_HEADERS);
require_resources(RSRC_SERVER_REQUEST_HEADERS);
}
bool
OperatorRMDestination::exec(const Resources &res) const
{
if (res._rri || (res.bufp && res.hdr_loc)) {
std::string value = "";
// Determine which TSMBuffer and TSMLoc to use
TSMBuffer bufp;
TSMLoc url_m_loc;
if (res._rri) {
bufp = res._rri->requestBufp;
url_m_loc = res._rri->requestUrl;
} else {
bufp = res.bufp;
if (TSHttpHdrUrlGet(res.bufp, res.hdr_loc, &url_m_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "TSHttpHdrUrlGet was unable to return the url m_loc");
returntrue;
}
}
// Never set an empty destination value (I don't think that ever makes sense?)
switch (_url_qual) {
case URL_QUAL_PATH:
const_cast<Resources &>(res).changed_url = true;
TSUrlPathSet(bufp, url_m_loc, value.c_str(), value.size());
Dbg(pi_dbg_ctl, "OperatorRMDestination::exec() deleting PATH");
break;
case URL_QUAL_QUERY:
if (_stop_list.size() > 0) {
int q_len = 0;
constchar *query = TSUrlHttpQueryGet(bufp, url_m_loc, &q_len);
if (q_len > 0) {
for (auto &q : _tokenize({query, static_cast<size_t>(q_len)}, '&')) {
auto eq_pos = q.find('=');
auto it = std::find(_stop_list.begin(), _stop_list.end(), (eq_pos != std::string_view::npos) ? q.substr(0, eq_pos) : q);
if (_keep == (it != _stop_list.end())) {
if (!value.empty()) {
value.append("&").append(q);
} else {
value = q;
}
}
}
}
Dbg(pi_dbg_ctl, "OperatorRMDestination::exec() rewrote QUERY to \"%s\"", value.c_str());
} else {
Dbg(pi_dbg_ctl, "OperatorRMDestination::exec() deleting QUERY");
}
const_cast<Resources &>(res).changed_url = true;
TSUrlHttpQuerySet(bufp, url_m_loc, value.c_str(), value.size());
break;
case URL_QUAL_PORT:
const_cast<Resources &>(res).changed_url = true;
TSUrlPortSet(bufp, url_m_loc, 0);
Dbg(pi_dbg_ctl, "OperatorRMDestination::exec() deleting PORT");
break;
default:
Dbg(pi_dbg_ctl, "RM Destination %i has no handler", _url_qual);
break;
}
} else {
Dbg(pi_dbg_ctl, "OperatorRMDestination::exec() unable to continue due to missing bufp=%p or hdr_loc=%p, rri=%p!", res.bufp,
res.hdr_loc, res._rri);
}
returntrue;
}
// OperatorSetRedirect
void
OperatorSetRedirect::initialize(Parser &p)
{
Operator::initialize(p);
_status.set_value(p.get_arg());
_location.set_value(p.get_value());
auto status = _status.get_int_value();
if (status < 300 || status > 399 || status == TS_HTTP_STATUS_NOT_MODIFIED) {
TSError("[%s] unsupported redirect status %d", PLUGIN_NAME, status);
}
require_resources(RSRC_SERVER_RESPONSE_HEADERS);
require_resources(RSRC_CLIENT_RESPONSE_HEADERS);
require_resources(RSRC_CLIENT_REQUEST_HEADERS);
require_resources(RSRC_RESPONSE_STATUS);
}
void
OperatorSetRedirect::initialize_hooks()
{
add_allowed_hook(TS_HTTP_READ_RESPONSE_HDR_HOOK);
add_allowed_hook(TS_HTTP_SEND_RESPONSE_HDR_HOOK);
add_allowed_hook(TS_REMAP_PSEUDO_HOOK);
}
void
EditRedirectResponse(TSHttpTxn txnp, const std::string &location, TSHttpStatus status, TSMBuffer bufp, TSMLoc hdr_loc)
{
// Set new location.
TSMLoc field_loc;
static std::string header("Location");
if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(bufp, hdr_loc, header.c_str(), header.size(), &field_loc)) {
if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(bufp, hdr_loc, field_loc, -1, location.c_str(), location.size())) {
Dbg(pi_dbg_ctl, " Adding header %s", header.c_str());
TSMimeHdrFieldAppend(bufp, hdr_loc, field_loc);
}
constchar *reason = TSHttpHdrReasonLookup(status);
size_t len = strlen(reason);
TSHttpHdrReasonSet(bufp, hdr_loc, reason, len);
TSHandleMLocRelease(bufp, hdr_loc, field_loc);
}
// Set the body.
static std::string msg = "<HTML>\n<HEAD>\n<TITLE>Document Has Moved</TITLE>\n</HEAD>\n"
"<BODY BGCOLOR=\"white\" FGCOLOR=\"black\">\n"
"<H1>Document Has Moved</H1>\n<HR>\n<FONT FACE=\"Helvetica,Arial\"><B>\n"
"Description: The document you requested has moved to a new location."
" The new location is \"" +
location + "\".\n</B></FONT>\n<HR>\n</BODY>\n";
TSHttpTxnErrorBodySet(txnp, TSstrdup(msg.c_str()), msg.length(), TSstrdup("text/html"));
}
bool
OperatorSetRedirect::exec(const Resources &res) const
{
if (res.bufp && res.hdr_loc && res.client_bufp && res.client_hdr_loc) {
std::string value;
_location.append_value(value, res);
bool remap = false;
if (nullptr != res._rri) {
remap = true;
Dbg(pi_dbg_ctl, "OperatorSetRedirect:exec() invoked from remap plugin");
} else {
Dbg(pi_dbg_ctl, "OperatorSetRedirect:exec() not invoked from remap plugin");
}
TSMBuffer bufp;
TSMLoc url_loc;
if (remap) {
// Handle when called from remap plugin.
bufp = res._rri->requestBufp;
url_loc = res._rri->requestUrl;
} else {
// Handle when not called from remap plugin.
bufp = res.client_bufp;
if (TS_SUCCESS != TSHttpHdrUrlGet(res.client_bufp, res.client_hdr_loc, &url_loc)) {
Dbg(pi_dbg_ctl, "Could not get client URL");
}
}
// Replace %{PATH} to original path
size_t pos_path = 0;
if ((pos_path = value.find("%{PATH}")) != std::string::npos) {
value.erase(pos_path, 7); // erase %{PATH} from the rewritten to url
int path_len = 0;
constchar *path = TSUrlPathGet(bufp, url_loc, &path_len);
if (path_len > 0) {
Dbg(pi_dbg_ctl, "Find %%{PATH} in redirect url, replace it with: %.*s", path_len, path);
value.insert(pos_path, path, path_len);
}
}
// Append the original query string
int query_len = 0;
constchar *query = TSUrlHttpQueryGet(bufp, url_loc, &query_len);
if ((get_oper_modifiers() & OPER_QSA) && (query_len > 0)) {
Dbg(pi_dbg_ctl, "QSA mode, append original query string: %.*s", query_len, query);
std::string connector = (value.find('?') == std::string::npos) ? "?" : "&";
value.append(connector);
value.append(query, query_len);
}
// Prepare the destination URL for the redirect.
constchar *start = value.c_str();
constchar *end = value.size() + start;
if (remap) {
// Set new location.
if (TS_PARSE_ERROR == TSUrlParse(bufp, url_loc, &start, end)) {
Dbg(pi_dbg_ctl, "Could not set Location field value to: %s", value.c_str());
}
// Set the new status.
TSHttpTxnStatusSet(res.txnp, static_cast<TSHttpStatus>(_status.get_int_value()));
const_cast<Resources &>(res).changed_url = true;
res._rri->redirect = 1;
} else {
Dbg(pi_dbg_ctl, "OperatorSetRedirect::exec() hook=%d", int(get_hook()));
// Set the new status code and reason.
TSHttpStatus status = static_cast<TSHttpStatus>(_status.get_int_value());
TSHttpHdrStatusSet(res.bufp, res.hdr_loc, status);
EditRedirectResponse(res.txnp, value, status, res.bufp, res.hdr_loc);
}
Dbg(pi_dbg_ctl, "OperatorSetRedirect::exec() invoked with destination=%s and status code=%d", value.c_str(),
_status.get_int_value());
}
returntrue;
}
// OperatorSetTimeoutOut
void
OperatorSetTimeoutOut::initialize(Parser &p)
{
Operator::initialize(p);
if (p.get_arg() == "active") {
_type = TO_OUT_ACTIVE;
} elseif (p.get_arg() == "inactive") {
_type = TO_OUT_INACTIVE;
} elseif (p.get_arg() == "connect") {
_type = TO_OUT_CONNECT;
} elseif (p.get_arg() == "dns") {
_type = TO_OUT_DNS;
} else {
_type = TO_OUT_UNDEFINED;
TSError("[%s] unsupported timeout qualifier: %s", PLUGIN_NAME, p.get_arg().c_str());
}
_timeout.set_value(p.get_value());
}
bool
OperatorSetTimeoutOut::exec(const Resources &res) const
{
switch (_type) {
case TO_OUT_ACTIVE:
Dbg(pi_dbg_ctl, "OperatorSetTimeoutOut::exec(active, %d)", _timeout.get_int_value());
TSHttpTxnActiveTimeoutSet(res.txnp, _timeout.get_int_value());
break;
case TO_OUT_INACTIVE:
Dbg(pi_dbg_ctl, "OperatorSetTimeoutOut::exec(inactive, %d)", _timeout.get_int_value());
TSHttpTxnNoActivityTimeoutSet(res.txnp, _timeout.get_int_value());
break;
case TO_OUT_CONNECT:
Dbg(pi_dbg_ctl, "OperatorSetTimeoutOut::exec(connect, %d)", _timeout.get_int_value());
TSHttpTxnConnectTimeoutSet(res.txnp, _timeout.get_int_value());
break;
case TO_OUT_DNS:
Dbg(pi_dbg_ctl, "OperatorSetTimeoutOut::exec(dns, %d)", _timeout.get_int_value());
TSHttpTxnDNSTimeoutSet(res.txnp, _timeout.get_int_value());
break;
default:
TSError("[%s] unsupported timeout", PLUGIN_NAME);
break;
}
returntrue;
}
// OperatorSkipRemap
// Deprecated: Remove for v10.0.0
void
OperatorSkipRemap::initialize(Parser &p)
{
Operator::initialize(p);
if (p.get_arg() == "1" || p.get_arg() == "true" || p.get_arg() == "TRUE") {
_skip_remap = true;
}
}
bool
OperatorSkipRemap::exec(const Resources &res) const
{
Dbg(pi_dbg_ctl, "OperatorSkipRemap::exec() skipping remap: %s", _skip_remap ? "True" : "False");
TSHttpTxnCntlSet(res.txnp, TS_HTTP_CNTL_SKIP_REMAPPING, _skip_remap);
returntrue;
}
// OperatorRMHeader
bool
OperatorRMHeader::exec(const Resources &res) const
{
TSMLoc field_loc, tmp;
if (res.bufp && res.hdr_loc) {
Dbg(pi_dbg_ctl, "OperatorRMHeader::exec() invoked on %s", _header.c_str());
field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, _header.c_str(), _header.size());
while (field_loc) {
Dbg(pi_dbg_ctl, " Deleting header %s", _header.c_str());
tmp = TSMimeHdrFieldNextDup(res.bufp, res.hdr_loc, field_loc);
TSMimeHdrFieldDestroy(res.bufp, res.hdr_loc, field_loc);
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
field_loc = tmp;
}
}
returntrue;
}
// OperatorAddHeader
void
OperatorAddHeader::initialize(Parser &p)
{
OperatorHeaders::initialize(p);
_value.set_value(p.get_value());
}
bool
OperatorAddHeader::exec(const Resources &res) const
{
std::string value;
_value.append_value(value, res);
// Never set an empty header (I don't think that ever makes sense?)
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set header %s to an empty value, skipping", _header.c_str());
returntrue;
}
if (res.bufp && res.hdr_loc) {
Dbg(pi_dbg_ctl, "OperatorAddHeader::exec() invoked on %s: %s", _header.c_str(), value.c_str());
TSMLoc field_loc;
if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(res.bufp, res.hdr_loc, _header.c_str(), _header.size(), &field_loc)) {
if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, value.c_str(), value.size())) {
Dbg(pi_dbg_ctl, " Adding header %s", _header.c_str());
TSMimeHdrFieldAppend(res.bufp, res.hdr_loc, field_loc);
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
}
}
returntrue;
}
// OperatorSetHeader
void
OperatorSetHeader::initialize(Parser &p)
{
OperatorHeaders::initialize(p);
_value.set_value(p.get_value());
}
bool
OperatorSetHeader::exec(const Resources &res) const
{
std::string value;
_value.append_value(value, res);
// Never set an empty header (I don't think that ever makes sense?)
if (value.empty()) {
Dbg(pi_dbg_ctl, "Would set header %s to an empty value, skipping", _header.c_str());
returntrue;
}
if (res.bufp && res.hdr_loc) {
TSMLoc field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, _header_wks ? _header_wks : _header.c_str(), _header.size());
Dbg(pi_dbg_ctl, "OperatorSetHeader::exec() invoked on %s: %s", _header.c_str(), value.c_str());
if (!field_loc) {
// No existing header, so create one
if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(res.bufp, res.hdr_loc, _header.c_str(), _header.size(), &field_loc)) {
if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, value.c_str(), value.size())) {
Dbg(pi_dbg_ctl, " Adding header %s", _header.c_str());
TSMimeHdrFieldAppend(res.bufp, res.hdr_loc, field_loc);
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
}
} else {
TSMLoc tmp = nullptr;
bool first = true;
while (field_loc) {
tmp = TSMimeHdrFieldNextDup(res.bufp, res.hdr_loc, field_loc);
if (first) {
first = false;
if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, value.c_str(), value.size())) {
Dbg(pi_dbg_ctl, " Overwriting header %s", _header.c_str());
}
} else {
TSMimeHdrFieldDestroy(res.bufp, res.hdr_loc, field_loc);
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
field_loc = tmp;
}
}
}
returntrue;
}
// OperatorSetBody
void
OperatorSetBody::initialize(Parser &p)
{
Operator::initialize(p);
// we want the arg since body only takes one value
_value.set_value(p.get_arg());
}
void
OperatorSetBody::initialize_hooks()
{
add_allowed_hook(TS_REMAP_PSEUDO_HOOK);
add_allowed_hook(TS_HTTP_SEND_RESPONSE_HDR_HOOK);
}
bool
OperatorSetBody::exec(const Resources &res) const
{
std::string value;
_value.append_value(value, res);
char *msg = TSstrdup(_value.get_value().c_str());
TSHttpTxnErrorBodySet(res.txnp, msg, _value.size(), nullptr);
returntrue;
}
// OperatorCounter
void
OperatorCounter::initialize(Parser &p)
{
Operator::initialize(p);
_counter_name = p.get_arg();
// Sanity
if (_counter_name.length() == 0) {
TSError("[%s] counter name is empty", PLUGIN_NAME);
return;
}
// Check if counter already created by another rule
if (TSStatFindName(_counter_name.c_str(), &_counter) == TS_ERROR) {
_counter = TSStatCreate(_counter_name.c_str(), TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
if (_counter == TS_ERROR) {
TSError("[%s] TSStatCreate() failed. Can't create counter: %s", PLUGIN_NAME, _counter_name.c_str());
return;
}
Dbg(pi_dbg_ctl, "OperatorCounter::initialize(%s) created counter with id: %d", _counter_name.c_str(), _counter);
} else {
Dbg(pi_dbg_ctl, "OperatorCounter::initialize(%s) reusing id: %d", _counter_name.c_str(), _counter);
}
}
bool
OperatorCounter::exec(const Resources & /* ATS_UNUSED res */) const
{
// Sanity
if (_counter == TS_ERROR) {
returntrue;
}
Dbg(pi_dbg_ctl, "OperatorCounter::exec() invoked on %s", _counter_name.c_str());
TSStatIntIncrement(_counter, 1);
returntrue;
}
// OperatorRMCookie
bool
OperatorRMCookie::exec(const Resources &res) const
{
if (res.bufp && res.hdr_loc) {
Dbg(pi_dbg_ctl, "OperatorRMCookie::exec() invoked on cookie %s", _cookie.c_str());
TSMLoc field_loc;
// Find Cookie
field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE);
if (nullptr == field_loc) {
Dbg(pi_dbg_ctl, "OperatorRMCookie::exec, no cookie");
returntrue;
}
int cookies_len = 0;
constchar *cookies = TSMimeHdrFieldValueStringGet(res.bufp, res.hdr_loc, field_loc, -1, &cookies_len);
std::string updated_cookie;
if (CookieHelper::cookieModifyHelper(cookies, cookies_len, updated_cookie, CookieHelper::COOKIE_OP_DEL, _cookie)) {
if (updated_cookie.empty()) {
if (TS_SUCCESS == TSMimeHdrFieldDestroy(res.bufp, res.hdr_loc, field_loc)) {
Dbg(pi_dbg_ctl, "OperatorRMCookie::exec, empty cookie deleted");
}
} elseif (TS_SUCCESS == TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, updated_cookie.c_str(),
updated_cookie.size())) {
Dbg(pi_dbg_ctl, "OperatorRMCookie::exec, updated_cookie = [%s]", updated_cookie.c_str());
}
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
}
returntrue;
}
// OperatorAddCookie
void
OperatorAddCookie::initialize(Parser &p)
{
OperatorCookies::initialize(p);
_value.set_value(p.get_value());
}
bool
OperatorAddCookie::exec(const Resources &res) const
{
std::string value;
_value.append_value(value, res);
if (res.bufp && res.hdr_loc) {
Dbg(pi_dbg_ctl, "OperatorAddCookie::exec() invoked on cookie %s", _cookie.c_str());
TSMLoc field_loc;
// Find Cookie
field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE);
if (nullptr == field_loc) {
Dbg(pi_dbg_ctl, "OperatorAddCookie::exec, no cookie");
if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(res.bufp, res.hdr_loc, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE, &field_loc)) {
value = _cookie + '=' + value;
if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, value.c_str(), value.size())) {
Dbg(pi_dbg_ctl, "Adding cookie %s", _cookie.c_str());
TSMimeHdrFieldAppend(res.bufp, res.hdr_loc, field_loc);
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
}
returntrue;
}
int cookies_len = 0;
constchar *cookies = TSMimeHdrFieldValueStringGet(res.bufp, res.hdr_loc, field_loc, -1, &cookies_len);
std::string updated_cookie;
if (CookieHelper::cookieModifyHelper(cookies, cookies_len, updated_cookie, CookieHelper::COOKIE_OP_ADD, _cookie, value) &&
TS_SUCCESS ==
TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, updated_cookie.c_str(), updated_cookie.size())) {
Dbg(pi_dbg_ctl, "OperatorAddCookie::exec, updated_cookie = [%s]", updated_cookie.c_str());
}
}
returntrue;
}
// OperatorSetCookie
void
OperatorSetCookie::initialize(Parser &p)
{
OperatorCookies::initialize(p);
_value.set_value(p.get_value());
}
bool
OperatorSetCookie::exec(const Resources &res) const
{
std::string value;
_value.append_value(value, res);
if (res.bufp && res.hdr_loc) {
Dbg(pi_dbg_ctl, "OperatorSetCookie::exec() invoked on cookie %s", _cookie.c_str());
TSMLoc field_loc;
// Find Cookie
field_loc = TSMimeHdrFieldFind(res.bufp, res.hdr_loc, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE);
if (nullptr == field_loc) {
Dbg(pi_dbg_ctl, "OperatorSetCookie::exec, no cookie");
if (TS_SUCCESS == TSMimeHdrFieldCreateNamed(res.bufp, res.hdr_loc, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE, &field_loc)) {
value = _cookie + "=" + value;
if (TS_SUCCESS == TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, value.c_str(), value.size())) {
Dbg(pi_dbg_ctl, "Adding cookie %s", _cookie.c_str());
TSMimeHdrFieldAppend(res.bufp, res.hdr_loc, field_loc);
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
}
returntrue;
}
int cookies_len = 0;
constchar *cookies = TSMimeHdrFieldValueStringGet(res.bufp, res.hdr_loc, field_loc, -1, &cookies_len);
std::string updated_cookie;
if (CookieHelper::cookieModifyHelper(cookies, cookies_len, updated_cookie, CookieHelper::COOKIE_OP_SET, _cookie, value) &&
TS_SUCCESS ==
TSMimeHdrFieldValueStringSet(res.bufp, res.hdr_loc, field_loc, -1, updated_cookie.c_str(), updated_cookie.size())) {
Dbg(pi_dbg_ctl, "OperatorSetCookie::exec, updated_cookie = [%s]", updated_cookie.c_str());
}
TSHandleMLocRelease(res.bufp, res.hdr_loc, field_loc);
}
returntrue;
}
bool
CookieHelper::cookieModifyHelper(constchar *cookies, constsize_t cookies_len, std::string &updated_cookies,
const CookieHelper::CookieOp cookie_op, const std::string &cookie_key,
const std::string &cookie_value)
{
if (0 == cookie_key.size()) {
Dbg(pi_dbg_ctl, "CookieHelper::cookieModifyHelper, empty cookie_key");
returnfalse;
}
for (size_t idx = 0; idx < cookies_len;) {
// advance any leading spaces
for (; idx < cookies_len && std::isspace(cookies[idx]); idx++) {
;