Forum Discussion
21 Replies
Sort By
- hooleylistCirrostratusHi Jayme,
- Jayme_41167NimbostratusHi Aaron,
- hooleylistCirrostratusI did preliminary testing of this iRule to handle proxied requests. If you try this and run into issues, can you provide details?
web proxy example This is a simple, incomplete example web proxy iRule. It only supports limited proxy functionality of converting the requested host (from an absolute URI or the Host header) to an IP address and sending the request on. It doesn't support CONNECT/HTTPS or most other RFC2616 requirements for a web proxy. when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: New HTTP [HTTP::method] request to [HTTP::host], [HTTP::uri]" Check if the URI is absolute and http:// if {[string tolower [HTTP::uri]] starts_with "http://"}{ Parse the host value from the URI set host [URI::host [HTTP::uri]] log local0. "[IP::client_addr]:[TCP::client_port]: Parsed $host from URI [HTTP::uri]" } else { set host [HTTP::host] } Check if host header has a port if {$host contains ":"}{ Scan the host header to parse the host and port if {[scan $host {%[^:]:%s} host port] == 2}{ log local0. "[IP::client_addr]:[TCP::client_port]: Parsed \$host:\$port: $host:$port" } else { Host value was host: without a port. Use the requested port. set port [TCP::local_port] } } else { Host header didn't have a port. Use the requested port. set port [TCP::local_port] } Check if the host header isn't an IP address (ie, it contains an alpha character) if {[string match {*[a-zA-Z]*} $host]}{ log local0. "[IP::client_addr]:[TCP::client_port]: Host value not an IP: $host" Perform a DNS lookup of the hostname NAME::lookup $host Hold the request until name resolution completes HTTP::collect } elseif {[catch {IP::addr $host mask 255.255.255.255}]==0}{ log local0. "[IP::client_addr]:[TCP::client_port]: Host is an IP: [HTTP::host]" Request was to a valid IP address, so use that as the destination node $host $port } else { Couldn't parse host header. Could use the destination IP address as the destination? HTTP::respond 400 content "Invalid Host header" log local0. "[IP::client_addr]:[TCP::client_port]: Invalid host header: [HTTP::host]" } } when NAME_RESOLVED { set response [NAME::response] log local0. "[IP::client_addr]:[TCP::client_port]: Resolution response: $response (elements: [llength $response])" Check if there is a resolution answer and it's an IP address switch [llength $response] { 0 { No response, or response wasn't an IP address log local0. "[IP::client_addr]:[TCP::client_port]: Non-existent/invalid response: $response" HTTP::respond 500 content "Couldn't process request" } default { Response was one or more list entries. Use the first list element. Check if it's an IP address. if {[catch "IP::addr [lindex $response 0] mask 255.255.255.255"]==0}{ Request was to a valid IP address, so use that as the destination if {$port != "" and [string is integer $port]}{ log local0. "[IP::client_addr]:[TCP::client_port]: Using destination with parsed port [lindex $response 0]:$port" node [lindex $response 0] $port } else { log local0. "[IP::client_addr]:[TCP::client_port]: Using destination with default port $response:[TCP::local_port]" node [lindex $response 0] $::default_port } } else { No response, or response wasn't an IP address log local0. "[IP::client_addr]:[TCP::client_port]: Non-existent/invalid response: $response" HTTP::respond 500 content "Couldn't process request" } } } Release the request HTTP::release }
- Jayme_41167NimbostratusHi Aaron,
- hooleylistCirrostratusSorry about that. I had made a few tweaks to the host header parsing and didn't check the syntax afterwards. I updated the last version to also check for absolute HTTP URIs and parse the host from there if present instead of using the Host header.
- Fadhil_Marus__TNimbostratushi aaron,
- hooleylistCirrostratusHi Fadhil,
- Fadhil_Marus__TNimbostratusHi aaron,
- Fadhil_Marus__TNimbostratusHi Arron,
- hooleylistCirrostratusHi Fadhil,