HTTP POST redirect preserving POST data
Problem this snippet solves:
Use Javascript in an iRule to redirect HTTP POST requests to HTTPS. When an HTTP 30x redirect it sent to a client that has sent a POST request, the user-agent transparently issues a new GET request. As a result, the original POST request payload is lost.
The idea behind this iRule is when client sends a POST to an HTTP virtual server, LTM replies with an HTML page which contains a form. The form contains post-data that the client just sent. The form will be auto-submitted by Javascript via HTTPS.
Here is how this iRule works:
- if method = post, it issues HTTP::collect, which will invoke HTTP_REQUEST_DATA
-
then it scans POST-data and prepares new content to respond to client. the new content will include
- form with all INPUT field names retrieved from POST-data
- the "action" parameter in the form points to external server
- all input fields are set as hidden (so client won't see the data during the process)
- Javascript that submits the form automatically
- then iRule replies to client with HTTP::respond command with content prepared in step 2
Code :
when RULE_INIT { set static::ext_url "https://10.10.71.3/test.post" } when HTTP_REQUEST { # Check if request was a POST if { [string tolower [HTTP::method]] eq "post" } { # Check if there is a Content-Length header if { [HTTP::header exists "Content-Length"] } { if { [HTTP::header "Content-Length"] > 1048000 }{ # Content-Length over 1Mb so collect 1Mb set content_length 1048000 } else { # Content-Length under 1Mb so collect actual length set content_length [HTTP::header "Content-Length"] } } else { # Response did not have Content-Length header, so use default of 1Mb set content_length 1048000 } # Don't collect content if Content-Length header value was 0 if { $content_length > 0 } { HTTP::collect $content_length } } } when HTTP_REQUEST_DATA { set content "< script type=text/javascript language=javascript> \ function s(){ document.f.submit(); } \" HTTP::respond 200 content $content }
Published Mar 18, 2015
Version 1.0