File: test_hooks.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (346 lines) | stat: -rw-r--r-- 9,568 bytes parent folder | download | duplicates (2)
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
/*
 * 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.
 */

/*
Regression test code for TS API HTTP hooks.  The code assumes there will only be one active transaction at a time.  It
verifies the event data parameter to the continuations triggered by the hooks is correct.
*/

#include <fstream>
#include <cstdlib>

#include <ts/ts.h>

// TSReleaseAssert() doesn't seem to produce any logging output for a debug build, so do both kinds of assert.
//
#define ALWAYS_ASSERT(EXPR) \
  {                         \
    TSAssert(EXPR);         \
    TSReleaseAssert(EXPR);  \
  }

namespace
{
#define PINAME "test_hooks"
char PIName[] = PINAME;

// NOTE:  It's important to flush this after writing so that a gold test using this plugin can examine the log before TS
// terminates.
//
std::fstream logFile;

TSVConn activeVConn;

TSHttpSsn activeSsn;

TSHttpTxn activeTxn;

int
transactionContFunc(TSCont, TSEvent event, void *eventData)
{
  logFile << "Transaction: event=" << TSHttpEventNameLookup(event) << std::endl;

  TSDebug(PIName, "Transaction: event=%s(%d) eventData=%p", TSHttpEventNameLookup(event), event, eventData);

  switch (event) {
  case TS_EVENT_HTTP_TXN_CLOSE: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Transaction: ssn=%p", TSHttpTxnSsnGet(txn));

    // Don't assume any order of continuation execution on the same hook.
    ALWAYS_ASSERT((txn == activeTxn) or !activeTxn)

    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_READ_REQUEST_HDR: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Transaction: ssn=%p", TSHttpTxnSsnGet(txn));

    ALWAYS_ASSERT(txn == activeTxn)
    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  default: {
    ALWAYS_ASSERT(false)
  } break;

  } // end switch

  return 0;
}

TSCont tCont;

int
sessionContFunc(TSCont, TSEvent event, void *eventData)
{
  logFile << "Session: event=" << TSHttpEventNameLookup(event) << std::endl;

  TSDebug(PIName, "Session: event=%s(%d) eventData=%p", TSHttpEventNameLookup(event), event, eventData);

  switch (event) {
  case TS_EVENT_HTTP_SSN_CLOSE: {
    auto ssn = static_cast<TSHttpSsn>(eventData);

    // Don't assume any order of continuation execution on the same hook.
    ALWAYS_ASSERT((ssn == activeSsn) or !activeSsn)

    TSHttpSsnReenable(ssn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_TXN_START: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    // Don't assume any order of continuation execution on the same hook.
    ALWAYS_ASSERT((txn == activeTxn) or !activeTxn)

    TSDebug(PIName, "Session: ssn=%p", TSHttpTxnSsnGet(txn));

    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnHookAdd(txn, TS_HTTP_READ_REQUEST_HDR_HOOK, tCont);
    TSHttpTxnHookAdd(txn, TS_HTTP_TXN_CLOSE_HOOK, tCont);

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_TXN_CLOSE: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Session: ssn=%p", TSHttpTxnSsnGet(txn));

    // Don't assume any order of continuation execution on the same hook.
    ALWAYS_ASSERT((txn == activeTxn) or !activeTxn)

    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_READ_REQUEST_HDR: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Session: ssn=%p", TSHttpTxnSsnGet(txn));

    ALWAYS_ASSERT(txn == activeTxn)
    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  default: {
    ALWAYS_ASSERT(false)
  } break;

  } // end switch

  return 0;
}

TSCont sCont;

int
globalContFunc(TSCont, TSEvent event, void *eventData)
{
  logFile << "Global: event=" << TSHttpEventNameLookup(event) << std::endl;

  TSDebug(PIName, "Global: event=%s(%d) eventData=%p", TSHttpEventNameLookup(event), event, eventData);

  switch (event) {
  case TS_EVENT_VCONN_START: {
    ALWAYS_ASSERT(!activeVConn)

    auto vConn = static_cast<TSVConn>(eventData);

    activeVConn = vConn;

    logFile << "Global: ssl flag=" << TSVConnIsSsl(vConn) << std::endl;

    TSVConnReenable(vConn);
  } break;
  case TS_EVENT_SSL_CERT:
  case TS_EVENT_SSL_SERVERNAME: {
    auto vConn = static_cast<TSVConn>(eventData);

    ALWAYS_ASSERT(vConn == activeVConn)

    logFile << "Global: ssl flag=" << TSVConnIsSsl(vConn) << std::endl;

    TSVConnReenable(vConn);
  } break;

  case TS_EVENT_VCONN_CLOSE: {
    auto vConn = static_cast<TSVConn>(eventData);

    ALWAYS_ASSERT(vConn == activeVConn)

    logFile << "Global: ssl flag=" << TSVConnIsSsl(vConn) << std::endl;

    TSVConnReenable(vConn);

    activeVConn = nullptr;
  } break;

  case TS_EVENT_HTTP_SSN_START: {
    ALWAYS_ASSERT(!activeSsn)

    auto ssn = static_cast<TSHttpSsn>(eventData);

    activeSsn = ssn;

    TSHttpSsnHookAdd(ssn, TS_HTTP_READ_REQUEST_HDR_HOOK, sCont);
    TSHttpSsnHookAdd(ssn, TS_HTTP_SSN_CLOSE_HOOK, sCont);
    TSHttpSsnHookAdd(ssn, TS_HTTP_TXN_START_HOOK, sCont);
    TSHttpSsnHookAdd(ssn, TS_HTTP_TXN_CLOSE_HOOK, sCont);

    TSHttpSsnReenable(ssn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_SSN_CLOSE: {
    auto ssn = static_cast<TSHttpSsn>(eventData);

    ALWAYS_ASSERT(ssn == activeSsn)

    activeSsn = nullptr;

    TSHttpSsnReenable(ssn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_TXN_START: {
    ALWAYS_ASSERT(!activeTxn)

    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Global: ssn=%p", TSHttpTxnSsnGet(txn));

    activeTxn = txn;

    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_TXN_CLOSE: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Global: ssn=%p", TSHttpTxnSsnGet(txn));

    ALWAYS_ASSERT(txn == activeTxn)
    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    activeTxn = nullptr;

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  case TS_EVENT_HTTP_READ_REQUEST_HDR: {
    auto txn = static_cast<TSHttpTxn>(eventData);

    TSDebug(PIName, "Global: ssn=%p", TSHttpTxnSsnGet(txn));

    ALWAYS_ASSERT(txn == activeTxn)
    ALWAYS_ASSERT(TSHttpTxnSsnGet(txn) == activeSsn)

    TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE);
  } break;

  default: {
    ALWAYS_ASSERT(false)
  } break;

  } // end switch

  return 0;
}

TSCont gCont;

} // end anonymous namespace

void
TSPluginInit(int argc, const char *argv[])
{
  TSPluginRegistrationInfo info;

  info.plugin_name   = PIName;
  info.vendor_name   = "Apache Software Foundation";
  info.support_email = "dev@trafficserver.apache.org";

  if (TSPluginRegister(&info) != TS_SUCCESS) {
    TSError(PINAME ": Plugin registration failed");

    return;
  }

  const char *fileSpec = std::getenv("OUTPUT_FILE");

  if (nullptr == fileSpec) {
    TSError(PINAME ": Environment variable OUTPUT_FILE not found.");

    return;
  }

  // Disable output buffering for logFile, so that explicit flushing is not necessary.
  logFile.rdbuf()->pubsetbuf(nullptr, 0);

  logFile.open(fileSpec, std::ios::out);
  if (!logFile.is_open()) {
    TSError(PINAME ": could not open log file \"%s\"", fileSpec);

    return;
  }

  // Mutex to protect the logFile object.
  //
  TSMutex mtx = TSMutexCreate();

  gCont = TSContCreate(globalContFunc, mtx);

  // Setup the global hook
  TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, gCont);
  TSHttpHookAdd(TS_HTTP_SSN_START_HOOK, gCont);
  TSHttpHookAdd(TS_HTTP_SSN_CLOSE_HOOK, gCont);
  TSHttpHookAdd(TS_HTTP_TXN_START_HOOK, gCont);
  TSHttpHookAdd(TS_HTTP_TXN_CLOSE_HOOK, gCont);
  TSHttpHookAdd(TS_SSL_CERT_HOOK, gCont);
  TSHttpHookAdd(TS_SSL_SERVERNAME_HOOK, gCont);
  // NOTE: as of January 2019 these two hooks are only triggered for TLS connections.  It seems that, at trafficserver
  // startup, spurious data on the TLS TCP port may cause trafficserver to attempt (and fail) to create a TLS
  // connection.  If this happens, it will result in TS_VCONN_START_HOOK being triggered, and then TS_VCONN_CLOSE_HOOK
  // will be triggered when the connection closes due to failure.
  //
  TSHttpHookAdd(TS_VCONN_START_HOOK, gCont);
  TSHttpHookAdd(TS_VCONN_CLOSE_HOOK, gCont);

  // TSHttpHookAdd(TS_SSL_SESSION_HOOK, gCont); -- Event is TS_EVENT_SSL_SESSION_NEW -- Event data is TSHttpSsn
  // TSHttpHookAdd(TS_SSL_SERVER_VERIFY_HOOK, gCont);
  // TSHttpHookAdd(TS_SSL_VERIFY_CLIENT_HOOK, gCont);
  // TSHttpHookAdd(TS_VCONN_OUTBOUND_START_HOOK, gCont);
  // TSHttpHookAdd(TS_VCONN_OUTBOUND_CLOSE_HOOK, gCont);

  sCont = TSContCreate(sessionContFunc, mtx);

  tCont = TSContCreate(transactionContFunc, mtx);
}