4 Web Servers
A Web server is a unit with the web-server^ signature. The most common way to construct one is to provide a web-config^ unit to the web-server@ unit. The most common way to construct a web-config^ unit is to use configuration-table->web-config@ to produce one from a configuration table file, such as the one that is shipped with Racket in default-configuration-table-path.
4.1 Server Units
signature
web-server^ : signature
Runs the server and returns a procedure that shuts down the server.
procedure
(serve-ports ip op) → void
ip : input-port? op : output-port? Serves a single connection represented by the ports ip and op.
value
web-server@ :
(unit/c (web-config^ tcp^) (web-server^))
Logs the incoming request with the given format to the given file
Performs HTTP Basic Authentication with the given password file
Allows the "/conf/refresh-passwords" URL to refresh the password file.
Allows the "/conf/collect-garbage" URL to call the garbage collector.
Allows the "/conf/refresh-servlets" URL to refresh the servlets cache.
Executes servlets mapping URLs to the given servlet root directory under htdocs.
Serves files under the "/" URL in the given htdocs directory.
4.2 Configuration Units
signature
web-config^ : signature
value
Passed to tcp-accept.
value
virtual-hosts : (string? . -> . host?)
Contains the configuration of individual virtual hosts.Specifies the initial timeout given to a connection.
value
Specifies the port to serve HTTP on.Passed to tcp-listen.Passed to servlets:make through make-default-path->servlet.
procedure
(configuration-table->web-config@ path [ #:port port #:listen-ip listen-ip #:make-servlet-namespace make-servlet-namespace]) → (unit/c (import) (export web-config^)) path : path-string? port : (or/c false/c port-number?) = #f listen-ip : (or/c false/c string?) = #f
make-servlet-namespace : make-servlet-namespace/c = (make-make-servlet-namespace)
procedure
(configuration-table-sexpr->web-config@ sexpr [ #:web-server-root web-server-root #:port port #:listen-ip listen-ip #:make-servlet-namespace make-servlet-namespace]) → (unit/c (import) (export web-config^)) sexpr : list?
web-server-root : path-string? = (directory-part default-configuration-table-path) port : (or/c false/c port-number?) = #f listen-ip : (or/c false/c string?) = #f
make-servlet-namespace : make-servlet-namespace/c = (make-make-servlet-namespace)
4.3 Configuration Table
value
configuration-table-sexpr? : (any . -> . boolean?)
procedure
sexpr : configuration-table-sexpr?
procedure
(configuration-table->sexpr ctable)
→ configuration-table-sexpr? ctable : configuration-table?
`((port ,integer?) (max-waiting ,exact-integer?) (initial-connection-timeout ,integer?) (default-host-table ,host-table-sexpr?) (virtual-host-table (list ,symbol? ,host-table-sexpr?) ...))
`(host-table (default-indices ,string? ...) (log-format ,symbol?) (messages (servlet-message ,path-string?) (authentication-message ,path-string?) (servlets-refreshed ,path-string?) (passwords-refreshed ,path-string?) (file-not-found-message ,path-string?) (protocol-message ,path-string?) (collect-garbage ,path-string?)) (timeouts (default-servlet-timeout ,integer?) (password-connection-timeout ,integer?) (servlet-connection-timeout ,integer?) (file-per-byte-connection-timeout ,integer?) (file-base-connection-timeout ,integer)) (paths (configuration-root ,path-string?) (host-root ,path-string?) (log-file-path ,path-string?) (file-root ,path-string?) (servlet-root ,path-string?) (mime-types ,path-string?) (password-authentication ,path-string?)))
procedure
path : path-string?
procedure
(write-configuration-table ctable path) → void
ctable : configuration-table? path : path-string?
4.4 Configuration Table Structure
struct
(struct configuration-table ( port max-waiting initial-connection-timeout default-host virtual-hosts) #:extra-constructor-name make-configuration-table) port : port-number? max-waiting : natural-number/c initial-connection-timeout : natural-number/c default-host : host-table? virtual-hosts : (listof (cons/c string? host-table?))
struct
(struct host-table (indices log-format messages timeouts paths) #:extra-constructor-name make-host-table) indices : (listof string?) log-format : symbol? messages : messages? timeouts : timeouts? paths : paths?
struct
(struct host ( indices log-format log-path passwords responders timeouts paths) #:extra-constructor-name make-host) indices : (listof string?) log-format : symbol? log-path : (or/c false/c path-string?) passwords : (or/c false/c path-string?) responders : responders? timeouts : timeouts? paths : paths?
struct
(struct responders ( servlet servlet-loading authentication servlets-refreshed passwords-refreshed file-not-found protocol collect-garbage) #:extra-constructor-name make-responders) servlet : (url? any/c . -> . response?) servlet-loading : (url? any/c . -> . response?) authentication : (url? (cons/c symbol? string?) . -> . response?) servlets-refreshed : (-> response?) passwords-refreshed : (-> response?) file-not-found : (request? . -> . response?) protocol : (url? . -> . response?) collect-garbage : (-> response?)
struct
(struct messages ( servlet authentication servlets-refreshed passwords-refreshed file-not-found protocol collect-garbage) #:extra-constructor-name make-messages) servlet : string? authentication : string? servlets-refreshed : string? passwords-refreshed : string? file-not-found : string? protocol : string? collect-garbage : string?
struct
(struct timeouts ( default-servlet password servlet-connection file-per-byte file-base) #:extra-constructor-name make-timeouts) default-servlet : number? password : number? servlet-connection : number? file-per-byte : number? file-base : number?
struct
(struct paths ( conf host-base log htdocs servlet mime-types passwords) #:extra-constructor-name make-paths) conf : (or/c false/c path-string?) host-base : (or/c false/c path-string?) log : (or/c false/c path-string?) htdocs : (or/c false/c path-string?) servlet : (or/c false/c path-string?) mime-types : (or/c false/c path-string?) passwords : (or/c false/c path-string?)
4.5 Standard Responders
procedure
(file-response http-code short-version text-file header ...) → response? http-code : natural-number/c short-version : string? text-file : string? header : header?
procedure
(servlet-loading-responder url exn) → response?
url : url? exn : exn?
procedure
(gen-servlet-not-found file) → ((url url?) . -> . response?)
file : path-string?
procedure
(servlet-error-responder url exn) → response?
url : url? exn : exn?
procedure
(gen-servlet-responder file)
→ ((url url?) (exn any/c) . -> . response?) file : path-string?
procedure
(gen-servlets-refreshed file) → (-> response?)
file : path-string?
procedure
(gen-passwords-refreshed file) → (-> response?)
file : path-string?
procedure
(gen-authentication-responder file)
→ ((url url?) (header header?) . -> . response?) file : path-string?
procedure
(gen-protocol-responder file) → ((url url?) . -> . response?)
file : path-string?
procedure
(gen-file-not-found-responder file)
→ ((req request?) . -> . response?) file : path-string?
procedure
(gen-collect-garbage-responder file) → (-> response?)
file : path-string?