growlf has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.  Time for obscure question #23...

Lets say I have a need (albeit strange, perhaps) for a CGI application to act as an "in-line" path.  ie:

http://mydomain.com/MyCGI/parameter1/paremeter2.htm

Where MyCGI is the actual CGI script, and parameter1& 2 are exactly that.  The webserver is intended to be Apache 1.3 but may also be MS IIS at some point.  Is there a way to capture those parameters using CGI.pm or other module?  Will I have to set up the CGI as a "handler" in the webserver (I do not wish to do that, but it if MUST - oh well, so be it).

I have tried the following:

None of theses seem to return the "pseudo" querystring or my errant parameters.



*G*

Replies are listed 'Best First'.
Re: CGI as an Inline Path
by dws (Chancellor) on Mar 15, 2002 at 06:01 UTC
    You'll find that $ENV{'PATH_INFO'} holds what you're looking for.
      ++dws, and note that contrary to comments below, mod_rewrite or other trickery is not necessary.

      It sounds like you've already figured out how to get the script itself running even though it's not in the CGI directory -- but if not, Apache's ScriptAlias or ExecCGI directives should do the job. And of course Apache respects the script's shebang line even on Windows, so you don't need to worry about having a .cgi or .pl extension or anything like that.

      I couldn't tell you whether this would work with IIS, though.

        I couldn't tell you whether this would work with IIS, though.

        IIS does have some peculiarities in it's handling of PATH_INFO that you should be aware of, but there's a knowledge base article on the subject that may be of interest.

        Update: If you use CGI.pm's path_info instead of $ENV{'PATH_INFO'}, it has a hack that'll return what you expect regardless of how IIS is configured.

        sub path_info { # .... # hack to fix broken path info in IIS $self->{'.path_info'} =~ s/^\Q$ENV{'SCRIPT_NAME'}\E// if $IIS; }

            --k.


        Thank you - and yes, getting it to use an arbitrary CGI was an easy task without the handler code. My only issue was getting the darned query portion - which (*DOH!* thanks dws!! I really should sleep more *sigh* ) I was reminded from earlier.

        As fas as IIS goes - yes it is easy to set up a handler in it as well (just add one in 'site properties | ISAPI Filters').

        To do a simple passthrough on an Unix/Apache server I simply (as you suggested) put in a script alias for my cgi app in my appache configration file. ie

        ScriptAlias /MyCGI/ "/usr/local/www/cgi-bin/sitescript"



        *G*

      You'll find that $ENV{'PATH_INFO'} holds what you're looking for.

      And %ENV has more interesting information, so dumping it is not a bad idea:

      #!/usr/bin/perl -w use Data::Dumper; print "\n"; # The shortest CGI header possible, # the server will assume text/plain. print Dumper \%ENV;
      Or, without Data::Dumper:
      #!/usr/bin/perl -w print map "\n$_ => $ENV{$_}", keys %ENV;

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: CGI as an Inline Path
by abstracts (Hermit) on Mar 15, 2002 at 06:13 UTC
    If I'm getting the question right, you have a cgi application on say http://www.mydomain.com/mycgi.cgi. Instead of passing parameters the usual way (ie http://blabla/mycgi.cgi?param1=value1&param2=value2), you want to pass them by using http://blabla/mycgi/value1/value2.

    If this is what you need, then this is really an Apache question and what you need to use is mod_rewrite. The docs are at URL Rewriting Guide. Here is a glimpse of what you need to add to httpd.conf or .htaccess:

    RewriteEngine on RewriteRule ^/mycgi/([^/]+)/([^/]+)\.htm$ /mycgi.cgi?param1=$1&p +aram2=$2 [R]
    I have not tested this, but it should not be far. And I don't know about IIS.

    Hope this helps

    Aziz,,,

Re: CGI as an Inline Path
by grep (Monsignor) on Mar 15, 2002 at 06:07 UTC
    To actually have the name of your cgi script inline like that, you'll most likely have to go to Apache's mod_rewite

    Sorry not a perl answer but, I can't think of a perl solution :(

    grep
    grep> cd /pub
    grep> more beer