Sample Null Transform Plugin

This section provides a step-by-step description of what the null transform plugin does, along with sections of code that apply. For context, you can find each code snippet in the complete source code. Some of the error checking details are left out - to give the description a step-by-step flow, only the highlights of the transform are included.

Below is an overview of the null transform plugin:

  1. Gets a handle to HTTP transactions.

    1. void
    2. TSPluginInit (int argc, const char *argv[]) {
    3. TSHttpHookAdd (TS_HTTP_READ_RESPONSE_HDR_HOOK,
    4. TSContCreate (transform_plugin, NULL));

    With this TSPluginInit routine, the plugin is called back every time Traffic Server reads a response header.

  2. Checks to see if the transaction response is transformable.

    1. static int transform_plugin (TSCont contp, TSEvent event, void *edata) {
    2. TSHttpTxn txnp = (TSHttpTxn) edata;
    3. switch (event) {
    4. case TS_EVENT_HTTP_READ_RESPONSE_HDR:
    5. if (transformable (txnp)) {
    6. transform_add (txnp);
    7. }

    The default behavior for transformations is to cache the transformed content (you can also tell Traffic Server to cache untransformed content, if you want). Therefore, only responses received directly from an origin server need to be transformed. Objects served from cache are already transformed. To determine whether the response is from the origin server, the routine transformable checks the response header for the “200 OK” server response.

    1. static int transformable (TSHttpTxn txnp)
    2. {
    3. TSMBuffer bufp;
    4. TSMLoc hdr_loc;
    5. TSHttpStatus resp_status;
    6. TSHttpTxnServerRespGet (txnp, &bufp, &hdr_loc);
    7. if (TS_HTTP_STATUS_OK == (resp_status =
    8. TSHttpHdrStatusGet (bufp, hdr_loc)) ) {
    9. return 1;
    10. } else {
    11. return 0;
    12. }
    13. }
  3. If the response is transformable, then the plugin creates a transformation vconnection that gets called back when the response data is ready to be transformed (as it is streaming from the origin server).

    1. static void transform_add (TSHttpTxn txnp)
    2. {
    3. TSVConn connp;
    4. connp = TSTransformCreate (null_transform, txnp);
    5. TSHttpTxnHookAdd (txnp, TS_HTTP_RESPONSE_TRANSFORM_HOOK, connp);
    6. }

    The previous code fragment shows that the handler function for the transformation vconnection is null_transform.

  4. Get a handle to the output vconnection (that receives data from the transformation).

    1. output_conn = TSTransformOutputVConnGet (contp);
  5. Get a handle to the input VIO. (See the handle_transform function.)

    1. input_vio = TSVConnWriteVIOGet (contp);

    This is so that the transformation can get information about the upstream vconnection’s write operation to the input buffer.

  6. Initiate a write to the output vconnection of the specified number of bytes. When the write is initiated, the transformation expects to receive WRITE_READY, WRITE_COMPLETE, or ERROR events from the output vconnection. See the handle_transform function for the following code fragment:

    1. data->output_vio = TSVConnWrite (output_conn, contp,
    2. data->output_reader, TSVIONBytesGet (input_vio));
  7. Copy data from the input buffer to the output buffer. See the handle_transform function for the following code fragment:

    1. TSIOBufferCopy (TSVIOBufferGet (data->output_vio),
    2. TSVIOReaderGet (input_vio), towrite, 0);
  8. Tell the input buffer that the transformation has read the data. See the handle_transform function for the following code fragment:

    1. TSIOBufferReaderConsume (TSVIOReaderGet (input_vio), towrite);
  9. Modify the input VIO to tell it how much data has been read (increase the value of ndone). See the handle_transform function for the following code fragment:

    1. TSVIONDoneSet (input_vio, TSVIONDoneGet (input_vio) + towrite);
  10. If there is more data left to read ( if ndone < nbytes), then the handle_transform function wakes up the downstream vconnection with a reenable and wakes up the upstream vconnection by sending it WRITE_READY:

    1. if (TSVIONTodoGet (input_vio) > 0) {
    2. if (towrite > 0) {
    3. TSVIOReenable (data->output_vio);
    4. TSContCall (TSVIOContGet (input_vio),
    5. TS_EVENT_VCONN_WRITE_READY, input_vio);
    6. }
    7. } else {

    The process of passing data through the transformation is illustrated in the following diagram. The downstream vconnections send WRITE_READY events when they need more data; when data is available, the upstream vconnections reenable the downstream vconnections. In this instance, the TSVIOReenable function sends TS_EVENT_IMMEDIATE.

    Passing Data Through a Transformation {#PassingDataThroughaTransformation}

Passing Data Through a Transformation

Passing Data Through a Transformation

  1. If the handle_transform function finds there is no more data to read, then it sets nbytes to ndone on the output (downstream) VIO and wakes up the output vconnection with a reenable. It then triggers the end of the write operation from the upstream vconnection by sending the upstream vconnection a WRITE_COMPLETE event.

    1. TSVIONBytesSet (data->output_vio, TSVIONDoneGet (input_vio));
    2. TSVIOReenable (data->output_vio);
    3. TSContCall (TSVIOContGet (input_vio),
    4. TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
    5. }

    When the upstream vconnection receives the WRITE_COMPLETE event, it will probably shut down the write operation.

  2. Similarly, when the downstream vconnection has consumed all of the data, it sends the transformation a WRITE_COMPLETE event. The transformation handles this event with a shut down (the transformation shuts down the write operation to the downstream vconnection). See the null_plugin function for the following code fragment:

    1. case TS_EVENT_VCONN_WRITE_COMPLETE:
    2. TSVConnShutdown (TSTransformOutputVConnGet (contp), 0, 1
    3. break;

    The following diagram illustrates the flow of events:

    Ending the Transformation {#EndingTransformation}

    Ending the Transformation

    Ending the Transformation