diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 9d4b573d..4ccf73b4 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -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; diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index bf78584a..63374d05 100755 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -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) diff --git a/lib/server.c b/lib/server.c index 73367406..1da15295 100644 --- a/lib/server.c +++ b/lib/server.c @@ -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) {