2000-09-26 10:35:26 -04:00
|
|
|
;;; Server support for NCSA's WWW Common Gateway Interface -*- Scheme -*-
|
2002-08-27 05:03:22 -04:00
|
|
|
|
|
|
|
;;; This file is part of the Scheme Untergrund Networking package.
|
|
|
|
|
2000-09-26 10:35:26 -04:00
|
|
|
;;; Copyright (c) 1995 by Olin Shivers.
|
2002-08-27 05:03:22 -04:00
|
|
|
;;; For copyright information, see the file COPYING which comes with
|
|
|
|
;;; the distribution.
|
2000-09-26 10:35:26 -04:00
|
|
|
|
|
|
|
;;; See http://hoohoo.ncsa.uiuc.edu/cgi/interface.html for a sort of "spec".
|
|
|
|
|
|
|
|
;;; PROBLEMS:
|
|
|
|
;;; - The handlers could be made -- closed over their parameters
|
|
|
|
;;; (e.g., root vars, etc.)
|
|
|
|
|
2002-09-22 11:41:41 -04:00
|
|
|
;;; This code provides a request handler for the HTTP server that implements
|
2000-09-26 10:35:26 -04:00
|
|
|
;;; a CGI interface to external programs for doing HTTP transactions.
|
|
|
|
|
|
|
|
;;; About HTML forms
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; This info is in fact independent of CGI, but important to know about,
|
|
|
|
;;; as many CGI scripts are written for responding to forms-entry in
|
|
|
|
;;; HTML browsers.
|
|
|
|
;;;
|
|
|
|
;;; The form's field data are turned into a single string, of the form
|
|
|
|
;;; name=val&name=val
|
|
|
|
;;; where the <name> and <val> parts are URI encoded to hide their
|
|
|
|
;;; &, =, and + chars, among other things. After URI encoding, the
|
|
|
|
;;; space chars are converted to + chars, just for fun. It is important
|
|
|
|
;;; to encode the spaces this way, because the perfectly general %xx escape
|
|
|
|
;;; mechanism might be insufficiently confusing. This variant encoding is
|
|
|
|
;;; called "form-url encoding."
|
|
|
|
;;;
|
|
|
|
;;; If the form's method is POST,
|
|
|
|
;;; Browser sends the form's field data in the entity block, e.g.,
|
|
|
|
;;; "button=on&ans=yes". The request's Content-type: is application/
|
|
|
|
;;; x-www-form-urlencoded, and the request's Content-length: is the
|
|
|
|
;;; number of bytes in the form data.
|
|
|
|
;;;
|
|
|
|
;;; If the form's method is GET,
|
|
|
|
;;; Browser sends the form's field data in the URL's <search> part.
|
|
|
|
;;; (So the server will pass to the CGI script as $QUERY_STRING,
|
|
|
|
;;; and perhaps also on in argv[]).
|
|
|
|
;;;
|
|
|
|
;;; In either case, the data is "form-url encoded" (as described above).
|
|
|
|
|
|
|
|
;;; ISINDEX queries:
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; (Likewise for ISINDEX URL queries from browsers.)
|
|
|
|
;;; Browser url-form encodes the query (see above), which then becomes the
|
|
|
|
;;; ?<search> part of the URI. (Hence the CGI script will split the individual
|
|
|
|
;;; fields into argv[].)
|
|
|
|
|
|
|
|
|
|
|
|
;;; CGI interface:
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; - The URL's <search> part is assigned to env var $QUERY_STRING, undecoded.
|
|
|
|
;;; - If it contains no raw "=" chars, it is split at "+" chars. The
|
|
|
|
;;; substrings are URI decoded, and become the elts of argv[].
|
|
|
|
;;; - The CGI script is run with stdin hooked up to the socket. If it's going
|
|
|
|
;;; to read the entity, it should read $CONTENT_LENGTH bytes worth.
|
|
|
|
;;; - A bunch of env vars are set; see below.
|
2002-08-26 05:59:14 -04:00
|
|
|
;;; - If the script begins with "nph-" its output is the entire response.
|
2000-09-26 10:35:26 -04:00
|
|
|
;;; Otherwise, it replies to the server, we peel off a little header
|
2002-08-26 05:59:14 -04:00
|
|
|
;;; that is used to construct the real header for the response.
|
2002-04-14 12:57:38 -04:00
|
|
|
;;; See the "spec" for further details. (URL above).
|
2000-09-26 10:35:26 -04:00
|
|
|
;;;
|
|
|
|
;;; The "spec" also talks about PUT, but when I tried this on a dummy script,
|
|
|
|
;;; the NSCA httpd server generated buggy output. So I am only implementing
|
|
|
|
;;; the POST and GET ops; any other op generates a "405 Method not allowed"
|
2002-08-26 05:59:14 -04:00
|
|
|
;;; response.
|
2000-09-26 10:35:26 -04:00
|
|
|
|
|
|
|
;;; Parameters
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;;; path for scripts
|
|
|
|
(define cgi-default-bin-path "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin")
|
|
|
|
|
2002-09-22 11:41:41 -04:00
|
|
|
;;; The request handler for CGI scripts. (car path) is the script to run.
|
2002-09-02 09:43:03 -04:00
|
|
|
;;; cgi-bin-path is used, if PATH-variable isn't defined
|
2002-04-25 05:35:18 -04:00
|
|
|
|
|
|
|
(define (cgi-handler bin-dir . maybe-cgi-bin-path)
|
|
|
|
(let-optionals
|
2002-08-29 04:32:39 -04:00
|
|
|
maybe-cgi-bin-path
|
|
|
|
((cgi-bin-path cgi-default-bin-path))
|
|
|
|
|
|
|
|
(let ((request-invariant-cgi-env ; environment variables that never change
|
|
|
|
`(("PATH" . ,(and (getenv "PATH") cgi-bin-path))
|
|
|
|
("SERVER_SOFTWARE" . ,sunet-version-identifier)
|
|
|
|
("SERVER_NAME" . ,(host-info:name (host-info (system-name))))
|
|
|
|
("GATEWAY_INTERFACE" . "CGI/1.1"))))
|
|
|
|
(lambda (path req)
|
|
|
|
(if (pair? path) ; Got to have at least one elt.
|
|
|
|
(compute-cgi path req bin-dir request-invariant-cgi-env)
|
|
|
|
(make-http-error-response http-status/bad-request req "Empty CGI script"))))))
|
2002-08-28 05:54:40 -04:00
|
|
|
|
|
|
|
(define (compute-cgi path req bin-dir request-invariant-cgi-env)
|
|
|
|
(let* ((prog (car path))
|
|
|
|
|
|
|
|
(filename (or (dotdot-check bin-dir (list prog))
|
2002-08-29 04:32:39 -04:00
|
|
|
(http-error http-status/bad-request req
|
|
|
|
"CGI scripts may not contain \"..\" elements.")))
|
2002-08-28 05:54:40 -04:00
|
|
|
|
|
|
|
(nph? (string-prefix? "nph-" prog)) ; PROG starts with "nph-" ?
|
2002-04-25 05:35:18 -04:00
|
|
|
; why did we had (string-suffix? "-nph" prog) here?
|
|
|
|
|
2002-08-28 05:54:40 -04:00
|
|
|
(search (http-url:search (request:url req))) ; Compute the
|
|
|
|
(argv (if (and search (not (string-index search #\=))) ; argv list.
|
|
|
|
(split-and-decode-search-spec search)
|
|
|
|
'()))
|
|
|
|
|
|
|
|
(env (cgi-env req bin-dir (cdr path) request-invariant-cgi-env))
|
|
|
|
|
|
|
|
(doit (lambda ()
|
|
|
|
(dup->inport (current-input-port) 0)
|
|
|
|
(dup->outport (current-output-port) 1)
|
2002-09-04 06:29:12 -04:00
|
|
|
(dup 1 2)
|
2002-08-28 05:54:40 -04:00
|
|
|
(apply exec/env filename env argv))))
|
|
|
|
|
|
|
|
(http-syslog (syslog-level debug) "[cgi-server] search: ~s, argv: ~s~%" search argv)
|
|
|
|
(let ((request-method (request:method req)))
|
|
|
|
(cond
|
|
|
|
((or (string=? request-method "GET")
|
|
|
|
(string=? request-method "POST")) ; Could do others also.
|
|
|
|
(if nph?
|
|
|
|
(let ((stat (wait (fork doit))))
|
|
|
|
(if (not (zero? stat))
|
2002-08-29 04:32:39 -04:00
|
|
|
(make-http-error-response
|
|
|
|
http-status/bad-request req
|
|
|
|
(format #f "Could not execute CGI script ~a."
|
|
|
|
filename))
|
2002-08-28 05:54:40 -04:00
|
|
|
stat)) ;; FIXME! must return http-response object!
|
2002-08-29 10:53:44 -04:00
|
|
|
(case (file-not-executable? filename)
|
|
|
|
((search-denied permission)
|
|
|
|
(make-http-error-response http-status/forbidden req
|
|
|
|
"Permission denied."))
|
|
|
|
((no-directory nonexistent)
|
|
|
|
(make-http-error-response http-status/not-found req
|
|
|
|
"File or directory doesn't exist."))
|
|
|
|
(else
|
2002-09-02 09:43:03 -04:00
|
|
|
(cgi-make-response (run/port* doit) path req)))))
|
2002-08-28 05:54:40 -04:00
|
|
|
|
2002-09-03 08:45:39 -04:00
|
|
|
(else
|
|
|
|
(make-http-error-response http-status/method-not-allowed req request-method))))))
|
2002-08-28 05:54:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
(define (split-and-decode-search-spec s)
|
|
|
|
(let recur ((i 0))
|
|
|
|
(cond
|
2001-08-20 07:31:03 -04:00
|
|
|
((string-index s #\+ i) => (lambda (j) (cons (unescape-uri s i j)
|
2002-08-28 05:54:40 -04:00
|
|
|
(recur (+ j 1)))))
|
2001-08-20 07:31:03 -04:00
|
|
|
(else (list (unescape-uri s i (string-length s)))))))
|
2000-09-26 10:35:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; Compute the CGI scripts' process environment by adding the standard CGI
|
|
|
|
;;; environment var bindings to the current process env -- return result
|
|
|
|
;;; as an alist.
|
|
|
|
;;;
|
|
|
|
;;; You are also supposed to add the headers as env vars in a particular
|
|
|
|
;;; format, but are allowed to bag it if the environment var storage
|
|
|
|
;;; requirements might overload the OS. I don't know what you can rely upon
|
|
|
|
;;; in Unix, so I am just bagging it, period.
|
|
|
|
;;;
|
|
|
|
;;; Suppose the URL is
|
|
|
|
;;; //machine/cgi-bin/test-script/foo/bar?quux%20a+b=c
|
|
|
|
;;; then:
|
2002-08-28 05:54:40 -04:00
|
|
|
;; PATH_INFO -- extra info after the script-name path prefix. "/foo/bar"
|
2000-09-26 10:35:26 -04:00
|
|
|
;;; PATH_TRANSLATED -- non-virtual version of above. "/u/Web/foo/bar/"
|
|
|
|
;;; SCRIPT_NAME virtual path to script "/cgi-bin/test-script"
|
|
|
|
;;; QUERY_STRING -- not decoded "quux%20a+b=c"
|
|
|
|
;;; The first three of these vars are *not* encoded, so information is lost
|
|
|
|
;;; if the URL's path elements contain encoded /'s (%2F). CGI loses.
|
|
|
|
|
2002-04-25 05:35:18 -04:00
|
|
|
(define (cgi-env req bin-dir path-suffix request-invariant-cgi-env)
|
2000-09-26 10:35:26 -04:00
|
|
|
(let* ((sock (request:socket req))
|
|
|
|
(raddr (socket-remote-address sock))
|
|
|
|
|
|
|
|
(headers (request:headers req))
|
|
|
|
|
|
|
|
;; Compute the $PATH_INFO and $PATH_TRANSLATED strings.
|
|
|
|
(path-info (uri-path-list->path path-suffix)) ; No encode or .. check.
|
|
|
|
(path-translated (path-list->file-name path-info bin-dir))
|
|
|
|
|
|
|
|
;; Compute the $SCRIPT_PATH string.
|
|
|
|
(url-path (http-url:path (request:url req)))
|
|
|
|
(script-path (take (- (length url-path) (length path-suffix))
|
|
|
|
url-path))
|
|
|
|
(script-name (uri-path-list->path script-path)))
|
|
|
|
|
|
|
|
(receive (rhost rport)
|
|
|
|
(socket-address->internet-address raddr)
|
|
|
|
(receive (lhost lport)
|
|
|
|
(socket-address->internet-address (socket-local-address sock))
|
|
|
|
|
|
|
|
`(("SERVER_PROTOCOL" . ,(version->string (request:version req)))
|
|
|
|
("SERVER_PORT" . ,(number->string lport))
|
|
|
|
("REQUEST_METHOD" . ,(request:method req))
|
|
|
|
|
|
|
|
("PATH_INFO" . ,path-info)
|
|
|
|
("PATH_TRANSLATED" . ,path-translated)
|
|
|
|
("SCRIPT_NAME" . ,script-name)
|
|
|
|
|
2002-01-08 09:02:39 -05:00
|
|
|
("REMOTE_HOST" . ,(host-name-or-ip raddr))
|
2001-09-12 14:53:50 -04:00
|
|
|
("REMOTE_ADDR" . ,(format-internet-host-address rhost))
|
2000-09-26 10:35:26 -04:00
|
|
|
|
|
|
|
;; ("AUTH_TYPE" . xx) ; Random authentication
|
|
|
|
;; ("REMOTE_USER" . xx) ; features I don't understand.
|
|
|
|
;; ("REMOTE_IDENT" . xx)
|
|
|
|
|
2002-04-25 05:35:18 -04:00
|
|
|
,@request-invariant-cgi-env ; Stuff that never changes (see cgi-handler).
|
2000-09-26 10:35:26 -04:00
|
|
|
|
2001-08-20 07:31:03 -04:00
|
|
|
,@(cond ((http-url:search (request:url req)) =>
|
|
|
|
(lambda (srch) `(("QUERY_STRING" . ,srch))))
|
|
|
|
(else '()))
|
|
|
|
|
|
|
|
,@(cond ((get-header headers 'content-type) =>
|
|
|
|
(lambda (ct) `(("CONTENT_TYPE" . ,ct))))
|
|
|
|
(else '()))
|
|
|
|
|
|
|
|
,@(cond ((get-header headers 'content-length) =>
|
|
|
|
(lambda (cl) ; Skip initial whitespace (& other non-digits).
|
2002-08-23 11:44:40 -04:00
|
|
|
(let ((first-digit (string-index cl char-set:digit))
|
2001-08-20 07:31:03 -04:00
|
|
|
(cl-len (string-length cl)))
|
|
|
|
(if first-digit
|
|
|
|
`(("CONTENT_LENGTH" . ,(substring cl first-digit cl-len)))
|
2002-08-29 04:32:39 -04:00
|
|
|
(http-error http-status/bad-request req
|
|
|
|
"Illegal `Content-length:' header.")))))
|
2001-08-20 07:31:03 -04:00
|
|
|
|
|
|
|
(else '()))
|
|
|
|
|
2000-09-26 10:35:26 -04:00
|
|
|
. ,(env->alist))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (take n lis)
|
|
|
|
(if (zero? n) '()
|
|
|
|
(cons (car lis) (take (- n 1) (cdr lis)))))
|
|
|
|
|
|
|
|
(define (drop n lis)
|
|
|
|
(if (zero? n) lis
|
|
|
|
(drop (- n 1) (cdr lis))))
|
|
|
|
|
|
|
|
|
|
|
|
;;; Script's output for request REQ is available on SCRIPT-PORT.
|
2002-08-26 05:59:14 -04:00
|
|
|
;;; The script isn't an "nph-" script, so we read the response, and mutate
|
|
|
|
;;; it into a real HTTP response, which we then send back to the HTTP client.
|
2000-09-26 10:35:26 -04:00
|
|
|
|
2002-09-02 09:43:03 -04:00
|
|
|
(define (cgi-make-response script-port path req)
|
2000-09-26 10:35:26 -04:00
|
|
|
(let* ((headers (read-rfc822-headers script-port))
|
|
|
|
(ctype (get-header headers 'content-type)) ; The script headers
|
|
|
|
(loc (get-header headers 'location))
|
2002-09-02 09:43:03 -04:00
|
|
|
(stat (extract-status-code-and-text
|
|
|
|
(get-header-lines headers 'status) req))
|
2002-08-29 04:32:39 -04:00
|
|
|
(extra-headers (delete-headers (delete-headers (delete-headers headers
|
|
|
|
'content-type)
|
|
|
|
'location)
|
|
|
|
'status)))
|
2001-08-20 07:31:03 -04:00
|
|
|
|
2002-04-14 12:57:38 -04:00
|
|
|
(http-syslog (syslog-level debug) "[cgi-server] headers: ~s~%" headers)
|
2002-08-28 05:54:40 -04:00
|
|
|
(http-syslog (syslog-level debug) "[cgi-server] request:method=~a~%"
|
|
|
|
(request:method req))
|
2002-09-02 10:33:06 -04:00
|
|
|
|
2002-09-02 09:43:03 -04:00
|
|
|
(if loc
|
2002-09-03 04:45:59 -04:00
|
|
|
(if (uri-has-protocol? (string-trim loc))
|
2002-09-02 09:43:03 -04:00
|
|
|
(make-http-error-response http-status/moved-perm req
|
|
|
|
loc loc)
|
|
|
|
(make-redirect-response (string-trim loc)))
|
|
|
|
;; Send the response header back to the client
|
|
|
|
(make-response ;code message seconds mime extras body
|
|
|
|
(car stat) ; code
|
|
|
|
(cdr stat) ; text
|
|
|
|
(time)
|
|
|
|
ctype
|
|
|
|
extra-headers
|
|
|
|
(make-writer-body
|
2002-09-03 09:33:43 -04:00
|
|
|
(lambda (out options)
|
2002-09-02 09:43:03 -04:00
|
|
|
(copy-inport->outport script-port out)
|
|
|
|
(close-input-port script-port)))))))
|
|
|
|
|
|
|
|
|
2002-09-03 04:45:59 -04:00
|
|
|
(define (uri-has-protocol? loc)
|
2002-09-02 09:43:03 -04:00
|
|
|
(receive (proto path search frag)
|
|
|
|
(parse-uri loc)
|
|
|
|
(if proto #t #f)))
|
|
|
|
|
|
|
|
(define (extract-status-code-and-text stat-lines req)
|
|
|
|
(cond
|
|
|
|
((not (pair? stat-lines)) ; No status header.
|
2002-09-03 09:33:43 -04:00
|
|
|
(cons http-status/ok "The idiot CGI script left out the status line."))
|
2002-09-02 09:43:03 -04:00
|
|
|
((null? (cdr stat-lines)) ; One line status header.
|
|
|
|
(with-fatal-error-handler*
|
|
|
|
(lambda (c d)
|
2002-09-03 07:55:53 -04:00
|
|
|
(http-error http-status/bad-gateway req
|
2002-09-02 09:43:03 -04:00
|
|
|
"CGI script generated an invalid status header."
|
|
|
|
(car stat-lines) c))
|
|
|
|
(lambda ()
|
|
|
|
(let ((status (string-trim (car stat-lines))))
|
|
|
|
(cons (string->number (substring status 0 3)) ; number
|
|
|
|
(substring/shared status 4)))))) ; text
|
|
|
|
(else ; Vas ist das?
|
2002-09-03 07:55:53 -04:00
|
|
|
(http-error http-status/bad-gateway req
|
2002-09-02 09:43:03 -04:00
|
|
|
"CGI script generated multi-line status header."))))
|