Forum Discussion

Jakov_40007's avatar
Jakov_40007
Icon for Nimbostratus rankNimbostratus
Mar 26, 2010

HTTP::path and HTTP::query question

I am new to iRules and having difficulty writing a rule that will rewrite a request from the client to a server by replacing the 'path' but will still retain the query string that follows. I have written the following rule, and it seems to be working fine for rewriting the path, but in the process the query string gets lost and never makes it to the server.

 

 

 

when HTTP_REQUEST {

 

 

if { [string tolower [http::host]] equals "www.abc.com" } {

 

if { [http::path] equals "/"} {

 

http::path "/en-CA/default.htm"

 

return

 

}

 

}

 

 

In this case, if I browse to http://www.abc.com/?xyz=8 what is reaching the server is http://www.abc.com/en-CA/default.htm, but the query (in this case 'xyz=8') gets stripped off. What I need it to do is send http://www.abc.com/en-CA/default.htm?xyz=8 or whaever the query value may be.

 

 

Thanks!
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    It looks like your rule should work to rewrite just the path (and preserve the query string). Can you add some logging to check?

     
     when HTTP_REQUEST { 
      
        log local0. "[IP::client_addr]:[TCP::client_port]: Original [HTTP::method] request to [HTTP::host][HTTP::uri]\ 
           with path [HTTP::path] and query [HTTP::query]" 
      
        if { [string tolower [HTTP::host]] equals "www.abc.com" } { 
           if { [HTTP::path] equals "/"} { 
              HTTP::path "/en-CA/default.htm" 
           } 
        } 
     } 
     when HTTP_REQUEST priority 501 { 
        log local0. "[IP::client_addr]:[TCP::client_port]: Current: [HTTP::method] request to [HTTP::host][HTTP::uri]\ 
           with path [HTTP::path] and query [HTTP::query]" 
     } 
     

    The second HTTP_REQUEST event allows you to log any changes without having any problems with cached HTTP command output.

    Aaron
  • Hi Aaron,

     

     

    Strangely the log shows no query:

     

     

    174.112.44.70:1713: Original GET request to www.abc.com/?me=8 with path / and query

     

     

    Thanks,

     

     

    Jakov

     

  • I added the log both inside and outside the rule, and here is what I get both inside and outside the rule now. Not sure why it came back as blank query before.

     

     

    Rule CWNA_S_HTTP_rule : 174.112.44.70:1982: Original GET request to www.abc.com/?me=8 with path / and query me=8

     

     

    Jakov

     

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    What does the second log line show? ie, is the change actually made to the path?

     

     

    Aaron
  • The second log shows no query:

     

     

    Rule CWNA_S_HTTP_rule : 174.112.44.70:1992: Current: GET request to www.abc.com/en-CA/default.htm with path /en-CA/default.htm and query
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    That seems like a bug. HTTP::path should only return or set the path in the URI without retrieving or modifying the query string. You could work around this using HTTP::uri instead:

     
     when HTTP_REQUEST { 
      
         Check if path is / 
        if {[HTTP::path] eq "/"}{ 
      
            Parse the URI into the path and query 
           scan [HTTP::uri] {%[^?]?%s} path query 
      
            Check if there was a query 
           if {[info exists query]}{ 
      
               Update path, preserving query 
      HTTP::uri "/en-CA/default.htm?$query" 
      
           } else { 
      
               Update path without query 
      HTTP::uri "/en-CA/default.htm" 
           } 
        } 
     } 
     

    I'd also suggest opening a case with F5 Support on the possible bug with HTTP::path removing the query string. Which LTM version are you running?

    Aaron

  • Hi Aaron,

     

     

    Your code above seems to be working and gets around the problem with HTTP::path. I called support and they seem to be in agreement that it is a bug and will be investigating.

     

     

    We are on 10.0.1 code build 283.

     

     

    Thanks for your excellent help on this!

     

     

    Jakov

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi Jakov,

     

     

    If/when you get a CR for this, could you reply with it for reference?

     

     

    Thanks,

     

    Aaron
  • Hi Aaron,

     

     

    For now I just have a case number: C655714

     

     

    The engineer is reviewing qkview.

     

     

    Thanks again!

     

     

    Jakov