minimal: multivhost + refactor
|
@ -847,10 +847,14 @@ lws_create_vhost(struct lws_context *context,
|
|||
} else
|
||||
vh->log_fd = (int)LWS_INVALID_FILE;
|
||||
#endif
|
||||
if (lws_context_init_server_ssl(info, vh))
|
||||
if (lws_context_init_server_ssl(info, vh)) {
|
||||
lwsl_err("%s: lws_context_init_server_ssl failed\n", __func__);
|
||||
goto bail1;
|
||||
if (lws_context_init_client_ssl(info, vh))
|
||||
}
|
||||
if (lws_context_init_client_ssl(info, vh)) {
|
||||
lwsl_err("%s: lws_context_init_client_ssl failed\n", __func__);
|
||||
goto bail1;
|
||||
}
|
||||
if (lws_context_init_server(info, vh)) {
|
||||
lwsl_err("init server failed\n");
|
||||
goto bail1;
|
||||
|
@ -867,8 +871,10 @@ lws_create_vhost(struct lws_context *context,
|
|||
/* for the case we are adding a vhost much later, after server init */
|
||||
|
||||
if (context->protocol_init_done)
|
||||
if (lws_protocol_init(context))
|
||||
if (lws_protocol_init(context)) {
|
||||
lwsl_err("%s: lws_protocol_init failed\n", __func__);
|
||||
goto bail1;
|
||||
}
|
||||
|
||||
return vh;
|
||||
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
|name|demonstrates|
|
||||
---|---
|
||||
server|Minimal examples providing a server
|
||||
client|Minimal examples providing a client
|
||||
server-http|Minimal examples providing an http server
|
||||
server-ws|Minimal examples providing a ws server (and an http server)
|
||||
client-http|Minimal examples providing an http client
|
||||
client-ws|Minimal examples providing a ws client
|
||||
client-server|Minimal examples providing client and server connections simultaneously
|
||||
|
||||
## FAQ
|
||||
|
||||
### What should I look at first
|
||||
|
||||
server/minimal-http-server
|
||||
Build and install lws itself first, these examples all want to link to it. Then
|
||||
|
||||
`server-http/minimal-http-server`
|
||||
|
||||
### Why are most of the sources split into a main C file file and a protocol file?
|
||||
|
||||
|
@ -70,3 +74,4 @@ external configuration data to a specific vhost + protocol
|
|||
combination using code. In lwsws, this is simply a matter of setting
|
||||
the desired JSON config.
|
||||
|
||||
|
||||
|
|
3
minimal-examples/client-http/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
|name|demonstrates|
|
||||
---|---
|
||||
minimal-http-client|Connects to and reads https://warmcat.com
|
|
@ -1,4 +1,3 @@
|
|||
|name|demonstrates|
|
||||
---|---
|
||||
minimal-http-client|Connects to and reads https://warmcat.com
|
||||
minimal-ws-client|Connects to the dumb-increment-protocol wss server at https://libwebsockets.org
|
6
minimal-examples/server-http/README.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
|Example|Demonstrates|
|
||||
---|---
|
||||
minimal-http-server|Serves a directory over http/1 or http/2, custom 404 handler
|
||||
minimal-http-server-libuv|Same as minimal-http-server but libuv event loop
|
||||
minimal-http-server-multivhost|Same as minimal-http-server but three different vhosts
|
||||
minimal-http-server-smp|Multiple service threads
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
|
@ -0,0 +1,12 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
set(SAMP lws-minimal-http-server-multivhost)
|
||||
set(SRCS minimal-http-server.c)
|
||||
|
||||
if (UNIX)
|
||||
set(CMAKE_C_FLAGS "-Wall -Wsign-compare -Wignored-qualifiers -Wtype-limits -Wuninitialized -Werror -Wundef ${CMAKE_C_FLAGS}" )
|
||||
endif()
|
||||
|
||||
add_executable(${SAMP} ${SRCS})
|
||||
target_link_libraries(${SAMP} -lwebsockets)
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
# lws minimal http server multivhost
|
||||
|
||||
This creates a single server that creates three vhosts listening on both :7681 and
|
||||
:7682. Two separate vhosts share listening on :7682.
|
||||
|
||||
|vhost|listens on port|serves|
|
||||
---|---|---
|
||||
localhost1|7681|./mount-origin-localhost1
|
||||
localhost2|7682|./mount-origin-localhost2
|
||||
localhost3|7682|./mount-origin-localhost3
|
||||
|
||||
Notice the last two both listen on 7682. If you visit http://localhost:7682,
|
||||
by default you will get mapped to the first one, localhost2.
|
||||
|
||||
However if you edit /etc/hosts on your machine and add
|
||||
|
||||
```
|
||||
127.0.0.1 localhost3
|
||||
```
|
||||
|
||||
so that you can visit http://localhost3:7682 in your browser, lws will use the
|
||||
`Host: localhost3` header sent by your browser to select the localhost3 vhost
|
||||
for the connection, and you will be served content from ./mount-origin-localhost3
|
||||
|
||||
## build
|
||||
|
||||
```
|
||||
$ cmake . && make
|
||||
```
|
||||
|
||||
## usage
|
||||
|
||||
```
|
||||
$ ./lws-minimal-http-server-multivhost
|
||||
[2018/03/16 09:37:20:0866] USER: LWS minimal http server-multivhost | visit http://localhost:7681 / 7682
|
||||
[2018/03/16 09:37:20:0867] NOTICE: Creating Vhost 'localhost1' port 7681, 1 protocols, IPv6 off
|
||||
[2018/03/16 09:37:20:0868] NOTICE: Creating Vhost 'localhost2' port 7682, 1 protocols, IPv6 off
|
||||
[2018/03/16 09:37:20:0869] NOTICE: Creating Vhost 'localhost3' port 7682, 1 protocols, IPv6 off
|
||||
[2018/03/16 09:37:20:0869] NOTICE: using listen skt from vhost localhost2
|
||||
```
|
||||
|
||||
Visit http://localhost:7681 and http://localhost:7682
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* lws-minimal-http-server-multivhost
|
||||
*
|
||||
* Copyright (C) 2018 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* This file is made available under the Creative Commons CC0 1.0
|
||||
* Universal Public Domain Dedication.
|
||||
*
|
||||
* This demonstrates the most minimal http server you can make with lws.
|
||||
*
|
||||
* To keep it simple, it serves stuff from the subdirectory
|
||||
* "./mount-origin" of the directory it was started in.
|
||||
* You can change that by changing mount.origin below.
|
||||
*/
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
static int interrupted;
|
||||
|
||||
static const struct lws_http_mount mount_localhost1 = {
|
||||
/* .mount_next */ NULL, /* linked-list "next" */
|
||||
/* .mountpoint */ "/", /* mountpoint URL */
|
||||
/* .origin */ "./mount-origin-localhost1",
|
||||
/* .def */ "index.html", /* default filename */
|
||||
/* .protocol */ NULL,
|
||||
/* .cgienv */ NULL,
|
||||
/* .extra_mimetypes */ NULL,
|
||||
/* .interpret */ NULL,
|
||||
/* .cgi_timeout */ 0,
|
||||
/* .cache_max_age */ 0,
|
||||
/* .auth_mask */ 0,
|
||||
/* .cache_reusable */ 0,
|
||||
/* .cache_revalidate */ 0,
|
||||
/* .cache_intermediaries */ 0,
|
||||
/* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
|
||||
/* .mountpoint_len */ 1, /* char count */
|
||||
/* .basic_auth_login_file */ NULL,
|
||||
}, mount_localhost2 = {
|
||||
/* .mount_next */ NULL, /* linked-list "next" */
|
||||
/* .mountpoint */ "/", /* mountpoint URL */
|
||||
/* .origin */ "./mount-origin-localhost2",
|
||||
/* .def */ "index.html", /* default filename */
|
||||
/* .protocol */ NULL,
|
||||
/* .cgienv */ NULL,
|
||||
/* .extra_mimetypes */ NULL,
|
||||
/* .interpret */ NULL,
|
||||
/* .cgi_timeout */ 0,
|
||||
/* .cache_max_age */ 0,
|
||||
/* .auth_mask */ 0,
|
||||
/* .cache_reusable */ 0,
|
||||
/* .cache_revalidate */ 0,
|
||||
/* .cache_intermediaries */ 0,
|
||||
/* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
|
||||
/* .mountpoint_len */ 1, /* char count */
|
||||
/* .basic_auth_login_file */ NULL,
|
||||
}, mount_localhost3 = {
|
||||
/* .mount_next */ NULL, /* linked-list "next" */
|
||||
/* .mountpoint */ "/", /* mountpoint URL */
|
||||
/* .origin */ "./mount-origin-localhost3",
|
||||
/* .def */ "index.html", /* default filename */
|
||||
/* .protocol */ NULL,
|
||||
/* .cgienv */ NULL,
|
||||
/* .extra_mimetypes */ NULL,
|
||||
/* .interpret */ NULL,
|
||||
/* .cgi_timeout */ 0,
|
||||
/* .cache_max_age */ 0,
|
||||
/* .auth_mask */ 0,
|
||||
/* .cache_reusable */ 0,
|
||||
/* .cache_revalidate */ 0,
|
||||
/* .cache_intermediaries */ 0,
|
||||
/* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
|
||||
/* .mountpoint_len */ 1, /* char count */
|
||||
/* .basic_auth_login_file */ NULL,
|
||||
};
|
||||
|
||||
void sigint_handler(int sig)
|
||||
{
|
||||
interrupted = 1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_context *context;
|
||||
int n = 0;
|
||||
|
||||
signal(SIGINT, sigint_handler);
|
||||
|
||||
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
|
||||
info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
|
||||
|
||||
lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_USER
|
||||
/* | LLL_INFO */ /* | LLL_DEBUG */, NULL);
|
||||
|
||||
lwsl_user("LWS minimal http server-multivhost | visit http://localhost:7681 / 7682\n");
|
||||
|
||||
/*
|
||||
* Because of LWS_SERVER_OPTION_EXPLICIT_VHOSTS, this only creates
|
||||
* the context and no longer creates a default vhost
|
||||
*/
|
||||
context = lws_create_context(&info);
|
||||
if (!context) {
|
||||
lwsl_err("lws init failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* it's our job now to create the vhosts we want:
|
||||
*
|
||||
* - "localhost1" listen on 7681 and serve ./mount-origin-localhost1/
|
||||
* - "localhost2" listen on 7682 and serve ./mount-origin-localhost2/
|
||||
* - "localhost3" share 7682 and serve ./mount-origin-localhost3/
|
||||
*
|
||||
* Note lws supports dynamic vhost creation and destruction at runtime.
|
||||
* When using multi-vhost with your own protocols, you must provide a
|
||||
* pvo for each vhost naming each protocol you want enabled on it.
|
||||
* minimal-ws-server-threads demonstrates how to provide pvos.
|
||||
*/
|
||||
|
||||
info.port = 7681;
|
||||
info.mounts = &mount_localhost1;
|
||||
info.error_document_404 = "/404.html";
|
||||
info.vhost_name = "localhost1";
|
||||
|
||||
if (!lws_create_vhost(context, &info)) {
|
||||
lwsl_err("Failed to create first vhost\n");
|
||||
goto bail;
|
||||
}
|
||||
|
||||
info.port = 7682;
|
||||
info.mounts = &mount_localhost2;
|
||||
info.error_document_404 = "/404.html";
|
||||
info.vhost_name = "localhost2";
|
||||
|
||||
if (!lws_create_vhost(context, &info)) {
|
||||
lwsl_err("Failed to create second vhost\n");
|
||||
goto bail;
|
||||
}
|
||||
|
||||
/* a second vhost listens on port 7682 */
|
||||
info.mounts = &mount_localhost3;
|
||||
info.error_document_404 = "/404.html";
|
||||
info.vhost_name = "localhost3";
|
||||
|
||||
if (!lws_create_vhost(context, &info)) {
|
||||
lwsl_err("Failed to create third vhost\n");
|
||||
goto bail;
|
||||
}
|
||||
|
||||
while (n >= 0 && !interrupted)
|
||||
n = lws_service(context, 1000);
|
||||
|
||||
bail:
|
||||
lws_context_destroy(context);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
<h1>404 (vhost localhost1)</h1>
|
||||
Sorry, that file doesn't exist.
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
@ -0,0 +1,14 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
|
||||
Hello from the <b>minimal http server multivhost example</b>.<br>
|
||||
<br>
|
||||
This was served from <i>./mount-origin-<b>localhost1</b>/index.html</i><br>
|
||||
<br>
|
||||
You can confirm the 404 page handler by going to this
|
||||
nonexistant <a href="notextant.html">page</a>.
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
|
@ -0,0 +1,9 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
<h1>404 (vhost localhost2)</h1>
|
||||
Sorry, that file doesn't exist.
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
@ -0,0 +1,14 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
|
||||
Hello from the <b>minimal http server multivhost example</b>.<br>
|
||||
<br>
|
||||
This was served from <i>./mount-origin-<b>localhost2</b>/index.html</i><br>
|
||||
<br>
|
||||
You can confirm the 404 page handler by going to this
|
||||
nonexistant <a href="notextant.html">page</a>.
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
|
@ -0,0 +1,9 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
<h1>404 (vhost localhost3)</h1>
|
||||
Sorry, that file doesn't exist.
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
@ -0,0 +1,14 @@
|
|||
<meta charset="UTF-8">
|
||||
<html>
|
||||
<body>
|
||||
<img src="libwebsockets.org-logo.png"><br>
|
||||
|
||||
Hello from the <b>minimal http server multivhost example</b>.<br>
|
||||
<br>
|
||||
This was served from <i>./mount-origin-<b>localhost3</b>/index.html</i><br>
|
||||
<br>
|
||||
You can confirm the 404 page handler by going to this
|
||||
nonexistant <a href="notextant.html">page</a>.
|
||||
</body>
|
||||
</html>
|
||||
|
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
|
@ -1,8 +1,5 @@
|
|||
|Example|Demonstrates|
|
||||
---|---
|
||||
minimal-http-server|Serves a directory over http/1 or http/2, custom 404 handler
|
||||
minimal-http-server-libuv|Same as minimal-http-server but libuv event loop
|
||||
minimal-http-server-smp|Multiple service threads
|
||||
minimal-ws-server|Serves an index.html over http that opens a ws shared chat client in a browser
|
||||
minimal-ws-server-pmd|Simple ws server with permessage-deflate support
|
||||
minimal-ws-server-pmd-bulk|Simple ws server showing how to pass bulk data with permessage-deflate
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 6.9 KiB After Width: | Height: | Size: 6.9 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 6.9 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 6.9 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 6.9 KiB |