Browse Source

Add websocket connect secure API (#853)

bel2125 5 years ago
parent
commit
ace251b5c1
2 changed files with 38 additions and 10 deletions
  1. 11 0
      include/civetweb.h
  2. 27 10
      src/civetweb.c

+ 11 - 0
include/civetweb.h

@@ -1458,6 +1458,17 @@ mg_connect_client_secure(const struct mg_client_options *client_options,
                          size_t error_buffer_size);
 
 
+CIVETWEB_API struct mg_connection *mg_connect_websocket_client_secure(
+    const struct mg_client_options *client_options,
+    char *error_buffer,
+    size_t error_buffer_size,
+    const char *path,
+    const char *origin,
+    mg_websocket_data_handler data_func,
+    mg_websocket_close_handler close_func,
+    void *user_data);
+
+
 #if defined(MG_LEGACY_INTERFACE) /* 2019-11-02 */
 enum { TIMEOUT_INFINITE = -1 };
 #endif

+ 27 - 10
src/civetweb.c

@@ -17784,6 +17784,7 @@ mg_connect_websocket_client_impl(const struct mg_client_options *client_options,
 
 	int port = client_options->port;
 	const char *host = client_options->host;
+	int i;
 
 	if (origin != NULL) {
 		handshake_req = "GET %s HTTP/1.1\r\n"
@@ -17810,16 +17811,32 @@ mg_connect_websocket_client_impl(const struct mg_client_options *client_options,
 #endif
 
 	/* Establish the client connection and request upgrade */
-	conn = mg_download(host,
-	                   port,
-	                   use_ssl,
-	                   error_buffer,
-	                   error_buffer_size,
-	                   handshake_req,
-	                   path,
-	                   host,
-	                   magic,
-	                   origin);
+	conn =
+	    mg_connect_client(host, port, use_ssl, error_buffer, error_buffer_size);
+
+	if (conn == NULL) {
+		/* error_buffer already filled */
+		return NULL;
+	}
+
+	i = mg_printf(conn, handshake_req, path, host, magic, origin);
+	if (i <= 0) {
+		mg_snprintf(conn,
+		            NULL, /* No truncation check for ebuf */
+		            error_buffer,
+		            error_buffer_size,
+		            "%s",
+		            "Error sending request");
+		mg_close_connection(conn);
+		return NULL;
+	}
+
+	conn->data_len = 0;
+	if (!get_response(conn, error_buffer, error_buffer_size, &i)) {
+		mg_close_connection(conn);
+		return NULL;
+	}
+	conn->request_info.local_uri = conn->request_info.request_uri;
 
 #if defined(__clang__)
 #pragma clang diagnostic pop