From bd70c2ff4fc89ae2e7991009c271c21b79bb1ddd Mon Sep 17 00:00:00 2001 From: Len Kranendonk Date: Tue, 24 Jun 2003 14:04:32 +0000 Subject: [PATCH] Any Content-Type proxying. Caching of proxied content. --- lib/WebGUI/Wobject/HttpProxy.pm | 46 +++++++++++++++++---------- lib/WebGUI/Wobject/HttpProxy/Parse.pm | 7 ++-- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/lib/WebGUI/Wobject/HttpProxy.pm b/lib/WebGUI/Wobject/HttpProxy.pm index 0dbe8e835..f760eb5de 100644 --- a/lib/WebGUI/Wobject/HttpProxy.pm +++ b/lib/WebGUI/Wobject/HttpProxy.pm @@ -23,6 +23,7 @@ use WebGUI::Privilege; use WebGUI::Session; use WebGUI::Wobject; use WebGUI::Wobject::HttpProxy::Parse; +use WebGUI::Cache; our @ISA = qw(WebGUI::Wobject); @@ -126,7 +127,7 @@ sub www_edit { #------------------------------------------------------------------- sub www_view { my (%formdata, @formUpload, $redirect, $response, $header, - $userAgent, $proxiedUrl, $request, $content); + $userAgent, $proxiedUrl, $request, $content, $ttl); my $output = $_[0]->displayTitle; $output .= $_[0]->description; @@ -148,9 +149,15 @@ sub www_view { $redirect=0; - return $output unless ($proxiedUrl ne ""); - - until($redirect == 5) { # We follow max 5 redirects to prevent bouncing/flapping + return $output unless ($proxiedUrl ne ""); + + my $cachedContent = WebGUI::Cache->new($proxiedUrl,"URL"); + my $cachedHeader = WebGUI::Cache->new($proxiedUrl,"HEADER"); + $header = $cachedHeader->get; + $content = $cachedContent->get; + unless ($content && $session{env}{REQUEST_METHOD}=~/GET/i) { + $redirect=0; + until($redirect == 5) { # We follow max 5 redirects to prevent bouncing/flapping $userAgent = new LWP::UserAgent; $userAgent->agent($session{env}{HTTP_USER_AGENT}); $userAgent->timeout($_[0]->get("timeout")); @@ -230,7 +237,7 @@ sub www_view { if($response->is_success) { $content = $response->content; - + $header = $response->content_type; if($response->content_type eq "text/html" || ($response->content_type eq "" && $content=~/get("filterHtml")); } - } elsif ($response->content_type eq "text/plain") { - $content = '
'.HTML::Entities::encode($response->content).'
'; - } elsif ($response->content_type =~ /image\//i) { - $content = '

'; - } elsif ($response->content_type ne "") { # content_type we don't know about - $content = "

Can't proxy \"".($response->content_type)."\" content.

- Try Fetching it directly here."; - } else { - $content = "

The request didn't return any data.

- Try Fetching it directly here."; - } + } } else { # Fetching page failed... $content = "Getting $proxiedUrl failed". "

GET status line: ".$response->status_line.""; } - return $output.$content; + if ($session{user}{userId} == 1) { + $ttl = $session{page}{cacheTimeoutVisitor}; + } else { + $ttl = $session{page}{cacheTimeout}; + } + + $cachedContent->set($content,$ttl); + $cachedHeader->set($header,$ttl); + } + + if($header ne "text/html") { + $session{header}{mimetype} = $header; + return $content; + } else { + return $output.$content; + } } 1; diff --git a/lib/WebGUI/Wobject/HttpProxy/Parse.pm b/lib/WebGUI/Wobject/HttpProxy/Parse.pm index 272e2a613..f800322db 100644 --- a/lib/WebGUI/Wobject/HttpProxy/Parse.pm +++ b/lib/WebGUI/Wobject/HttpProxy/Parse.pm @@ -27,9 +27,10 @@ my %linkElements = # from HTML::Element.pm a => 'href', img => [qw(src lowsrc usemap)], # lowsrc is a Netscape invention form => 'action', -# input => 'src', + input => 'src', 'link' => 'href', # need quoting since link is a perl builtin frame => 'src', + iframe => 'src', applet => 'codebase', area => 'href', script => 'src', @@ -121,14 +122,14 @@ sub start { $val = URI::URL::url($val)->abs($self->{Url},1); # make absolute } if ($val->scheme eq "http") { - if (lc($tag) ne "img" && lc($tag) ne "script" && lc($tag) ne "iframe" && $self->{rewriteUrls}) { # no rewrite for some + if ($self->{rewriteUrls} && lc($tag) ne "iframe") { if (lc($tag) eq "form" && lc($_) eq "action") { # Found FORM ACTION $self->{FormActionIsDefined}=1; $self->{FormAction} = $val; # set FormAction to include hidden field later $val = WebGUI::URL::page; # Form Action returns to us } else { $val = WebGUI::URL::page('proxiedUrl='.WebGUI::URL::escape($val). - '&wid='.$self->{wid}); # return to us + '&wid='.$self->{wid}.'&func=view'); # return to us } } }