-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathresources.h
More file actions
155 lines (131 loc) · 4.74 KB
/
Copy pathresources.h
File metadata and controls
155 lines (131 loc) · 4.74 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
/*
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.
*/
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Implement the classes for the various types of hash keys we support.
//
#pragma once
#include <string>
#include <unordered_map>
#include "swoc/TextView.h"
#include "regex_helper.h"
#include "ts/ts.h"
#include "ts/remap.h"
#include "lulu.h"
#include "tsutil/Regex.h"
#if TS_HAS_CRIPTS
#include "cripts/Certs.hpp"
#include "cripts/Transaction.hpp"
#endif
enum ResourceIDs {
RSRC_NONE = 0,
RSRC_SERVER_RESPONSE_HEADERS = 1 << 0, // 1
RSRC_SERVER_REQUEST_HEADERS = 1 << 1, // 2
RSRC_CLIENT_REQUEST_HEADERS = 1 << 2, // 4
RSRC_CLIENT_RESPONSE_HEADERS = 1 << 3, // 8
RSRC_RESPONSE_STATUS = 1 << 4, // 16
#if TS_HAS_CRIPTS
RSRC_CLIENT_CONNECTION = 1 << 5, // 32
RSRC_SERVER_CONNECTION = 1 << 6, // 64
RSRC_SERVER_CERTIFICATE = 1 << 7, // 128
RSRC_MTLS_CERTIFICATE = 1 << 8, // 256
#endif
};
///////////////////////////////////////////////////////////////////////////////
// Resources holds the minimum resources required to process a request.
//
#if !TS_HAS_CRIPTS
struct TransactionState {
TSHttpTxn txnp = nullptr;
TSHttpSsn ssnp = nullptr;
};
#endif
class Resources
{
public:
explicit Resources(TSHttpTxn txnptr, TSCont contptr) : contp(contptr) { _initialize(txnptr, "InkAPI"); }
explicit Resources(TSHttpTxn txnptr, TSRemapRequestInfo *rri) : _rri(rri) { _initialize(txnptr, "RemapAPI"); }
~Resources() { destroy(); }
// noncopyable
Resources(const Resources &) = delete;
void operator=(const Resources &) = delete;
void gather(const ResourceIDs ids, TSHttpHookID hook);
bool
ready() const
{
return _ready;
}
int
match(const regexHelper &re, const std::string &s) const
{
// For last capture to work safely, this has to make a copy of the subject string
// so the matches results will point into that and avoid any lifetime issues with
// the passed in `s`
_extended_info.subject_storage = s;
return re.regexMatch(_extended_info.subject_storage, _extended_info.matches);
}
const RegexMatches &
matches() const
{
return _extended_info.matches;
}
// Get a query parameter value by name, with caching
swoc::TextView get_query_param(const std::string &name, const char *query_str, int query_len) const;
void
reset_query_cache() const
{
_extended_info.query_params.clear();
_extended_info.query_parsed = false;
}
TSCont contp = nullptr;
TSRemapRequestInfo *_rri = nullptr;
TSMBuffer bufp = nullptr;
TSMLoc hdr_loc = nullptr;
TSMBuffer client_bufp = nullptr;
TSMLoc client_hdr_loc = nullptr;
TSMBuffer server_bufp = nullptr;
TSMLoc server_hdr_loc = nullptr;
#if TS_HAS_CRIPTS
cripts::Transaction state; // This now holds txpn / ssnp
cripts::Client::Connection *client_conn = nullptr;
cripts::Server::Connection *server_conn = nullptr;
cripts::Certs::Client *mtls_cert = nullptr;
cripts::Certs::Server *server_cert = nullptr;
#else
TransactionState state; // Without cripts, txnp / ssnp goes here
#endif
TSHttpStatus resp_status = TS_HTTP_STATUS_NONE;
void *geo_handle = nullptr;
TSHttpHookID hook = TS_HTTP_LAST_HOOK;
struct LifetimeExtension {
std::string subject_storage;
RegexMatches matches;
std::unordered_map<swoc::TextView, swoc::TextView> query_params;
bool query_parsed = false;
};
bool changed_url = false;
mutable LifetimeExtension _extended_info;
private:
void
_initialize(TSHttpTxn txnptr, const char *api_type)
{
state.txnp = txnptr;
state.ssnp = TSHttpTxnSsnGet(txnptr); // This is cheap, even if not used
Dbg(dbg_ctl, "Calling CTOR for Resources (%s)", api_type);
}
void destroy();
bool _ready = false;
};