[Asterisk-code-review] res ari: Add http prefix to generated docs (asterisk[13])

Torrey Searle asteriskteam at digium.com
Wed Aug 17 08:29:55 CDT 2016


Torrey Searle has uploaded a new change for review.

  https://gerrit.asterisk.org/3587

Change subject: res_ari: Add http prefix to generated docs
......................................................................

res_ari: Add http prefix to generated docs

updated the uri handler to include the url prefix of the http server
this enables res_ari to add it to the uris when generating docs

Change-Id: I279335a2625261a8492206c37219698f42591c2e
---
M include/asterisk/ari.h
M include/asterisk/http.h
M main/http.c
M res/res_ari.c
M tests/test_ari.c
5 files changed, 23 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/87/3587/1

diff --git a/include/asterisk/ari.h b/include/asterisk/ari.h
index c3df46a..1c54a69 100644
--- a/include/asterisk/ari.h
+++ b/include/asterisk/ari.h
@@ -144,10 +144,11 @@
  * for unit testing.
  *
  * \param uri Requested URI, relative to the docs path.
+ * \param prefix prefix that prefixes all http requests
  * \param headers HTTP headers.
  * \param[out] response RESTful HTTP response.
  */
-void ast_ari_get_docs(const char *uri, struct ast_variable *headers, struct ast_ari_response *response);
+void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers, struct ast_ari_response *response);
 
 /*! \brief Abstraction for reading/writing JSON to a WebSocket */
 struct ast_ari_websocket_session;
diff --git a/include/asterisk/http.h b/include/asterisk/http.h
index bb8973d..d5f54cc 100644
--- a/include/asterisk/http.h
+++ b/include/asterisk/http.h
@@ -101,6 +101,7 @@
 	AST_LIST_ENTRY(ast_http_uri) entry;
 	const char *description;
 	const char *uri;
+	const char *prefix;
 	ast_http_callback callback;
 	unsigned int has_subtree:1;
 	/*! Structure is malloc'd */
diff --git a/main/http.c b/main/http.c
index b2b35ff..ac5aae1 100644
--- a/main/http.c
+++ b/main/http.c
@@ -671,6 +671,8 @@
 
 	AST_RWLIST_WRLOCK(&uris);
 
+	urih->prefix = prefix;
+
 	if ( AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len ) {
 		AST_RWLIST_INSERT_HEAD(&uris, urih, entry);
 		AST_RWLIST_UNLOCK(&uris);
diff --git a/res/res_ari.c b/res/res_ari.c
index 4a0a22d..7b31358 100644
--- a/res/res_ari.c
+++ b/res/res_ari.c
@@ -579,7 +579,7 @@
 	}
 }
 
-void ast_ari_get_docs(const char *uri, struct ast_variable *headers,
+void ast_ari_get_docs(const char *uri, const char *prefix, struct ast_variable *headers,
 			  struct ast_ari_response *response)
 {
 	RAII_VAR(struct ast_str *, absolute_path_builder, NULL, ast_free);
@@ -685,13 +685,19 @@
 			}
 		}
 		if (host != NULL) {
-			ast_json_object_set(
-				obj, "basePath",
-				ast_json_stringf("http://%s/ari", host->value));
-		} else {
-			/* Without the host, we don't have the basePath */
-			ast_json_object_del(obj, "basePath");
+			if (prefix != NULL && strlen(prefix) > 0) {
+				ast_json_object_set(
+					obj, "basePath",
+					ast_json_stringf("http://%s%s/ari", host->value,prefix));
+			} else {
+				ast_json_object_set(
+					obj, "basePath",
+					ast_json_stringf("http://%s/ari", host->value));
+			}
 		}
+	} else {
+		/* Without the host, we don't have the basePath */
+		ast_json_object_del(obj, "basePath");
 	}
 
 	ast_ari_response_ok(response, obj);
@@ -969,7 +975,7 @@
 			ast_ari_response_error(&response, 405, "Method Not Allowed", "Unsupported method");
 		} else {
 			/* Skip the api-docs prefix */
-			ast_ari_get_docs(strchr(uri, '/') + 1, headers, &response);
+			ast_ari_get_docs(strchr(uri, '/') + 1, urih->prefix, headers, &response);
 		}
 	} else {
 		/* Other RESTful resources */
diff --git a/tests/test_ari.c b/tests/test_ari.c
index fc74544..a61a118 100644
--- a/tests/test_ari.c
+++ b/tests/test_ari.c
@@ -217,7 +217,7 @@
 
 	response = response_alloc();
 	headers = ast_variable_new("Host", "stasis.asterisk.org", __FILE__);
-	ast_ari_get_docs("resources.json", headers, response);
+	ast_ari_get_docs("resources.json", "", headers, response);
 	ast_test_validate(test, 200 == response->response_code);
 
 	/* basePath should be relative to the Host header */
@@ -247,7 +247,7 @@
 	}
 
 	response = response_alloc();
-	ast_ari_get_docs("resources.json", headers, response);
+	ast_ari_get_docs("resources.json",  "", headers, response);
 	ast_test_validate(test, 200 == response->response_code);
 
 	/* basePath should be relative to the Host header */
@@ -274,7 +274,7 @@
 	}
 
 	response = response_alloc();
-	ast_ari_get_docs("i-am-not-a-resource.json", headers, response);
+	ast_ari_get_docs("i-am-not-a-resource.json", "", headers, response);
 	ast_test_validate(test, 404 == response->response_code);
 
 	return AST_TEST_PASS;
@@ -297,7 +297,7 @@
 	}
 
 	response = response_alloc();
-	ast_ari_get_docs("../../../../sbin/asterisk", headers, response);
+	ast_ari_get_docs("../../../../sbin/asterisk", "", headers, response);
 	ast_test_validate(test, 404 == response->response_code);
 
 	return AST_TEST_PASS;

-- 
To view, visit https://gerrit.asterisk.org/3587
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I279335a2625261a8492206c37219698f42591c2e
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Torrey Searle <tsearle at gmail.com>



More information about the asterisk-code-review mailing list