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 INKHttpTxn
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.
/* * Simple plugin that illustrates: * - how to register locally to a transaction * - how to deal with data associated with a txn * * Note: for readability, error checking is omitted */ #include "InkAPI.h" #define DBG_TAG "txn" /* Structure to be associated to txns */ typedef struct { int i; float f; char *s; } TxnData; /* Allocate memory and init a TxnData structure */ TxnData * txn_data_alloc() { TxnData *data; data = INKmalloc(sizeof(TxnData)); data->i = 1; data->f = 0.5; data->s = "Constant String"; return data; } /* Free up a TxnData structure */ void txn_data_free(TxnData *data) { INKfree(data); } /* handler for event READ_REQUEST and TXN_CLOSE */ static int local_hook_handler (INKCont contp, INKEvent event, void *edata) { INKHttpTxn txnp = (INKHttpTxn) edata; TxnData *txn_data = INKContDataGet(contp); switch (event) { case INK_EVENT_HTTP_READ_REQUEST_HDR: /* Modify values of txn data */ txn_data->i = 2; txn_data->f = 3.5; txn_data->s = "Constant String 2"; break; case INK_EVENT_HTTP_TXN_CLOSE: /* Print txn data values */ INKDebug(DBG_TAG, "Txn data i=%d f=%f s=%s", txn_data->i, txn_data->f, txn_data->s); /* Then destroy the txn cont and it's data */ txn_data_free(txn_data); INKContDestroy(contp); break; default: INKAssert(!"Unexpected event"); break; } INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE); return 1; } /* Handler for event TXN_START */ static int global_hook_handler (INKCont contp, INKEvent event, void *edata) { INKHttpTxn txnp = (INKHttpTxn) edata; INKCont txn_contp; TxnData *txn_data; switch (event) { case INK_EVENT_HTTP_TXN_START: /* Create a new continuation for this txn and associate data to it */ txn_contp = INKContCreate(local_hook_handler, INKMutexCreate()); txn_data = txn_data_alloc(); INKContDataSet(txn_contp, txn_data); /* Registers locally to hook READ_REQUEST and TXN_CLOSE */ INKHttpTxnHookAdd(txnp, INK_HTTP_READ_REQUEST_HDR_HOOK, txn_contp); INKHttpTxnHookAdd(txnp, INK_HTTP_TXN_CLOSE_HOOK, txn_contp); break; default: INKAssert(!"Unexpected event"); break; } INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE); return 1; } void INKPluginInit (int argc, const char *argv[]) { INKCont contp; /* Note that we do not need a mutex for this txn as it registers globally and doesn't have any data associated with it */ contp = INKContCreate(global_hook_handler, NULL); /* Register gloabally */ INKHttpHookAdd(INK_HTTP_TXN_START_HOOK, contp); }
See Adding Hooks for background about HTTP transactions and HTTP hooks, as well as HTTP Hooks and Transactions. Also see the HTTP Transaction State Diagram for an illustration of the steps involved in a typical HTTP transaction.
The HTTP transaction functions are:
INKHttpTxnCachedReqGet — Note that it is an error to modify cached headers.
INKHttpTxnCachedRespGet — Note that it is an error to modify cached headers.
INKHttpTxnClientReqGet — Plugins that must read client request headers use this call to retrieve the HTTP header.