Forum Discussion

ims_243721's avatar
ims_243721
Icon for Nimbostratus rankNimbostratus
Jan 15, 2016

TCL encoding command not accepted

I need to encode variable with unicode

 

set uni_msg [URI::query [HTTP::uri] "msg"] set en_msg [encoding convertto unicode $uni_msg]

 

Seems that F5 interpreter not accepting encoding TCL command, Any hack for this ?

 

12 Replies

  • To correct myself, here's a conversion to unicode. Any UNIX machines default to UTF-8 handling of unicode, that's the format you'll get with this iRule. Note that Windows handles unicode as UTF-16, and you may need to adjust it.

    when HTTP_REQUEST {
      binary scan [HTTP::uri] H* myvar 
      log local0. "TEST: Original URI [HTTP::uri] | UTF-8 unicode $myvar"
    }
    
    Jan 15 16:26:20 waf12 info tmm7[13448]: Rule /Common/test_encode : TEST: Original URI /helloworld?asd=1 | UTF-8 unicode 2f68656c6c6f776f726c643f6173643d31
    

    It results the same in this text to UTF-8 converter tool: http://macchiato.com/unicode/convert.html

    • Hannes_Rapp's avatar
      Hannes_Rapp
      Icon for Nimbostratus rankNimbostratus
      No sorry, it's for percent encoding. Didn't pay attention to your question :(
    • Hannes_Rapp's avatar
      Hannes_Rapp
      Icon for Nimbostratus rankNimbostratus
      I've modified my answer, I believe this is what you're looking for. Good luck :)
  • Hi Ims,

    unfortunately there does not exist a simple hack to access the

    [encoding]
    command.

    In v9 the

    [encoding]
    command was official listed as a disabled command. In newer versions is not listed anymore... But instead its now completely removed / hidden from the iRule environment, since you can even find the command using
    [info command *]
    . I duno if this is a just documentation bug or an unintended behavior. But you may raise a ticket to clarify the situation with the F5 support.

    https://support.f5.com/kb/en-us/solutions/public/6000/300/sol6319.html

    You may also want to perform the needed unicode conversation using a

    [string map]
    command and a
    $charmap
    of your choice. This technique would allow you to define the unicode format and possible escape sequences as required by your application.

    If you need further assistence to setup the charmap, then provide a sample of the unicode format the application expects.

    Cheers, Kai

  • @kai thanks for your support, below example of my array

    $unicodeArray[1] = "0x13";
    $chrArray[6] = "a";
    $unicodeArray[2] = "0x14";
    $chrArray[7] = "b";
    $unicodeArray[3] = "0x25";
    $chrArray[8] = "c";
    $unicodeArray[4] = "0x26";
    $chrArray[9] = "d";
    
  • Hi Ims,

     

    An input string of "abcd" would then become converted to the string "0x13 0x14 0x25 0x26"? Or is the output formated otherwise?

     

    Cheers, Kai

     

  • I didnt get the point how the input string and final outcome should look like. Make a very precise example please.

     

    Cheers, Kai

     

  • Hi Ims,

     

    the custom char map would then look like this...

     

    Example1:

     

    when HTTP_REQUEST {
    
        set uri_input "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        set input [URI::decode $uri_input]
        set output [string trim [string map {
    
            "a" " 0x13" "b" " 0x14" "c" " 0x25" "d" " 0x26" "e" " 0x13" "f" " 0x14" "g" " 0x25" "h" " 0x26"
            "i" " 0x13" "j" " 0x14" "k" " 0x25" "l" " 0x26" "m" " 0x13" "n" " 0x14" "o" " 0x25" "p" " 0x26"
            "q" " 0x13" "r" " 0x14" "s" " 0x25" "t" " 0x26" "u" " 0x13" "v" " 0x14" "w" " 0x25" "x" " 0x26"
            "y" " 0x13" "z" " 0x14"
    
            "A" " 0x13" "B" " 0x14" "C" " 0x25" "D" " 0x26" "E" " 0x13" "F" " 0x14" "G" " 0x25" "H" " 0x26"
            "I" " 0x13" "J" " 0x14" "K" " 0x25" "L" " 0x26" "M" " 0x13" "N" " 0x14" "O" " 0x25" "P" " 0x26"
            "Q" " 0x13" "R" " 0x14" "S" " 0x25" "T" " 0x26" "U" " 0x13" "V" " 0x14" "W" " 0x25" "X" " 0x26"
            "Y" " 0x13" "Z" " 0x14"
    
            } $input ]]
    
    }

    If this technique clutters your code too much, then you could also outsource the functionality into a TCL procedure. But it would cause some added overhead for the procedure housekeeping...

     

    Example2:

     

    when HTTP_REQUEST {
    
        set uri_input "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        set input [URI::decode $uri_input]
        set output [call YOUR_RULE_NAME::MY_CHAR_MAP $input]
    
    }
    proc MY_CHAR_MAP { string } {
    
        return [string trim [string map {
    
            "a" " 0x13" "b" " 0x14" "c" " 0x25" "d" " 0x26" "e" " 0x13" "f" " 0x14" "g" " 0x25" "h" " 0x26"
            "i" " 0x13" "j" " 0x14" "k" " 0x25" "l" " 0x26" "m" " 0x13" "n" " 0x14" "o" " 0x25" "p" " 0x26"
            "q" " 0x13" "r" " 0x14" "s" " 0x25" "t" " 0x26" "u" " 0x13" "v" " 0x14" "w" " 0x25" "x" " 0x26"
            "y" " 0x13" "z" " 0x14"
    
            "A" " 0x13" "B" " 0x14" "C" " 0x25" "D" " 0x26" "E" " 0x13" "F" " 0x14" "G" " 0x25" "H" " 0x26"
            "I" " 0x13" "J" " 0x14" "K" " 0x25" "L" " 0x26" "M" " 0x13" "N" " 0x14" "O" " 0x25" "P" " 0x26"
            "Q" " 0x13" "R" " 0x14" "S" " 0x25" "T" " 0x26" "U" " 0x13" "V" " 0x14" "W" " 0x25" "X" " 0x26"
            "Y" " 0x13" "Z" " 0x14"
    
            } $string ]]
    
    }

    Cheers, Kai