cgi_path.html revision f9b3be308809978f797e0c57b296147532a4313c
fa9e4066f08beec538e775443c5be79dd423fcabahrens<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
fa9e4066f08beec538e775443c5be79dd423fcabahrens<HTML><HEAD>
fa9e4066f08beec538e775443c5be79dd423fcabahrens<TITLE>PATH_INFO Changes in the CGI Environment</TITLE>
fa9e4066f08beec538e775443c5be79dd423fcabahrens</HEAD>
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
fa9e4066f08beec538e775443c5be79dd423fcabahrens<BODY
fa9e4066f08beec538e775443c5be79dd423fcabahrens BGCOLOR="#FFFFFF"
fa9e4066f08beec538e775443c5be79dd423fcabahrens TEXT="#000000"
fa9e4066f08beec538e775443c5be79dd423fcabahrens LINK="#0000FF"
fa9e4066f08beec538e775443c5be79dd423fcabahrens VLINK="#000080"
fa9e4066f08beec538e775443c5be79dd423fcabahrens ALINK="#FF0000"
fa9e4066f08beec538e775443c5be79dd423fcabahrens>
fa9e4066f08beec538e775443c5be79dd423fcabahrens<!--#include virtual="header.html" -->
fa9e4066f08beec538e775443c5be79dd423fcabahrens<h1 ALIGN="CENTER">PATH_INFO Changes in the CGI Environment</H1>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<HR>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<h2><A name="over">Overview</A></h2>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>As implemented in Apache 1.1.1 and earlier versions, the method
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockApache used to create PATH_INFO in the CGI environment was
fa9e4066f08beec538e775443c5be79dd423fcabahrenscounterintuitive, and could result in crashes in certain cases. In
fa9e4066f08beec538e775443c5be79dd423fcabahrensApache 1.2 and beyond, this behavior has changed. Although this
fa9e4066f08beec538e775443c5be79dd423fcabahrensresults in some compatibility problems with certain legacy CGI
fa9e4066f08beec538e775443c5be79dd423fcabahrensapplications, the Apache 1.2 behavior is still compatible with the
fa9e4066f08beec538e775443c5be79dd423fcabahrensCGI/1.1 specification, and CGI scripts can be easily modified (<A
fa9e4066f08beec538e775443c5be79dd423fcabahrensHREF="#compat">see below</A>).
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<h2><A name="prob">The Problem</A></h2>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>Apache 1.1.1 and earlier implemented the PATH_INFO and SCRIPT_NAME
fa9e4066f08beec538e775443c5be79dd423fcabahrensenvironment variables by looking at the filename, not the URL. While
fa9e4066f08beec538e775443c5be79dd423fcabahrensthis resulted in the correct values in many cases, when the filesystem
fa9e4066f08beec538e775443c5be79dd423fcabahrenspath was overloaded to contain path information, it could result in
fa9e4066f08beec538e775443c5be79dd423fcabahrenserrant behavior. For example, if the following appeared in a config
fa9e4066f08beec538e775443c5be79dd423fcabahrensfile:
fa9e4066f08beec538e775443c5be79dd423fcabahrens<PRE>
fa9e4066f08beec538e775443c5be79dd423fcabahrens Alias /cgi-ralph /usr/local/httpd/cgi-bin/user.cgi/ralph
fa9e4066f08beec538e775443c5be79dd423fcabahrens</PRE>
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>In this case, <CODE>user.cgi</CODE> is the CGI script, the "/ralph"
fa9e4066f08beec538e775443c5be79dd423fcabahrensis information to be passed onto the CGI. If this configuration was in
fa9e4066f08beec538e775443c5be79dd423fcabahrensplace, and a request came for "<CODE>/cgi-ralph/script/</CODE>", the
fa9e4066f08beec538e775443c5be79dd423fcabahrenscode would set PATH_INFO to "<CODE>/ralph/script</CODE>", and
fa9e4066f08beec538e775443c5be79dd423fcabahrensSCRIPT_NAME to "<CODE>/cgi-</CODE>". Obviously, the latter is
fa9e4066f08beec538e775443c5be79dd423fcabahrensincorrect. In certain cases, this could even cause the server to
fa9e4066f08beec538e775443c5be79dd423fcabahrenscrash.</P>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<h2><A name="solution">The Solution</A></h2>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>Apache 1.2 and later now determine SCRIPT_NAME and PATH_INFO by
fa9e4066f08beec538e775443c5be79dd423fcabahrenslooking directly at the URL, and determining how much of the URL is
fa9e4066f08beec538e775443c5be79dd423fcabahrensclient-modifiable, and setting PATH_INFO to it. To use the above
fa9e4066f08beec538e775443c5be79dd423fcabahrensexample, PATH_INFO would be set to "<CODE>/script</CODE>", and
fa9e4066f08beec538e775443c5be79dd423fcabahrensSCRIPT_NAME to "<CODE>/cgi-ralph</CODE>". This makes sense and results
fa9e4066f08beec538e775443c5be79dd423fcabahrensin no server behavior problems. It also permits the script to be
fa9e4066f08beec538e775443c5be79dd423fcabahrensguaranteed that
fa9e4066f08beec538e775443c5be79dd423fcabahrens"<CODE>http://$SERVER_NAME:$SERVER_PORT$SCRIPT_NAME$PATH_INFO</CODE>"
fa9e4066f08beec538e775443c5be79dd423fcabahrenswill always be an accessible URL that points to the current script,
fa9e4066f08beec538e775443c5be79dd423fcabahrenssomething which was not necessarily true with previous versions of
fa9e4066f08beec538e775443c5be79dd423fcabahrensApache.
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>However, the "<CODE>/ralph</CODE>"
fa9e4066f08beec538e775443c5be79dd423fcabahrensinformation from the <CODE>Alias</CODE> directive is lost. This is
fa9e4066f08beec538e775443c5be79dd423fcabahrensunfortunate, but we feel that using the filesystem to pass along this
fa9e4066f08beec538e775443c5be79dd423fcabahrenssort of information is not a recommended method, and a script making
fa9e4066f08beec538e775443c5be79dd423fcabahrensuse of it "deserves" not to work. Apache 1.2b3 and later, however, do
fa9e4066f08beec538e775443c5be79dd423fcabahrensprovide <A HREF="#compat">a workaround.</A>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<h2><A name="compat">Compatibility with Previous Servers</A></h2>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>It may be necessary for a script that was designed for earlier
fa9e4066f08beec538e775443c5be79dd423fcabahrensversions of Apache or other servers to need the information that the
fa9e4066f08beec538e775443c5be79dd423fcabahrensold PATH_INFO variable provided. For this purpose, Apache 1.2 (1.2b3
fa9e4066f08beec538e775443c5be79dd423fcabahrensand later) sets an additional variable, FILEPATH_INFO. This
fa9e4066f08beec538e775443c5be79dd423fcabahrensenvironment variable contains the value that PATH_INFO would have had
fa9e4066f08beec538e775443c5be79dd423fcabahrenswith Apache 1.1.1.</P>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>A script that wishes to work with both Apache 1.2 and earlier
fa9e4066f08beec538e775443c5be79dd423fcabahrensversions can simply test for the existence of FILEPATH_INFO, and use
fa9e4066f08beec538e775443c5be79dd423fcabahrensit if available. Otherwise, it can use PATH_INFO. For example, in
fa9e4066f08beec538e775443c5be79dd423fcabahrensPerl, one might use:
fa9e4066f08beec538e775443c5be79dd423fcabahrens<PRE>
fa9e4066f08beec538e775443c5be79dd423fcabahrens $path_info = $ENV{'FILEPATH_INFO'} || $ENV{'PATH_INFO'};
fa9e4066f08beec538e775443c5be79dd423fcabahrens</PRE>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<P>By doing this, a script can work with all servers supporting the
fa9e4066f08beec538e775443c5be79dd423fcabahrensCGI/1.1 specification, including all versions of Apache.</P>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens<!--#include virtual="footer.html" -->
fa9e4066f08beec538e775443c5be79dd423fcabahrens</BODY>
fa9e4066f08beec538e775443c5be79dd423fcabahrens</HTML>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens