Forum Discussion

Sandeep_374170's avatar
Sandeep_374170
Icon for Nimbostratus rankNimbostratus
Oct 10, 2018

Is it possible to replace extended ASCII characters in URI to UTF-8 character set

Hi,

 

Is it possible to replace extended ASCII characters in URI to UTF-8 character set via iRule?

 

BR Sandeep

 

3 Replies

  • Do you want to decode uri encoded to manage uri based routing?

    Try

    [URI::decode [HTTP::uri]]
    
  • This is perhaps not the most optimal solution, but it does technically work. Only providing the framework here for your review:

    when RULE_INIT {
        set c {This is a Test° on Tuesd©y}
        set c {This is a Test on Tuesday}
        if { [set matchTuples [regexp -all -inline {&\d+;} $c]] ne "" } {
            foreach x $matchTuples {
                set num [findstr $x "&" 2 ";"]
                if { [expr { ( $num >= 80 ) && ( $num <= 191 ) }] } {
                    set html "%C2%[format %X $num]"
                    set c [string map "$x $html" $c]
                }
            }
        }
        log local0. "c = $c"
    }
    

    So basically, c would be your input URL. iRules automatically convert extended ascii characters to decimal (ex. ° = &176;), so if the regexp finds any "&\d+;" matches, those will be inserted into a list, the list converts each decimal to HEX, appends %C2 for extended ascii characters between 80 and 191, and then replaces the decimal encoding in the original string with the new HTML encoding.

    This example only handles the extended ascii characters between 80 and 191. If the decimal value is between 192 and 255, you'd append "%C3".

    Ref: https://www.utf8-chartable.de/unicode-utf8-table.pl

  • based on kevin's code, you can try this...

     

    when RULE_INIT {
        set map_list [list]
        for {set i 128} {$i<192} {incr i} {
            lappend map_list [binary format c $i] %C2%[format %X $i]
        }
            for {set i 192} {$i<256} {incr i} {
            lappend map_list [binary format c $i] %C3%[format %X $i]
        }
        set static::extended_ascii_map_list $map_list
    }
    
    when HTTP_REQUEST {
        HTTP::uri [string map $static::extended_ascii_map_list [HTTP::uri]]
    }