Browse Source

Rewrite request parsing (Step 26/?)

bel2125 8 years ago
parent
commit
7cb8c6edba
2 changed files with 21 additions and 8 deletions
  1. 20 8
      src/civetweb.c
  2. 1 0
      test/public_server.c

+ 20 - 8
src/civetweb.c

@@ -14900,22 +14900,34 @@ mg_connect_websocket_client(const char *host,
 	                   origin);
 
 	/* Connection object will be null if something goes wrong */
-	if ((conn == NULL)
-	    || (strcmp(conn->request_info.request_uri, "101") != 0)) {
+	if (con == NULL) {
 		if (!*error_buffer) {
-			/* if there is a connection, but it did not return 101,
-			 * error_buffer is not yet set */
+			/* There should be already an error message */
+			mg_snprintf(conn,
+			            NULL, /* No truncation check for ebuf */
+			            error_buffer,
+			            error_buffer_size,
+			            "Unexpected error");
+		}
+		return NULL;
+	}
+
+	if (conn->response_info.status_code != 101) {
+		/* We sent an "upgrade" request. For a correct websocket
+		 * protocol handshake, we expect a "101 Continue" response.
+		 * Otherwise it is a protocol violation. Maybe the HTTP
+		 * Server does not know websockets. */
+		if (!*error_buffer) {
+			/* set an error, if not yet set */
 			mg_snprintf(conn,
 			            NULL, /* No truncation check for ebuf */
 			            error_buffer,
 			            error_buffer_size,
 			            "Unexpected server reply");
 		}
+
 		DEBUG_TRACE("Websocket client connect error: %s\r\n", error_buffer);
-		if (conn != NULL) {
-			mg_free(conn);
-			conn = NULL;
-		}
+		mg_free(conn);
 		return conn;
 	}
 

+ 1 - 0
test/public_server.c

@@ -855,6 +855,7 @@ static void
 websock_server_ready(struct mg_connection *conn, void *udata)
 {
 	ck_assert_ptr_eq((void *)udata, (void *)7531);
+	ck_assert_ptr_ne((void *)conn, (void *)NULL);
 	WS_TEST_TRACE("Server: Websocket ready\n");
 
 	/* Send websocket welcome message */