2002-09-20 15:23:17 -04:00
|
|
|
#!/bin/sh
|
|
|
|
echo "Loading..."
|
2002-09-26 08:13:01 -04:00
|
|
|
|
2004-05-05 12:16:26 -04:00
|
|
|
exec scsh -lel SSAX-4.9/load.scm -lel sunet-2.1/load.scm -dm -o surflet-server -e main -s "$0" "$@"
|
2002-09-20 15:23:17 -04:00
|
|
|
!#
|
|
|
|
|
2003-01-19 11:57:27 -05:00
|
|
|
(define-structure surflet-server
|
2002-09-26 08:13:01 -04:00
|
|
|
(export main ; sh jump entry point
|
|
|
|
server) ; scsh entry point
|
2002-09-20 15:23:17 -04:00
|
|
|
(open httpd-core
|
|
|
|
httpd-make-options
|
|
|
|
httpd-basic-handlers
|
|
|
|
httpd-file-directory-handlers
|
2004-05-24 08:00:34 -04:00
|
|
|
httpd-cgi-handlers
|
|
|
|
httpd-seval-handlers
|
2003-01-19 11:57:27 -05:00
|
|
|
surflet-handler
|
2003-07-18 11:06:53 -04:00
|
|
|
surflet-handler/options
|
2002-09-20 15:23:17 -04:00
|
|
|
let-opt
|
2004-02-09 03:09:35 -05:00
|
|
|
scsh
|
|
|
|
scheme
|
|
|
|
srfi-37
|
|
|
|
signals)
|
2002-09-20 15:23:17 -04:00
|
|
|
|
|
|
|
(begin
|
|
|
|
|
|
|
|
(define (usage)
|
|
|
|
(format #f
|
2004-02-09 03:09:35 -05:00
|
|
|
"Usage: start-surflet-server
|
|
|
|
[-h DIR | --htdocs-dir=DIR] [-s DIR | --surflet-dir=DIR]
|
2004-05-24 08:00:34 -04:00
|
|
|
[--cgi-bin-dir=DIR]
|
2004-02-09 03:09:35 -05:00
|
|
|
[-i DIR | --images-dir=DIR] [-p NUM | --port=NUM]
|
|
|
|
[-l FILE | --log-file-name=FILE] [-r NUM | --requests=NUM]
|
|
|
|
[--help]
|
2002-09-20 15:23:17 -04:00
|
|
|
|
|
|
|
with
|
2004-05-13 04:34:03 -04:00
|
|
|
htdocs-dir directory of html files (default: root/htdocs)
|
|
|
|
surflet-dir directory of SUrflet files (default: root/surflets)
|
2004-05-24 08:00:34 -04:00
|
|
|
cgi-bin-dir directory of cgi files (default: root/cgi-bin)
|
2004-05-13 04:34:03 -04:00
|
|
|
images-dir directory of images files (default: root/img)
|
2004-05-17 04:35:47 -04:00
|
|
|
port port server is listening to (default: 8080)
|
2002-09-20 15:23:17 -04:00
|
|
|
log-file-name directory where to store the logfile in CLF
|
2004-05-11 08:10:13 -04:00
|
|
|
(default: /tmp/httpd.log)
|
2002-09-20 15:23:17 -04:00
|
|
|
requests maximal amount of simultaneous requests (default 5)
|
|
|
|
--help show this help
|
2004-05-24 08:00:34 -04:00
|
|
|
"))
|
2004-02-09 03:09:35 -05:00
|
|
|
|
|
|
|
(define (display-usage)
|
|
|
|
(display (usage) (current-error-port))
|
2004-05-11 08:03:30 -04:00
|
|
|
(exit 1))
|
2004-02-09 03:09:35 -05:00
|
|
|
|
|
|
|
(define (raise-usage-error msg . info)
|
|
|
|
(display msg (current-error-port))
|
|
|
|
(display " --- " (current-error-port))
|
|
|
|
(for-each
|
|
|
|
(lambda (i)
|
|
|
|
(display i (current-error-port))
|
|
|
|
(display " " (current-error-port)))
|
|
|
|
info)
|
|
|
|
(display "\n" (current-error-port))
|
|
|
|
(exit 1))
|
|
|
|
|
|
|
|
(define (parse-arguments arg-list)
|
|
|
|
(let ((number-option-proc
|
|
|
|
(lambda (alist-key)
|
|
|
|
(lambda (option name arg ops)
|
|
|
|
(cond
|
|
|
|
((not arg)
|
|
|
|
(raise-usage-error "Option requires a number" name arg))
|
|
|
|
((string->number arg)
|
|
|
|
=> (lambda (n) (cons (cons alist-key n) ops)))
|
|
|
|
(else
|
|
|
|
(raise-usage-error "Not a number" arg))))))
|
|
|
|
(absolute-file-option-proc
|
|
|
|
(lambda (alist-key)
|
|
|
|
(lambda (option name arg ops)
|
|
|
|
(cons (cons alist-key
|
|
|
|
(absolute-file-name arg)) ops)))))
|
|
|
|
|
|
|
|
(let ((htdocs-dir-option
|
|
|
|
(option '(#\h "htdocs-dir") #t #f
|
|
|
|
(absolute-file-option-proc 'htdocs-dir)))
|
|
|
|
(surflet-dir-option
|
|
|
|
(option '(#\s "surflet-dir") #t #f
|
|
|
|
(absolute-file-option-proc 'surflet-dir)))
|
2004-05-24 08:00:34 -04:00
|
|
|
(cgi-bin-dir-option
|
|
|
|
(option '(#\c "cgi-bin-dir") #t #f
|
|
|
|
(absolute-file-option-proc 'cgi-bin-dir)))
|
2004-02-09 03:09:35 -05:00
|
|
|
(images-dir-option
|
|
|
|
(option '(#\i "images-dir") #t #f
|
|
|
|
(absolute-file-option-proc 'images-dir)))
|
|
|
|
(port-option
|
|
|
|
(option '(#\p "port") #t #f
|
|
|
|
(number-option-proc 'port)))
|
|
|
|
(log-file-name-option
|
|
|
|
(option '(#\l "log-file-name") #t #f
|
|
|
|
(absolute-file-option-proc 'log-file-name)))
|
|
|
|
(requests-option
|
|
|
|
(option '(#\r "requests") #t #f
|
|
|
|
(number-option-proc 'requests)))
|
|
|
|
(help-option
|
|
|
|
(option '(#f "help") #f #f
|
|
|
|
(lambda (option name arg ops)
|
|
|
|
(display-usage)))))
|
|
|
|
(args-fold arg-list
|
|
|
|
(list htdocs-dir-option surflet-dir-option
|
2004-05-24 08:00:34 -04:00
|
|
|
cgi-bin-dir-option
|
2004-02-09 03:09:35 -05:00
|
|
|
images-dir-option port-option
|
|
|
|
log-file-name-option requests-option
|
|
|
|
help-option)
|
|
|
|
(lambda (op name arg ops)
|
|
|
|
(raise-usage-error
|
|
|
|
"Unknown command line argument: " op))
|
|
|
|
cons
|
|
|
|
'()))))
|
|
|
|
|
2004-05-13 04:34:03 -04:00
|
|
|
(define (make-options-from-args cmd-line-args default-options)
|
2004-02-09 03:09:35 -05:00
|
|
|
(let ((given (parse-arguments cmd-line-args)))
|
|
|
|
(map (lambda (p)
|
|
|
|
(or (assoc (car p) given) p))
|
|
|
|
default-options)))
|
|
|
|
|
|
|
|
(define (lookup-option alist option)
|
|
|
|
(cond
|
|
|
|
((assoc option alist)
|
|
|
|
=> cdr)
|
|
|
|
(else
|
|
|
|
(error "Internal error, option not found" option alist))))
|
2002-09-20 15:23:17 -04:00
|
|
|
|
2002-09-26 08:13:01 -04:00
|
|
|
(define (server . args)
|
|
|
|
(if (pair? args)
|
|
|
|
(main `(main ,@(car args)))
|
|
|
|
(main '(main))))
|
2002-09-20 15:23:17 -04:00
|
|
|
|
2004-05-24 08:00:34 -04:00
|
|
|
(define (become-nobody-if-root)
|
|
|
|
(cond ((zero? (user-uid))
|
|
|
|
(set-gid (->gid "nobody"))
|
|
|
|
(set-uid (->uid "nobody")))))
|
|
|
|
|
2002-09-20 15:23:17 -04:00
|
|
|
(define (main args)
|
2004-02-03 09:11:26 -05:00
|
|
|
(with-cwd
|
2004-05-12 15:59:43 -04:00
|
|
|
(file-name-directory (car args))
|
2004-05-13 04:34:03 -04:00
|
|
|
(let* ((default-options
|
|
|
|
`((htdocs-dir . ,(absolute-file-name "root/htdocs"))
|
|
|
|
(surflet-dir . ,(absolute-file-name "root/surflets"))
|
2004-05-24 08:00:34 -04:00
|
|
|
(cgi-bin-dir . ,(absolute-file-name "root/cgi-bin"))
|
2004-05-13 04:34:03 -04:00
|
|
|
(images-dir . ,(absolute-file-name "root/img"))
|
2004-05-24 08:00:34 -04:00
|
|
|
(port . 8080)
|
2004-05-13 04:34:03 -04:00
|
|
|
(log-file-name . "/tmp/httpd.log")
|
|
|
|
(requests . 5)))
|
|
|
|
(options (make-options-from-args (cdr args) default-options)))
|
2004-02-09 03:09:35 -05:00
|
|
|
|
|
|
|
(format #t "Going to run SUrflet server with:
|
2002-09-20 15:23:17 -04:00
|
|
|
htdocs-dir: ~a
|
2003-01-19 11:57:27 -05:00
|
|
|
surflet-dir: ~a
|
2004-05-24 08:00:34 -04:00
|
|
|
cgi-bin-dir: ~a
|
2002-10-22 12:01:25 -04:00
|
|
|
images-dir: ~a
|
2002-09-20 15:23:17 -04:00
|
|
|
port: ~a
|
|
|
|
log-file-name: ~a
|
|
|
|
a maximum of ~a simultaneous requests, syslogging activated,
|
|
|
|
and home-dir-handler (public_html) activated.
|
|
|
|
|
|
|
|
"
|
2004-02-09 03:09:35 -05:00
|
|
|
(lookup-option options 'htdocs-dir)
|
|
|
|
(lookup-option options 'surflet-dir)
|
2004-05-24 08:00:34 -04:00
|
|
|
(lookup-option options 'cgi-bin-dir)
|
2004-02-09 03:09:35 -05:00
|
|
|
(lookup-option options 'images-dir)
|
|
|
|
(lookup-option options 'port)
|
|
|
|
(lookup-option options 'log-file-name)
|
|
|
|
(lookup-option options 'requests))
|
|
|
|
|
|
|
|
(httpd
|
|
|
|
(make-httpd-options
|
|
|
|
with-port (lookup-option options 'port)
|
|
|
|
with-root-directory (cwd)
|
|
|
|
with-simultaneous-requests (lookup-option options 'requests)
|
|
|
|
with-syslog? #t
|
|
|
|
with-log-file (lookup-option options 'log-file-name)
|
2004-05-24 08:00:34 -04:00
|
|
|
with-post-bind-thunk become-nobody-if-root
|
2004-02-09 03:09:35 -05:00
|
|
|
;; The following settings are made to avoid dns lookups.
|
|
|
|
with-reported-port (lookup-option options 'port)
|
|
|
|
with-fqdn "localhost"
|
|
|
|
with-resolve-ips? #f
|
|
|
|
with-request-handler
|
|
|
|
(alist-path-dispatcher
|
|
|
|
(list
|
2004-05-24 08:00:34 -04:00
|
|
|
(cons "cgi-bin" (cgi-handler (lookup-option options 'cgi-bin-dir)))
|
|
|
|
(cons "seval" seval-handler)
|
2004-02-09 03:09:35 -05:00
|
|
|
(cons "source" (rooted-file-or-directory-handler
|
|
|
|
(lookup-option options 'surflet-dir)
|
|
|
|
(with-file-name->content-type
|
|
|
|
(lambda (file-name)
|
|
|
|
(if (string-ci=?
|
|
|
|
(file-name-extension file-name) ".scm")
|
|
|
|
"text/plain"))
|
|
|
|
(make-file-directory-options))))
|
|
|
|
(cons "img" (rooted-file-handler
|
|
|
|
(lookup-option options 'images-dir)))
|
|
|
|
(cons "surflet" (surflet-handler
|
|
|
|
(with-surflet-path
|
|
|
|
(lookup-option options 'surflet-dir)))))
|
2004-05-24 08:00:34 -04:00
|
|
|
(tilde-home-dir-handler "public_html"
|
|
|
|
(rooted-file-or-directory-handler
|
|
|
|
(lookup-option options 'htdocs-dir)))))))))
|
2004-02-09 03:09:35 -05:00
|
|
|
))
|
2002-09-20 15:23:17 -04:00
|
|
|
;; EOF
|
|
|
|
|
|
|
|
;;; Local Variables:
|
|
|
|
;;; mode:scheme
|
2003-01-18 12:56:18 -05:00
|
|
|
;;; End:
|