[asterisk-commits] dlee: branch dlee/ari-url-shuffle r391621 - in /team/dlee/ari-url-shuffle: in...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 13 08:11:46 CDT 2013


Author: dlee
Date: Thu Jun 13 08:11:45 2013
New Revision: 391621

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=391621
Log:
Attach a WebSocket to a RESTful URL

Modified:
    team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h
    team/dlee/ari-url-shuffle/res/res_stasis_http.c
    team/dlee/ari-url-shuffle/tests/test_stasis_http.c

Modified: team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h?view=diff&rev=391621&r1=391620&r2=391621
==============================================================================
--- team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h (original)
+++ team/dlee/ari-url-shuffle/include/asterisk/stasis_http.h Thu Jun 13 08:11:45 2013
@@ -83,6 +83,8 @@
 	int response_code;
 	/*! Corresponding text for the response code */
 	const char *response_text; // Shouldn't http.c handle this?
+	/*! Flag to indicate that no further response is needed */
+	int no_response:1;
 };
 
 /*!
@@ -108,14 +110,17 @@
  * Only call from res_stasis_http and test_stasis_http. Only public to allow
  * for unit testing.
  *
+ * \param ser TCP/TLS connection.
  * \param uri HTTP URI, relative to the API path.
  * \param method HTTP method.
  * \param get_params HTTP \c GET parameters.
  * \param headers HTTP headers.
  * \param[out] response RESTful HTTP response.
  */
-void stasis_http_invoke(const char *uri, enum ast_http_method method, struct ast_variable *get_params,
-			struct ast_variable *headers, struct stasis_http_response *response);
+void stasis_http_invoke(struct ast_tcptls_session_instance *ser,
+	const char *uri, enum ast_http_method method,
+	struct ast_variable *get_params, struct ast_variable *headers,
+	struct stasis_http_response *response);
 
 /*!
  * \internal

Modified: team/dlee/ari-url-shuffle/res/res_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/res/res_stasis_http.c?view=diff&rev=391621&r1=391620&r2=391621
==============================================================================
--- team/dlee/ari-url-shuffle/res/res_stasis_http.c (original)
+++ team/dlee/ari-url-shuffle/res/res_stasis_http.c Thu Jun 13 08:11:45 2013
@@ -495,11 +495,21 @@
 	}
 }
 
-void stasis_http_invoke(const char *uri,
-			enum ast_http_method method,
-			struct ast_variable *get_params,
-			struct ast_variable *headers,
-			struct stasis_http_response *response)
+static void invoke_websocket(struct ast_websocket_server *ws_server,
+	struct ast_tcptls_session_instance *ser, const char *uri,
+	enum ast_http_method method, struct ast_variable *get_vars,
+	struct ast_variable *headers, struct ast_variable *path_vars)
+{
+	struct ast_http_uri fake_urih = {
+		.data = ws_server,
+	};
+	ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_vars, headers);
+}
+
+void stasis_http_invoke(struct ast_tcptls_session_instance *ser,
+	const char *uri, enum ast_http_method method,
+	struct ast_variable *get_params, struct ast_variable *headers,
+	struct stasis_http_response *response)
 {
 	RAII_VAR(char *, response_text, NULL, ast_free);
 	RAII_VAR(struct stasis_rest_handlers *, root, NULL, ao2_cleanup);
@@ -555,6 +565,16 @@
 		stasis_http_response_error(
 			response, 405, "Method Not Allowed",
 			"Invalid method");
+		return;
+	}
+
+	if (handler->ws_server && method == AST_HTTP_GET) {
+		/* WebSocket! */
+		invoke_websocket(handler->ws_server, ser, uri, method,
+			get_params, headers, path_vars);
+		/* Set the response code to Switching Protocols so the caller
+		 * knows what happened */
+		response->no_response = 1;
 		return;
 	}
 
@@ -816,7 +836,14 @@
 		}
 	} else {
 		/* Other RESTful resources */
-		stasis_http_invoke(uri, method, get_params, headers, &response);
+		stasis_http_invoke(ser, uri, method, get_params, headers,
+			&response);
+	}
+
+	if (response.no_response) {
+		/* The handler indicates no further response is necessary.
+		 * Probably because it already handled it */
+		return 0;
 	}
 
 	/* Leaving message unset is only allowed for 204 (No Content).

Modified: team/dlee/ari-url-shuffle/tests/test_stasis_http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-url-shuffle/tests/test_stasis_http.c?view=diff&rev=391621&r1=391620&r2=391621
==============================================================================
--- team/dlee/ari-url-shuffle/tests/test_stasis_http.c (original)
+++ team/dlee/ari-url-shuffle/tests/test_stasis_http.c Thu Jun 13 08:11:45 2013
@@ -343,7 +343,7 @@
 				 "head2", "head-two",
 				 "path_vars");
 
-	stasis_http_invoke("foo", AST_HTTP_GET, get_params, headers, response);
+	stasis_http_invoke(NULL, "foo", AST_HTTP_GET, get_params, headers, response);
 
 	ast_test_validate(test, 1 == invocation_count);
 	ast_test_validate(test, 200 == response->response_code);
@@ -380,7 +380,7 @@
 				 "path_vars",
 				 "bam", "foshizzle");
 
-	stasis_http_invoke("foo/foshizzle", AST_HTTP_GET, get_params, headers, response);
+	stasis_http_invoke(NULL, "foo/foshizzle", AST_HTTP_GET, get_params, headers, response);
 
 	ast_test_validate(test, 1 == invocation_count);
 	ast_test_validate(test, 200 == response->response_code);
@@ -417,7 +417,7 @@
 				 "path_vars",
 				 "bam", "foshizzle");
 
-	stasis_http_invoke("foo/foshizzle/bang", AST_HTTP_DELETE, get_params, headers, response);
+	stasis_http_invoke(NULL, "foo/foshizzle/bang", AST_HTTP_DELETE, get_params, headers, response);
 
 	ast_test_validate(test, 1 == invocation_count);
 	ast_test_validate(test, 204 == response->response_code);
@@ -467,7 +467,7 @@
 				 "head2", "head-two",
 				 "path_vars");
 
-	stasis_http_invoke("foo/bar", AST_HTTP_POST, get_params, headers, response);
+	stasis_http_invoke(NULL, "foo/bar", AST_HTTP_POST, get_params, headers, response);
 
 	ast_test_validate(test, 1 == invocation_count);
 	ast_test_validate(test, 200 == response->response_code);
@@ -496,7 +496,7 @@
 
 	fixture = setup_invocation_test();
 	response = response_alloc();
-	stasis_http_invoke("foo", AST_HTTP_POST, get_params, headers, response);
+	stasis_http_invoke(NULL, "foo", AST_HTTP_POST, get_params, headers, response);
 
 	ast_test_validate(test, 0 == invocation_count);
 	ast_test_validate(test, 405 == response->response_code);
@@ -524,7 +524,7 @@
 
 	fixture = setup_invocation_test();
 	response = response_alloc();
-	stasis_http_invoke("foo/fizzle/i-am-not-a-resource", AST_HTTP_GET, get_params, headers, response);
+	stasis_http_invoke(NULL, "foo/fizzle/i-am-not-a-resource", AST_HTTP_GET, get_params, headers, response);
 
 	ast_test_validate(test, 0 == invocation_count);
 	ast_test_validate(test, 404 == response->response_code);




More information about the asterisk-commits mailing list