]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Introduce a new manager action, GetConfigJSON, which is intended to improve
authorRussell Bryant <russell@russellbryant.com>
Fri, 23 Feb 2007 00:45:04 +0000 (00:45 +0000)
committerRussell Bryant <russell@russellbryant.com>
Fri, 23 Feb 2007 00:45:04 +0000 (00:45 +0000)
performance of the GUI.  This encodes the configuration into the JSON format
in a manager header, "JSON: ".  The encoded information can be directly used
as a javascript object, so no parsing is needed.  For large configuration
files, this can greatly improve loading times in the GUI. Furthermore, the
encoding takes up a lot less space when being transmitted than the other
alternatives.  (Inspired by discussion with Pari)

Here is an example of what you get:
http://localhost:8088/asterisk/rawman?action=getconfigjson&filename=users.conf

Response: Success
JSON: {"general":["hasvoicemail=yes"],"6000":["fullname=russell","secret=1234"]}

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@56323 65c4cc65-6c06-0410-ace0-fbb531ad65f3

main/manager.c

index c1b09c2cb65dde0be334f6b50fc6564edab1bf34..a88de0cda094ea6cc9b1590e64546eb97d624eb0 100644 (file)
@@ -1059,6 +1059,52 @@ static int action_getconfig(struct mansession *s, const struct message *m)
        return 0;
 }
 
+static char mandescr_getconfigjson[] =
+"Description: A 'GetConfigJSON' action will dump the contents of a configuration\n"
+"file by category and contents in JSON format.  This only makes sense to be used\n"
+"using rawman over the HTTP interface.\n"
+"Variables:\n"
+"   Filename: Configuration filename (e.g. foo.conf)\n";
+
+static int action_getconfigjson(struct mansession *s, const struct message *m)
+{
+       struct ast_config *cfg;
+       const char *fn = astman_get_header(m, "Filename");
+       char *category = NULL;
+       struct ast_variable *v;
+       int comma1 = 0;
+
+       if (ast_strlen_zero(fn)) {
+               astman_send_error(s, m, "Filename not specified");
+               return 0;
+       }
+
+       if (!(cfg = ast_config_load_with_comments(fn))) {
+               astman_send_error(s, m, "Config file not found");
+               return 0;
+       }
+
+       astman_start_ack(s, m);
+       astman_append(s, "JSON: {");
+       while ((category = ast_category_browse(cfg, category))) {
+               int comma2 = 0;
+               astman_append(s, "%s\"%s\":[", comma1 ? "," : "", category);
+               if (!comma1)
+                       comma1 = 1;
+               for (v = ast_variable_browse(cfg, category); v; v = v->next) {
+                       astman_append(s, "%s\"%s=%s\"", comma2 ? "," : "", v->name, v->value);
+                       if (!comma2)
+                               comma2 = 1;
+               }
+               astman_append(s, "]");
+       }
+       astman_append(s, "}\r\n\r\n");
+
+       ast_config_destroy(cfg);
+
+       return 0;
+}
+
 /* helper function for action_updateconfig */
 static void handle_updates(struct mansession *s, const struct message *m, struct ast_config *cfg)
 {
@@ -2963,6 +3009,7 @@ int init_manager(void)
                ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
                ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
                ast_manager_register2("GetConfig", EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig);
+               ast_manager_register2("GetConfigJSON", EVENT_FLAG_CONFIG, action_getconfigjson, "Retrieve configuration (JSON format)", mandescr_getconfigjson);
                ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig);
                ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
                ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);