Forum Discussion

Jeff_Siegel_152's avatar
Jeff_Siegel_152
Icon for Nimbostratus rankNimbostratus
Jun 26, 2014

HTTP::Response with Chunking

I’m trying to use an I-Rule to mimic an application response. It may be failing due to chunking. The message I want to mimic has something like this in it

 

HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/xml;charset=utf-8 Transfer-Encoding: chunked Date: Thu, 26 Jun 2014 17:40:25 GMT … followed by a SOAP message

 

My I-Rule finds messages where this is an appropriate response, then uses HTTP::respond to send stuff to the client that looks more like the first message. It looks something like this:

 

HTTP::respond 200 content "" Content-Type "text/xml;charset=utf-8" Date "Thu, 26 Jun 2014 17:40:25 GMT"

 

Unlike the real message, the I-Rule generated message does not do chunking. Is there a way to make sure the Response is chunked? If I add ‘ Transfer-Encoding "chunked" ‘ to the end of the HTTP:respond line, it just hangs the I-Rule. Thanks,

 

Jeff

 

2 Replies

  • A few things to consider:

    1. The HTTP::respond command automatically calculates and inserts a Content-Length header, which is not allowed when a Transfer-Encoding header exists. To get around that, use the TCP::respond command instead and write the entire payload manually.

    2. You may also notice in the Apache response that the first line of the content is either a number or a hexadecimal value. This "prefix" is generally required for Transfer-Encoding. It tells the browser how much data to expect in each response.

    So given the above, you could do something like this:

    when HTTP_REQUEST {
        TCP::respond  "HTTP/1.1 200 OK\r\nContent-Type: text/xml;charset=utf-8\r\nTransfer-Encoding: chunked\r\n\r\n129\r\nGambardella, MatthewXML Developer's GuideComputer44.952000-10-01An in-depth look at creating applications with XML.\r\n0\r\n\r\n"
    }
    

    Notice the 129 as the start of the payload. This is the hexadecimal encoding of the content length (in this case 297 without the escape "\" characters). If this is the end of the data, then it's followed by a 0 chunk (\r\n0\r\n\r\n).

    http://en.wikipedia.org/wiki/Chunked_transfer_encoding

  • Do you have to do all of this in the HTTP_REQUEST_DATA event? Are you looking for something in the request payload?