introduce libwebsocket_client_connect_extended

Signed-off-by: David Brooks <dave@bcs.co.nz>
Signed-off-by: Andy Green <andy@warmcat.com>
--
diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 84b744f..f8d0b69 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -358,3 +358,45 @@
 
 	return NULL;
 }
+
+
+/**
+ * libwebsocket_client_connect_extended() - Connect to another websocket server
+ * @context:	Websocket context
+ * @address:	Remote server address, eg, "myserver.com"
+ * @port:	Port to connect to on the remote server, eg, 80
+ * @ssl_connection:	0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
+ *			signed certs
+ * @path:	Websocket path on server
+ * @host:	Hostname on server
+ * @origin:	Socket origin name
+ * @protocol:	Comma-separated list of protocols being asked for from
+ *		the server, or just one.  The server will pick the one it
+ *		likes best.
+ * @ietf_version_or_minus_one: -1 to ask to connect using the default, latest
+ * 		protocol supported, or the specific protocol ordinal
+ * @userdata: Pre-allocated user data
+ *
+ *	This function creates a connection to a remote server
+ */
+
+struct libwebsocket *
+libwebsocket_client_connect_extended(struct libwebsocket_context *context,
+			      const char *address,
+			      int port,
+			      int ssl_connection,
+			      const char *path,
+			      const char *host,
+			      const char *origin,
+			      const char *protocol,
+			      int ietf_version_or_minus_one,
+            void *userdata)
+{
+	struct libwebsocket *ws =
+		libwebsocket_client_connect(context, address, port, ssl_connection, path, host, origin, protocol, ietf_version_or_minus_one) ;
+
+	if (ws && !ws->user_space && userdata)
+		ws->user_space = userdata ;
+
+	return ws ;
+  }
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 15b59f4..f34c553 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -391,7 +391,7 @@
 #ifdef LWS_OPENSSL_SUPPORT
 	}
 #endif
-	if (wsi->user_space)
+	if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
 		free(wsi->user_space);
 
 	free(wsi);
@@ -1198,7 +1198,7 @@
 		 * default to first protocol
 		 */
 		wsi->protocol = &context->protocols[0];
-
+		wsi->c_callback = wsi->protocol->callback;
 		free(wsi->c_protocol);
 
 		goto check_accept;
@@ -1235,10 +1235,12 @@
 	 */
 	n = 0;
 	wsi->protocol = NULL;
-	while (context->protocols[n].callback) {
+	while (context->protocols[n].callback && !wsi->protocol) {  /* Stop after finding first one?? */
 		if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
-					   context->protocols[n].name) == 0)
+					   context->protocols[n].name) == 0) {
 			wsi->protocol = &context->protocols[n];
+			wsi->c_callback = wsi->protocol->callback;
+		}
 		n++;
 	}
 
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index f8bfda4..c24f09d 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -675,6 +675,18 @@
 			      const char *protocol,
 			      int ietf_version_or_minus_one);
 
+LWS_EXTERN struct libwebsocket *
+libwebsocket_client_connect_extended(struct libwebsocket_context *clients,
+			      const char *address,
+			      int port,
+			      int ssl_connection,
+			      const char *path,
+			      const char *host,
+			      const char *origin,
+			      const char *protocol,
+			      int ietf_version_or_minus_one,
+			      void *userdata);
+
 LWS_EXTERN const char *
 libwebsocket_canonical_hostname(struct libwebsocket_context *context);
 
diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html
index afffe0d..18b1620 100644
--- a/libwebsockets-api-doc.html
+++ b/libwebsockets-api-doc.html
@@ -468,6 +468,51 @@
 This function creates a connection to a remote server
 </blockquote>
 <hr>
+<h2>libwebsocket_client_connect_extended - Connect to another websocket server</h2>
+<i>struct libwebsocket *</i>
+<b>libwebsocket_client_connect_extended</b>
+(<i>struct libwebsocket_context *</i> <b>context</b>,
+<i>const char *</i> <b>address</b>,
+<i>int</i> <b>port</b>,
+<i>int</i> <b>ssl_connection</b>,
+<i>const char *</i> <b>path</b>,
+<i>const char *</i> <b>host</b>,
+<i>const char *</i> <b>origin</b>,
+<i>const char *</i> <b>protocol</b>,
+<i>int</i> <b>ietf_version_or_minus_one</b>,
+<i>void *</i> <b>userdata</b>)
+<h3>Arguments</h3>
+<dl>
+<dt><b>context</b>
+<dd>Websocket context
+<dt><b>address</b>
+<dd>Remote server address, eg, "myserver.com"
+<dt><b>port</b>
+<dd>Port to connect to on the remote server, eg, 80
+<dt><b>ssl_connection</b>
+<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
+signed certs
+<dt><b>path</b>
+<dd>Websocket path on server
+<dt><b>host</b>
+<dd>Hostname on server
+<dt><b>origin</b>
+<dd>Socket origin name
+<dt><b>protocol</b>
+<dd>Comma-separated list of protocols being asked for from
+the server, or just one.  The server will pick the one it
+likes best.
+<dt><b>ietf_version_or_minus_one</b>
+<dd>-1 to ask to connect using the default, latest
+protocol supported, or the specific protocol ordinal
+<dt><b>userdata</b>
+<dd>Pre-allocated user data
+</dl>
+<h3>Description</h3>
+<blockquote>
+This function creates a connection to a remote server
+</blockquote>
+<hr>
 <h2>callback - User server actions</h2>
 <i>LWS_EXTERN int</i>
 <b>callback</b>
@@ -509,6 +554,11 @@
 after the server completes a handshake with
 an incoming client
 </blockquote>
+<h3>LWS_CALLBACK_CLIENT_CONNECTION_ERROR</h3>
+<blockquote>
+the request client connection has
+been unable to complete a handshake with the remote server
+</blockquote>
 <h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
 <blockquote>
 after your client connection completed
@@ -730,7 +780,7 @@
 (<i>struct libwebsocket_context *</i> <b>context</b>,
 <i>struct libwebsocket_extension *</i> <b>ext</b>,
 <i>struct libwebsocket *</i> <b>wsi</b>,
-<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
+<i>enum libwebsocket_extension_callback_reasons</i> <b>reason</b>,
 <i>void *</i> <b>user</b>,
 <i>void *</i> <b>in</b>,
 <i>size_t</i> <b>len</b>)
@@ -815,7 +865,7 @@
 <h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
 <b>struct libwebsocket_protocols</b> {<br>
 &nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
-&nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket_context * context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br>
+&nbsp; &nbsp; <i>callback_function *</i> <b>callback</b>;<br>
 &nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
 &nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
 &nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
@@ -861,7 +911,7 @@
 <h2>struct libwebsocket_extension - An extension we know how to cope with</h2>
 <b>struct libwebsocket_extension</b> {<br>
 &nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
-&nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket_context *context,struct libwebsocket_extension *ext,struct libwebsocket *wsi,enum libwebsocket_extension_callback_reasons reason,void *user, void *in, size_t len)</i>;<br>
+&nbsp; &nbsp; <i>extension_callback_function *</i> <b>callback</b>;<br>
 &nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
 &nbsp; &nbsp; <i>void *</i> <b>per_context_private_data</b>;<br>
 };<br>