Forum Discussion

Jorge_Cabanilla's avatar
Jorge_Cabanilla
Icon for Nimbostratus rankNimbostratus
Jul 08, 2010

Send a binary file as content in http:response, I need help

Hi,

 

 

Some one knows how to send a binary file as content in an HTTP:respond?

 

Is it possible to assign a file to a variable?

 

How to read a file?

 

 

I need the next:

 

 

set ::my_variable

 

 

Then in the http:response assign it as follows:

 

HTTP::respond 200 content $my_variable

 

 

Thanks

 

 

 

8 Replies

  • Hi Jorge,

     

     

    You can base64 encode the data before saving it to a variable and then use 'HTTP::respond 200 content [b64decode $my_b64_encoded_data]' to send it. This is done in the Maintenance Page examples in the iRule Codeshare:

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/Automatic_maintenance_page___Sorry_page_with_images.html

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/LTMMaintenancePage.html

     

     

    Aaron
  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus
    You have 2 choices.

     

     

    1. Hardcode the content to be sent back in the iRule itself.

     

    2. Or read the content from a DataGroup (Class).

     

     

    I did write an iRule last year for a customer that let you use the F5 as a web server, and used datagroups as the 'filesystem' repository...

     

    If you're going to go down that route, then the hardest thing is to get the binary content into the DataGroup itself. (That's only 'hard' because you have to encode the binary file and then add it into key/value pairs in a string DataGroup (I used a perl script and iControl to read a real filesystem, encode and upload the values, using the filename as the key. And adding in the mimetype as well so the iRule didn't need to figure it out).

     

     

    (Sorry to be vague, but I don't have the iRule or perl code that I wrote - But it is my recommendation to go this way... It's harder at first, but oh so easy to add new files... Just drop them in a directory on a server and run the upload command).

     

     

    H
  • The file that I need to send as content is already encoded as wbxml, Does the LTM has that type of encryption?

     

  • I tested it using the class external file, but my file is a wbxml type, so in order to retrieve the content I'm using the next command

     

     

    set ::xbxml_content [lindex $::answer_403 0]

     

     

    but the length is zero , This is because it is a binary file (the options that I have on the class menu for file content type are string,integer,ip) so I choosed string, is it possible to hardcoded the binary content to a plain text? basically the file contains several binary characters like: NUL, SOH,ETX

     

     

    Thanks

     

  • Regardless of what content is being sent, you can base64 encode it when you add it to the LTM configuration and then base64 decode it using b64decode.

     

     

    If you want to send the raw xbxml to the client from an iRule, you could base64 encode it, add the string either to a datagroup or directly in the iRule under the RULE_INIT event and then use 'HTTP::respond 200 content [b64decode $static::my_b64encoded_string]', assuming you're on 10.0.0 or higher. If you're not on 10.x and want a 9.3 or 9.4 example, can you confirm which version you're running?

     

     

    Aaron
  • Yes it works using the b64 encryption method and a class . With version 9.4.7 the b64encode command is not available, anyway this is what I did:

     

     

    1.- Encode the wbxml file to b64 using http://www.motobit.com/util/base64-decoder-encoder.asp

     

    2.- Create the class file( type string) , the file is on /var/config/class/

     

    3.- In the event when rule_init, declare the variable:

     

    set wbxml_bin [b64decode [lindex $wbxml_b64 0]]

     

    4.- Send the response

     

    clientside {HTTP::respond 200 content $::wbxml_bin Content-Type "application/vnd.syncml+wbxml" Connection Close}

     

     

     

    Thanks so much all for your help
  • Yes it works using the b64 encryption method and a class . With version 9.4.7 the b64encode command is not available, anyway this is what I did:

     

     

    1.- Encode the wbxml file to b64 using http://www.motobit.com/util/base64-decoder-encoder.asp

     

    2.- Create the class file( type string) , the file is on /var/config/class/

     

    3.- In the event when rule_init, declare the variable:

     

    set wbxml_bin [b64decode [lindex $wbxml_b64 0]]

     

    4.- Send the response

     

    clientside {HTTP::respond 200 content $::wbxml_bin Content-Type "application/vnd.syncml+wbxml" Connection Close}

     

     

     

    Thanks so much all for your help