[asterisk-commits] dlee: branch dlee/ari-url-shuffle r391763 - /team/dlee/ari-url-shuffle/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 13 21:48:57 CDT 2013


Author: dlee
Date: Thu Jun 13 21:48:55 2013
New Revision: 391763

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=391763
Log:
Allow clients to connect without specifying subprotocol if only one is registered.

Modified:
    team/dlee/ari-url-shuffle/res/res_http_websocket.c

Modified: team/dlee/ari-url-shuffle/res/res_http_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_http_websocket.c?view=diff&rev=391763&r1=391762&r2=391763
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_http_websocket.c (original)
+++ team/dlee/ari-url-shuffle/res/res_http_websocket.c Thu Jun 13 21:48:55 2013
@@ -497,6 +497,27 @@
 	return 0;
 }
 
+/*!
+ * \brief If the server has exactly one configured protocol, return it.
+ */
+static struct websocket_protocol *one_protocol(
+	struct ast_websocket_server *server)
+{
+	struct websocket_protocol *protocol;
+	struct ao2_iterator i;
+	SCOPED_AO2LOCK(lock, server->protocols);
+
+	if (ao2_container_count(server->protocols) != 1) {
+		return NULL;
+	}
+
+	i = ao2_iterator_init(server->protocols, 0);
+	protocol = ao2_iterator_next(&i);
+	ast_assert(protocol != NULL);
+	ao2_iterator_destroy(&i);
+	return protocol;
+}
+
 int ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
 {
 	struct ast_variable *v;
@@ -541,11 +562,18 @@
 		ast_http_error(ser, 426, "Upgrade Required", NULL);
 		return -1;
 	} else if (ast_strlen_zero(requested_protocols)) {
-		ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols requested\n",
-			ast_sockaddr_stringify(&ser->remote_address));
-		fputs("HTTP/1.1 400 Bad Request\r\n"
-		      "Sec-WebSocket-Version: 7, 8, 13\r\n\r\n", ser->f);
-		return -1;
+		/* If there's only a single protocol registered, and the
+		 * client doesn't specify what protocol it's using, go ahead
+		 * and accept the connection */
+		protocol_handler = one_protocol(server);
+		if (!protocol_handler) {
+			/* Multiple registered subprotocols; client must specify */
+			ast_log(LOG_WARNING, "WebSocket connection from '%s' could not be accepted - no protocols requested\n",
+				ast_sockaddr_stringify(&ser->remote_address));
+			fputs("HTTP/1.1 400 Bad Request\r\n"
+				"Sec-WebSocket-Version: 7, 8, 13\r\n\r\n", ser->f);
+			return -1;
+		}
 	} else if (key1 && key2) {
 		/* Specification defined in http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76 and
 		 * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00 -- not currently supported*/
@@ -557,10 +585,8 @@
 	}
 
 	/* Iterate through the requested protocols trying to find one that we have a handler for */
-	while ((protocol = strsep(&requested_protocols, ","))) {
-		if ((protocol_handler = ao2_find(server->protocols, ast_strip(protocol), OBJ_KEY))) {
-			break;
-		}
+	while (!protocol_handler && (protocol = strsep(&requested_protocols, ","))) {
+		protocol_handler = ao2_find(server->protocols, ast_strip(protocol), OBJ_KEY);
 	}
 
 	/* If no protocol handler exists bump this back to the requester */




More information about the asterisk-commits mailing list