-
Notifications
You must be signed in to change notification settings - Fork 870
Expand file tree
/
Copy pathconditions.cc
More file actions
1883 lines (1604 loc) · 49.8 KB
/
Copy pathconditions.cc
File metadata and controls
1883 lines (1604 loc) · 49.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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.
*/
//////////////////////////////////////////////////////////////////////////////////////////////
// conditions.cc: Implementation of the condition classes
//
//
#include <sys/time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <cctype>
#include <cinttypes>
#include <sstream>
#include <array>
#include <atomic>
#include "swoc/TextView.h"
#include "ts/ts.h"
#include "conditions.h"
#include "lulu.h"
static const sockaddr *getClientAddr(TSHttpTxn txnp, int txn_private_slot);
static const char *
get_hook_name(TSHttpHookID hook)
{
if (hook == TS_REMAP_PSEUDO_HOOK) {
return "REMAP_PSEUDO_HOOK";
}
if (hook == TS_HTTP_LAST_HOOK) {
return "UNKNOWN_HOOK";
}
if (const char *name = TSHttpHookNameLookup(hook); name != nullptr) {
return name;
}
return "UNKNOWN_HOOK";
}
static const char *
get_url_type_name(ConditionUrl::UrlType type)
{
switch (type) {
case ConditionUrl::CLIENT:
return "CLIENT-URL";
case ConditionUrl::SERVER:
return "SERVER-URL";
case ConditionUrl::FROM:
return "FROM-URL";
case ConditionUrl::TO:
return "TO-URL";
case ConditionUrl::URL:
default:
return "URL";
}
}
#if TS_HAS_CRIPTS
#include "cripts/Certs.hpp"
#endif
// ConditionStatus
void
ConditionStatus::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods(), [](const std::string &s) -> DataType {
auto status = Parser::parseNumeric<DataType>(s);
if (status > 999) {
throw std::runtime_error("Invalid status code: " + s);
}
return status;
});
_matcher = std::move(match);
require_resources(RSRC_SERVER_RESPONSE_HEADERS);
require_resources(RSRC_CLIENT_RESPONSE_HEADERS);
require_resources(RSRC_RESPONSE_STATUS);
}
void
ConditionStatus::initialize_hooks()
{
add_allowed_hook(TS_HTTP_READ_RESPONSE_HDR_HOOK);
add_allowed_hook(TS_HTTP_SEND_RESPONSE_HDR_HOOK);
}
bool
ConditionStatus::eval(const Resources &res)
{
Dbg(pi_dbg_ctl, "Evaluating STATUS()");
return static_cast<MatcherType *>(_matcher.get())->test(res.resp_status, res);
}
void
ConditionStatus::append_value(std::string &s, const Resources &res)
{
s += std::to_string(res.resp_status);
Dbg(pi_dbg_ctl, "Appending STATUS(%d) to evaluation value -> %s", res.resp_status, s.c_str());
}
// ConditionMethod
void
ConditionMethod::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
require_resources(RSRC_CLIENT_REQUEST_HEADERS);
}
bool
ConditionMethod::eval(const Resources &res)
{
std::string s;
append_value(s, res);
Dbg(pi_dbg_ctl, "Evaluating METHOD()");
return static_cast<const MatcherType *>(_matcher.get())->test(s, res);
}
void
ConditionMethod::append_value(std::string &s, const Resources &res)
{
TSMBuffer bufp;
TSMLoc hdr_loc;
int len;
bufp = res.client_bufp;
hdr_loc = res.client_hdr_loc;
if (bufp && hdr_loc) {
const char *value = TSHttpHdrMethodGet(bufp, hdr_loc, &len);
Dbg(pi_dbg_ctl, "Appending METHOD(%s) to evaluation value -> %.*s", _qualifier.c_str(), len, value);
s.append(value, len);
}
}
// ConditionRandom: random 0 to (N-1)
void
ConditionRandom::initialize(Parser &p)
{
struct timeval tv;
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
gettimeofday(&tv, nullptr);
_seed = getpid() * tv.tv_usec;
_max = strtol(_qualifier.c_str(), nullptr, 10);
match->set(p.get_arg(), mods(), [](const std::string &s) -> DataType { return Parser::parseNumeric<DataType>(s); });
_matcher = std::move(match);
}
bool
ConditionRandom::eval(const Resources &res)
{
Dbg(pi_dbg_ctl, "Evaluating RANDOM()");
return static_cast<const MatcherType *>(_matcher.get())->test(rand_r(&_seed) % _max, res);
}
void
ConditionRandom::append_value(std::string &s, const Resources & /* res ATS_UNUSED */)
{
s += std::to_string(rand_r(&_seed) % _max);
Dbg(pi_dbg_ctl, "Appending RANDOM(%d) to evaluation value -> %s", _max, s.c_str());
}
// ConditionAccess: access(file)
void
ConditionAccess::initialize(Parser &p)
{
struct timeval tv;
Condition::initialize(p);
gettimeofday(&tv, nullptr);
_next = tv.tv_sec + 2;
_last = !access(_qualifier.c_str(), R_OK);
}
void
ConditionAccess::append_value(std::string &s, const Resources &res)
{
if (eval(res)) {
s += "OK";
} else {
s += "NOT OK";
}
}
bool
ConditionAccess::eval(const Resources & /* res ATS_UNUSED */)
{
struct timeval tv;
gettimeofday(&tv, nullptr);
if (tv.tv_sec > _next) {
// There is a small "race" here, where we could end up calling access() a few times extra. I think
// that is OK, and not worth protecting with a lock.
bool check = !access(_qualifier.c_str(), R_OK);
tv.tv_sec += 2;
std::atomic_thread_fence(std::memory_order_seq_cst);
_next = tv.tv_sec; // I hope this is an atomic "set"...
_last = check; // This sure ought to be
}
Dbg(pi_dbg_ctl, "Evaluating ACCESS(%s) -> %d", _qualifier.c_str(), _last);
return _last;
}
// ConditionHeader: request or response header
void
ConditionHeader::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
require_resources(RSRC_CLIENT_REQUEST_HEADERS);
require_resources(RSRC_CLIENT_RESPONSE_HEADERS);
require_resources(RSRC_SERVER_REQUEST_HEADERS);
require_resources(RSRC_SERVER_RESPONSE_HEADERS);
}
void
ConditionHeader::append_value(std::string &s, const Resources &res)
{
TSMBuffer bufp;
TSMLoc hdr_loc;
int len;
switch (_type) {
case CLIENT:
bufp = res.client_bufp;
hdr_loc = res.client_hdr_loc;
break;
case SERVER:
bufp = res.server_bufp;
hdr_loc = res.server_hdr_loc;
break;
case HEADER:
default:
bufp = res.bufp;
hdr_loc = res.hdr_loc;
break;
}
if (bufp && hdr_loc) {
TSMLoc field_loc;
field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, _qualifier_wks ? _qualifier_wks : _qualifier.c_str(), _qualifier.size());
Dbg(pi_dbg_ctl, "Getting Header: %s, field_loc: %p", _qualifier.c_str(), field_loc);
while (field_loc) {
const char *value = TSMimeHdrFieldValueStringGet(bufp, hdr_loc, field_loc, -1, &len);
TSMLoc next_field_loc = TSMimeHdrFieldNextDup(bufp, hdr_loc, field_loc);
Dbg(pi_dbg_ctl, "Appending HEADER(%s) to evaluation value -> %.*s", _qualifier.c_str(), len, value);
s.append(value, len);
// multiple headers with the same name must be semantically the same as one value which is comma separated
if (next_field_loc) {
s += ',';
}
TSHandleMLocRelease(bufp, hdr_loc, field_loc);
field_loc = next_field_loc;
}
}
}
bool
ConditionHeader::eval(const Resources &res)
{
std::string s;
append_value(s, res);
Dbg(pi_dbg_ctl, "Evaluating HEADER()");
return static_cast<const MatcherType *>(_matcher.get())->test(s, res);
}
// ConditionUrl: request or response header. TODO: This is not finished, at all!!!
void
ConditionUrl::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
if (_type == SERVER) {
require_resources(RSRC_SERVER_REQUEST_HEADERS);
}
}
void
ConditionUrl::set_qualifier(const std::string &q)
{
Condition::set_qualifier(q);
Dbg(pi_dbg_ctl, "\tParsing %%{URL:%s}", q.c_str());
std::string::size_type pos = q.find(':');
if (pos != std::string::npos) {
std::string qual_part = q.substr(0, pos);
std::string sub_qual = q.substr(pos + 1);
_url_qual = parse_url_qualifier(qual_part);
if (_url_qual == URL_QUAL_QUERY) {
if (!sub_qual.empty()) {
_query_param = std::move(sub_qual);
Dbg(pi_dbg_ctl, "\tQuery parameter sub-key: %s", _query_param.c_str());
}
} else {
TSError("[%s] Sub-qualifier syntax (component:subkey) is only supported for QUERY component, got: %s", PLUGIN_NAME,
qual_part.c_str());
}
} else {
_url_qual = parse_url_qualifier(q);
}
}
void
ConditionUrl::log_error(const Resources &res, const char *message) const
{
if (has_config_location()) {
TSError("[%s] %s at hook=%s: %%{%s%s%s} in %s:%d", PLUGIN_NAME, message, get_hook_name(res.hook), get_url_type_name(_type),
_qualifier.empty() ? "" : ":", _qualifier.c_str(), get_config_filename().c_str(), get_config_lineno());
} else {
TSError("[%s] %s at hook=%s: %%{%s%s%s}", PLUGIN_NAME, message, get_hook_name(res.hook), get_url_type_name(_type),
_qualifier.empty() ? "" : ":", _qualifier.c_str());
}
}
void
ConditionUrl::append_value(std::string &s, const Resources &res)
{
TSMLoc url = nullptr;
TSMBuffer bufp = nullptr;
if (_type == CLIENT) {
// CLIENT always uses the pristine URL
Dbg(pi_dbg_ctl, " Using the pristine url");
if (TSHttpTxnPristineUrlGet(res.state.txnp, &bufp, &url) != TS_SUCCESS) {
log_error(res, "Error getting the pristine URL");
return;
}
} else if (_type == SERVER) {
Dbg(pi_dbg_ctl, " Using the server request url");
bufp = res.server_bufp;
if (bufp && res.server_hdr_loc) {
if (TSHttpHdrUrlGet(bufp, res.server_hdr_loc, &url) != TS_SUCCESS) {
log_error(res, "Error getting the server request URL");
return;
}
} else {
Dbg(pi_dbg_ctl, " Server request not available");
return;
}
} else if (res._rri != nullptr) {
// called at the remap hook
bufp = res._rri->requestBufp;
if (_type == URL) {
Dbg(pi_dbg_ctl, " Using the request url");
url = res._rri->requestUrl;
} else if (_type == FROM) {
Dbg(pi_dbg_ctl, " Using the from url");
url = res._rri->mapFromUrl;
} else if (_type == TO) {
Dbg(pi_dbg_ctl, " Using the to url");
url = res._rri->mapToUrl;
} else {
log_error(res, "Invalid URL option value");
return;
}
} else {
if (_type == URL) {
bufp = res.bufp;
TSMLoc hdr_loc = res.hdr_loc;
if (TSHttpHdrUrlGet(bufp, hdr_loc, &url) != TS_SUCCESS) {
log_error(res, "Error getting the URL");
return;
}
} else {
log_error(res, "Rule not supported");
return;
}
}
int i;
const char *q_str;
switch (_url_qual) {
case URL_QUAL_HOST:
q_str = TSUrlHostGet(bufp, url, &i);
s.append(q_str, i);
Dbg(pi_dbg_ctl, " Host to match is: %.*s", i, q_str);
break;
case URL_QUAL_PORT:
i = TSUrlPortGet(bufp, url);
s.append(std::to_string(i));
Dbg(pi_dbg_ctl, " Port to match is: %d", i);
break;
case URL_QUAL_PATH:
q_str = TSUrlPathGet(bufp, url, &i);
s.append(q_str, i);
Dbg(pi_dbg_ctl, " Path to match is: %.*s", i, q_str);
break;
case URL_QUAL_QUERY:
q_str = TSUrlHttpQueryGet(bufp, url, &i);
if (_query_param.empty()) {
s.append(q_str, i);
Dbg(pi_dbg_ctl, " Query parameters to match is: %.*s", i, q_str);
} else {
swoc::TextView value = res.get_query_param(_query_param, q_str, i);
if (value.data() != nullptr && value.size() > 0) {
s.append(value.data(), value.size());
Dbg(pi_dbg_ctl, " Query parameter %s value is: %.*s", _query_param.c_str(), static_cast<int>(value.size()), value.data());
} else {
Dbg(pi_dbg_ctl, " Query parameter %s is empty or not present", _query_param.c_str());
}
}
break;
case URL_QUAL_SCHEME:
q_str = TSUrlSchemeGet(bufp, url, &i);
s.append(q_str, i);
Dbg(pi_dbg_ctl, " Scheme to match is: %.*s", i, q_str);
break;
case URL_QUAL_URL:
case URL_QUAL_NONE: {
// TSUrlStringGet returns an allocated char * we must free
char *non_const_q_str = TSUrlStringGet(bufp, url, &i);
s.append(non_const_q_str, i);
Dbg(pi_dbg_ctl, " URL to match is: %.*s", i, non_const_q_str);
TSfree(non_const_q_str);
break;
}
}
}
bool
ConditionUrl::eval(const Resources &res)
{
std::string s;
append_value(s, res);
return static_cast<const Matchers<std::string> *>(_matcher.get())->test(s, res);
}
// ConditionDBM: do a lookup against a DBM
void
ConditionDBM::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
std::string::size_type pos = _qualifier.find_first_of(',');
if (pos != std::string::npos) {
_file = _qualifier.substr(0, pos);
//_dbm = mdbm_open(_file.c_str(), O_RDONLY, 0, 0, 0);
// if (NULL != _dbm) {
// Dbg(pi_dbg_ctl, "Opened DBM file %s", _file.c_str());
// _key.set_value(_qualifier.substr(pos + 1));
// } else {
// TSError("[%s] Failed to open DBM file: %s", PLUGIN_NAME, _file.c_str());
// }
} else {
TSError("[%s] Malformed DBM condition", PLUGIN_NAME);
}
}
void
ConditionDBM::append_value(std::string & /* s ATS_UNUSED */, const Resources & /* res ATS_UNUSED */)
{
// std::string key;
// if (!_dbm) {
// return;
// }
// _key.append_value(key, res);
// if (key.size() > 0) {
// datum k, v;
// Dbg(pi_dbg_ctl, "Looking up DBM(\"%s\")", key.c_str());
// k.dptr = const_cast<char*>(key.c_str());
// k.dsize = key.size();
// TSMutexLock(_mutex);
// //v = mdbm_fetch(_dbm, k);
// TSMutexUnlock(_mutex);
// if (v.dsize > 0) {
// Dbg(pi_dbg_ctl, "Appending DBM(%.*s) to evaluation value -> %.*s", k.dsize, k.dptr, v.dsize, v.dptr);
// s.append(v.dptr, v.dsize);
// }
// }
}
bool
ConditionDBM::eval(const Resources &res)
{
std::string s;
append_value(s, res);
Dbg(pi_dbg_ctl, "Evaluating DBM()");
return static_cast<const MatcherType *>(_matcher.get())->test(s, res);
}
// ConditionCookie: request or response header
void
ConditionCookie::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
require_resources(RSRC_CLIENT_REQUEST_HEADERS);
}
void
ConditionCookie::append_value(std::string &s, const Resources &res)
{
TSMBuffer bufp = res.client_bufp;
TSMLoc hdr_loc = res.client_hdr_loc;
TSMLoc field_loc;
int error;
int cookies_len;
int cookie_value_len;
const char *cookies;
const char *cookie_value;
const char *const cookie_name = _qualifier.c_str();
const int cookie_name_len = _qualifier.length();
// Sanity
if (bufp == nullptr || hdr_loc == nullptr) {
return;
}
// Find Cookie
field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, TS_MIME_FIELD_COOKIE, TS_MIME_LEN_COOKIE);
if (field_loc == nullptr) {
return;
}
// Get all cookies
cookies = TSMimeHdrFieldValueStringGet(bufp, hdr_loc, field_loc, -1, &cookies_len);
if (cookies == nullptr || cookies_len <= 0) {
goto out_release_field;
}
// Find particular cookie's value
error = get_cookie_value(cookies, cookies_len, cookie_name, cookie_name_len, &cookie_value, &cookie_value_len);
if (error == TS_ERROR) {
goto out_release_field;
}
Dbg(pi_dbg_ctl, "Appending COOKIE(%s) to evaluation value -> %.*s", cookie_name, cookie_value_len, cookie_value);
s.append(cookie_value, cookie_value_len);
// Unwind
out_release_field:
TSHandleMLocRelease(bufp, hdr_loc, field_loc);
}
bool
ConditionCookie::eval(const Resources &res)
{
std::string s;
append_value(s, res);
Dbg(pi_dbg_ctl, "Evaluating COOKIE()");
return static_cast<const MatcherType *>(_matcher.get())->test(s, res);
}
// ConditionInternalTxn: Is the txn internal?
bool
ConditionInternalTxn::eval(const Resources &res)
{
bool ret = (0 != TSHttpTxnIsInternal(res.state.txnp));
Dbg(pi_dbg_ctl, "Evaluating INTERNAL-TRANSACTION() -> %d", ret);
return ret;
}
void
ConditionIp::initialize(Parser &p)
{
Condition::initialize(p);
if (_cond_op == MATCH_IP_RANGES) { // Special hack for IP ranges
auto match = std::make_unique<MatcherTypeIp>(_cond_op);
match->set(p.get_arg(), mods(), [](const std::string & /*s*/) { return static_cast<const sockaddr *>(nullptr); });
_matcher = std::move(match);
} else {
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
}
}
void
ConditionIp::set_qualifier(const std::string &q)
{
Condition::set_qualifier(q);
Dbg(pi_dbg_ctl, "\tParsing %%{IP:%s} qualifier", q.c_str());
if (q == "CLIENT") {
_ip_qual = IP_QUAL_CLIENT;
} else if (q == "INBOUND") {
_ip_qual = IP_QUAL_INBOUND;
} else if (q == "SERVER") {
_ip_qual = IP_QUAL_SERVER;
} else if (q == "OUTBOUND") {
_ip_qual = IP_QUAL_OUTBOUND;
} else {
TSError("[%s] Unknown IP() qualifier: %s", PLUGIN_NAME, q.c_str());
}
}
bool
ConditionIp::eval(const Resources &res)
{
if (_matcher->op() == MATCH_IP_RANGES) {
const sockaddr *addr = nullptr;
switch (_ip_qual) {
case IP_QUAL_CLIENT:
addr = getClientAddr(res.state.txnp, _txn_private_slot);
break;
case IP_QUAL_INBOUND:
addr = TSHttpTxnIncomingAddrGet(res.state.txnp);
break;
case IP_QUAL_SERVER:
addr = TSHttpTxnServerAddrGet(res.state.txnp);
break;
case IP_QUAL_OUTBOUND:
addr = TSHttpTxnOutgoingAddrGet(res.state.txnp);
break;
}
if (addr) {
return static_cast<const Matchers<const sockaddr *> *>(_matcher.get())->test(addr, res);
} else {
return false;
}
} else {
std::string s;
append_value(s, res);
bool rval = static_cast<const Matchers<std::string> *>(_matcher.get())->test(s, res);
Dbg(pi_dbg_ctl, "Evaluating IP(): %s - rval: %d", s.c_str(), rval);
return rval;
}
}
void
ConditionIp::append_value(std::string &s, const Resources &res)
{
bool ip_set = false;
char ip[INET6_ADDRSTRLEN];
switch (_ip_qual) {
case IP_QUAL_CLIENT:
ip_set = (nullptr != getIP(getClientAddr(res.state.txnp, _txn_private_slot), ip));
break;
case IP_QUAL_INBOUND:
ip_set = (nullptr != getIP(TSHttpTxnIncomingAddrGet(res.state.txnp), ip));
break;
case IP_QUAL_SERVER:
ip_set = (nullptr != getIP(TSHttpTxnServerAddrGet(res.state.txnp), ip));
break;
case IP_QUAL_OUTBOUND:
Dbg(pi_dbg_ctl, "Requesting output ip");
ip_set = (nullptr != getIP(TSHttpTxnOutgoingAddrGet(res.state.txnp), ip));
break;
}
if (ip_set) {
s += ip;
}
}
// ConditionTransactCount
void
ConditionTransactCount::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods(), [](const std::string &s) -> DataType { return Parser::parseNumeric<DataType>(s); });
_matcher = std::move(match);
}
bool
ConditionTransactCount::eval(const Resources &res)
{
if (res.state.ssnp) {
int n = TSHttpSsnTransactionCount(res.state.ssnp);
Dbg(pi_dbg_ctl, "Evaluating TXN-COUNT()");
return static_cast<MatcherType *>(_matcher.get())->test(n, res);
}
Dbg(pi_dbg_ctl, "\tNo session found, returning false");
return false;
}
void
ConditionTransactCount::append_value(std::string &s, Resources const &res)
{
if (res.state.ssnp) {
char value[32]; // enough for UINT64_MAX
int count = TSHttpSsnTransactionCount(res.state.ssnp);
int length = ink_fast_itoa(count, value, sizeof(value));
if (length > 0) {
Dbg(pi_dbg_ctl, "Appending TXN-COUNT %s to evaluation value %.*s", _qualifier.c_str(), length, value);
s.append(value, length);
}
}
}
// ConditionNow: time related conditions, such as time since epoch (default), hour, day etc.
// Time related functionality for statements. We return an int64_t here, to assure that
// gettimeofday() / Epoch does not lose bits.
int64_t
ConditionNow::get_now_qualified(NowQualifiers qual, const Resources &resources) const
{
time_t now;
// First short circuit for the Epoch qualifier, since it needs less data
time(&now);
if (NOW_QUAL_EPOCH == qual) {
return static_cast<int64_t>(now);
} else {
struct tm res;
PrivateSlotData private_data;
private_data.raw = reinterpret_cast<uint64_t>(TSUserArgGet(resources.state.txnp, _txn_private_slot));
if (private_data.timezone == 1) {
gmtime_r(&now, &res);
} else {
localtime_r(&now, &res);
}
switch (qual) {
case NOW_QUAL_YEAR:
return static_cast<int64_t>(res.tm_year + 1900); // This makes more sense
break;
case NOW_QUAL_MONTH:
return static_cast<int64_t>(res.tm_mon);
break;
case NOW_QUAL_DAY:
return static_cast<int64_t>(res.tm_mday);
break;
case NOW_QUAL_HOUR:
return static_cast<int64_t>(res.tm_hour);
break;
case NOW_QUAL_MINUTE:
return static_cast<int64_t>(res.tm_min);
break;
case NOW_QUAL_WEEKDAY:
return static_cast<int64_t>(res.tm_wday);
break;
case NOW_QUAL_YEARDAY:
return static_cast<int64_t>(res.tm_yday);
break;
default:
TSReleaseAssert(!"All cases should have been handled");
break;
}
}
return 0;
}
void
ConditionNow::initialize(Parser &p)
{
Condition::initialize(p);
auto match = std::make_unique<MatcherType>(_cond_op);
match->set(p.get_arg(), mods(), [](const std::string &s) -> DataType { return Parser::parseNumeric<DataType>(s); });
_matcher = std::move(match);
}
void
ConditionNow::set_qualifier(const std::string &q)
{
Condition::set_qualifier(q);
Dbg(pi_dbg_ctl, "\tParsing %%{NOW:%s} qualifier", q.c_str());
if (q == "EPOCH") {
_now_qual = NOW_QUAL_EPOCH;
} else if (q == "YEAR") {
_now_qual = NOW_QUAL_YEAR;
} else if (q == "MONTH") {
_now_qual = NOW_QUAL_MONTH;
} else if (q == "DAY") {
_now_qual = NOW_QUAL_DAY;
} else if (q == "HOUR") {
_now_qual = NOW_QUAL_HOUR;
} else if (q == "MINUTE") {
_now_qual = NOW_QUAL_MINUTE;
} else if (q == "WEEKDAY") {
_now_qual = NOW_QUAL_WEEKDAY;
} else if (q == "YEARDAY") {
_now_qual = NOW_QUAL_YEARDAY;
} else {
TSError("[%s] Unknown NOW() qualifier: %s", PLUGIN_NAME, q.c_str());
}
}
void
ConditionNow::append_value(std::string &s, const Resources &res)
{
s += std::to_string(get_now_qualified(_now_qual, res));
Dbg(pi_dbg_ctl, "Appending NOW() to evaluation value -> %s", s.c_str());
}
bool
ConditionNow::eval(const Resources &res)
{
int64_t now = get_now_qualified(_now_qual, res);
Dbg(pi_dbg_ctl, "Evaluating NOW()");
return static_cast<const MatcherType *>(_matcher.get())->test(now, res);
}
std::string
ConditionGeo::get_geo_string(const sockaddr * /* addr ATS_UNUSED */, void * /* geo_handle ATS_UNUSED */) const
{
TSError("[%s] No Geo library available!", PLUGIN_NAME);
return "";
}
int64_t
ConditionGeo::get_geo_int(const sockaddr * /* addr ATS_UNUSED */, void * /* geo_handle ATS_UNUSED */) const
{
TSError("[%s] No Geo library available!", PLUGIN_NAME);
return 0;
}
void
ConditionGeo::initialize(Parser &p)
{
Condition::initialize(p);
if (is_int_type()) {
auto match = std::make_unique<Matchers<int64_t>>(_cond_op);
match->set(p.get_arg(), mods(), [](const std::string &s) -> int64_t { return Parser::parseNumeric<int64_t>(s); });
_matcher = std::move(match);
} else {
// The default is to have a string matcher
auto match = std::make_unique<Matchers<std::string>>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
}
}
void
ConditionGeo::set_qualifier(const std::string &q)
{
Condition::set_qualifier(q);
Dbg(pi_dbg_ctl, "\tParsing %%{GEO:%s} qualifier", q.c_str());
if (q == "COUNTRY") {
_geo_qual = GEO_QUAL_COUNTRY;
is_int_type(false);
} else if (q == "COUNTRY-ISO") {
_geo_qual = GEO_QUAL_COUNTRY_ISO;
is_int_type(true);
} else if (q == "ASN") {
_geo_qual = GEO_QUAL_ASN;
is_int_type(true);
} else if (q == "ASN-NAME") {
_geo_qual = GEO_QUAL_ASN_NAME;
is_int_type(false);
} else {
TSError("[%s] Unknown Geo() qualifier: %s", PLUGIN_NAME, q.c_str());
}
}
void
ConditionGeo::append_value(std::string &s, const Resources &res)
{
if (is_int_type()) {
s += std::to_string(get_geo_int(getClientAddr(res.state.txnp, _txn_private_slot), res.geo_handle));
} else {
s += get_geo_string(getClientAddr(res.state.txnp, _txn_private_slot), res.geo_handle);
}
Dbg(pi_dbg_ctl, "Appending GEO() to evaluation value -> %s", s.c_str());
}
bool
ConditionGeo::eval(const Resources &res)
{
bool ret = false;
Dbg(pi_dbg_ctl, "Evaluating GEO()");
if (is_int_type()) {
int64_t geo = get_geo_int(getClientAddr(res.state.txnp, _txn_private_slot), res.geo_handle);
ret = static_cast<const Matchers<int64_t> *>(_matcher.get())->test(geo, res);
} else {
std::string s;
append_value(s, res);
ret = static_cast<const Matchers<std::string> *>(_matcher.get())->test(s, res);
}
return ret;
}
// ConditionId: Some identifier strings, currently:
// PROCESS: The process UUID string
// REQUEST: The request (HttpSM::sm_id) counter
// UNIQUE: The combination of UUID-sm_id
void
ConditionId::initialize(Parser &p)
{
Condition::initialize(p);
if (_id_qual == ID_QUAL_REQUEST) {
auto match = std::make_unique<Matchers<uint64_t>>(_cond_op);
match->set(p.get_arg(), mods(), [](const std::string &s) -> uint64_t { return Parser::parseNumeric<uint64_t>(s); });
_matcher = std::move(match);
} else {
// The default is to have a string matcher
auto match = std::make_unique<Matchers<std::string>>(_cond_op);
match->set(p.get_arg(), mods());
_matcher = std::move(match);
}
}
void
ConditionId::set_qualifier(const std::string &q)
{
Condition::set_qualifier(q);
Dbg(pi_dbg_ctl, "\tParsing %%{ID:%s} qualifier", q.c_str());
if (q == "UNIQUE") {
_id_qual = ID_QUAL_UNIQUE;