Forum Discussion

Domai_23823's avatar
Domai_23823
Icon for Nimbostratus rankNimbostratus
May 11, 2018

Delete URI after pool is selected - iRule

Hello All,

So I was asked to come up with an iRule to accomplish below- URI based pool+port selection, delete the uri and send it to the selected pool+port, subsequent connections should also go to the same pool.

Let me elaborate with examples

app.example.com/apple/ui - port 9887 (apple_9887_pool)

app.example.com/android/ui - port 7889 (andriod_7889_pool)

So once based on uri the pool is selected apple_9887_pool for /apple/ui and andriod_7889_pool for /andriod/ui the uri need to be deleted and the subsequent connections now which will be

https://app.example.com:9887 or https://app.example.com:7889

should go to the same pool. The below is what I have so far...

when HTTP_REQUEST
{
  set host [string tolower [HTTP::host]]
  set uri [string tolower [HTTP::uri]]
  switch -glob $host
  {
    "app.example.com"
      {
        if { $uri starts_with "/apple/ui" } {
          [string map -nocase {"/apple/ui" ""} [HTTP::uri]]
          pool apple_9887_pool
        }
        elseif  $uri starts_with "/android/ui" } {
          [string map -nocase {"/android/ui" ""} [HTTP::uri]]
          pool andriod_7889_pool
        }
        else {
          pool apple_andriod_default_pool
        }
      }
}

apple_andriod_default_pool has both the pool members with 9887 and 7889 port.

Is this the right way to approach this?

5 Replies

  • Don't see any issue with iRule except line no 13. Missed

    {
    after elseif.

    elseif { $uri starts_with "/android/ui" } {
    
    • Domai's avatar
      Domai
      Icon for Altostratus rankAltostratus

      f5_rock yes I missed that...while copying.Thank you.

       

  • Hi,

    You can’t set an empty value in URI.

    You must be sure it always starts with a /

    HTTP::uri [string map -nocase {"/android/ui/" "/" "/android/ui" "/"} [HTTP::uri]]
    
  • Hi,

     

    try this code:

     

    when CLIENT_ACCEPTED {
      set inject_cookie 0
      set site_mode "default"
    }
    
    when HTTP_REQUEST {
      set host [string tolower [HTTP::host]]
      set uri [string tolower [HTTP::uri]]
      switch -glob $host {
        "app.example.com" {
          if { $uri starts_with "/apple/ui" } {
            HTTP::uri [string map -nocase {"/apple/ui/" "/" "/apple/ui" "/"} [HTTP::uri]]
            set inject_cookie 1
            set site_mode "apple"
          } elseif  { $uri starts_with "/android/ui" } {
            HTTP::uri [string map -nocase {"/android/ui/" "/" "/android/ui" "/"} [HTTP::uri]]
            set inject_cookie 1
            set site_mode "android"
          } elseif {[HTTP::cookie exists site_mode]}{
            set site_mode [HTTP::cookie value site_mode]
          }
          switch $site_mode {
            "apple" {pool apple_9887_pool}
            "android" {pool andriod_7889_pool}
            "default" {pool apple_andriod_default_pool}
          }
        }
      }
    }
    
    when HTTP_RESPONSE {
      if {$inject_cookie} {
        switch $site_mode {
          "apple" {
            HTTP::cookie remove site_mode
            HTTP::cookie insert name site_mode value apple path "/"
            HTTP::cookie expires site_mode 300 relative
            HTTP::cookie secure site_mode enable
            set inject_cookie 0
          }
          "android" {
            HTTP::cookie remove site_mode
            HTTP::cookie insert name site_mode value android path "/"
            HTTP::cookie expires site_mode 300 relative
            HTTP::cookie secure site_mode enable
            set inject_cookie 0
          }
          "default" {
            HTTP::cookie remove site_mode
            HTTP::cookie insert name site_mode value deleted path "/"
            HTTP::cookie expires site_mode 0 absolute
            HTTP::cookie secure site_mode enable
            set inject_cookie 0
          }
        }
      }
    }