HTTP Transactions

The HTTP transaction functions enable you to set up plugin callbacks to HTTP transactions and obtain/modify information about particular HTTP transactions.

As described in the section on HTTP sessions, an HTTP transaction is an object defined for the lifetime of a single request from a client and the corresponding response from Traffic Server. The ``TSHttpTxn`` structure is the main handle given to a plugin for manipulating a transaction’s internal state. Additionally, an HTTP transaction has a reference back to the HTTP session that created it.

The sample code below illustrates how to register locally to a transaction and associate data to the transaction.

  1. /*
  2. * Simple plugin that illustrates:
  3. * - how to register locally to a transaction
  4. * - how to deal with data that's associated with a tranaction
  5. *
  6. * Note: for readability, error checking is omitted
  7. */
  8. #include <ts/ts.h>
  9. #define DBG_TAG "txn"
  10. /* Structure to be associated to txns */
  11. typedef struct {
  12. int i;
  13. float f;
  14. char *s;
  15. } TxnData;
  16. /* Allocate memory and init a TxnData structure */
  17. TxnData *
  18. txn_data_alloc()
  19. {
  20. TxnData *data;
  21. data = TSmalloc(sizeof(TxnData));
  22. data->i = 1;
  23. data->f = 0.5;
  24. data->s = "Constant String";
  25. return data;
  26. }
  27. /* Free up a TxnData structure */
  28. void
  29. txn_data_free(TxnData *data)
  30. {
  31. TSfree(data);
  32. }
  33. /* Handler for event READ_REQUEST and TXN_CLOSE */
  34. static int
  35. local_hook_handler (TSCont contp, TSEvent event, void *edata)
  36. {
  37. TSHttpTxn txnp = (TSHttpTxn) edata;
  38. TxnData *txn_data = TSContDataGet(contp);
  39. switch (event) {
  40. case TS_EVENT_HTTP_READ_REQUEST_HDR:
  41. /* Modify values of txn data */
  42. txn_data->i = 2;
  43. txn_data->f = 3.5;
  44. txn_data->s = "Constant String 2";
  45. break;
  46. case TS_EVENT_HTTP_TXN_CLOSE:
  47. /* Print txn data values */
  48. TSDebug(DBG_TAG, "Txn data i=%d f=%f s=%s", txn_data->i, txn_data->f, txn_data->s);
  49. /* Then destroy the txn cont and its data */
  50. txn_data_free(txn_data);
  51. TSContDestroy(contp);
  52. break;
  53. default:
  54. TSAssert(!"Unexpected event");
  55. break;
  56. }
  57. TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  58. return 1;
  59. }
  60. /* Handler for event TXN_START */
  61. static int
  62. global_hook_handler (TSCont contp, TSEvent event, void *edata)
  63. {
  64. TSHttpTxn txnp = (TSHttpTxn) edata;
  65. TSCont txn_contp;
  66. TxnData *txn_data;
  67. switch (event) {
  68. case TS_EVENT_HTTP_TXN_START:
  69. /* Create a new continuation for this txn and associate data to it */
  70. txn_contp = TSContCreate(local_hook_handler, TSMutexCreate());
  71. txn_data = txn_data_alloc();
  72. TSContDataSet(txn_contp, txn_data);
  73. /* Registers locally to hook READ_REQUEST and TXN_CLOSE */
  74. TSHttpTxnHookAdd(txnp, TS_HTTP_READ_REQUEST_HDR_HOOK, txn_contp);
  75. TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
  76. break;
  77. default:
  78. TSAssert(!"Unexpected event");
  79. break;
  80. }
  81. TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
  82. return 1;
  83. }
  84. void
  85. TSPluginInit (int argc, const char *argv[])
  86. {
  87. TSCont contp;
  88. /* Note that we do not need a mutex for this txn since it registers globally
  89. and doesn't have any data associated with it */
  90. contp = TSContCreate(global_hook_handler, NULL);
  91. /* Register gloabally */
  92. TSHttpHookAdd(TS_HTTP_TXN_START_HOOK, contp);
  93. }

See Adding Hooks for background on HTTP transactions and HTTP hooks, as well as Hooks and Transactions. See also the HTTP Transaction State Diagram for an illustration of the steps involved in a typical HTTP transaction.

The HTTP transaction functions are: