cgi_path.html revision 25503838e438bb909e3ff880125732c7ed5e64ad
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce<TITLE>PATH_INFO Changes in the CGI Environment</TITLE>
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce BGCOLOR="#FFFFFF"
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce TEXT="#000000"
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce LINK="#0000FF"
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce VLINK="#000080"
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce ALINK="#FF0000"
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce<!--#include virtual="header.html" -->
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce<H1 ALIGN="CENTER">PATH_INFO Changes in the CGI Environment</H1>
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorce<P>As implemented in Apache 1.1.1 and earlier versions, the method
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo SorceApache used to create PATH_INFO in the CGI environment was
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcecounterintuitive, and could result in crashes in certain cases. In
a9d46b86993ee8d87fddf0ba50665c0b1b78ebb7Simo SorceApache 1.2 and beyond, this behavior has changed. Although this
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorceresults in some compatibility problems with certain legacy CGI
1dd679584241a0f9b29072c7eed1c5c5e4a577e4Simo Sorceapplications, the Apache 1.2 behavior is still compatible with the
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo SorceCGI/1.1 specification, and CGI scripts can be easily modified (<A
efc65e78fa4e01e6cecc8690a9899af61213be62Fabiano Fidêncio<P>Apache 1.1.1 and earlier implemented the PATH_INFO and SCRIPT_NAME
65a38b8c9cabde6c46cc0e9868f54cb9bb10afbfFabiano Fidêncioenvironment variables by looking at the filename, not the URL. While
7171a7584dda534dde5409f3e7f4657e845ece15Fabiano Fidênciothis resulted in the correct values in many cases, when the filesystem
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcepath was overloaded to contain path information, it could result in
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceerrant behavior. For example, if the following appeared in a config
f35f4e4c8bd5b834504c0554552d78db3624706aFabiano Fidêncio Alias /cgi-ralph /usr/local/httpd/cgi-bin/user.cgi/ralph
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce<P>In this case, <CODE>user.cgi</CODE> is the CGI script, the "/ralph"
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceis information to be passed onto the CGI. If this configuration was in
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceplace, and a request came for "<CODE>/cgi-ralph/script/</CODE>", the
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozekcode would set PATH_INFO to "<CODE>/ralph/script</CODE>", and
7128fadade544efcd86b113a5090b00d20993671Jakub HrozekSCRIPT_NAME to "<CODE>/cgi-</CODE>". Obviously, the latter is
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceincorrect. In certain cases, this could even cause the server to
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce<P>Apache 1.2 and later now determine SCRIPT_NAME and PATH_INFO by
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozeklooking directly at the URL, and determining how much of the URL is
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozekclient-modifiable, and setting PATH_INFO to it. To use the above
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozekexample, PATH_INFO would be set to "<CODE>/script</CODE>", and
7128fadade544efcd86b113a5090b00d20993671Jakub HrozekSCRIPT_NAME to "<CODE>/cgi-ralph</CODE>". This makes sense and results
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcein no server behavior problems. It also permits the script to be
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceguaranteed that
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce"<CODE>http://$SERVER_NAME:$SERVER_PORT$SCRIPT_NAME$PATH_INFO</CODE>"
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcewill always be an accessible URL that points to the current script,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcesomething which was not necessarily true with previous versions of
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozekinformation from the <CODE>Alias</CODE> directive is lost. This is
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozekunfortunate, but we feel that using the filesystem to pass along this
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozeksort of information is not a recommended method, and a script making
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceuse of it "deserves" not to work. Apache 1.2b3 and later, however, do
7128fadade544efcd86b113a5090b00d20993671Jakub Hrozek<H2><A name="compat">Compatibility with Previous Servers</A></H2>
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce<P>It may be necessary for a script that was designed for earlier
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceversions of Apache or other servers to need the information that the
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceold PATH_INFO variable provided. For this purpose, Apache 1.2 (1.2b3
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceand later) sets an additional variable, FILEPATH_INFO. This
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceenvironment variable contains the value that PATH_INFO would have had
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcewith Apache 1.1.1.</P>
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce<P>A script that wishes to work with both Apache 1.2 and earlier
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceversions can simply test for the existence of FILEPATH_INFO, and use
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceit if available. Otherwise, it can use PATH_INFO. For example, in
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo SorcePerl, one might use:
f35f4e4c8bd5b834504c0554552d78db3624706aFabiano Fidêncio $path_info = $ENV{'FILEPATH_INFO'} || $ENV{'PATH_INFO'};
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce<P>By doing this, a script can work with all servers supporting the
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo SorceCGI/1.1 specification, including all versions of Apache.</P>
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce<!--#include virtual="footer.html" -->