From 3a3b26658e0a3780f3f982916f7e4f3d6ed64046 Mon Sep 17 00:00:00 2001 From: Luigi Rizzo Date: Wed, 18 Oct 2006 05:31:54 +0000 Subject: [PATCH] merge xml_translate() and html_translate() into one function since they do similar things. Add a small form on top of the html output so request like http://foo:8088/asterisk/manager will suggest you what to do. Note: i suspect there is still a bug somewhere in the session matching code, as sometimes you have to login twice in order for the following commands to be recognised. Apart from this, the cli now is basically usable from a web form! git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@45475 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/manager.c | 85 +++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 57 deletions(-) diff --git a/main/manager.c b/main/manager.c index 309a3fc3d3..3630254b0d 100644 --- a/main/manager.c +++ b/main/manager.c @@ -285,7 +285,7 @@ static void xml_copy_escape(char **dst, size_t *maxlen, const char *src, int mod } } -static char *xml_translate(char *in, struct ast_variable *vars) +static char *__xml_translate(char *in, struct ast_variable *vars, int xml) { struct ast_variable *v; char *dest = NULL; @@ -298,7 +298,7 @@ static char *xml_translate(char *in, struct ast_variable *vars) int escaped = 0; int inobj = 0; int x; - + for (v = vars; v; v = v->next) { if (!dest && !strcasecmp(v->name, "ajaxdest")) dest = v->value; @@ -335,10 +335,11 @@ static char *xml_translate(char *in, struct ast_variable *vars) ast_verbose("inobj %d in_data %d line <%s>\n", inobj, in_data, val); if (ast_strlen_zero(val)) { if (in_data) { /* close data */ - ast_build_string(&tmp, &len, "'"); + ast_build_string(&tmp, &len, xml ? "'" : "\n"); in_data = 0; } - ast_build_string(&tmp, &len, " />\n"); + ast_build_string(&tmp, &len, xml ? " />\n" : + "
\r\n"); inobj = 0; continue; } @@ -356,77 +357,41 @@ static char *xml_translate(char *in, struct ast_variable *vars) } } if (!inobj) { - ast_build_string(&tmp, &len, "<%s", dest, objtype); + if (xml) + ast_build_string(&tmp, &len, "<%s", dest, objtype); + else + ast_build_string(&tmp, &len, "\n"); inobj = 1; } if (!in_data) { - ast_build_string(&tmp, &len, " "); - xml_copy_escape(&tmp, &len, var, 1 | 2); - ast_build_string(&tmp, &len, "='"); + ast_build_string(&tmp, &len, xml ? " " : ""); + xml_copy_escape(&tmp, &len, var, xml ? 1 | 2 : 0); + ast_build_string(&tmp, &len, xml ? "='" : ""); xml_copy_escape(&tmp, &len, val, 0); if (!strcmp(var, "Opaque-data")) { in_data = 1; } else { - ast_build_string(&tmp, &len, "'"); + ast_build_string(&tmp, &len, xml ? "'" : "\n"); } } else { xml_copy_escape(&tmp, &len, val, 0); } } if (inobj) - ast_build_string(&tmp, &len, " />\n"); + ast_build_string(&tmp, &len, xml ? " />\n" : "\n"); return out; } +static char *xml_translate(char *in, struct ast_variable *vars) +{ + return __xml_translate(in, vars, 1); +} + static char *html_translate(char *in) { - int x; - int colons = 0; - int breaks = 0; - size_t len; - int count = 1; - char *tmp, *var, *val, *out; - - for (x=0; in[x]; x++) { - if (in[x] == ':') - colons++; - if (in[x] == '\n') - breaks++; - } - len = strlen(in) + colons * 40 + breaks * 40; /* , "
*/ - out = ast_malloc(len); - if (!out) - return 0; - tmp = out; - while (*in) { - var = in; - while (*in && (*in >= 32)) - in++; - if (*in) { - if ((count % 4) == 0){ - ast_build_string(&tmp, &len, "
\r\n"); - } - count = 0; - while (*in && (*in < 32)) { - *in = '\0'; - in++; - count++; - } - val = strchr(var, ':'); - if (val) { - *val = '\0'; - val++; - if (*val == ' ') - val++; - ast_build_string(&tmp, &len, "%s%s\r\n", var, val); - } - } - } - return out; + return __xml_translate(in, NULL, 0); } - - static struct ast_manager_user *ast_get_manager_by_name_locked(const char *name) { struct ast_manager_user *user = NULL; @@ -2394,7 +2359,7 @@ static char *generic_http_callback(int format, struct sockaddr_in *requestor, co { struct mansession *s = NULL; unsigned long ident = 0; - char workspace[512]; + char workspace[1024]; char cookie[128]; size_t len = sizeof(workspace); int blastaway = 0; @@ -2456,8 +2421,14 @@ static char *generic_http_callback(int format, struct sockaddr_in *requestor, co if (format == FORMAT_XML) { ast_build_string(&c, &len, "\n"); } else if (format == FORMAT_HTML) { +#define ROW_FMT "%s\r\n" +#define TEST_STRING \ + "
action: cmd
\ + user pass
+
" ast_build_string(&c, &len, "\r\n"); - ast_build_string(&c, &len, "\r\n"); + ast_build_string(&c, &len, ROW_FMT, "

  Manager Tester

"); + ast_build_string(&c, &len, ROW_FMT, TEST_STRING); } { char template[32];

  Manager Tester