#!/usr/local/web/bin/perl ### ---------------------------------------------------------------------- ### # # CGI Request "proxy" program. # # takes CGI submission, passes it to another CGI program and passes # back the response to the browser. Useful if you have a CGI on a machine # behind a firewall or similar and you want to access it from somewhere # else (like your firewall :-). # # This is for POST, but you could easily adapt it for GET or other # request methods # # You may want to make it an nph- script. YMMV. # # *Requires* perl 5.001 or greater and the LWP module. # # (c) Copyright 1998, Martin Gleeson # # # You may use and/or modify this script freely, but not redistribute it # without my permission. # # Version 1.0, 4 February 1998 # use strict; ### ---------------------------------------------------------------------- ### # User configurable settings ### ---------------------------------------------------------------------- ### # The URL that you want to "proxy" my $url = 'http://whatever.com.au/cgi-bin/foo.cgi'; ### ---------------------------------------------------------------------- ### # End of configurable settings - no editing required below this line. ### ---------------------------------------------------------------------- ### use LWP::UserAgent; # Create a user agent object my $ua = new LWP::UserAgent; # We might want to append the QUERY_STRING, if there is one. $url = $url . '?' . $ENV{'QUERY_STRING'} if $ENV{'QUERY_STRING'}; # Create a request my $req = new HTTP::Request POST => "$url"; # Accept all data types. This is specifically for Microsloth's # IIS, which won't properly default to */* and throws a 406. $req->header('Accept' => '*/*'); # Pass on the Referer, Content-Length & User Agent $req->header('Referer' => $ENV{'HTTP_REFERER'}); $req->header('Content-Length' => $ENV{'CONTENT_LENGTH'}); $ua->agent($ENV{'HTTP_USER_AGENT'}); # Get the POSTed input my $input = join('',); # This is an HTML form $req->content_type('application/x-www-form-urlencoded'); # Give the request object the POSTed input $req->content($input); # Pass request to the user agent and get a response back my ($code, $msg, $headers, $content); my $res = new HTTP::Response $code, $msg, $headers, $content; $res = $ua->request($req); # Send the headers back to the browser print $res->headers_as_string(), "\n"; # Send the content of the HTTP response back to the browser my $content = $res->content(); # NOTE: you may need to rewrite part of it, especially if it # contains a form that refers back to itself. # $content =~ s#whatever#something else#; print $content; # Done! exit 0;