1 /**
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17  */
18 /**
19  * @file Plugin.h
20  *
21  * @brief Contains the base interface used in creating Global and Transaction plugins.
22  * \note This interface can never be implemented directly, it should be implemented
23  *   through extending GlobalPlugin, TransactionPlugin, or TransformationPlugin.
24  */
25 
26 #pragma once
27 
28 #include "tscpp/api/Request.h"
29 #include "tscpp/api/Transaction.h"
30 #include "tscpp/api/noncopyable.h"
31 
32 namespace atscppapi
33 {
34 /**
35  * @brief The base interface used when creating a Plugin.
36  *
37  * \note This interface can never be implemented directly, it should be implemented
38  *   through extending GlobalPlugin, TransactionPlugin, or TransformationPlugin.
39  *
40  * @see TransactionPlugin
41  * @see GlobalPlugin
42  * @see TransformationPlugin
43  */
44 class Plugin : noncopyable
45 {
46 public:
47   /**
48    * A enumeration of the available types of Hooks. These are used with GlobalPlugin::registerHook()
49    * and TransactionPlugin::registerHook().
50    */
51   enum HookType {
52     HOOK_READ_REQUEST_HEADERS_PRE_REMAP = 0, /**< This hook will be fired before remap has occurred. */
53     HOOK_READ_REQUEST_HEADERS_POST_REMAP,    /**< This hook will be fired directly after remap has occurred. */
54     HOOK_SEND_REQUEST_HEADERS,               /**< This hook will be fired right before request headers are sent to the origin */
55     HOOK_READ_RESPONSE_HEADERS, /**< This hook will be fired right after response headers have been read from the origin */
56     HOOK_SEND_RESPONSE_HEADERS, /**< This hook will be fired right before the response headers are sent to the client */
57     HOOK_OS_DNS,                /**< This hook will be fired right after the OS DNS lookup */
58     HOOK_READ_REQUEST_HEADERS,  /**< This hook will be fired after the request is read. */
59     HOOK_READ_CACHE_HEADERS,    /**< This hook will be fired after the CACHE hdrs. */
60     HOOK_CACHE_LOOKUP_COMPLETE, /**< This hook will be fired after cache lookup complete. */
61     HOOK_TXN_CLOSE, /**< This hook will be fired after send response headers, only for TransactionPlugins::registerHook()!. */
62     HOOK_SELECT_ALT /**< This hook will be fired after select alt. */
63   };
64 
65   /**
66    * This method must be implemented when you hook HOOK_READ_REQUEST_HEADERS_PRE_REMAP
67    */
68   virtual void
handleReadRequestHeadersPreRemap(Transaction & transaction)69   handleReadRequestHeadersPreRemap(Transaction &transaction)
70   {
71     transaction.resume();
72   };
73 
74   /**
75    * This method must be implemented when you hook HOOK_READ_REQUEST_HEADERS_POST_REMAP
76    */
77   virtual void
handleReadRequestHeadersPostRemap(Transaction & transaction)78   handleReadRequestHeadersPostRemap(Transaction &transaction)
79   {
80     transaction.resume();
81   };
82 
83   /**
84    * This method must be implemented when you hook HOOK_SEND_REQUEST_HEADERS
85    */
86   virtual void
handleSendRequestHeaders(Transaction & transaction)87   handleSendRequestHeaders(Transaction &transaction)
88   {
89     transaction.resume();
90   };
91 
92   /**
93    * This method must be implemented when you hook HOOK_READ_RESPONSE_HEADERS
94    */
95   virtual void
handleReadResponseHeaders(Transaction & transaction)96   handleReadResponseHeaders(Transaction &transaction)
97   {
98     transaction.resume();
99   };
100 
101   /**
102    * This method must be implemented when you hook HOOK_SEND_RESPONSE_HEADERS
103    */
104   virtual void
handleSendResponseHeaders(Transaction & transaction)105   handleSendResponseHeaders(Transaction &transaction)
106   {
107     transaction.resume();
108   };
109 
110   /**
111    * This method must be implemented when you hook HOOK_OS_DNS
112    */
113   virtual void
handleOsDns(Transaction & transaction)114   handleOsDns(Transaction &transaction)
115   {
116     transaction.resume();
117   };
118 
119   /**
120    * This method must be implemented when you hook HOOK_READ_REQUEST_HEADERS
121    */
122   virtual void
handleReadRequestHeaders(Transaction & transaction)123   handleReadRequestHeaders(Transaction &transaction)
124   {
125     transaction.resume();
126   };
127 
128   /**
129    * This method must be implemented when you hook HOOK_READ_CACHE_HEADERS
130    */
131   virtual void
handleReadCacheHeaders(Transaction & transaction)132   handleReadCacheHeaders(Transaction &transaction)
133   {
134     transaction.resume();
135   };
136 
137   /**
138    * This method must be implemented when you hook HOOK_CACHE_LOOKUP_COMPLETE
139    */
140   virtual void
handleReadCacheLookupComplete(Transaction & transaction)141   handleReadCacheLookupComplete(Transaction &transaction)
142   {
143     transaction.resume();
144   };
145 
146   /**
147    * This method must be implemented when you hook HOOK_TXN_CLOSE
148    */
149   virtual void
handleTxnClose(Transaction & transaction)150   handleTxnClose(Transaction &transaction)
151   {
152     transaction.resume();
153   };
154 
155   /**
156    * This method must be implemented when you hook HOOK_SELECT_ALT
157    */
handleSelectAlt(const Request & clientReq,const Request & cachedReq,const Response & cachedResp)158   virtual void handleSelectAlt(const Request &clientReq, const Request &cachedReq, const Response &cachedResp){};
159 
~Plugin()160   virtual ~Plugin(){};
161 
162 protected:
163   /**
164    * \note This interface can never be implemented directly, it should be implemented
165    *   through extending GlobalPlugin, TransactionPlugin, or TransformationPlugin.
166    *
167    * @private
168    */
Plugin()169   Plugin(){};
170 };
171 
172 /**< Human readable strings for each HookType, you can access them as HOOK_TYPE_STRINGS[HOOK_OS_DNS] for example. */
173 extern const std::string HOOK_TYPE_STRINGS[];
174 
175 bool RegisterGlobalPlugin(const char *name, const char *vendor, const char *email);
176 inline bool
RegisterGlobalPlugin(std::string const & name,std::string const & vendor,std::string const & email)177 RegisterGlobalPlugin(std::string const &name, std::string const &vendor, std::string const &email)
178 {
179   return RegisterGlobalPlugin(name.c_str(), vendor.c_str(), email.c_str());
180 }
181 
182 } // namespace atscppapi
183