![]() |
libwebsockets
Lightweight C library for HTML5 websockets
|
lwsws is an implementation of a very lightweight, ws-capable generic web server, which uses libwebsockets to implement everything underneath.
If you are basically implementing a standalone server with lws, you can avoid reinventing the wheel and use a debugged server including lws.
Just enable -DLWS_WITH_LWSWS=1 at cmake-time.
It enables libuv and plugin support automatically.
NOTICE on Ubuntu, the default libuv package is called "libuv-0.10". This is ancient.
You should replace this with libuv1 and libuv1-dev before proceeding.
lwsws uses JSON config files, they're pure JSON except:
There is a single file intended for global settings
/etc/lwsws/conf
and a config directory intended to take one file per vhost
/etc/lwsws/conf.d/warmcat.com
To get started quickly, an example config reproducing the old test server on port 7681, non-SSL is provided. To set it up
One server can run many vhosts, where SSL is in use SNI is used to match the connection to a vhost and its vhost-specific SSL keys during SSL negotiation.
Listing multiple vhosts looks something like this
That sets up three vhosts all called "localhost" on ports 443 and 7681 with SSL, and port 80 without SSL but with a forced redirect to https://localhost
The vhost name field is used to match on incoming SNI or Host: header, so it must always be the host name used to reach the vhost externally.
Vhosts by default have available the union of any initial protocols from context creation time, and any protocols exposed by plugins.
Vhosts can select which plugins they want to offer and give them per-vhost settings using this syntax
The "x":"y" parameters like "status":"ok" are made available to the protocol during its per-vhost LWS_CALLBACK_PROTOCOL_INIT ( is a pointer to a linked list of struct lws_protocol_vhost_options containing the name and value pointers).
To indicate that a protocol should be used when no Protocol: header is sent by the client, you can use "default": "1"
host-ssl-cert
, host-ssl-ca
and host-ssl-key
are given, then the vhost supports SSL.Each vhost may have its own certs, SNI is used during the initial connection negotiation to figure out which certs to use by the server name it's asking for from the request DNS name.
keeplive-timeout
(in secs) defaults to 60 for lwsws, it may be set as a vhost optioninterface
lets you specify which network interface to listen on, if not given listens on all"enable-client-ssl"
: "1"
enables the vhost's client SSL context, you will need this if you plan to create client conections on the vhost that will use SSL. You don't need it if you only want http / ws client connections.If you need to allow weaker ciphers,you can provide an alternative list here per-vhost.
The values are derived from /usr/include/openssl/ssl.h
would equate to
allows you to set arbitrary headers on every file served by the vhost
recommended vhost headers for good client security are
Where mounts are given in the vhost definition, then directory contents may be auto-served if it matches the mountpoint.
Mount protocols are used to control what kind of translation happens
Eg, with this mountpoint
The uri /file.jpg would serve /var/www/mysite.com/file.jpg, since / matched.
1) Some protocols may want "per-mount options" in name:value format. You can provide them using "pmo"
{ "mountpoint": "/stuff", "origin": "callback://myprotocol", "pmo": [{ "myname": "myvalue" }] }
2) When using a cgi:// protcol origin at a mountpoint, you may also give cgi environment variables specific to the mountpoint like this
This allows you to customize one cgi depending on the mountpoint (and / or vhost).
3) It's also possible to set the cgi timeout (in secs) per cgi:// mount, like this
4) callback://
protocol may be used when defining a mount to associate a named protocol callback with the URL namespace area. For example
All handling of client access to /formtest[anything] will be passed to the callback registered to the protocol "protocol-post-demo".
This is useful for handling POST http body content or general non-cgi http payload generation inside a plugin.
See the related notes in README.coding.md
5) Cache policy of the files in the mount can also be set. If no options are given, the content is marked uncacheable.
6) You can also define a list of additional mimetypes per-mount
Normally a file suffix MUST match one of the canned mimetypes or one of the extra mimetypes, or the file is not served. This adds a little bit of security because even if there is a bug somewhere and the mount dirs are circumvented, lws will not serve, eg, /etc/passwd.
If you provide an extra mimetype entry
"*": ""
Then any file is served, if the mimetype was not known then it is served without a Content-Type: header.
Protcols and extensions may also be provided from "plugins", these are lightweight dynamic libraries. They are scanned for at init time, and any protocols and extensions found are added to the list given at context creation time.
Protocols receive init (LWS_CALLBACK_PROTOCOL_INIT) and destruction (LWS_CALLBACK_PROTOCOL_DESTROY) callbacks per-vhost, and there are arrangements they can make per-vhost allocations and get hold of the correct pointer from the wsi at the callback.
This allows a protocol to choose to strictly segregate data on a per-vhost basis, and also allows the plugin to handle its own initialization and context storage.
To help that happen conveniently, there are some new apis
dumb increment, mirror and status protocol plugins are provided as examples.
Packages that have their own lws plugins can install them in their own preferred dir and ask lwsws to scan there by using a config fragment like this, in its own conf.d/ file managed by the other package
One provided protocol can be used to monitor the server status.
Enable the protocol like this on a vhost's ws-protocols section
"update-ms" is used to control how often updated JSON is sent on a ws link.
And map the provided HTML into the vhost in the mounts section
You might choose to put it on its own vhost which has "interface": "lo", so it's not externally visible.
lwsws needs a service file like this as /usr/lib/systemd/system/lwsws.service
You can find this prepared in ./lwsws/usr-lib-systemd-system-lwsws.service
For correct operation with logrotate, /etc/logrotate.d/lwsws
(if that's where we're putting the logs) should contain
You can find this prepared in /lwsws/etc-logrotate.d-lwsws
Prepare the log directory like this