Rewriting Single Part HTTP Responses As Multipart

Problem this snippet solves:

This iRule illustrates how to transform a single part HTTP response into a multi-part response, as suggested in AskF5 SOL9396: Downloading a linearized PDF file may fail when using the Firefox browser. Click here to view the solution.

Code :

when HTTP_REQUEST {
   if { [HTTP::header exists "Range"] and ([HTTP::header "Range"] contains ",") } { 
      set multirange_request 1   
   } else {
      set multirange_request 0
   }
}

when HTTP_RESPONSE {
   if { $multirange_request and [HTTP::status] == 206 /
    and !([HTTP::header "Content-Type"] starts_with "multipart/") } {
      if {([HTTP::header exists "Content-Length"]) && /
       ([HTTP::header "Content-Length"] <= 1000000)}{
         set content_length [HTTP::header "Content-Length"]
      } else {
         # may need to be larger if client requests really big chunks
         set content_length 1000000
      }
      if { [info exists content_length] && $content_length > 0} {
         HTTP::collect $content_length
      }
   }
}

when HTTP_RESPONSE_DATA {
   HTTP::payload replace 0 0 "{BR}--MPBOUNDARY{BR}Content-Type: [HTTP::header "Content-Type"]/
     {BR}Content-Range: [HTTP::header  "Content-Range"]\r\n\r\n"
   HTTP::payload replace [HTTP::payload length] 0 "{BR}--MPBOUNDARY--{BR}"
   HTTP::header replace "Content-Type" "multipart/byteranges; boundary=MPBOUNDARY"
   HTTP::header remove "Content-Range"
}
Published Mar 18, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment