Apache-RewritingProxy
view release on metacpan or search on metacpan
----------------------------
revision 1.13
date: 2000/01/08 18:30:12; author: khhaga01; state: Exp; lines: +1 -3
Miscellaneous cleanup. This is pretty much version 0.7 of this guy.
Subclassing works. Cookies work well. Redirects work seemingly
perfectly.
----------------------------
revision 1.12
date: 2000/01/08 18:04:37; author: khhaga01; state: Exp; lines: +51 -20
RewritingProxy can now be subclassed and other handlers written.
See the README file in the eg directory for an attempt at an explanation
of how to do it.
JavaScript is handled better. RewritingProxy gets the right scripts
Regex handling is improved again.
Forms handling works better.
----------------------------
revision 1.11
date: 1999/12/28 20:59:48; author: khhaga01; state: Exp; lines: +12 -3
Referer now acts as it is supposed to.
User-Agent is also sent correctly.
Still some problems getting all cookies from the remote server.
The HTTP::Cookies stuff doesn't seem to be finding all of the cookies
sent to the proxy server.
----------------------------
revision 1.10
date: 1999/12/28 20:37:22; author: khhaga01; state: Exp; lines: +56 -18
Made lots of changes to the texthandler area. I am trying to
find a simple way to enable someone else to subclass RewritingProxy
and provide their own textHandler method to parse the text content
differently. Once that has been accomplished, imageHandler and
linkHandler will naturally follow quite soon.
The convoluted regexes escaping special chars in urls have been
all consolidated into one little subroutine.
Cookies are still written to a text file in an attempt to find out
why not all of them are stored.
REFERER is not being proxied properly. REFERER is being sent as the
REFERER the client sees. This needs to be fixed soon. Should be fixed before
the next commit.
----------------------------
revision 1.9
date: 1999/12/27 17:04:19; author: khhaga01; state: Exp; lines: +0 -5
Took out the part of the cookie code that named the
cookie nnnnnnnnnnnn where the n's are part of the IP address.
not 100% and is not very elegant. The file locking for the cookie
database file is very primitive.
----------------------------
revision 1.4
date: 1999/11/05 20:13:20; author: khhaga01; state: Exp; lines: +3 -1
*** empty log message ***
----------------------------
revision 1.3
date: 1999/11/05 20:10:28; author: khhaga01; state: Exp; lines: +32 -2
Fixed some of the meta refresh pages. I now see that
tag and rewrite the URL properly. Cookies are still a
major problem. It looks like I may have to build some
sort of cookie database and give the client a cookie poining
to his/her entry in that database. Looks like the safest way to go.
----------------------------
revision 1.2
date: 1999/11/04 20:34:39; author: khhaga01; state: Exp; lines: +5 -0
Added the version number and some other silliness I didn't know about
real perl modules. Fixed Makefile.PL to actually check for the necessary
modules.
----------------------------
RewritingProxy.pm view on Meta::CPAN
package Apache::RewritingProxy;
use strict;
use Apache::Constants qw(:common);
use vars '$req';
use vars '$res';
use vars '$proxiedCookieJar';
use vars '$replayCookies';
use vars '$serverCookies';
use vars '$jar';
use vars '$textHandlerSub';
use vars qw($VERSION @ISA @EXPORT);
$|=1;
$VERSION = '0.7';
# use DynaLoader ();
# @ISA = qw(DynaLoader Exporter);
RewritingProxy.pm view on Meta::CPAN
sub fetchURL
{
# This is the guy who actually grabs the page and then parses it.
# My goal is to find all of the links made in the urlToFetch
# and rewrite them to be absolute links passing through this module
# again.
use Apache::Util qw(:all);
use LWP::UserAgent;
use HTML::TokeParser;
use HTTP::Cookies;
use CGI;
my $r = shift;
my $url = shift;
my $ua = new LWP::UserAgent;
# As we form the request to go to the remote server,
# We should stuff any cookies that might be relavant
# into the request. We need to use the Table class
# here to fetch the cookies and see what cookies
# apply to $url. We then sent those cookies
# in the request after yanking out our own URL from
# the cookies.
# Fetch a cookie named RewritingProxy from the client.
my $cookieKey;
my $clientCookies = $r->header_in('Cookie');
my @clientCookiePairs = split (/; /, $clientCookies);
my $thisClientCookiePair;
foreach $thisClientCookiePair (@clientCookiePairs)
{
my ($name,$value) = split (/=/, $thisClientCookiePair);
$cookieKey = $value if ($name eq "RewritingProxyCookieJar");
}
# Set the cookie to be the client's current IP (doesn' really matter).
# Set the cookie to expire in 6 months.
# TODO: Make this thing refresh if a client keeps using the proxy.
if (!$cookieKey)
{
$cookieKey = $r->get_remote_host();
my $cookieString = "RewritingProxyCookieJar=$cookieKey; expires=".
ht_time(time+518400). "; path=/; domain=".$r->get_server_name;
$r->header_out('Set-Cookie'=>$cookieString);
}
# We now need to open the User's cookie jar and see if any cookies
# need to be sent to this particular server.
$jar = "$Apache::RewritingProxy::cacheRoot/cookies/$cookieKey";
$serverCookies = HTTP::Cookies->new(
File => "$jar",
ignore_discard=>1,
AutoSave=>1);
# Load the cookies into memory...
$serverCookies->load() if (-e $jar);
# Let's take care of Referer also.
my $referer = $r->header_in('Referer');
my $script_name = $r->location;
$referer =~ s/(.*$script_name\/)//i;
# Let's carry the User Agent to the server also.
# TODO: We need to include the proxied via header here.
my $browser = $r->header_in('User-Agent');
$ua->agent($browser);
# We have to append the query string since it got munged by
# apache when this was first requested.
my $rurl = $url;
$rurl .= "?". $r->args if ($r->args && $url !~ /\?/);
if ($r->method eq 'GET')
{
$req = new HTTP::Request 'GET' => "$rurl";
$req->header('Referer'=>"$referer");
$serverCookies->add_cookie_header($req);
# This needs to be a simple request or else the redirects will
# not work very nicely. LWP is too smart sometimes.
$res = $ua->simple_request($req);
}
elsif ($r->method eq 'POST')
{
# This is a little bit of tricky ju ju here.
# We will use another PERLy package to
# prepare the URL and pack in the encoded form data.
use URI::URL;
my %FORM;
$req = new HTTP::Request 'POST' => "$rurl";
$req->header('Referer'=>"$referer");
$req->content_type('application/x-www-form-urlencoded');
$serverCookies->add_cookie_header($req);
# $req->content('$buffer');
my $pair;
my @pairs = split (/&/, $r->content);
# TODO: This next bit more efficiently.
# It works for the occasional cgi, but not for constant
# hammering away at this code. There has to be a better
# and more OOP way.
foreach $pair (@pairs)
{
my ($name, $value) = split (/=/, $pair);
RewritingProxy.pm view on Meta::CPAN
#Old cookie jar...
# TODO: Make this much much better. We still need to lock the cookie
# jar to keep simultaneous requests from killing each others' changes.
while (-e "$Apache::RewritingProxy::cacheRoot/cookies/$cookieKey.lock")
{sleep(1);}
open (LOCK, ">$Apache::RewritingProxy::cacheRoot/cookies/$cookieKey.lock");
print LOCK " ";
close LOCK;
# New cookies sent by the server...
my $responseCookies = HTTP::Cookies->new;
$responseCookies->extract_cookies($res);
$responseCookies->scan(\&storeCookies);
# Store the old plus the new...
# $proxiedCookieJar->save();
unlink ("$Apache::RewritingProxy::cacheRoot/cookies/$cookieKey.lock");
sub storeCookies
{
my $version = shift;
my $key = shift;
my $val = shift;
my $path = shift;
my $domain = shift;
my $port = shift;
my $path_spec = shift;
my $secure = shift;
my $expires = shift;
my $discard = shift;
my $hash = shift;
# if (!$expires )
# {
# $expires = ht_time(time+3600, '%Y-%m-%d %H:%M:%S',0);
# }
my $proxiedCookieJar = HTTP::Cookies->new(
File => "$jar",
ignore_discard=>1,
AutoSave=>1);
$proxiedCookieJar->load();
$proxiedCookieJar->set_cookie($version,$key,$val,$path,
$domain,$port,$path_spec,$secure,$expires);
$proxiedCookieJar->save();
}
if ($res->code =~ /^3/)
{
# This means it was a server redirect.
# We should process the headers and insert
# ourselves into the headers everywhere we need to.
my $textHeaders = $res->headers_as_string;
( run in 0.474 second using v1.01-cache-2.11-cpan-4e96b696675 )