Forum Discussion
iRULE regsub error
Hi Smart People
I am having some problems with an iRule that has been created on our F5 as a work around to a mixed content error we are receiving on a web page.
Our page uses HTTPS but there is some JS that is calling an HTTP page and causing the issue. I have an iRule in place to change it to an HTTPS reply, but I am having some problems with one of regsub commands.
The JSON result is {"result":{pathTo3DJSON":"http://genericsite.com:80/extrainfo/somefile.extension"}} and I am replacing the http with https and removing the :80 port number.
Running the iRule returns this - {"result":{pathTo3DJSON":"https://genericsite.com/extrainfo/somefile.extension"}}}}
I have an additional 2 } (braces) at the end of the result causing an error.
This is the code that I have so far. I just need to remove 2 } from the end of the result.
when HTTP_REQUEST {
if { [IP::client_addr] starts_with "my.public.IP.address" } {
}
if { [HTTP::has_responded] } { return }
if { [string tolower [HTTP::path]] == "/website/generate3dview.wcr" } {
set doRewrite [HTTP::path]
}
}
when HTTP_RESPONSE {
if { [info exists doRewrite] } {
HTTP::collect [HTTP::header Content-Length]
set clen [HTTP::header Content-Length]
incr clen -2
unset doRewrite
}
}
when HTTP_RESPONSE_DATA {
regsub "http://" [HTTP::payload] "https://" fixeddata
regsub ":80" $fixeddata "" fixeddata1
regsub "}}}}" $fixeddata1 "}}" fixeddata2
HTTP::payload replace 0 $clen $fixeddata2
HTTP::release
}
The error I receive on the F5 iRule page is
01070151:3: Rule [/Common/testing_irule] error: /Common/testing_irule:17: error: [unexpected extra argument "}}}""][when HTTP_RESPONSE_DATA {
regsub "http://" [HTTP::payload] "https://" fixeddata
# HTTP::payload replace 0 $clen $fixeddata
regsub ":80" $fixeddata "" fixeddata1
regsub "}}}}" $fixeddata1 "}}" fixeddata2]
/Common/testing_irule:21: error: [command is not valid in the current scope][HTTP::payload replace 0 $clen $fixeddata2]
/Common/testing_irule:22: error: [command is not valid in the current scope][HTTP::release]
/Common/testing_irule:23: error: [command is not valid in the current scope][}]
Thanks
To match a literal } in a string, you will need to escape it
"\}\}\}\}"
- Simon_BlakelyEmployee
To match a literal } in a string, you will need to escape it
"\}\}\}\}"
- Jonesey3000Nimbostratus
Thank you Simon. That did the trick.
- Simon_BlakelyEmployee
For an easier irule, try something like
regsub {http:\/\/(.*):80} [HTTP::payload] {https://\1} fixeddata % puts $payload \{\"result\":\{pathTo3DJSON\":\"http://genericsite.com:80/extrainfo/somefile.extension\"\}\} % regsub {http:\/\/(.*):80} $payload {https://\1} fixeddata % puts $fixeddata \{\"result\":\{pathTo3DJSON\":\"https://genericsite.com/extrainfo/somefile.extension\"\}\}
The regsub matches the whole string starting with http and ending with :80
It also creates a matching group of the fqdn between :// and :80.
You then substitute https://<first matching group> into the result.
Much more efficient.
Recent Discussions
Related Content
* 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