#!/usr/bin/q -c exit cgi_main /* $Id: myreq.q,v 1.2 2004/01/19 23:15:24 agraef Exp $ */ /* This is a little Q-CGI script which creates a page showing some information about the current request. It gathers the information from the appropriate environment variables and retrieves the request body from standard input. This works in essentially the same way no matter how the script is invoked, i.e., whether it is run through the Common Gateway Interface or directly through the Q Apache module. To try this script, copy it to a place where Apache can execute it, fire up your browser and type in the script URL (something like http://localhost/ cgi-bin/myreq.q, depending on where you put the script). You should then see a page titled "Your Request". If not, then probably you installed the script in the wrong place, or you first have to configure Apache for script execution as explained in the Apache manual. The script is useful for debugging your HTML forms, too. To these ends, just use the script URL as the ACTION field of the form, then you can see exactly which query or request body is transmitted to the server. */ /* FIXME: Are these variables documented somewhere? I compiled the following list from the API docs and traces of the subprocess_env field, so there might be some stuff missing in this list. */ def VARS = [// CGI vars: "GATEWAY_INTERFACE", "PATH_INFO", "PATH_TRANSLATED", "QUERY_STRING", "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_NAME", "SERVER_PROTOCOL", // Common vars: "CONTENT_TYPE", "CONTENT_LENGTH", "HTTP_HOST", "HTTP_USER_AGENT", "HTTP_ACCEPT", "HTTP_ACCEPT_LANGUAGE", "HTTP_ACCEPT_ENCODING", "HTTP_ACCEPT_CHARSET", "HTTP_KEEP_ALIVE", "HTTP_CONNECTION", "HTTP_CACHE_CONTROL", // "SERVER_SIGNATURE", // only available when mod_q is used? "SERVER_SOFTWARE", "SERVER_NAME", "SERVER_ADDR", "SERVER_PORT", "SERVER_ADMIN", "REMOTE_HOST", "REMOTE_ADDR", "REMOTE_PORT", "REMOTE_USER", "REMOTE_IDENT", "PATH", "DOCUMENT_ROOT", "SCRIPT_FILENAME", // Only set if the request is the result of an error redirect: "REDIRECT_QUERY_STRING", "REDIRECT_URL", // Only set on Winblows: "SystemRoot", "COMSPEC", "WINDIR"], VALS = map getenv VARS; /* Templates for the header and body of the HTML page sent in the response. */ def HEADER = "Content-Type: text/html\n\n\ Your Request

\n\ Served to you by %s on %s %s.

\n", BEGIN_TABLE = "\n", TABLE_ROW = "\n", END_TABLE = "
%s
%s
\n"; /* Select the (VAR,VAL) pairs which are actually defined in the environment. */ ok (VAR,VAL) = isstr VAL and then not null VAL; /* The `cgi_main' function. It generates the result page on OUTPUT (preceded by the appropriate header), and may return a HTTP result code (such as 0 = ok, 404 = not found, 500 = internal server error, etc.) which will be returned to the server. Note that the name of the function may actually be anything when running under CGI (provided you change the shebang at the beginning of the script accordingly), but the Q Apache module expects it to be `cgi_main'. */ /* We use a quick hack here to return ourselves as text/plain if we are invoked with the `source' arg. */ cgi_main = printf "Content-Type: text/plain\n\n%s" (fget (fopen SCRIPT "r")) || 0 where SCRIPT = getenv "SCRIPT_FILENAME" if QUERY = "source" where QUERY:String = getenv "QUERY_STRING"; /* The real stuff starts here: compose a page from the environment and the request body in INPUT. */ cgi_main = printf HEADER (SCRIPT,SELF,TIME,TZ) || writes "Environment

\n" || writes BEGIN_TABLE || do (printf TABLE_ROW) (filter ok (zip VARS VALS)) || writes END_TABLE || ifelse (null BODY) () (printf "

Request Body

\n

%s\n
" (join "
\n" (split "\n" BODY))) || 0 where SCRIPT = getenv "SCRIPT_NAME", SERVER = getenv "HTTP_HOST", SELF = "http://"++SERVER++SCRIPT, TIME = strftime "%c" (localtime time), TZ = tzname!0, BODY = fget INPUT;