Forum Discussion
Danielseyoum
Jul 26, 2007Altostratus
Case sensitivity challenge
I have application that is case sensitive and I have to make sure the case is formated in a way the application will respond.
incoming uri:
/about us --> should be --> /About Us
/...
Jul 30, 2007
Remove the "-nocase" flag as that isn't supported in iRules.
Here's an alternate solution that will extract all space delimited tokens in the first path section of the URI and will convert the first character in each token to upper case.
when HTTP_REQUEST {
set new_uri "/"
set args [split [string map { "%20" " " } [lindex [split [HTTP::uri] "/"] 1]] " "]
for {set i 0} {$i < [llength $args]} {incr i} {
set arg [lindex $args $i]
if { $arg ne "" } {
set CamelCaseToken [string replace $arg 0 0 [string toupper [string range $arg 0 0]]]
if { $new_uri eq "/" } {
set new_uri "${new_uri}${CamelCaseToken}"
} else {
set new_uri "${new_uri}%20${CamelCaseToken}"
}
} else {
set new_uri "${new_uri}%20"
}
}
HTTP::uri $new_uri
}
This iRule will ignore any part of the path past the first directory (since that's what you gave in your example (from the lindex in the 2nd line).
Examples
http://www.foo.com/hi there -> http://www.foo.com/%20Hi%20There
http://www.foo.com/how are you -> http://www.foo.com/How%20Are%20%20You
Now, if you want to support sub directories, you'll have to not just extract the first element of the path, but split the path apart and then process each section.
This code will work with multiple paths and capitalize all first words (delimited by spaces) in all directories in the URI. I've also added a check for special characters that you do not want capitalized (since your original post had one)
when HTTP_REQUEST {
set new_uri ""
Split the URI into it's directories
set dirs [split [HTTP::uri] "/"]
Loop over all the directories
for {set i 0} {$i < [llength $dirs]} {incr i} {
set dir [lindex $dirs $i]
if { $dir ne "" } {
Convert all %20's into Spaces, and then split by that space
If we kept %20s the split would return a bunch of empty elements
set args [split [string map { "%20" " " } $dir]]
set new_sub_dir ""
set content_added 0
Loop over all the different words in each directory
for {set j 0} {$j < [llength $args]} {incr j} {
set arg [lindex $args $j]
if { $arg ne "" } {
Check for special words that you don't want capitolized
switch $arg {
"the" -
"of" {
set CamelCaseToken $arg
}
default {
set CamelCaseToken [string replace $arg 0 0 [string toupper [string range $arg 0 0]]]
}
}
if { $content_added eq 0 } {
set new_sub_dir "${new_sub_dir}${CamelCaseToken}"
} else {
set new_sub_dir "${new_sub_dir}%20${CamelCaseToken}"
}
set content_added 1
} else {
set new_sub_dir "${new_sub_dir}%20"
}
}
set new_uri "${new_uri}/${new_sub_dir}"
}
}
HTTP::uri $new_uri
}
Examples:
http://www.foo.com/hi there/how are you
-> http://www.foo.com/Hi%20There/How%20Are%20You
http://www.foo.com/howdypartner/ its time to party
-> http://www.foo.com/Howdypartner/%20Its%20Time%20To%20Party
http://www.foo.com/welcome to the party
-> http://www.foo.com/Welcome%20To%20the%20Party
Hope this helps...
-Joe
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects