You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by Manish Pandey <ma...@gmail.com> on 2010/12/08 04:58:12 UTC

Re: Printing origin server response

Hi Traffic Server Gurus,

Would appreciate some help here -- How can I access the underlying string or
char* for the body of the server response?
There are many functions for working with server response headers -
including URL, mime type, etc, but I see nothing for the body itself.

I am trying to write a plugin with behavior that depends on the body of the
response from the http server.

Thanks,
Manish


How do I access the actual string or char*
> representing the body here?  The data seems to be hidden below a few
levels
> of buffers and abstractions:
>
> if (towrite > 0) {
>       /* Copy the data from the read buffer to the output buffer. */
>       if (TSIOBufferCopy(TSVIOBufferGet(data->output_vio),
> TSVIOReaderGet(input_vio), towrite, 0) == TS_ERROR) {
>         TSError("[null-plugin] unable to copy IO buffers\n");
>         goto Lerror;
>       }


On Tue, Nov 30, 2010 at 7:14 AM, Manish Pandey <mp...@alumni.cmu.edu>
wrote:
> Hi Eric,
> Thanks for the pointers.
>>1. With a transformation plugin you could 'print' the full response -- but
>> where do you want to print it?
> I'd like to print it as a debug message to stdio/stderr on the window in
> which the program is running, and check on the transformations that are
> applied to it.
> 2. The null-transform plugin can be the basis of the plugin I want to
> develop, but a couple of questions. Here is the code snippet which seems
to
> do the actual copying. How do I access the actual string or char*
> representing the body here?  The data seems to be hidden below a few
levels
> of buffers and abstractions:
>
> if (towrite > 0) {
>       /* Copy the data from the read buffer to the output buffer. */
>       if (TSIOBufferCopy(TSVIOBufferGet(data->output_vio),
> TSVIOReaderGet(input_vio), towrite, 0) == TS_ERROR) {
>         TSError("[null-plugin] unable to copy IO buffers\n");
>         goto Lerror;
>       }
>
> Thanks,
> Manish
> On Sun, Nov 28, 2010 at 10:47 PM, Eric Balsa <er...@apache.org> wrote:
>>
>> Hi Manish,
>>
>> 1. With a transformation plugin you could 'print' the full response --
>> but where do you want to print it?
>>
>> 2. There is no HTML parsing inside TrafficServer. TS does not care
>> about content; it proxies/caches *anything* as long as it's served
>> over HTTP/HTTPS.
>>
>> To solve your problem, you would need to develop a transformation
>> plugin
>> (
http://trafficserver.apache.org/docs/v2/sdk/HTTPTransformationPlugins.html)
>> and do some simple string parsing on the buffer.
>>
>> There are a few plugin examples available to get you started at:
>>
>>
http://svn.apache.org/repos/asf/trafficserver/traffic/trunk/example/null-transform/
>>
>>
http://svn.apache.org/repos/asf/trafficserver/traffic/trunk/example/bnull-transform/
>>
>>
http://svn.apache.org/repos/asf/trafficserver/traffic/trunk/example/append-transform/
>>
>> Regards,
>> --Eric
>>
>> On Sun, Nov 28, 2010 at 9:05 PM, Manish Pandey <ma...@gmail.com>
wrote:
>> > Hi folks,
>> >
>> > I am developing a trafficserver plugin, and would like to find out
>> > 1. How can I print a origin server response sent to the trafficserver,
>> > e.g.,
>> > the html document with embedded javascript etc.
>> > 2. Also, how can I detect specific strings in the response to do
>> > response
>> > filtering? Is there any html parser that is available
>> >   in TS that I can use for this?
>> >
>> > Any pointers will be appreciated.
>> >
>> > Thanks,
>> > Manish
>> >
>
>

Re: Printing origin server response

Posted by Eric Balsa <er...@apache.org>.
Manish,

TS doesn't natively buffer the entire origin response for someone to
possibly operate on it with something like TSHttpBodyGet(), it streams
it from origin to client (or cache to client as the case may be).
Using a transformation plugin, you get to intercept that streaming as
it occurs and operate on the data as it streams. This is asynchronous,
meaning the plugin gets called back as there is data available to be
read or written and each time you are called back, you may just have a
small chunk of the entire response. Most of this is abstracted inside
a VIO/TSIOBuffer, etc. Every TSIOBuffer needs an associated
TSIOBufferReader to read what's in the buffer.

These APIs are explained at:
http://trafficserver.apache.org/docs/v2/sdk/IOBuffers_IO.html
http://trafficserver.apache.org/docs/v2/sdk/INKIOBufferReaderStart.html
http://trafficserver.apache.org/docs/v2/sdk/INKIOBufferBlockReadStart.html

You asked about how to convert from TSIOBufferReader to some
string(ish) representation. This would be done with the following API
calls:

int64 length;

TSIOBufferBlock block = TSIOBufferReaderStart(readerp);
const char * output_string = TSIOBufferBlockReadStart(block, readerp, &length);
TSDebug("plugin","I read the following data %.*s", length, output_string);

  * Note: there could be more 'blocks' available for reading in which
case you can call INKIOBufferBlockNext as mentioned in the docs.

Regards,
--Eric

On Tue, Dec 7, 2010 at 7:58 PM, Manish Pandey <ma...@gmail.com> wrote:
> Hi Traffic Server Gurus,
>
> Would appreciate some help here -- How can I access the underlying string or
> char* for the body of the server response?
> There are many functions for working with server response headers -
> including URL, mime type, etc, but I see nothing for the body itself.
>
> I am trying to write a plugin with behavior that depends on the body of the
> response from the http server.
>
> Thanks,
> Manish
>
>
> How do I access the actual string or char*
>> representing the body here?  The data seems to be hidden below a few
> levels
>> of buffers and abstractions:
>>
>> if (towrite > 0) {
>>       /* Copy the data from the read buffer to the output buffer. */
>>       if (TSIOBufferCopy(TSVIOBufferGet(data->output_vio),
>> TSVIOReaderGet(input_vio), towrite, 0) == TS_ERROR) {
>>         TSError("[null-plugin] unable to copy IO buffers\n");
>>         goto Lerror;
>>       }
>
>
> On Tue, Nov 30, 2010 at 7:14 AM, Manish Pandey <mp...@alumni.cmu.edu>
> wrote:
>> Hi Eric,
>> Thanks for the pointers.
>>>1. With a transformation plugin you could 'print' the full response -- but
>>> where do you want to print it?
>> I'd like to print it as a debug message to stdio/stderr on the window in
>> which the program is running, and check on the transformations that are
>> applied to it.
>> 2. The null-transform plugin can be the basis of the plugin I want to
>> develop, but a couple of questions. Here is the code snippet which seems
> to
>> do the actual copying. How do I access the actual string or char*
>> representing the body here?  The data seems to be hidden below a few
> levels
>> of buffers and abstractions:
>>
>> if (towrite > 0) {
>>       /* Copy the data from the read buffer to the output buffer. */
>>       if (TSIOBufferCopy(TSVIOBufferGet(data->output_vio),
>> TSVIOReaderGet(input_vio), towrite, 0) == TS_ERROR) {
>>         TSError("[null-plugin] unable to copy IO buffers\n");
>>         goto Lerror;
>>       }
>>
>> Thanks,
>> Manish
>> On Sun, Nov 28, 2010 at 10:47 PM, Eric Balsa <er...@apache.org> wrote:
>>>
>>> Hi Manish,
>>>
>>> 1. With a transformation plugin you could 'print' the full response --
>>> but where do you want to print it?
>>>
>>> 2. There is no HTML parsing inside TrafficServer. TS does not care
>>> about content; it proxies/caches *anything* as long as it's served
>>> over HTTP/HTTPS.
>>>
>>> To solve your problem, you would need to develop a transformation
>>> plugin
>>> (
> http://trafficserver.apache.org/docs/v2/sdk/HTTPTransformationPlugins.html)
>>> and do some simple string parsing on the buffer.
>>>
>>> There are a few plugin examples available to get you started at:
>>>
>>>
> http://svn.apache.org/repos/asf/trafficserver/traffic/trunk/example/null-transform/
>>>
>>>
> http://svn.apache.org/repos/asf/trafficserver/traffic/trunk/example/bnull-transform/
>>>
>>>
> http://svn.apache.org/repos/asf/trafficserver/traffic/trunk/example/append-transform/
>>>
>>> Regards,
>>> --Eric
>>>
>>> On Sun, Nov 28, 2010 at 9:05 PM, Manish Pandey <ma...@gmail.com>
> wrote:
>>> > Hi folks,
>>> >
>>> > I am developing a trafficserver plugin, and would like to find out
>>> > 1. How can I print a origin server response sent to the trafficserver,
>>> > e.g.,
>>> > the html document with embedded javascript etc.
>>> > 2. Also, how can I detect specific strings in the response to do
>>> > response
>>> > filtering? Is there any html parser that is available
>>> >   in TS that I can use for this?
>>> >
>>> > Any pointers will be appreciated.
>>> >
>>> > Thanks,
>>> > Manish
>>> >
>>
>>
>