Forum Discussion

Ding_Hsu's avatar
Ding_Hsu
Icon for Nimbostratus rankNimbostratus
Apr 11, 2021
Solved

is it possible to log all header to html page

Hi Guys,

I try to use the irule to log all http headers when the request is coming to the VS, and then F5 generate the response page to the client.

I find the rule to log the all header to /var/log/ltm, and it's ok to log all header to /var/log/ltm :

  1. when HTTP_REQUEST {
  2. set LogString "Client [IP::client_addr]:[TCP::client_port] -> [HTTP::host][HTTP::uri]"
  3. log local0. "============================================="
  4. log local0. "$LogString (request)"
  5. foreach aHeader [HTTP::header names] {
  6. log local0. "$aHeader: [HTTP::header value $aHeader]"
  7. }
  8. log local0. "============================================="
  9. }

When i try to combine the another irule to let the F5 send the html, the page only display one header not all headers

The another irule is

when HTTP_REQUEST {
   HTTP::respond 200 content {
      <html><head><title>Test Page</title></head><body>
            Here is your header information
         </body></html>
   }
}

And i combine two rules

when HTTP_REQUEST {

set LogString "Client [IP::client_addr]:[TCP::client_port] -> [HTTP::host][HTTP::uri]"

log local0. "============================================="

log local0. "$LogString (request)"

foreach aHeader [HTTP::header names] {

   HTTP::respond 200 content "

   <html>

     <head>

      <title>Test Page</title>

     </head>

     <body>

     Your Request Header information: </BR>

  HTTP method: $HTTP_Method </BR>

  HTTP URI: $HTTP_URI </BR>

  HTTP Path: $HTTP_Path </BR>

  HTTP Query: $HTTP_Query </BR>

  HTTP Version: $HTTP_Version </BR>

  HTTP Host: $HTTP_Host </BR>

  User Agent: $User_Agent </BR>

$aHeader: [HTTP::header value $aHeader]

     </body>

</html>   "

}

}

How can i combine those two rules to log all header to the response page.

Regards,

Ding

  • 1.You can just add the log local0 in statement in the foreach loop and not write all the headers to "LogString" variable like log local0. "$headerName:[HTTP::header value $headerName]" or you can still use "LogString" but without the "append" as this time it will just take the value of the firt header and log it, then the second and then the 3th header and value and so on.

     

     

     

    2.Also you can also research the Split string ?splitChars? to split the string in a list before logging the output "LogString" variable, so that you can make it a list (you add something like "#" after each append of the HTTP header and value and use it as match for the split string) beffore logging it out but the previous example is easyer.

     

    Use of manipulations like "Split string" etc.:

     

    https://devcentral.f5.com/s/articles/irules-101-15-tcl-list-handling-commands

     

     

    I suggest reading the F5 devcental course on irules, so that you can work better with irules. After that play writting irules and in no time you are good to go as I see you are playng with them but just need some more time.

4 Replies

  • The order of things in an event like "HTTP_REQUEST" is important try placing the genaration of the custom response Page at the end of the combine irule outside of the foreach loop.

     

     

    Also for a response just use "HTTP_RESPONSE" as an event not "HTTP_REQUEST" and only use the irule to log the headers to the ltm log, not the combine irule as generating a custom respnse page to the servers is not going to work (they don't want it as this is how HTTP works.).

     

    But know that if you attach the two irules under the same VIP for logging the HTTP headers in the request and generating custom response page and the irule for logging the server response headers, when the clieny matches the iRule with "HTTP_REQUEST" that generates custom page, you may not see the server pool member response as the F5 is generating the custom page and responding to the client before even a HTTP response is given by the the real server but I don't know what you are trying to do. If you add the two irules under the same VIP better use if else condition to send the custom response page to the clients just in some cases, so that the real pool member server could be asked for a response.

     

     

     

    Also better read more about irules as you will need this knowedge to write irules on your own:

     

    https://devcentral.f5.com/s/articles/irules-101-01-introduction-to-irules

  • Hi Nikoolayy1,

     

    Thank you for your detailed answer.

    I change the order of the event "HTTP::respond", and it can log all headers in a response page =)

     

    when HTTP_REQUEST {

      set LogString "Client [IP::client_addr]:[TCP::client_port] -> [HTTP::host][HTTP::uri]</BR>"

      log local0. "============================================="

      log local0. "$LogString (request) - request: [HTTP::method]"

      

      foreach headerName [HTTP::header names] {

        append LogString "$headerName:[HTTP::header value $headerName]"

       

      }

       

      HTTP::respond 200 content "$LogString"

      log local0. "Log content $LogString"

    }

     

    I just want to see the request header that is sent from the client for troubleshooting, and that's why i didn't use the "HTTP_RESPONSE". But now i facing the another problem is i can't delimit the multiple header name and its header value, it's because "append string", all header has been concatenated.

     

     

    Regards,

    Ding

  • 1.You can just add the log local0 in statement in the foreach loop and not write all the headers to "LogString" variable like log local0. "$headerName:[HTTP::header value $headerName]" or you can still use "LogString" but without the "append" as this time it will just take the value of the firt header and log it, then the second and then the 3th header and value and so on.

     

     

     

    2.Also you can also research the Split string ?splitChars? to split the string in a list before logging the output "LogString" variable, so that you can make it a list (you add something like "#" after each append of the HTTP header and value and use it as match for the split string) beffore logging it out but the previous example is easyer.

     

    Use of manipulations like "Split string" etc.:

     

    https://devcentral.f5.com/s/articles/irules-101-15-tcl-list-handling-commands

     

     

    I suggest reading the F5 devcental course on irules, so that you can work better with irules. After that play writting irules and in no time you are good to go as I see you are playng with them but just need some more time.

  • HI Nikoolayy1,

    Thank you very much, I'll give it a try.

     

    Ding