Jeremy Smith's blog

Entry Is Labelled

Cross Server SSIs

Here is my stab at it, Grayden. I restricted proxying to an explicitly allowed set of hosts, made printing the resultant headers from the request optional, and enabled caching.

#!/usr/bin/perl
use warnings;
use strict;
use CGI qw(:standard);
use LWP::UserAgent;
use URI;

###########################################################
## This work is licensed under the                       ##
## Creative Commons Attribution License.                 ##
## To view a copy of this license, visit                 ##
## http://creativecommons.org/licenses/by/2.0/           ##
## or send a letter to:                                  ##
##     Creative Commons                                  ##
##     559 Nathan Abbott Way                             ##
##     Stanford, California 94305, USA.                  ##
##                                                       ##
## Essentially this means you can do whatever            ##
## you want with the code as long as you credit          ##
## Grayden MacLennan (grayden.maclennan@case.edu)        ##
## as the original author.                               ##
##                                                       ##
## This document was last modified on September 21, 2005 ##
###########################################################


######
# httpgrabber.cgi
# by Grayden MacLennan
# grayden.maclennan@case.edu
#
# 2005-09-21
#
# This is a VERY simple program that grabs content from any
# http-accessible source and spits it out again.
#
# The original motivation behind this program was to allow
# me to do Server Side Includes (SSI) on one server while the
# included content sits on another erver.  Normally, SSI only
# works within a local file system, so this program in effect
# gives a LOCAL path to a REMOTE resource.
#
#
# --Example of usage--
#
#  What you'd do for a normal local file:
#
#    <!--#include virtual="/somepath/includefile.inc" -->
#
#  What you'd LOVE to do but can't:
#
#    <!--#include virtual="http://remote.server.com/somepath/includefile.inc" -->
#
#  What you do to get around the problem:
#
#    <!--#include virtual="/cgi-bin/httpgrabber.cgi?url=http://remote.server.com/somepath/includefile.inc" -->
#
#  To not print out the headers of the request
#
#    <!--#include virtual="/cgi-bin/httpgrabber.cgi?url=http://remote.server.com/somepath/includefile.inc&header=no" -->
#
######

my @allowedHosts = qw(
                       blog.case.edu
                       www.case.edu
                     );

my $uri = URI->new(param("url"))->canonical 
    or die "URL parameter was not a well formed URL and could not be parsed";
my $ua = LWP::UserAgent->new;
my $host = $uri->host;
die $uri->host, " is not an allowed host to pull content from\n"
    unless grep /^$host$/, @allowedHosts;

my $mirrorFile = $uri->host .  join '.', $uri->path_segments, 'cache';
my $resp = $ua->mirror($uri, $mirrorFile);

die $resp->status_line if(!$resp->is_success && $resp->code != 304);

my $printHeader = param("header");
print $resp->{_headers}->as_string unless($printHeader && $printHeader eq "no");

{
    open CACHE, $mirrorFile or die "Could not open cache file because $!";
    undef $/;
    print <CACHE>;
    close CACHE;
}

Comments

  1. gravatar

    This is completely unrelated to the main topic of discussion, but how did you get your code chunk to land in its own scrollable box? I don't see any peculiar markup in its viscinity...

  2. gravatar
    how did you get your code chunk to land in its own scrollable box?
    pre {
        border: solid 1px black;
        color: black;
        background-color: silver;
        margin-top: 10px;
        margin-bottom: 10px;
        margin-left: 30px;
        margin-right: 50px;
        padding: 1em;
        overflow: auto;
        width: auto;
        width:expression(document.body.clientWidth > 450? "450px": "auto" );
    }
    
    

    .code, code
    {
    font-size: 8pt;
    font-family: Courier;
    color: black;
    }