From f6585285cbe248901866dbd36e2b85c723b8f4c6 Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Fri, 6 May 2016 14:24:59 +0800
Subject: [PATCH] protocol plugins set default
Signed-off-by: Andy Green <andy@warmcat.com>
---
README.lwsws.md | 15 ++++++++++++++-
changelog | 3 ++-
lib/context.c | 28 +++++++++++++++++++++++++---
lib/private-libwebsockets.h | 1 +
lib/server.c | 9 ++++++---
5 files changed, 48 insertions(+), 8 deletions(-)
diff --git a/README.lwsws.md b/README.lwsws.md
index 63b02668..9d6c82a3 100644
--- a/README.lwsws.md
+++ b/README.lwsws.md
@@ -146,7 +146,7 @@ Vhosts can select which plugins they want to offer and give them per-vhost setti
```
"ws-protocols": [{
- "warmcat,timezoom": {
+ "warmcat-timezoom": {
"status": "ok"
}
}]
@@ -157,6 +157,19 @@ The "x":"y" parameters like "status":"ok" are made available to the protocol dur
LWS_CALLBACK_PROTOCOL_INIT (@in 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"
+
+```
+ "ws-protocols": [{
+ "warmcat-timezoom": {
+ "status": "ok",
+ "default": "1"
+ }
+ }]
+
+```
+
Other vhost options
-------------------
diff --git a/changelog b/changelog
index a6db5862..c135caba 100644
--- a/changelog
+++ b/changelog
@@ -26,7 +26,8 @@ Fixes
.woff application/font-woff
.xml application/xml
-
+5) Allow per-vhost setting of which protocol should get used
+when the protocol: header is not sent by the client
v2.0.0
diff --git a/lib/context.c b/lib/context.c
index 23d218d0..8c0a69be 100644
--- a/lib/context.c
+++ b/lib/context.c
@@ -111,13 +111,15 @@ int
lws_protocol_init(struct lws_context *context)
{
struct lws_vhost *vh = context->vhost_list;
- const struct lws_protocol_vhost_options *pvo;
+ const struct lws_protocol_vhost_options *pvo, *pvo1;
struct lws wsi;
int n;
memset(&wsi, 0, sizeof(wsi));
wsi.context = context;
+ lwsl_notice("%s\n", __func__);
+
while (vh) {
wsi.vhost = vh;
@@ -128,12 +130,32 @@ lws_protocol_init(struct lws_context *context)
pvo = lws_vhost_protocol_options(vh,
vh->protocols[n].name);
- if (pvo)
+ if (pvo) {
/*
* linked list of options specific to
* vh + protocol
*/
- pvo = pvo->options;
+ pvo1 = pvo;
+ pvo = pvo1->options;
+
+ while (pvo) {
+ lwsl_notice(" vh %s prot %s opt %s\n",
+ vh->name,
+ vh->protocols[n].name,
+ pvo->name);
+
+ if (!strcmp(pvo->name, "default")) {
+ lwsl_notice("Setting default "
+ "protocol for vh %s to %s\n",
+ vh->name,
+ vh->protocols[n].name);
+ vh->default_protocol_index = n;
+ }
+ pvo = pvo->next;
+ }
+
+ pvo = pvo1->options;
+ }
/*
* inform all the protocols that they are doing their one-time
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index cb1976be..4ed08953 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -678,6 +678,7 @@ struct lws_vhost {
int allow_non_ssl_on_ssl_port;
unsigned int user_supplied_ssl_ctx:1;
#endif
+ unsigned char default_protocol_index;
};
/*
diff --git a/lib/server.c b/lib/server.c
index 0e06cfa4..1343a243 100644
--- a/lib/server.c
+++ b/lib/server.c
@@ -942,11 +942,14 @@ upgrade_ws:
/*
* some clients only have one protocol and
* do not send the protocol list header...
- * allow it and match to protocol 0
+ * allow it and match to the vhost's default
+ * protocol (which itself defaults to zero)
*/
- lwsl_info("defaulting to prot 0 handler\n");
+ lwsl_info("defaulting to prot handler %d\n",
+ wsi->vhost->default_protocol_index);
n = 0;
- wsi->protocol = &wsi->vhost->protocols[0];
+ wsi->protocol = &wsi->vhost->protocols[
+ (int)wsi->vhost->default_protocol_index];
}
/* allocate wsi->user storage */
--
GitLab