Added #:ssl? for making an https server. It is hard-wired to using

"server-cert.pem" and "private-key.pem" in `server-root-path', but
that seems to be the same as the --ssl command-line option.  The ssl
server is created using the same code that "private/launch.ss" uses,
so it might be a good idea to abstract it into a separate file.

Also, `mime-types-path' defaults to "mime.types" in the
`server-root-path', but if the file is missing, then it uses
"mime.types" in the `default-configuration-table-path', which is a
sensible choice for just getting a server running.

svn: r12529
This commit is contained in:
Eli Barzilay 2008-11-20 05:53:04 +00:00
parent f6aa15c531
commit a41971ba6d

View File

@ -3,7 +3,11 @@
#lang scheme/base #lang scheme/base
(require (prefix-in net: net/sendurl) (require (prefix-in net: net/sendurl)
scheme/contract scheme/contract
scheme/list) scheme/list
scheme/unit
net/tcp-unit
net/tcp-sig
net/ssl-tcp-unit)
(require web-server/web-server (require web-server/web-server
web-server/managers/lru web-server/managers/lru
web-server/managers/manager web-server/managers/manager
@ -44,6 +48,7 @@
#:banner? boolean? #:banner? boolean?
#:listen-ip (or/c false/c string?) #:listen-ip (or/c false/c string?)
#:port number? #:port number?
#:ssl? boolean?
#:manager manager? #:manager manager?
#:servlet-namespace (listof module-path?) #:servlet-namespace (listof module-path?)
#:server-root-path path? #:server-root-path path?
@ -71,6 +76,8 @@
[listen-ip "127.0.0.1"] [listen-ip "127.0.0.1"]
#:port #:port
[the-port 8000] [the-port 8000]
#:ssl?
[ssl? #f]
#:manager #:manager
[manager [manager
@ -102,8 +109,18 @@
(gen-file-not-found-responder (gen-file-not-found-responder
(build-path server-root-path "conf" "not-found.html"))] (build-path server-root-path "conf" "not-found.html"))]
#:mime-types-path #:mime-types-path
[mime-types-path (build-path server-root-path "mime.types")]) [mime-types-path (let ([p (build-path server-root-path "mime.types")])
(define standalone-url (format "http://localhost:~a~a" the-port servlet-path)) (if (file-exists? p)
p
(build-path
(directory-part default-configuration-table-path)
"mime.types")))])
(define standalone-url
(string-append (if ssl? "https" "http")
"://localhost"
(if (and (not ssl?) (= the-port 80))
"" (format ":~a" the-port))
servlet-path))
(define make-servlet-namespace (define make-servlet-namespace
(make-make-servlet-namespace #:to-be-copied-module-specs servlet-namespace)) (make-make-servlet-namespace #:to-be-copied-module-specs servlet-namespace))
(define sema (make-semaphore 0)) (define sema (make-semaphore 0))
@ -153,20 +170,26 @@
(define shutdown-server (define shutdown-server
(serve #:dispatch dispatcher (serve #:dispatch dispatcher
#:listen-ip listen-ip #:listen-ip listen-ip
#:port the-port)) #:port the-port
(define welcome #:tcp@ (if ssl?
(if banner? (let ()
(lambda () (define-unit-binding ssl-tcp@
(printf "Your Web application is running at ~a.\n" standalone-url) (make-ssl-tcp@
(printf "Click 'Stop' at any time to terminate the Web Server.\n")) (build-path server-root-path "server-cert.pem")
(void))) (build-path server-root-path "private-key.pem")
(define (bye) #f #f #f #f #f)
(when banner? (printf "\nWeb Server stopped.\n")) (import) (export tcp^))
(shutdown-server)) ssl-tcp@)
tcp@)))
(when launch-browser? (when launch-browser?
((send-url) standalone-url #t)) ((send-url) standalone-url #t))
(welcome) (when banner?
(with-handlers ([exn:break? (lambda (exn) (bye))]) (printf "Your Web application is running at ~a.\n" standalone-url)
(semaphore-wait/enable-break sema) (printf "Click 'Stop' at any time to terminate the Web Server.\n"))
;; We can get here if a /quit url is visited (let ([bye (lambda ()
(bye))) (when banner? (printf "\nWeb Server stopped.\n"))
(shutdown-server))])
(with-handlers ([exn:break? (lambda (exn) (bye))])
(semaphore-wait/enable-break sema)
;; We can get here if a /quit url is visited
(bye))))