1 /** @file
2 
3   Test adding continuation from same hook point
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 #include <ts/ts.h>
24 
25 #define PLUGIN_TAG "test"
26 
27 int
transactionHandler(TSCont continuation,TSEvent event,void * d)28 transactionHandler(TSCont continuation, TSEvent event, void *d)
29 {
30   if (!(event == TS_EVENT_HTTP_PRE_REMAP || event == TS_EVENT_HTTP_TXN_CLOSE)) {
31     TSError("[" PLUGIN_TAG "] unexpected event on transactionHandler: %i\n", event);
32     return 0;
33   }
34 
35   TSHttpTxn transaction = static_cast<TSHttpTxn>(d);
36 
37   switch (event) {
38   case TS_EVENT_HTTP_PRE_REMAP: {
39     TSDebug(PLUGIN_TAG, " -- transactionHandler :: TS_EVENT_HTTP_PRE_REMAP");
40   } break;
41 
42   case TS_EVENT_HTTP_TXN_CLOSE:
43     TSDebug(PLUGIN_TAG, " -- transactionHandler :: TS_EVENT_HTTP_TXN_CLOSE");
44     TSContDataSet(continuation, nullptr);
45     TSContDestroy(continuation);
46     break;
47 
48   default:
49     break;
50   }
51 
52   TSHttpTxnReenable(transaction, TS_EVENT_HTTP_CONTINUE);
53 
54   return 0;
55 }
56 
57 int
sessionHandler(TSCont continuation,TSEvent event,void * d)58 sessionHandler(TSCont continuation, TSEvent event, void *d)
59 {
60   TSHttpTxn txnp = (TSHttpTxn)d;
61   TSCont txn_contp;
62 
63   switch (event) {
64   case TS_EVENT_HTTP_PRE_REMAP: {
65     TSDebug(PLUGIN_TAG, " -- sessionHandler :: TS_EVENT_HTTP_PRE_REMAP");
66     txn_contp = TSContCreate(transactionHandler, nullptr);
67 
68     /* Registers locally to hook PRE_REMAP_HOOK and TXN_CLOSE */
69     TSHttpTxnHookAdd(txnp, TS_HTTP_PRE_REMAP_HOOK, txn_contp);
70     TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
71   } break;
72 
73   case TS_EVENT_HTTP_SSN_CLOSE: {
74     TSDebug(PLUGIN_TAG, " -- sessionHandler :: TS_EVENT_HTTP_SSN_CLOSE");
75     const TSHttpSsn session = static_cast<TSHttpSsn>(d);
76 
77     TSHttpSsnReenable(session, TS_EVENT_HTTP_CONTINUE);
78     TSContDestroy(continuation);
79     return 0;
80   } break;
81 
82   default:
83     TSAssert(!"Unexpected event");
84     break;
85   }
86 
87   TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
88   return 0;
89 }
90 
91 int
globalHandler(TSCont continuation,TSEvent event,void * data)92 globalHandler(TSCont continuation, TSEvent event, void *data)
93 {
94   TSHttpSsn session = static_cast<TSHttpSsn>(data);
95 
96   switch (event) {
97   case TS_EVENT_HTTP_SSN_START: {
98     TSDebug(PLUGIN_TAG, " -- globalHandler :: TS_EVENT_HTTP_SSN_START");
99     TSCont cont = TSContCreate(sessionHandler, TSMutexCreate());
100 
101     TSHttpSsnHookAdd(session, TS_HTTP_PRE_REMAP_HOOK, cont);
102     TSHttpSsnHookAdd(session, TS_HTTP_SSN_CLOSE_HOOK, cont);
103 
104     TSDebug(PLUGIN_TAG, "New session, cont is %p", cont);
105   } break;
106 
107   default:
108     return 0;
109   }
110 
111   TSHttpSsnReenable(session, TS_EVENT_HTTP_CONTINUE);
112 
113   return 0;
114 }
115 
116 void
TSPluginInit(int argc,const char ** argv)117 TSPluginInit(int argc, const char **argv)
118 {
119   TSPluginRegistrationInfo info;
120 
121   info.plugin_name   = const_cast<char *>(PLUGIN_TAG);
122   info.support_email = const_cast<char *>("shinrich@verizonmedia.com");
123   info.vendor_name   = const_cast<char *>("Verizon Media");
124 
125   TSReturnCode ret;
126 #if (TS_VERSION_MAJOR >= 7)
127   ret = TSPluginRegister(&info);
128 #else
129   ret = TSPluginRegister(TS_SDK_VERSION_3_0, &info);
130 #endif
131 
132   if (TS_ERROR == ret) {
133     TSError("[" PLUGIN_TAG "] plugin registration failed\n");
134     return;
135   }
136 
137   TSCont continuation = TSContCreate(globalHandler, nullptr);
138 
139   TSHttpHookAdd(TS_HTTP_SSN_START_HOOK, continuation);
140 }
141