1 /** @file
2 
3   An example plugin that demonstrates session hook usage.
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 
24 #include <stdio.h>
25 #include "ts/ts.h"
26 #include "tscore/ink_defs.h"
27 
28 #define PLUGIN_NAME "session_hooks"
29 
30 static int transaction_count_stat;
31 static int session_count_stat;
32 
33 static void
txn_handler(TSHttpTxn txnp,TSCont contp)34 txn_handler(TSHttpTxn txnp, TSCont contp)
35 {
36   TSMgmtInt num_txns = 0;
37 
38   TSStatIntIncrement(transaction_count_stat, 1);
39   num_txns = TSStatIntGet(transaction_count_stat);
40   TSDebug(PLUGIN_NAME, "The number of transactions is %" PRId64, num_txns);
41 }
42 
43 static void
handle_session(TSHttpSsn ssnp,TSCont contp)44 handle_session(TSHttpSsn ssnp, TSCont contp)
45 {
46   TSMgmtInt num_ssn = 0;
47 
48   TSStatIntIncrement(session_count_stat, 1);
49   num_ssn = TSStatIntGet(session_count_stat);
50   TSDebug(PLUGIN_NAME, "The number of sessions is %" PRId64, num_ssn);
51   TSHttpSsnHookAdd(ssnp, TS_HTTP_TXN_START_HOOK, contp);
52 }
53 
54 static int
ssn_handler(TSCont contp,TSEvent event,void * edata)55 ssn_handler(TSCont contp, TSEvent event, void *edata)
56 {
57   TSHttpSsn ssnp;
58   TSHttpTxn txnp;
59 
60   switch (event) {
61   case TS_EVENT_HTTP_SSN_START:
62 
63     ssnp = (TSHttpSsn)edata;
64     handle_session(ssnp, contp);
65     TSHttpSsnReenable(ssnp, TS_EVENT_HTTP_CONTINUE);
66     return 0;
67 
68   case TS_EVENT_HTTP_TXN_START:
69     txnp = (TSHttpTxn)edata;
70     txn_handler(txnp, contp);
71     TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
72     return 0;
73 
74   default:
75     TSDebug(PLUGIN_NAME, "In the default case: event = %d", event);
76     break;
77   }
78   return 0;
79 }
80 
81 void
TSPluginInit(int argc,const char * argv[])82 TSPluginInit(int argc, const char *argv[])
83 {
84   TSCont contp;
85   TSPluginRegistrationInfo info;
86 
87   info.plugin_name   = PLUGIN_NAME;
88   info.vendor_name   = "Apache Software Foundation";
89   info.support_email = "dev@trafficserver.apache.org";
90 
91   if (TSPluginRegister(&info) != TS_SUCCESS) {
92     TSError("[%s] Plugin registration failed.\n", PLUGIN_NAME);
93 
94     goto error;
95   }
96 
97   transaction_count_stat = TSStatCreate("transaction.count", TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
98   session_count_stat     = TSStatCreate("session.count", TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
99 
100   contp = TSContCreate(ssn_handler, NULL);
101   TSHttpHookAdd(TS_HTTP_SSN_START_HOOK, contp);
102 
103 error:
104   TSError("[%s] Plugin not initialized", PLUGIN_NAME);
105 }
106