diff --git a/lib/client.c b/lib/client.c index 8c17109d7..3c94c4fee 100755 --- a/lib/client.c +++ b/lib/client.c @@ -589,11 +589,22 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context, wsi->protocol = &context->protocols[n]; break; } else { - char* sp = strstr(context->protocols[n].name, p); - if (sp && (sp[len] == ',' || sp[len] == '\0')) { - wsi->protocol = &context->protocols[n]; - break; + int found = 0; + const char* nameptr = context->protocols[n].name; + char* sp = strstr(nameptr, p); + while (sp && !found) { + if ((sp[len] == ',' || sp[len] == '\0') + && (sp == nameptr + || (sp > nameptr + && (sp[-1] == ',' || sp[-1] == ' ')))) { + wsi->protocol = &context->protocols[n]; + found = 1; + break; + } + sp = strstr(sp + 1, p); } + if (found) + break; } n++; } diff --git a/test-server/test-client.c b/test-server/test-client.c index e94dbed67..4102a8a1f 100644 --- a/test-server/test-client.c +++ b/test-server/test-client.c @@ -221,13 +221,13 @@ callback_lws_mirror(struct libwebsocket_context *context, static struct libwebsocket_protocols protocols[] = { { - "dumb-increment-protocol,fake-nonexistant-protocol", + "dumb-increment-protocol-nonexistant,dumb-increment-protocol,fake-nonexistant-protocol", callback_dumb_increment, 0, 20, }, { - "fake-nonexistant-protocol,lws-mirror-protocol", + "lws-mirror-protocol-nonexistant,fake-nonexistant-protocol,lws-mirror-protocol", callback_lws_mirror, 0, 128,