-
Notifications
You must be signed in to change notification settings - Fork 861
Expand file tree
/
Copy pathstatement.h
More file actions
285 lines (243 loc) · 7.33 KB
/
statement.h
File metadata and controls
285 lines (243 loc) · 7.33 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
/*
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.
*/
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Base class for all Conditions and Operations. We share the "linked" list, and the
// resource management / requirements.
//
#pragma once
#include <string>
#include <ctime>
#include <vector>
#include "ts/ts.h"
#include "resources.h"
#include "parser.h"
#include "lulu.h"
namespace header_rewrite_ns
{
constexpr int NUM_STATE_FLAGS = 16;
constexpr int NUM_STATE_INT8S = 4;
constexpr uint64_t STATE_INT8_MASKS[NUM_STATE_INT8S] = {
// These would change if the number of flag bits changes
0x0000000000FF0000ULL, // Bits 16-23
0x00000000FF000000ULL, // Bits 24-31
0x000000FF00000000ULL, // Bits 32-39
0x0000FF0000000000ULL, // Bits 40-47
};
constexpr uint64_t STATE_INT16_MASK = 0xFFFF000000000000ULL; // Bits 48-63
} // namespace header_rewrite_ns
// URL data (both client and server)
enum UrlQualifiers {
URL_QUAL_NONE,
URL_QUAL_HOST,
URL_QUAL_PORT,
URL_QUAL_PATH,
URL_QUAL_QUERY,
URL_QUAL_SCHEME,
URL_QUAL_URL,
};
enum NextHopQualifiers {
NEXT_HOP_NONE,
NEXT_HOP_HOST,
NEXT_HOP_PORT,
NEXT_HOP_STRATEGY,
};
// NOW data
enum NowQualifiers {
NOW_QUAL_EPOCH,
NOW_QUAL_YEAR,
NOW_QUAL_MONTH,
NOW_QUAL_DAY,
NOW_QUAL_HOUR,
NOW_QUAL_MINUTE,
NOW_QUAL_WEEKDAY,
NOW_QUAL_YEARDAY,
};
// GEO data
enum GeoQualifiers {
GEO_QUAL_COUNTRY,
GEO_QUAL_COUNTRY_ISO,
GEO_QUAL_ASN,
GEO_QUAL_ASN_NAME,
};
// ID data
enum IdQualifiers {
ID_QUAL_REQUEST,
ID_QUAL_PROCESS,
ID_QUAL_UNIQUE,
};
// IP
enum IpQualifiers {
IP_QUAL_CLIENT,
IP_QUAL_INBOUND,
// These two might not necessarily get populated, e.g. on a cache hit.
IP_QUAL_SERVER,
IP_QUAL_OUTBOUND,
};
enum NetworkSessionQualifiers {
NET_QUAL_LOCAL_ADDR, ///< Local address.
NET_QUAL_LOCAL_PORT, ///< Local port.
NET_QUAL_REMOTE_ADDR, ///< Remote address.
NET_QUAL_REMOTE_PORT, ///< Remote port.
NET_QUAL_TLS, ///< TLS protocol
NET_QUAL_H2, ///< 'h2' or not.
NET_QUAL_IPV4, ///< 'ipv4' or not.
NET_QUAL_IPV6, ///< 'ipv6' or not.
NET_QUAL_IP_FAMILY, ///< IP protocol family.
NET_QUAL_STACK, ///< Full protocol stack.
#if TS_HAS_CRIPTS
NET_QUAL_CERT_PEM, ///< The PEM encoded certificate
NET_QUAL_CERT_SIG, ///< The signature of the certificate
NET_QUAL_CERT_SUBJECT, ///< The subject of the certificate
NET_QUAL_CERT_ISSUER, ///< The issuer of the certificate
NET_QUAL_CERT_SERIAL, ///< The serial number of the certificate
NET_QUAL_CERT_NOT_BEFORE, ///< The not before date of the certificate
NET_QUAL_CERT_NOT_AFTER, ///< The not after date of the certificate
NET_QUAL_CERT_VERSION, ///< The version of the certificate
NET_QUAL_CERT_SAN_DNS, ///< The DNS names in the SAN
NET_QUAL_CERT_SAN_IP, ///< The IP addresses in the SAN
NET_QUAL_CERT_SAN_EMAIL, ///< The email addresses in the SAN
NET_QUAL_CERT_SAN_URI, ///< The URIs in the SAN
#endif
};
class Statement
{
public:
Statement() { Dbg(dbg_ctl, "Calling CTOR for Statement"); }
virtual ~Statement()
{
Dbg(dbg_ctl, "Calling DTOR for Statement");
delete _next;
}
// noncopyable
Statement(const Statement &) = delete;
void operator=(const Statement &) = delete;
// Validate that this statement is allowed on the given hook. Used during parsing only.
bool is_hook_valid(TSHttpHookID hook) const;
// Which hooks are this "statement" applicable for? Used during parsing only.
void
add_allowed_hook(const TSHttpHookID hook)
{
_allowed_hooks.push_back(hook);
}
// Linked list.
void append(Statement *stmt);
ResourceIDs get_resource_ids() const;
virtual void
initialize(Parser &)
{
TSReleaseAssert(_initialized == false);
initialize_hooks();
if (need_txn_slot()) {
_txn_slot = acquire_txn_slot();
}
if (need_ssn_slot()) {
_ssn_slot = acquire_ssn_slot();
}
if (need_txn_private_slot()) {
_txn_private_slot = acquire_txn_private_slot();
}
_initialized = true;
}
bool
initialized() const
{
return _initialized;
}
void
require_resources(const ResourceIDs ids)
{
_rsrc = static_cast<ResourceIDs>(_rsrc | ids);
}
static int acquire_txn_slot();
static int acquire_txn_private_slot();
static int acquire_ssn_slot();
static int acquire_state_slot(TSUserArgType type);
protected:
virtual void initialize_hooks();
UrlQualifiers parse_url_qualifier(const std::string &q) const;
NextHopQualifiers parse_next_hop_qualifier(const std::string &q) const;
TSHttpCntlType parse_http_cntl_qualifier(const std::string &q) const;
virtual bool
need_txn_slot() const
{
return false;
}
virtual bool
need_txn_private_slot() const
{
return false;
}
virtual bool
need_ssn_slot() const
{
return false;
}
// Scope-aware helpers for state variable access
uint64_t
_get_state_data(TSUserArgType scope, const Resources &res) const
{
if (scope == TS_USER_ARGS_SSN) {
return reinterpret_cast<uint64_t>(TSUserArgGet(res.state.ssnp, _ssn_slot));
}
return reinterpret_cast<uint64_t>(TSUserArgGet(res.state.txnp, _txn_slot));
}
void
_set_state_data(TSUserArgType scope, const Resources &res, uint64_t data) const
{
if (scope == TS_USER_ARGS_SSN) {
TSUserArgSet(res.state.ssnp, _ssn_slot, reinterpret_cast<void *>(data));
} else {
TSUserArgSet(res.state.txnp, _txn_slot, reinterpret_cast<void *>(data));
}
}
bool
_check_state_handle(TSUserArgType scope, const Resources &res) const
{
if (scope == TS_USER_ARGS_SSN) {
return res.state.ssnp != nullptr;
}
return res.state.txnp != nullptr;
}
static const char *
_scope_label(TSUserArgType scope)
{
return (scope == TS_USER_ARGS_SSN) ? "SESSION" : "STATE";
}
Statement *_next = nullptr; // Linked list
int _txn_slot = -1;
int _txn_private_slot = -1;
int _ssn_slot = -1;
private:
ResourceIDs _rsrc = RSRC_NONE;
std::vector<TSHttpHookID> _allowed_hooks;
bool _initialized = false;
};
union PrivateSlotData {
uint64_t raw;
struct {
uint64_t timezone : 1; // TIMEZONE_LOCAL, or TIMEZONE_GMT
uint64_t ip_source : 2; // IP_SRC_PEER, IP_SRC_PROXY, IP_SRC_FORWARDED, or IP_SRC_PLUGIN
uint64_t unused : 61;
};
};
enum { TIMEZONE_LOCAL, TIMEZONE_GMT };
enum {
IP_SRC_PEER, // Immediate connection
IP_SRC_PROXY, // PROXY protocl
IP_SRC_PLUGIN, // Plugin
// IP_SRC_FORWARDED, // Forwarded header field (TS core needs to support the header first. It can be done by a plugin as well.)
};