Rename "path handler" -> "request handler".

This commit is contained in:
sperber 2002-09-22 15:41:41 +00:00
parent 74e26ddeb5
commit c0281e834a
13 changed files with 54 additions and 65 deletions

View File

@ -12,7 +12,7 @@
;;; - The handlers could be made -- closed over their parameters ;;; - The handlers could be made -- closed over their parameters
;;; (e.g., root vars, etc.) ;;; (e.g., root vars, etc.)
;;; This code provides a path-handler for the HTTP server that implements ;;; This code provides a request handler for the HTTP server that implements
;;; a CGI interface to external programs for doing HTTP transactions. ;;; a CGI interface to external programs for doing HTTP transactions.
;;; About HTML forms ;;; About HTML forms
@ -75,7 +75,7 @@
;;; path for scripts ;;; path for scripts
(define cgi-default-bin-path "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin") (define cgi-default-bin-path "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin")
;;; The path handler for CGI scripts. (car path) is the script to run. ;;; The request handler for CGI scripts. (car path) is the script to run.
;;; cgi-bin-path is used, if PATH-variable isn't defined ;;; cgi-bin-path is used, if PATH-variable isn't defined
(define (cgi-handler bin-dir . maybe-cgi-bin-path) (define (cgi-handler bin-dir . maybe-cgi-bin-path)

View File

@ -14,17 +14,12 @@
;;; net connections, read and parse requests, and handler errors. ;;; net connections, read and parse requests, and handler errors.
;;; It does not have the code to actually handle requests. That's up ;;; It does not have the code to actually handle requests. That's up
;;; to other modules, and could vary from server to server. To build ;;; to other modules, and could vary from server to server. To build
;;; a complete server, you need to define path handlers (see below) -- ;;; a complete server, you need to define request handlers (see below) --
;;; they determine how requests are to be handled. ;;; they determine how requests are to be handled.
;;; ;;;
;;; The RFC detailing the HTTP 1.0 protocol, RFC 1945, can be found at ;;; The RFC detailing the HTTP 1.0 protocol, RFC 1945, can be found at
;;; http://www.w3.org/Protocols/rfc1945/rfc1945 ;;; http://www.w3.org/Protocols/rfc1945/rfc1945
;;; (httpd options)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The server top-level. PATH-HANDLER is the top-level request path handler --
;;; the procedure that actually deals with the request.
(define server/protocol "HTTP/1.0") (define server/protocol "HTTP/1.0")
(define (httpd options) (define (httpd options)
@ -173,7 +168,7 @@
(lambda () (lambda ()
(let ((initial-req (parse-http-request sock options))) (let ((initial-req (parse-http-request sock options)))
(let redirect-loop ((req initial-req)) (let redirect-loop ((req initial-req))
(let ((response ((httpd-options-path-handler options) (let ((response ((httpd-options-request-handler options)
(http-url:path (request:url req)) (http-url:path (request:url req))
req))) req)))
(if (eq? (response-code response) (if (eq? (response-code response)

View File

@ -11,14 +11,14 @@
;;; (home-dir-handler user-public-dir) -> handler ;;; (home-dir-handler user-public-dir) -> handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Return a path handler that looks things up in a specific directory ;;; Return a request handler that looks things up in a specific directory
;;; in the user's home directory. If ph = (home-dir-handler "public_html") ;;; in the user's home directory. If ph = (home-dir-handler "public_html")
;;; then ph is a path-handler that serves files out of peoples' public_html ;;; then ph is a request handler that serves files out of peoples' public_html
;;; subdirectory. So ;;; subdirectory. So
;;; (ph '("shivers" "hk.html") req) ;;; (ph '("shivers" "hk.html") req)
;;; will serve the file ;;; will serve the file
;;; ~shivers/public_html/hk.html ;;; ~shivers/public_html/hk.html
;;; The path handler treats the URL path as (<user> . <file-path>), ;;; The request handler treats the URL path as (<user> . <file-path>),
;;; serving ;;; serving
;;; ~<user>/<user-public-dir>/<file-path> ;;; ~<user>/<user-public-dir>/<file-path>
@ -35,7 +35,7 @@
file-serve-response file-serve-response
req)))) req))))
;;; (tilde-home-dir-handler user-public-dir default-path-handler) ;;; (tilde-home-dir-handler user-public-dir default-request-handler)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; If the car of the path is a tilde-marked home directory (e.g., "~kgk"), ;;; If the car of the path is a tilde-marked home directory (e.g., "~kgk"),
;;; do home-directory service as in HOME-DIR-HANDLER, otherwise punt to the ;;; do home-directory service as in HOME-DIR-HANDLER, otherwise punt to the
@ -47,8 +47,8 @@
(and (> (string-length head) 0) (and (> (string-length head) 0)
(char=? (string-ref head 0) #\~))))) (char=? (string-ref head 0) #\~)))))
(define (tilde-home-dir-handler user-public-dir default-ph) (define (tilde-home-dir-handler user-public-dir default-handler)
(make-request-handler (make-predicate-handler
tilde-home-dir? tilde-home-dir?
(lambda (path req) (lambda (path req)
(let* ((tilde-home (car path)) ; Yes. (let* ((tilde-home (car path)) ; Yes.
@ -58,7 +58,7 @@
"/" "/"
user-public-dir))) user-public-dir)))
(make-rooted-file-path-response subdir (cdr path) file-serve-response req))) (make-rooted-file-path-response subdir (cdr path) file-serve-response req)))
default-ph)) default-handler))
;;; Make a handler that serves files relative to a particular root ;;; Make a handler that serves files relative to a particular root

View File

@ -19,39 +19,33 @@
;;; ;;;
;;; The object-oriented view: ;;; The object-oriented view:
;;; One way to look at this is to think of the request's METHOD as a ;;; One way to look at this is to think of the request's METHOD as a
;;; generic operation on the URL. Recursive path handlers do method ;;; generic operation on the URL. Recursive request handlers do method
;;; lookup to determine how to implement a given operation on a particular ;;; lookup to determine how to implement a given operation on a particular
;;; path. ;;; path.
;;; ;;;
;;; The REQUEST is a request record, as defined in httpd-core.scm, containing ;;; The REQUEST is a request record, as defined in httpd-core.scm, containing
;;; the details of the client request. However, path handlers should *not* ;;; the details of the client request.
;;; read the request entity from, or write the reply to the request's socket.
;;; Path-handler I/O should be done on the current i/o ports: if the handler
;;; needs to read an entity, it should read it from (CURRENT-INPUT-PORT); when
;;; the handler wishes to write a reply, it should write it to
;;; (CURRENT-OUTPUT-PORT). This makes it easy for the procedure that called
;;; the handler to establish I/O indirections or filters if it so desires.
;; general request-handler-combinator: ;; general request handler combinator:
;; predicate: path x request --> boolean ;; predicate: path x request --> boolean
;; if #t, handler is called ;; if #t, handler is called
;; if #f, default-handler is called ;; if #f, default-handler is called
(define (make-request-handler predicate handler default-handler) (define (make-predicate-handler predicate handler default-handler)
(lambda (path req) (lambda (path req)
(if (predicate path req) (if (predicate path req)
(handler path req) (handler path req)
(default-handler path req)))) (default-handler path req))))
;; same as make-request-handler except that the predicate is only ;; same as MAKE-PREDICATE-HANDLER except that the predicate is only
;; called with the path: ;; called with the path:
;; predicate: path --> boolean ;; predicate: path --> boolean
(define (make-path-handler predicate handler default-handler) (define (make-path-predicate-handler predicate handler default-handler)
(make-request-handler (make-predicate-handler
(lambda (path req) (predicate path)) handler default-handler)) (lambda (path req) (predicate path)) handler default-handler))
;; selects handler according to host-field of http-request ;; selects handler according to host-field of http-request
(define (make-host-name-handler hostname handler default-handler) (define (make-host-name-handler hostname handler default-handler)
(make-request-handler (make-predicate-handler
(lambda (path req) (lambda (path req)
;; we expect only one host-header-field ;; we expect only one host-header-field
(string=? hostname (string-trim (get-header (request:headers req) 'host)))) (string=? hostname (string-trim (get-header (request:headers req) 'host))))
@ -67,10 +61,10 @@
;;; (alist-path-dispatcher handler-alist default-handler) -> handler ;;; (alist-path-dispatcher handler-alist default-handler) -> handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This function creates a table-driven path-handler that dispatches off ;;; This function creates a table-driven request handler that dispatches off
;;; of the car of the request path. The handler uses the car to index into ;;; of the car of the request path. The handler uses the car to index into
;;; a path-handler alist. If it finds a hit, it recurses using the table's ;;; a request handler alist. If it finds a hit, it recurses using the table's
;;; path-handler. If no hits, it handles the path with a default handler. ;;; request handler. If no hits, it handles the path with a default handler.
;;; An alist handler is passed the tail of the original path; the ;;; An alist handler is passed the tail of the original path; the
;;; default handler gets the entire original path. ;;; default handler gets the entire original path.
;;; ;;;
@ -87,8 +81,8 @@
default-handler default-handler
handler-alist)) handler-alist))
;;; The null path handler -- handles nothing, sends back an error response. ;;; The null request handler -- handles nothing, sends back an error response.
;;; Can be useful as the default in table-driven path handlers. ;;; Can be useful as the default in table-driven request handlers.
(define (null-path-handler path req) (define (null-request-handler path req)
(make-http-error-response http-status/not-found req)) (make-http-error-response http-status/not-found req))

View File

@ -6,14 +6,14 @@
;;; For copyright information, see the file COPYING which comes with ;;; For copyright information, see the file COPYING which comes with
;;; the distribution. ;;; the distribution.
;;; This file contains a few example top-level path-handlers and ;;; This file contains a few example top-level request handlers and
;;; other useful fragments. ;;; other useful fragments.
;;; - /h/<user>/<file-path> => serve <file-path> from ~user/public_html. ;;; - /h/<user>/<file-path> => serve <file-path> from ~user/public_html.
;;; - /seval You may POST Scheme code to this URL, and receive the output. ;;; - /seval You may POST Scheme code to this URL, and receive the output.
;;; - Otherwise, serve files from the standard HTTP demon repository. ;;; - Otherwise, serve files from the standard HTTP demon repository.
(define ph1 (define rh1
(alist-path-dispatcher (alist-path-dispatcher
`(("h" . ,(home-dir-handler "public_html")) `(("h" . ,(home-dir-handler "public_html"))
("seval" . ,seval-handler) ("seval" . ,seval-handler)
@ -27,15 +27,15 @@
;;; /usr/local/etc/httpd/cgi-bin/<prog> ;;; /usr/local/etc/httpd/cgi-bin/<prog>
;;; - Otherwise, just serve files out of the standard directory. ;;; - Otherwise, just serve files out of the standard directory.
(define ph2 (define rh2
(alist-path-dispatcher (alist-path-dispatcher
`(("cgi-bin" . ,(cgi-handler "/usr/local/etc/httpd/cgi-bin"))) `(("cgi-bin" . ,(cgi-handler "/usr/local/etc/httpd/cgi-bin")))
(tilde-home-dir-handler "public_html" (tilde-home-dir-handler "public_html"
(rooted-file-handler "/usr/local/etc/httpd/htdocs")))) (rooted-file-handler "/usr/local/etc/httpd/htdocs"))))
;;; Greatest hits path handler. ;;; Greatest hits request handler.
(define ph3 (define rh3
(alist-path-dispatcher (alist-path-dispatcher
`(("h" . ,(home-dir-handler "public_html")) `(("h" . ,(home-dir-handler "public_html"))
("seval" . ,seval-handler) ("seval" . ,seval-handler)
@ -53,7 +53,7 @@
(set-gid (->uid "nobody")) (set-gid (->uid "nobody"))
(set-uid (->gid "nobody")) (set-uid (->gid "nobody"))
(initialise-request-invariant-cgi-env) (initialise-request-invariant-cgi-env)
(httpd (make-httpd-options with-path-handler ph (httpd (make-httpd-options with-request-handler rh3
with-port 8001 with-port 8001
with-root-directory "/usr/local/etc/httpd/htdocs"))) with-root-directory "/usr/local/etc/httpd/htdocs")))

View File

@ -10,7 +10,7 @@
;;; (info-handler parse-info reference find-icon address) -> handler ;;; (info-handler parse-info reference find-icon address) -> handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This function creates a path handler that converts GNU info pages ;;; This function creates a request handler that converts GNU info pages
;;; on-the-fly. It is highly parameterizable to accomodate a wide ;;; on-the-fly. It is highly parameterizable to accomodate a wide
;;; range of environments. The parameters specify how to find the ;;; range of environments. The parameters specify how to find the
;;; source code for the info pages, and how to generate certain ;;; source code for the info pages, and how to generate certain

View File

@ -15,7 +15,7 @@
icon-name icon-name
fqdn fqdn
reported-port reported-port
path-handler request-handler
server-admin server-admin
simultaneous-requests simultaneous-requests
logfile logfile
@ -38,8 +38,8 @@
set-httpd-options-fqdn!) set-httpd-options-fqdn!)
(reported-port httpd-options-reported-port (reported-port httpd-options-reported-port
set-httpd-options-reported-port!) set-httpd-options-reported-port!)
(path-handler httpd-options-path-handler (request-handler httpd-options-request-handler
set-httpd-options-path-handler!) set-httpd-options-request-handler!)
(server-admin httpd-options-server-admin (server-admin httpd-options-server-admin
set-httpd-options-server-admin!) set-httpd-options-server-admin!)
(simultaneous-requests httpd-options-simultaneous-requests (simultaneous-requests httpd-options-simultaneous-requests
@ -55,7 +55,7 @@
#f ; icon-name #f ; icon-name
#f ; fqdn #f ; fqdn
#f ; reported-port #f ; reported-port
#f ; path-handler #f ; request-handler
#f ; server-admin #f ; server-admin
#f ; simultaneous-requests #f ; simultaneous-requests
#f #f
@ -79,8 +79,8 @@
(httpd-options-fqdn options)) (httpd-options-fqdn options))
(set-httpd-options-reported-port! new-options (set-httpd-options-reported-port! new-options
(httpd-options-reported-port options)) (httpd-options-reported-port options))
(set-httpd-options-path-handler! new-options (set-httpd-options-request-handler! new-options
(httpd-options-path-handler options)) (httpd-options-request-handler options))
(set-httpd-options-server-admin! new-options (set-httpd-options-server-admin! new-options
(httpd-options-server-admin options)) (httpd-options-server-admin options))
(set-httpd-options-simultaneous-requests! (set-httpd-options-simultaneous-requests!
@ -114,8 +114,8 @@
(make-httpd-options-transformer set-httpd-options-fqdn!)) (make-httpd-options-transformer set-httpd-options-fqdn!))
(define with-reported-port (define with-reported-port
(make-httpd-options-transformer set-httpd-options-reported-port!)) (make-httpd-options-transformer set-httpd-options-reported-port!))
(define with-path-handler (define with-request-handler
(make-httpd-options-transformer set-httpd-options-path-handler!)) (make-httpd-options-transformer set-httpd-options-request-handler!))
(define with-server-admin (define with-server-admin
(make-httpd-options-transformer set-httpd-options-server-admin!)) (make-httpd-options-transformer set-httpd-options-server-admin!))
(define with-simultaneous-requests (define with-simultaneous-requests

View File

@ -21,9 +21,9 @@ exec scsh -lm ../packages.scm -dm -o http-top -e top -s "$0" "$@"
scheme) scheme)
(begin (begin
;; Kitche-sink path handler. ;; Kitchen-sink request handler.
(define ph (define rh
(alist-path-dispatcher (alist-path-dispatcher
`(("h" . ,(home-dir-handler "public_html")) `(("h" . ,(home-dir-handler "public_html"))
("seval" . ,seval-handler) ("seval" . ,seval-handler)
@ -44,8 +44,8 @@ exec scsh -lm ../packages.scm -dm -o http-top -e top -s "$0" "$@"
(set-uid (->uid "nobody")))) (set-uid (->uid "nobody"))))
;; invariant environment is know initilialized by cgi-handler itself ;; invariant environment is know initilialized by cgi-handler itself
;; (initialise-request-invariant-cgi-env) ;; (initialise-request-invariant-cgi-env)
(httpd (with-path-handler (httpd (with-request-handler
ph rh
(with-port (with-port
8001 8001
(with-root-directory "/usr/local/etc/httpd"))))))) (with-root-directory "/usr/local/etc/httpd")))))))

View File

@ -11,7 +11,7 @@
;;; HTML forms ;;; HTML forms
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This path handler is suitable for receiving code entered into an ;;; This request handler is suitable for receiving code entered into an
;;; HTML text form. The Scheme code being uploaded is being POST'd to us ;;; HTML text form. The Scheme code being uploaded is being POST'd to us
;;; (from a form). See http-forms.scm for info on the format of this kind ;;; (from a form). See http-forms.scm for info on the format of this kind
;;; of request. After parsing the request into the submitted string, we ;;; of request. After parsing the request into the submitted string, we
@ -38,7 +38,7 @@
(syntax-rules () (syntax-rules ()
((do/timeout secs body ...) (do/timeout* secs (lambda () body ...))))) ((do/timeout secs body ...) (do/timeout* secs (lambda () body ...)))))
;;; The path handler for seval ops. ;;; The request handler for seval ops.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (seval-handler path req) (define (seval-handler path req)

View File

@ -153,7 +153,7 @@ exec scsh -lm ${SUNETHOME:-../..}/packages.scm -lm ${SUNETHOME:-../..}/httpd/ser
(with-simultaneous-requests simultaneous-requests (with-simultaneous-requests simultaneous-requests
(with-syslog? #t (with-syslog? #t
(with-logfile log-file-name (with-logfile log-file-name
(with-path-handler (with-request-handler
(alist-path-dispatcher (alist-path-dispatcher
(list (cons "h" (home-dir-handler "public_html")) (list (cons "h" (home-dir-handler "public_html"))
; (cons "seval" seval-handler) ; (cons "seval" seval-handler)

View File

@ -256,7 +256,7 @@
with-icon-name with-icon-name
with-fqdn with-fqdn
with-reported-port with-reported-port
with-path-handler with-request-handler
with-server-admin with-server-admin
with-simultaneous-requests with-simultaneous-requests
with-logfile with-logfile
@ -269,7 +269,7 @@
httpd-options-icon-name httpd-options-icon-name
httpd-options-fqdn httpd-options-fqdn
httpd-options-reported-port httpd-options-reported-port
httpd-options-path-handler httpd-options-request-handler
httpd-options-server-admin httpd-options-server-admin
httpd-options-simultaneous-requests httpd-options-simultaneous-requests
httpd-options-logfile httpd-options-logfile
@ -365,12 +365,12 @@
time->http-date-string)) time->http-date-string))
(define-interface httpd-basic-handlers-interface (define-interface httpd-basic-handlers-interface
(export make-request-handler (export make-predicate-handler
make-path-handler make-path-predicate-handler
make-host-name-handler make-host-name-handler
make-path-prefix-handler make-path-prefix-handler
alist-path-dispatcher alist-path-dispatcher
null-path-handler)) null-request-handler))
(define-interface httpd-file-directory-handlers-interface (define-interface httpd-file-directory-handlers-interface
(export home-dir-handler (export home-dir-handler

View File

@ -138,7 +138,7 @@ exec scsh -lm packages.scm -dm -o http-test -e main -s "$0" "$@"
(with-simultaneous-requests simultaneous-requests (with-simultaneous-requests simultaneous-requests
(with-syslog? #t (with-syslog? #t
(with-logfile log-file-name (with-logfile log-file-name
(with-path-handler (with-request-handler
(alist-path-dispatcher (alist-path-dispatcher
(list (cons "h" (home-dir-handler "public_html")) (list (cons "h" (home-dir-handler "public_html"))
(cons "seval" seval-handler) (cons "seval" seval-handler)

View File

@ -121,7 +121,7 @@ exec scsh -lm packages.scm -dm -o http-test -e main -s "$0" "$@"
(httpd (with-port port (httpd (with-port port
(with-syslog? #t (with-syslog? #t
(with-logfile log-file-name (with-logfile log-file-name
(with-path-handler (with-request-handler
(tilde-home-dir-handler "public_html" (tilde-home-dir-handler "public_html"
(alist-path-dispatcher (alist-path-dispatcher
(list (cons "cgi" (cgi-handler cgi-bin-dir))) (list (cons "cgi" (cgi-handler cgi-bin-dir)))