Advertisement
Guest User

Untitled

a guest
Aug 9th, 2011
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <ts/ts.h>
  5. #include <stdint.h>
  6. #include <fstream>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include <cstring>
  10. #include <libconfig.h++>
  11. #include "ZookeeperRecord.h"
  12. #include "PluginCoreData.h"
  13. #include "EspressoException.hpp"
  14. #include "EspressoPlugin.h"
  15. #include "SingleResponseHandler.h"
  16. #include "ZookeeperRecord.h"
  17. #include "ZooKeeperClient.h"
  18. #include "Logger.h"
  19. #include <execinfo.h>
  20.  
  21.  
  22. static const int LOCAL_IP_ADDRESS = 0x0100007f;
  23. static const int LOCAL_PORT = 8080;
  24.  
  25. static const int FETCH_EVENT_SUCCESS = 10000;
  26. static const int FETCH_EVENT_FAILURE = 10001;
  27. static const int FETCH_EVENT_TIMEOUT = 10002;
  28.  
  29.  
  30. typedef struct pluginData{
  31. TSIOBuffer req_buffer;
  32. TSIOBufferReader req_reader;
  33. TSIOBuffer resp_buffer;
  34. TSIOBufferReader resp_reader;
  35. TSVConn vconn;
  36. } pluginData;
  37.  
  38.  
  39.  
  40. static int
  41. per_transaction_local_thread(TSCont contp, TSEvent event, void *edata)
  42. {
  43.  
  44. switch(event)
  45. {
  46. case FETCH_EVENT_SUCCESS: {
  47. LOG_DEBUG("Fetch event SUCCESS! \n");
  48.  
  49. char data1[] = "HTTP/1.1 200 OK\r\nContent-Length:11\r\n\r\nhello world";
  50. pluginData *data = (pluginData *)TSContDataGet(contp);
  51. int amountWritten = TSIOBufferWrite(data->resp_buffer, data1,strlen(data1));
  52. LOG_DEBUG("The data to write is %s and is %d bytes long", data1, amountWritten);
  53. TSVConnWrite(data->vconn, contp,data->resp_reader, amountWritten );
  54.  
  55. }
  56. break;
  57.  
  58. case TS_EVENT_NET_ACCEPT:
  59. {
  60. LOG_DEBUG("Event is net_accept");
  61. pluginData *data = new pluginData;
  62. data->vconn = (TSVConn) edata;
  63. data->req_buffer = TSIOBufferCreate();
  64. data->req_reader = TSIOBufferReaderAlloc(data->req_buffer);
  65. data->resp_buffer = TSIOBufferCreate();
  66. data->resp_reader = TSIOBufferReaderAlloc(data->resp_buffer);
  67. TSContDataSet(contp, data);
  68. TSVConnRead(data->vconn, contp, data->req_buffer, INT64_MAX);
  69. }
  70. break;
  71. case TS_EVENT_VCONN_READ_READY:
  72. {
  73. LOG_DEBUG("Event is read_ready");
  74. pluginData *data = (pluginData *)TSContDataGet(contp);
  75.  
  76. char request[] = "GET / HTTP/1.0\r\nHost:localhost:8080\r\n\r\n";
  77. TSFetchEvent eventIds;
  78. eventIds.success_event_id = FETCH_EVENT_SUCCESS;
  79. eventIds.failure_event_id = FETCH_EVENT_FAILURE;
  80. eventIds.timeout_event_id = FETCH_EVENT_TIMEOUT;
  81. TSFetchUrl((const char *)request,strlen(request), LOCAL_IP_ADDRESS, LOCAL_PORT , contp, AFTER_BODY, eventIds);
  82. }
  83. break;
  84. case TS_EVENT_VCONN_WRITE_COMPLETE:
  85. {
  86. LOG_DEBUG("Event is write_complete");
  87.  
  88. pluginData *data = (pluginData *)TSContDataGet(contp);
  89. if (data->req_buffer) {
  90. TSIOBufferDestroy(data->req_buffer);
  91. data->req_buffer = NULL;
  92. }
  93. if (data->resp_buffer) {
  94. TSIOBufferDestroy(data->resp_buffer);
  95. data->req_buffer = NULL;
  96. }
  97. TSVConnShutdown(data->vconn,1,1);
  98. TSVConnClose(data->vconn);
  99. TSContDestroy(contp);
  100. delete data;
  101. }
  102. break;
  103. default:
  104. {
  105. LOG_DEBUG("unhandled event");
  106. }
  107. }
  108.  
  109. }
  110.  
  111.  
  112. /**
  113. * The plugin continuation which handles the handling of the hook
  114. * @param contp
  115. * The Espresso Plugin continuation
  116. * @param event
  117. * The event is associated with the continuation
  118. * @param edata
  119. * The data associated with continuation
  120. */
  121. static int
  122. espresso_plugin_boot(TSCont contp, TSEvent event, void *edata) {
  123.  
  124. TSDebug("espresso", "Espresso plugin started processing the request\n");
  125. TSVConn vconn;
  126. TSHttpTxn txnp;
  127.  
  128. switch (event) {
  129. /** The hook for read request */
  130. case TS_EVENT_HTTP_READ_REQUEST_HDR: {
  131. TSDebug("espresso", "Event is TS_HTTP_READ_REQUEST_HDR_HOOK");
  132.  
  133. // Extract the transaction from the edata
  134. txnp = (TSHttpTxn) edata;
  135.  
  136. /**
  137. * Check if it's an internal request - It it's internal request
  138. * skip the from processing it. Otherwise we end up on infinite recursion.
  139. */
  140. if ( TSHttpIsInternalRequest(txnp) == TS_SUCCESS) {
  141. TSDebug("espresso", "Internal request, skipping any further processing \n");
  142. //resume
  143. TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  144. return 0;
  145. }
  146.  
  147. //Important Note, this always has to spawn a new continuation as it's shared by multiple requests, *Global continuation*
  148. TSCont localCont = TSContCreate(per_transaction_local_thread, TSMutexCreate());
  149. TSHttpTxnServerIntercept (localCont, txnp);
  150. TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  151. LOG_DEBUG("Resuming the transaction");
  152. }
  153. return 0;
  154.  
  155. }
  156. }
  157.  
  158.  
  159. /**
  160. * Check the Traffic Server version check.
  161. */
  162. int
  163. check_ts_version() {
  164.  
  165. const char *ts_version = TSTrafficServerVersionGet();
  166. int result = 0;
  167.  
  168. if (ts_version) {
  169. int major_ts_version = 0;
  170. int minor_ts_version = 0;
  171. int patch_ts_version = 0;
  172.  
  173. if (sscanf(ts_version, "%d.%d.%d", &major_ts_version, &minor_ts_version, &patch_ts_version) != 3) {
  174. return 0;
  175. }
  176.  
  177. /** Need at least TS 2.0 */
  178. if (major_ts_version >= 2) {
  179. result = 1;
  180. }
  181. }
  182. return result;
  183. }
  184.  
  185. /**
  186. * The plugin intialization module
  187. *
  188. */
  189. void
  190. TSPluginInit(int argc, const char *argv[]) {
  191. TSPluginRegistrationInfo info;
  192.  
  193. info.plugin_name = (char *)"espresso";
  194.  
  195. //These are special debug messages, the first parameter on TSDebug should be specified in the config file
  196. //proxy.config.diags.debug.enabled should be set to 1 to print debug messages
  197. TSDebug("espresso", "\n plugin init");
  198.  
  199. if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) {
  200. TSError("[espresso] Plugin registration failed.\n");
  201. }
  202.  
  203. if (!check_ts_version()) {
  204. TSError("[espresso] Plugin requires Traffic Server 3.0 " "or later\n");
  205. }
  206.  
  207. TSCont espressoPlugin = TSContCreate(espresso_plugin_boot, TSMutexCreate());
  208. TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, espressoPlugin);
  209. TSDebug("espresso", "\n Added hook on TS_HTTP_READ_REQUEST_HDR");
  210.  
  211. return;
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement