[asterisk-dev] [patch] manager.c and http interface

Luigi Rizzo rizzo at icir.org
Mon Oct 16 08:21:31 MST 2006


it appears that as it is now, manager.c cannot handle generic CLI
commands through the web interface.
This is because CLI commands expect to produce their output to a
file descriptor (s->fd), but for http connections the
file descriptor is not there

I am attaching below a quick and dirty patch that opens a temporary
file to buffer the output, and then dumps it to the output buffer
from where it can be reformatted in the various supported formats.

It is not terribly efficient, but it adds a lot of functionality
and lilely it can be simplified a lot, and also used to simplify
external code e.g. the case s->fd == -1 in astman_append.

Any objection to have it committed ?
Of course the XML and HTTP formatting code should be improved
a bit to handle the (currently unformatted) response from most CLI
commands, but that poses no backward compatibility issues as
the functionality is not there now...

	cheers
	luigi

Index: main/manager.c
===================================================================
--- main/manager.c	(revision 45219)
+++ main/manager.c	(working copy)
@@ -2435,6 +2435,11 @@
 			ast_build_string(&c, &len, "<body bgcolor=\"#ffffff\"><table align=center bgcolor=\"#f1f1f1\" width=\"500\">\r\n");
 			ast_build_string(&c, &len, "<tr><td colspan=\"2\" bgcolor=\"#f1f1ff\"><h1>&nbsp;&nbsp;Manager Tester</h1></td></tr>\r\n");
 		}
+		if (1) {
+			char template[32];
+			ast_copy_string(template, "/tmp/ast-http-XXXXXX", sizeof(template));
+			s->fd = mkstemp(template);
+		}
 		if (process_message(s, &m)) {
 			if (s->authenticated) {
 				if (option_verbose > 1) {
@@ -2451,6 +2456,25 @@
 			}
 			s->needdestroy = 1;
 		}
+		if (s->fd > -1) {	/* have temporary output */
+			char *buf;
+			off_t len = lseek(s->fd, 0, SEEK_END);	/* how many chars available */
+
+			if (len > 0 && (buf = ast_calloc(1, len+1))) {
+				if (!s->outputstr)
+					s->outputstr = ast_calloc(1, sizeof(*s->outputstr));
+				if (s->outputstr) {
+					lseek(s->fd, 0, SEEK_SET);
+					read(s->fd, buf, len);
+					ast_verbose("--- fd %d has %d bytes ---\n%s\n---\n", s->fd, (int)len, buf);
+					ast_dynamic_str_append(&s->outputstr, 0, "%s", buf);
+				}
+				free(buf);
+			}
+			close(s->fd);
+			s->fd = -1;
+		}
+
 		if (s->outputstr) {
 			char *tmp;
 			if (format == FORMAT_XML)


More information about the asterisk-dev mailing list