▼ |  | Web Server: HTTP Server

1 | Dispatching Server
2 | Dispatchers
3 | Launching Servers
4 | Web Servers
5 | Internal APIs
6 | Troubleshooting and Tips
| Index

► | 3 | Launching Servers

3.1 | Simple Single Servlet Servers

On this page:

serve
serve/ ports
serve/ ips+ ports
serve/ web-config@
raw: dispatch-server-connect@
make-ssl-connect@
do-not-return
3.1`` Simple Single Servlet Servers
dispatch/ servlet
serve/ launch/ wait

top ← prev up next →

### 3`` Launching Servers

(require web-server/web-server) | package: web-server-lib

This module provides functions for launching dispatching servers.

> > procedure
> > (serve |
> > |  | #:dispatch dispatch |
> > | [ | #:confirmation-channel confirmation-channel |
> > |  | #:connection-close? connection-close? |
> > |  | #:dispatch-server-connect@ dispatch-server-connect@ |
> > |  | #:tcp@ tcp@ |
> > |  | #:port port |
> > |  | #:listen-ip listen-ip |
> > |  | #:max-waiting max-waiting |
> > |  | #:initial-connection-timeout initial-connection-timeout]) |
> → (-> void)
> dispatch : dispatcher/c
> confirmation-channel : (or/c false/c async-channel?) = #f
> connection-close? : boolean? = #f
> | dispatch-server-connect@ |  | : |  |
> (unit/c (import)
> (export dispatch-server-connect^))
> |  |  | = |  | raw:dispatch-server-connect@
> tcp@ : (unit/c (import) (export tcp^)) = raw:tcp@
> port : listen-port-number? = 80
> listen-ip : (or/c string? false/c) = #f
> max-waiting : integer? = 511
> initial-connection-timeout : integer? = 60

Constructs an appropriate dispatch-server-config^, invokes the dispatch-server@, and calls its serve function.
If connection-close? is #t, then every connection is closed after one request. Otherwise, the client decides based on what HTTP version it uses.
The #:dispatch-server-connect@ argument supports the conversion of raw connections; for example, make-ssl-connect@ produces a unit to serve SSL by converting raw TCP ports to SSL ports; see also How do I set up the server to use HTTPS?. The #:tcp@ argument supports replacing TCP connections with other kinds of connections (and was formerly recommended for SLL support). Beware that the server expects the tcp-accept operation from tcp@ to be effectively atomic; new connections are not accepted while tcp-accept is in progress.
Here’s an example of a simple web server that serves files from a given path:

> (define (start-file-server base)
> (serve
> #:dispatch
> (files:make
> #:url->path (make-url->path base)
> #:path->mime-type
> (lambda (path)
> #"application/octet-stream"))
> #:port 8080))

Changed in version 1.1 of package web-server-lib: Added the #:dispatch-server-connect@ argument.

> > procedure
> > (serve/ports |
> > |  | #:dispatch dispatch |
> > | [ | #:confirmation-channel confirmation-channel |
> > |  | #:connection-close? connection-close? |
> > |  | #:dispatch-server-connect@ dispatch-server-connect@ |
> > |  | #:tcp@ tcp@ |
> > |  | #:ports ports |
> > |  | #:listen-ip listen-ip |
> > |  | #:max-waiting max-waiting |
> > |  | #:initial-connection-timeout initial-connection-timeout]) |
> → (-> void)
> dispatch : dispatcher/c
> confirmation-channel : (or/c false/c async-channel?) = #f
> connection-close? : boolean? = #f
> | dispatch-server-connect@ |  | : |  |
> (unit/c (import)
> (export dispatch-server-connect^))
> |  |  | = |  | raw:dispatch-server-connect@
> tcp@ : (unit/c (import) (export tcp^)) = raw:tcp@
> ports : (listof listen-port-number?) = (list 80)
> listen-ip : (or/c string? false/c) = #f
> max-waiting : integer? = 511
> initial-connection-timeout : integer? = 60

Calls serve multiple times, once for each port, and returns a function that shuts down all of the server instances.

Changed in version 1.1 of package web-server-lib: Added the #:dispatch-server-connect@ argument.

> > procedure
> > (serve/ips+ports |
> > |  | #:dispatch dispatch |
> > | [ | #:confirmation-channel confirmation-channel |
> > |  | #:connection-close? connection-close? |
> > |  | #:dispatch-server-connect@ dispatch-server-connect@ |
> > |  | #:tcp@ tcp@ |
> > |  | #:ips+ports ips+ports |
> > |  | #:max-waiting max-waiting |
> > |  | #:initial-connection-timeout initial-connection-timeout]) |
> → (-> void)
> dispatch : dispatcher/c
> confirmation-channel : (or/c false/c async-channel?) = #f
> connection-close? : boolean? = #f
> | dispatch-server-connect@ |  | : |  |
> (unit/c (import)
> (export dispatch-server-connect^))
> |  |  | = |  | raw:dispatch-server-connect@
> tcp@ : (unit/c (import) (export tcp^)) = raw:tcp@
> | ips+ports |  | : |  | (listof (cons/c (or/c string? false/c) (listof listen-port-number?)))
> |  |  | = |  | (list (cons #f (list 80)))
> max-waiting : integer? = 511
> initial-connection-timeout : integer? = 60

Calls serve/ports multiple times, once for each ip, and returns a function that shuts down all of the server instances.

Changed in version 1.1 of package web-server-lib: Added the #:dispatch-server-connect@ argument.

> > procedure
> > (serve/web-config@ |
> > |  | config@ |
> > | [ | #:dispatch-server-connect@ dispatch-server-connect@ |
> > |  | #:tcp@ tcp@]) |
> → (-> void)
> config@ : (unit/c (import) (export web-config^))
> | dispatch-server-connect@ |  | : |  |
> (unit/c (import)
> (export dispatch-server-connect^))
> |  |  | = |  | raw:dispatch-server-connect@
> tcp@ : (unit/c (import) (export tcp^)) = raw:tcp@

Starts the Web Server with the settings defined by the given web-config^ unit.
Combine serve/web-config@ with configuration-table->web-config@ and configuration-table-sexpr->web-config@:

> (serve/web-config@
> (configuration-table->web-config@
> default-configuration-table-path))

Changed in version 1.1 of package web-server-lib: Added the #:dispatch-server-connect@ argument.

> value raw:dispatch-server-connect@
> : (unit/c (import) (export dispatch-server-connect^))

A default implementation of the dispatch server’s connection-conversion abstraction that performs no conversion.

Added in version 1.1 of package web-server-lib.

> > procedure
> > (make-ssl-connect@ |  | server-cert-file |
> > |  | server-key-file) |
> → (unit/c (import) (export dispatch-server-connect^))
> server-cert-file : path-string?
> server-key-file : path-string?

Constructs an implementation of the dispatch server’s connection-conversion abstraction for OpenSSL.

Added in version 1.1 of package web-server-lib.

> procedure (do-not-return) → void

This function does not return. If you are writing a script to load the Web Server you are likely to want to call this functions at the end of your script.

#### 3.1`` Simple Single Servlet Servers

(require web-server/servlet-dispatch)
| package: web-server-lib

These functions optimize the construction of dispatchers and launching of servers for single servlets and interactive development.

> > procedure
> > (dispatch/servlet |
> > |  | start |
> > | [ | #:regexp regexp |
> > |  | #:stateless? stateless? |
> > |  | #:stuffer stuffer |
> > |  | #:manager manager |
> > |  | #:current-directory servlet-current-directory |
> > |  | #:responders-servlet-loading responders-servlet-loading |
> > |  | #:responders-servlet responders-servlet]) |
> → dispatcher/c
> start : (request? . -> . response?)
> regexp : regexp? = #rx""
> stateless? : boolean? = #f
> stuffer : (stuffer/c serializable? bytes?) = default-stuffer
> | manager |  | : |  | manager?
> |  |  | = |  | (make-threshold-LRU-manager #f (* 1024 1024 64))
> servlet-current-directory : path-string? = (current-directory)
> | responders-servlet-loading |  | : |  | (url? any/c . -> . can-be-response?)
> |  |  | = |  | servlet-loading-responder
> | responders-servlet |  | : |  | (url? any/c . -> . can-be-response?)
> |  |  | = |  | servlet-error-responder

serve/servlet starts a server and uses a particular dispatching sequence. For some applications, this nails down too much, but users are conflicted, because the interface is so convenient. For those users, dispatch/servlet does the hardest part of serve/servlet and constructs a dispatcher just for the start servlet.
The dispatcher responds to requests that match regexp. The current directory of servlet execution is servlet-current-directory.

If stateless? is true, then the servlet is run as a stateless

> #lang web-server

module and stuffer is used as the stuffer.
The servlet is loaded with manager as its continuation manager. (The default manager limits the amount of memory to 64 MB and deals with memory pressure as discussed in the make-threshold-LRU-manager documentation.)
The servlet is run in the (current-namespace).
If a servlet fails to load, responders-servlet-loading is used. If a servlet errors during its operation, responders-servlet is used.

> > procedure
> > (serve/launch/wait |  | make-dispatcher |
> > | [ | #:connection-close? connection-close? |
> > |  | #:launch-path launch-path |
> > |  | #:banner? banner? |
> > |  | #:listen-ip listen-ip |
> > |  | #:port port |
> > |  | #:max-waiting max-waiting |
> > |  | #:ssl-cert ssl-cert |
> > |  | #:ssl-key ssl-key]) |
> → void
> make-dispatcher : (semaphore? . -> . dispatcher/c)
> connection-close? : boolean? = #f
> launch-path : (or/c false/c string?) = #f
> banner? : boolean? = #f
> listen-ip : (or/c false/c string?) = "127.0.0.1"
> port : number? = 8000
> max-waiting : exact-nonnegative-integer? = 511
> ssl-cert : (or/c false/c path-string?) = #f
> ssl-key : (or/c false/c path-string?) = #f

The other interesting part of serve/servlet is its ability to start up a server and immediately launch a browser at it. This is provided by serve/launch/wait.
It starts a server using the result of make-dispatcher as the dispatcher. make-dispatcher is supplied a semaphore that if posted, will cause the server to quit.
If launch-path is not false, then a browser is launched with that path appended to the URL to the server itself.
If banner? is true, then a banner is printed informing the user of the server’s URL.
The server listens on listen-ip and port port. If listen-ip is #f, then the server accepts connections to all of the listening machine’s addresses. Otherwise, the server accepts connections only at the interface(s) associated with the given string. For example, providing "127.0.0.1" (the default) as listen-ip creates a server that accepts only connections to "127.0.0.1" (the loopback interface) from the local machine.
max-waiting is passed to serve to control the TCP backlog.
If ssl-key and ssl-cert are not false, then the server runs in HTTPS mode with ssl-cert and ssl-key as paths to the certificate and private key.
If connection-close? is #t, then every connection is closed after one request. Otherwise, the client decides based on what HTTP version it uses.
top ← prev up next →