-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathresources.cc
More file actions
233 lines (202 loc) · 7.45 KB
/
Copy pathresources.cc
File metadata and controls
233 lines (202 loc) · 7.45 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
/*
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.
*/
//////////////////////////////////////////////////////////////////////////////////////////////
// resources.cc: Implementation of the resources class.
//
//
#include "ts/ts.h"
#include "resources.h"
#include "lulu.h"
#if TS_HAS_CRIPTS
#include "cripts/Connections.hpp"
#endif
// Collect all resources
void
Resources::gather(const ResourceIDs ids, TSHttpHookID hook)
{
this->hook = hook;
Dbg(pi_dbg_ctl, "Building resources, hook=%s", TSHttpHookNameLookup(hook));
Dbg(pi_dbg_ctl, "Gathering resources for hook %s with IDs %d", TSHttpHookNameLookup(hook), ids);
// If we need the client request headers, make sure it's also available in the client vars.
if (ids & RSRC_CLIENT_REQUEST_HEADERS) {
Dbg(pi_dbg_ctl, "\tAdding TXN client request header buffers");
if (TSHttpTxnClientReqGet(state.txnp, &client_bufp, &client_hdr_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "could not gather bufp/hdr_loc for request");
return;
}
}
if (ids & RSRC_SERVER_REQUEST_HEADERS) {
Dbg(pi_dbg_ctl, "\tAdding TXN server request header buffers");
if (TSHttpTxnServerReqGet(state.txnp, &server_bufp, &server_hdr_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "could not gather bufp/hdr_loc for server request");
// Not a fatal error - server request may not be available in all hooks
}
}
switch (hook) {
case TS_HTTP_READ_RESPONSE_HDR_HOOK:
// Read response headers from server
if ((ids & RSRC_SERVER_RESPONSE_HEADERS) || (ids & RSRC_RESPONSE_STATUS)) {
Dbg(pi_dbg_ctl, "\tAdding TXN server response header buffers");
if (TSHttpTxnServerRespGet(state.txnp, &bufp, &hdr_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "could not gather bufp/hdr_loc for response");
return;
}
if (ids & RSRC_RESPONSE_STATUS) {
Dbg(pi_dbg_ctl, "\tAdding TXN server response status resource");
resp_status = TSHttpHdrStatusGet(bufp, hdr_loc);
}
}
break;
case TS_HTTP_SEND_REQUEST_HDR_HOOK:
Dbg(pi_dbg_ctl, "Processing TS_HTTP_SEND_REQUEST_HDR_HOOK");
if (ids & RSRC_SERVER_REQUEST_HEADERS) {
if (server_bufp && server_hdr_loc) {
bufp = server_bufp;
hdr_loc = server_hdr_loc;
} else {
Dbg(pi_dbg_ctl, "\tAdding TXN server request header buffers");
if (TSHttpTxnServerReqGet(state.txnp, &bufp, &hdr_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "could not gather bufp/hdr_loc for request");
return;
}
}
}
break;
case TS_HTTP_READ_REQUEST_HDR_HOOK:
case TS_HTTP_PRE_REMAP_HOOK:
// Read request from client
if (ids & RSRC_CLIENT_REQUEST_HEADERS) {
bufp = client_bufp;
hdr_loc = client_hdr_loc;
}
break;
case TS_HTTP_SEND_RESPONSE_HDR_HOOK:
// Send response headers to client
if (ids & RSRC_CLIENT_RESPONSE_HEADERS) {
Dbg(pi_dbg_ctl, "\tAdding TXN client response header buffers");
if (TSHttpTxnClientRespGet(state.txnp, &bufp, &hdr_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "could not gather bufp/hdr_loc for request");
return;
}
if (ids & RSRC_RESPONSE_STATUS) {
Dbg(pi_dbg_ctl, "\tAdding TXN client response status resource");
resp_status = TSHttpHdrStatusGet(bufp, hdr_loc);
}
}
break;
case TS_REMAP_PSEUDO_HOOK:
// Pseudo-hook for a remap instance
if (client_bufp && client_hdr_loc) {
Dbg(pi_dbg_ctl, "\tAdding TXN client request header buffers for remap instance");
bufp = client_bufp;
hdr_loc = client_hdr_loc;
}
break;
case TS_HTTP_TXN_START_HOOK:
// Get TCP Info at transaction start
if (client_bufp && client_hdr_loc) {
Dbg(pi_dbg_ctl, "\tAdding TXN client request header buffers for TXN Start instance");
bufp = client_bufp;
hdr_loc = client_hdr_loc;
}
break;
case TS_HTTP_TXN_CLOSE_HOOK:
// Get TCP Info at transaction close
Dbg(pi_dbg_ctl, "\tAdding TXN close buffers");
if (TSHttpTxnClientRespGet(state.txnp, &bufp, &hdr_loc) != TS_SUCCESS) {
Dbg(pi_dbg_ctl, "could not gather bufp/hdr_loc for request");
return;
}
break;
default:
break;
}
// The following is all the new infrastructure borrowed / reused from
// the Cripts library.
#if TS_HAS_CRIPTS
if (ids & (RSRC_CLIENT_CONNECTION | RSRC_MTLS_CERTIFICATE | RSRC_SERVER_CERTIFICATE)) {
Dbg(pi_dbg_ctl, "\tAdding Cripts Client::Connection");
client_conn = new cripts::Client::Connection();
client_conn->set_state(&state);
}
if (ids & RSRC_SERVER_CONNECTION) {
Dbg(pi_dbg_ctl, "\tAdding Cripts Server::Connection");
server_conn = new cripts::Server::Connection();
server_conn->set_state(&state);
}
if (ids & RSRC_MTLS_CERTIFICATE) {
Dbg(pi_dbg_ctl, "\tAdding Cripts Certs::Client");
mtls_cert = new cripts::Certs::Client(*client_conn);
}
if (ids & RSRC_SERVER_CERTIFICATE) {
Dbg(pi_dbg_ctl, "\tAdding Cripts Certs::Server");
server_cert = new cripts::Certs::Server(*client_conn);
}
#endif
_ready = true;
}
void
Resources::destroy()
{
if (bufp) {
if (hdr_loc) {
TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
}
}
if (client_bufp && (client_bufp != bufp)) {
if (client_hdr_loc && (client_hdr_loc != hdr_loc)) { // TODO: Is this check really necessary?
TSHandleMLocRelease(client_bufp, TS_NULL_MLOC, client_hdr_loc);
}
}
if (server_bufp && (server_bufp != bufp) && (server_bufp != client_bufp)) {
if (server_hdr_loc && (server_hdr_loc != hdr_loc) && (server_hdr_loc != client_hdr_loc)) {
TSHandleMLocRelease(server_bufp, TS_NULL_MLOC, server_hdr_loc);
}
}
#if TS_HAS_CRIPTS
delete client_conn;
delete server_conn;
delete mtls_cert;
delete server_cert;
#endif
_ready = false;
}
swoc::TextView
Resources::get_query_param(const std::string &name, const char *query_str, int query_len) const
{
// Note: Query parameter names and values are matched as-is without URL decoding.
// For example, searching for "my%20param" matches the literal string, not "my param".
if (!_extended_info.query_parsed) {
if (query_str && query_len > 0) {
swoc::TextView query_view(query_str, query_len);
while (!query_view.empty()) {
swoc::TextView param = query_view.take_prefix_at('&');
swoc::TextView param_name = param.take_prefix_at('=');
swoc::TextView param_value = param;
if (!param_name.empty()) {
// We only allow caching / using the first instance of a query param
_extended_info.query_params[param_name] = param_value;
}
}
}
_extended_info.query_parsed = true;
}
auto it = _extended_info.query_params.find(swoc::TextView(name));
if (it != _extended_info.query_params.end()) {
return it->second;
}
return swoc::TextView();
}