2
votes

I am trying to wrap my head around some CGI scripting (the embedded box I'm on doesn't quite like PHP) and I have a question about how CGI perl scripts interact with apache and some HTML sites on it.

Basically, I need part of the HTML file to load based on a config file outside of the document root. Obviously, this is where CGI steps in. Now, the problem is that CGI scripts also shouldn't be in your doc root, so I can't just put the script in my doc root and make it fire out HTML. So, I put my script in /var/www/cgi/, and pointed apache to it.

That part runs nicely, and it fires out an HTML page like it should. However, the original site has some resources in its doc root (css files, java applets, images, etc), that my new CGI-made HTML doesn't have access to. So, what would be the best way to make the HTML page "build itself" off the CGI script? I have read a few things on Server side includes, which is one option. There is also the option of putting the CGI output in a <div src="script"></div>, and a couple others. The question is, what is the best way of doing this? I would appreciate any advice. Thanks beforehand!

2
What do you mean by, “doesn't have access to?” If you (e.g.) put in a <img src="/foo.png" />, do you get an error or something?BRPocock
Well, I suppose it would be possible to reference all the resources in that fashion, but then I would have to remake the pages with these new "semi-absolute" filepaths (still relative to doc root, but absolute otherwise). And besides, then I would have to point all my users to something in the CGI folder...SuperTron
I suppose the important question is: what do you need the CGI to do?BRPocock
I'm just reading a piece of a config file (using regex) and putting it in a applet parameter, php would be perfect for it hahaSuperTron

2 Answers

2
votes

Well, Perl might be overkill for something like that (like you said, you can do it with X-SSI), but it sounds like what you might want is to embed the fragment of Perl code into your otherwise-static HTML documents?

If you have mod_include, you can do something like:

   <applet … >
     <!--#exec cgi="/cgi-bin/readconfig.cgi" -->
   </applet>

and just have readconfig.cgi write out a fragment like

   #!/usr/bin/perl -WT

   print "Content-Type: text/html\n\n";

   open my $config, '<', '/foo/bar/baz.qux'
       or die "<!-- can't read baz.qux: $! -->";

   my $line = <$config> until $line =~ /interesting: ([a-z]+[0-9]+)/;
   my $interesting = $1;
   print qq[
          <param value="$interesting" />
   ];

You'll need to enable Options +includes to parse X-SSI, possibly by dropping it in an .htaccess file, depending on your setup…

0
votes

Maybe I'm misunderstanding you, but wouldn't a call to chdir to document root in your Perl code do the trick?