File: sample-null-transformation-plugin.en.rst

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (221 lines) | stat: -rw-r--r-- 8,034 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
.. Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.

.. include:: ../../../common.defs

.. _developer-plugins-http-transformations-null-transform:

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.

    .. code-block:: c

        void
        TSPluginInit (int argc, const char *argv[]) {
            TSHttpHookAdd (TS_HTTP_READ_RESPONSE_HDR_HOOK,
                    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.

    .. code-block:: c

        static int transform_plugin (TSCont contp, TSEvent event, void *edata) {
            TSHttpTxn txnp = (TSHttpTxn) edata;
            switch (event) {
                case TS_EVENT_HTTP_READ_RESPONSE_HDR:
                    if (transformable (txnp)) {
                        transform_add (txnp);
                    }

    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.

    .. code-block:: c

        static int transformable (TSHttpTxn txnp)
        {
            TSMBuffer bufp;
            TSMLoc hdr_loc;
            TSHttpStatus resp_status;
            TSHttpTxnServerRespGet (txnp, &bufp, &hdr_loc);

            if (TS_HTTP_STATUS_OK == (resp_status =
                        TSHttpHdrStatusGet (bufp, hdr_loc)) ) {
                return 1;
            } else {
                return 0;
            }
        }

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).

    .. code-block:: c

        static void transform_add (TSHttpTxn txnp)
        {
            TSVConn connp;
            connp = TSTransformCreate (null_transform, txnp);
            TSHttpTxnHookAdd (txnp, TS_HTTP_RESPONSE_TRANSFORM_HOOK, connp);
        }

    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).

    .. code-block:: c

        output_conn = TSTransformOutputVConnGet (contp);

5.  Get a handle to the input VIO. (See the ``handle_transform``
    function.)

    .. code-block:: c

        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:

    .. code-block:: c

        data->output_vio = TSVConnWrite (output_conn, contp,
            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:

    .. code-block:: c

        TSIOBufferCopy (TSVIOBufferGet (data->output_vio),
                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:

    .. code-block:: c

        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:

    .. code-block:: c

        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 re-enable and wakes up the upstream vconnection by sending it
    ``WRITE_READY``:

    .. code-block:: c

        if (TSVIONTodoGet (input_vio) > 0) {
            if (towrite > 0) {
                TSVIOReenable (data->output_vio);

                TSContCall (TSVIOContGet (input_vio),
                        TS_EVENT_VCONN_WRITE_READY, input_vio);
            }
            } 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 re-enable the downstream
    vconnections. In this instance, the ``TSVIOReenable`` function sends
    ``TS_EVENT_IMMEDIATE``.

    **Passing Data Through a Transformation**
    {#PassingDataThroughaTransformation}

.. figure:: /static/images/sdk/vconnection1.jpg
      :alt: Passing Data Through a Transformation

      Passing Data Through a Transformation

11. 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
    re-enable. It then triggers the end of the write operation from the
    upstream vconnection by sending the upstream vconnection a
    ``WRITE_COMPLETE`` event.

    .. code-block:: c

      TSVIONBytesSet (data->output_vio, TSVIONDoneGet (input_vio));
         TSVIOReenable (data->output_vio);
         TSContCall (TSVIOContGet (input_vio),
            TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
      }

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

12. 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:

    .. code-block:: c

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

    The following diagram illustrates the flow of events:

    **Ending the Transformation** {#EndingTransformation}

    .. figure:: /static/images/sdk/vconnection2.jpg
       :alt: Ending the Transformation

       Ending the Transformation