Search code examples
javascriptcommonjsringojs

What's the difference between scriptName and pathInfo in JSGI/Level0/A/Draft2?


I'm using RingoJS 0.8, and I created a basic jsgi application on top of a ringo/httpserver instance. I'm trying to understand the purpose of each key in a jsgi request object according to the spec: http://wiki.commonjs.org/wiki/JSGI/Level0/A/Draft2; and I understood most of the keys, save the scriptName key.

When I run my application and print the values of each key, scriptName always returns an empty string while pathInfo returns the pathname of the URL. I'm trying to understand what scriptName's actually supposed to convey and how's it's intended to be different from pathInfo.


Solution

  • scriptName and pathInfo are best understood when taking the historic background of CGI into account, as that's where those fields stem from.

    With CGI scripts, you have a server-side directory structure, which contains a CGI script in some location. For CGIs, the job of the web server is to map the URLs in HTTP requests to CGI scripts. Now this mapping process basically splits the path in the URL into components, and then tries (starting at a root directory) to map each component to a directory name or a filename. Once a component is matched to a filename, the CGI script to execute is found. All URL path components leading up to and including the filename are then passed as the "script name" to the CGI, remaining (unmatched) URL path compoments are passed to the CGI script as "path info".

    Let's illustrate this abstract description with an example. Let's assume you have a CGI script stored in your filesystem at /x/y/foo/bar.cgi, where /x/y/ is the root directory used by your webserver to map requests to. If a request comes in for the URL /foo/bar.cgi/baz/qux, the webserver will match this rquest to the /x/y/foo/bar.cgi CGI script, passing /foo/bar.cgi as "script name" and the remaining /baz/qux as "path info" to this CGI script.

    However, in JSGI it's basically up to the server and potential middleware how the mapping of URLs to JSGI apps is done. There might be an analogue file system-based lookup mechanism at work, where splitting the original request's path into "script name" and "path info" still makes sense. Or there might not be.

    If you run atop Ringo's bare ringo/httpserver, no script name mapping is done at all, as your app is always considered to be running at the root (i.e. corresponding to the / URL path). If your main app delegates to other JSGI apps, it's then up for you to decide (and document) if and how you set scriptName and pathInfo.