client: allow http[s] to select targeted protocol name in vhost
authorAndy Green <andy@warmcat.com>
Wed, 15 Feb 2017 01:12:39 +0000 (09:12 +0800)
committerAndy Green <andy@warmcat.com>
Sat, 18 Feb 2017 09:27:22 +0000 (17:27 +0800)
lib/client-handshake.c
lib/libwebsockets.c
lib/server.c

index 9d4b573..4ccf73b 100644 (file)
@@ -562,6 +562,7 @@ lws_client_connect_via_info(struct lws_client_connect_info *i)
 {
        struct lws *wsi;
        int v = SPEC_LATEST_SUPPORTED;
+       const struct lws_protocols *p;
 
        if (i->context->requested_kill)
                return NULL;
@@ -590,7 +591,6 @@ lws_client_connect_via_info(struct lws_client_connect_info *i)
        wsi->ietf_spec_revision = v;
        wsi->user_space = NULL;
        wsi->state = LWSS_CLIENT_UNCONNECTED;
-       wsi->protocol = NULL;
        wsi->pending_timeout = NO_PENDING_TIMEOUT;
        wsi->position_in_fds_table = -1;
        wsi->u.hdr.c_port = i->port;
@@ -599,6 +599,15 @@ lws_client_connect_via_info(struct lws_client_connect_info *i)
                wsi->vhost = i->context->vhost_list;
 
        wsi->protocol = &wsi->vhost->protocols[0];
+
+       /* for http[s] connection, allow protocol selection by name */
+
+       if (i->method && i->vhost && i->protocol) {
+               p = lws_vhost_name_to_protocol(i->vhost, i->protocol);
+               if (p)
+                       wsi->protocol = p;
+       }
+
        if (wsi && !wsi->user_space && i->userdata) {
                wsi->user_space_externally_allocated = 1;
                wsi->user_space = i->userdata;
index bf78584..63374d0 100755 (executable)
@@ -857,6 +857,18 @@ lws_protocol_get(struct lws *wsi)
        return wsi->protocol;
 }
 
+LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
+lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
+{
+       int n;
+
+       for (n = 0; n < vh->count_protocols; n++)
+               if (!strcmp(name, vh->protocols[n].name))
+                       return &vh->protocols[n];
+
+       return NULL;
+}
+
 LWS_VISIBLE int
 lws_callback_all_protocol(struct lws_context *context,
                          const struct lws_protocols *protocol, int reason)
index 7336740..1da1529 100644 (file)
@@ -251,18 +251,6 @@ lws_select_vhost(struct lws_context *context, int port, const char *servername)
        return NULL;
 }
 
-LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
-lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
-{
-       int n;
-
-       for (n = 0; n < vh->count_protocols; n++)
-               if (!strcmp(name, vh->protocols[n].name))
-                       return &vh->protocols[n];
-
-       return NULL;
-}
-
 LWS_VISIBLE LWS_EXTERN const char *
 lws_get_mimetype(const char *file, const struct lws_http_mount *m)
 {