20070610 perl redirect
From usenix.org.uk
Here's a snippet of code to HTTP proxy. I've seen a lot of scripts around that make a proxy interface for web, but none that do the job silently. It is a simple script, but could save a little time when requiring the functionality.
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
# Copyright (C) 2007 Ed Neville
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# or visit http://www.gnu.org/licenses/gpl.txt
#
# 20070510 ewn redirect.pl script
# this script just gets a page off a server. it does not
# rewrite any urls, if the urls are not relative then they
# will be written out as read in.
# other methods of redirect would include frameset.
# this script would ideally provide https.
#
# example usage
#
# <VirtualHost *>
# ServerName www.example.com
# SuexecUserGroup ed ed
# ScriptAlias /cgi-bin /var/www/sites/www.example.com/cgi-bin
# suPHP_Engine on
# suPHP_ConfigPath /etc/php4/apache
# AddHandler x-httpd-php .php
# <Directory "/cgi-bin">
# Options +ExecCGI
# </Directory>
#
# ErrorDocument 404 /cgi-bin/redirect.pl
# CustomLog /var/www/sites/www.example.com/log/web.log combined
# ErrorLog /var/www/sites/www.example.com/log/error.log
# </VirtualHost>
#
my %config = ( host => 'http://www.google.co.uk' );
sub headers {
print( "Status: 200 OK\r\n" );
print( "Content-type: text/plain\r\n\r\n" );
}
sub agent_headers {
while( <STDIN> ) {
print( $_ );
}
foreach( sort keys( %ENV ) ) {
print( "$_ ${ENV{$_}}\r\n" );
}
}
sub set_config {
$config{'redirect_to'} = $config{'host'} . $ENV{'REQUEST_URI'};
$config{'method'} = $ENV{'REQUEST_METHOD'};
}
sub get_redirect {
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(
$config{'method'} => $config{'redirect_to'} );
if( $config{'method'} eq "POST" ) {
my $data = "";
while( <STDIN> ) {
$data .= $_;
}
$req->content = $data;
}
my $response = $ua->request($req);
print( "Status: " . $response->status_line );
print( "\r\n\r\n" );
print( $response->content );
}
sub main {
set_config();
#headers();
get_redirect();
#agent_headers();
}
main();