2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
23 * This program implements a tiny http server supporting the "get" method
24 * only and was inspired by micro-httpd by Jef Poskanzer
27 #include <sys/types.h>
33 #include <netinet/in.h>
35 #include <sys/socket.h>
36 #include <sys/signal.h>
37 #include <arpa/inet.h>
42 #include "asterisk/cli.h"
43 #include "asterisk/http.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/strings.h"
48 #define DEFAULT_PREFIX "asterisk"
50 struct ast_http_server_instance {
53 struct sockaddr_in requestor;
54 ast_http_callback callback;
57 static struct ast_http_uri *uris;
59 static int httpfd = -1;
60 static pthread_t master = AST_PTHREADT_NULL;
61 static char prefix[MAX_PREFIX];
62 static int prefix_len = 0;
63 static struct sockaddr_in oldsin;
66 static char *httpstatus_callback(struct sockaddr_in *req, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength)
69 size_t reslen = sizeof(result);
71 struct ast_variable *v;
72 char iabuf[INET_ADDRSTRLEN];
74 ast_build_string(&c, &reslen,
76 "<title>Asterisk HTTP Status</title>\r\n"
77 "<body bgcolor=\"#ffffff\">\r\n"
78 "<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
79 "<h2> Asterisk™ HTTP Status</h2></td></tr>\r\n");
81 ast_build_string(&c, &reslen, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
82 ast_build_string(&c, &reslen, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
83 ast_inet_ntoa(iabuf, sizeof(iabuf), oldsin.sin_addr));
84 ast_build_string(&c, &reslen, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
85 ntohs(oldsin.sin_port));
86 ast_build_string(&c, &reslen, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
89 ast_build_string(&c, &reslen, "<tr><td><i>Submitted Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
92 ast_build_string(&c, &reslen, "</table><center><font size=\"-1\"><i>Asterisk and Digium are registered trademarks of Digium, Inc.</i></font></center></body>\r\n");
93 return strdup(result);
96 static struct ast_http_uri statusuri = {
97 .callback = httpstatus_callback,
98 .description = "Asterisk HTTP General Status",
103 char *ast_http_error(int status, const char *title, const char *extra_header, const char *text)
107 "Content-type: text/html\r\n"
110 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
112 "<title>%d %s</title>\r\n"
117 "<address>Asterisk Server</address>\r\n"
118 "</body></html>\r\n",
119 (extra_header ? extra_header : ""), status, title, title, text);
123 int ast_http_uri_link(struct ast_http_uri *urih)
125 struct ast_http_uri *prev=uris;
126 if (!uris || strlen(uris->uri) <= strlen(urih->uri)) {
130 while (prev->next && (strlen(prev->next->uri) > strlen(urih->uri)))
133 urih->next = prev->next;
139 void ast_http_uri_unlink(struct ast_http_uri *urih)
141 struct ast_http_uri *prev = uris;
148 if (prev->next == urih) {
149 prev->next = urih->next;
156 static char *handle_uri(struct sockaddr_in *sin, char *uri, int *status, char **title, int *contentlength)
163 struct ast_http_uri *urih=NULL;
165 struct ast_variable *vars=NULL, *v, *prev = NULL;
170 params = strchr(uri, '?');
174 while ((var = strsep(¶ms, "&"))) {
175 val = strchr(var, '=');
183 if ((v = ast_variable_new(var, val))) {
193 if (!strncasecmp(uri, prefix, prefix_len)) {
195 if (!*uri || (*uri == '/')) {
200 len = strlen(urih->uri);
201 if (!strncasecmp(urih->uri, uri, len)) {
202 if (!uri[len] || uri[len] == '/') {
206 if (!*turi || urih->has_subtree) {
217 c = urih->callback(sin, uri, vars, status, title, contentlength);
218 ast_variables_destroy(vars);
220 c = ast_http_error(404, "Not Found", NULL, "The requested URL was not found on this serer.");
222 *title = strdup("Not Found");
227 static void *ast_httpd_helper_thread(void *data)
231 struct ast_http_server_instance *ser = data;
232 char *uri, *c, *title=NULL;
233 int status = 200, contentlength = 0;
236 if (fgets(buf, sizeof(buf), ser->f)) {
239 while(*uri && (*uri > 32)) uri++;
245 /* Skip white space */
246 while (*uri && (*uri < 33)) uri++;
250 while (*c && (*c > 32)) c++;
256 if (!strcasecmp(buf, "get"))
257 c = handle_uri(&ser->requestor, uri, &status, &title, &contentlength);
259 c = ast_http_error(501, "Not Implemented", NULL, "Attempt to use unimplemented / unsupported method");\
261 c = ast_http_error(400, "Bad Request", NULL, "Invalid Request");
263 c = ast_http_error(500, "Internal Error", NULL, "Internal Server Error");
266 strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
267 ast_cli(ser->fd, "HTTP/1.1 GET %d %s\r\n", status, title ? title : "OK");
268 ast_cli(ser->fd, "Server: Asterisk\r\n");
269 ast_cli(ser->fd, "Date: %s\r\n", timebuf);
271 ast_cli(ser->fd, "Content-length: %d\r\n", contentlength);
272 ast_cli(ser->fd, "Connection: close\r\n");
273 ast_cli(ser->fd, "%s", c);
284 static void *http_root(void *data)
287 struct sockaddr_in sin;
289 struct ast_http_server_instance *ser;
292 ast_wait_for_input(httpfd, -1);
293 sinlen = sizeof(sin);
294 fd = accept(httpfd, (struct sockaddr *)&sin, &sinlen);
296 if ((errno != EAGAIN) && (errno != EINTR))
297 ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
300 ser = calloc(1, sizeof(*ser));
303 if ((ser->f = fdopen(ser->fd, "w+"))) {
304 if (ast_pthread_create(&launched, NULL, ast_httpd_helper_thread, ser)) {
305 ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
310 ast_log(LOG_WARNING, "fdopen failed!\n");
315 ast_log(LOG_WARNING, "Out of memory!\n");
322 static void http_server_start(struct sockaddr_in *sin)
324 char iabuf[INET_ADDRSTRLEN];
328 /* Do nothing if nothing has changed */
329 if (!memcmp(&oldsin, sin, sizeof(oldsin))) {
330 ast_log(LOG_DEBUG, "Nothing changed in http\n");
334 memcpy(&oldsin, sin, sizeof(oldsin));
336 /* Shutdown a running server if there is one */
337 if (master != AST_PTHREADT_NULL) {
338 pthread_cancel(master);
339 pthread_kill(master, SIGURG);
340 pthread_join(master, NULL);
346 /* If there's no new server, stop here */
347 if (!sin->sin_family)
351 httpfd = socket(AF_INET, SOCK_STREAM, 0);
353 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
357 setsockopt(httpfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
358 if (bind(httpfd, (struct sockaddr *)sin, sizeof(*sin))) {
359 ast_log(LOG_NOTICE, "Unable to bind http server to %s:%d: %s\n",
360 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port),
366 if (listen(httpfd, 10)) {
367 ast_log(LOG_NOTICE, "Unable to listen!\n");
372 flags = fcntl(httpfd, F_GETFL);
373 fcntl(httpfd, F_SETFL, flags | O_NONBLOCK);
374 if (ast_pthread_create(&master, NULL, http_root, NULL)) {
375 ast_log(LOG_NOTICE, "Unable to launch http server on %s:%d: %s\n",
376 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port),
383 static int __ast_http_load(int reload)
385 struct ast_config *cfg;
386 struct ast_variable *v;
388 struct sockaddr_in sin;
390 struct ast_hostent ahp;
391 char newprefix[MAX_PREFIX];
392 memset(&sin, 0, sizeof(sin));
394 strcpy(newprefix, DEFAULT_PREFIX);
395 cfg = ast_config_load("http.conf");
397 v = ast_variable_browse(cfg, "general");
399 if (!strcasecmp(v->name, "enabled"))
400 enabled = ast_true(v->value);
401 else if (!strcasecmp(v->name, "bindport"))
402 sin.sin_port = ntohs(atoi(v->value));
403 else if (!strcasecmp(v->name, "bindaddr")) {
404 if ((hp = ast_gethostbyname(v->value, &ahp))) {
405 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
407 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
409 } else if (!strcasecmp(v->name, "prefix"))
410 ast_copy_string(newprefix, v->value, sizeof(newprefix));
413 ast_config_destroy(cfg);
416 sin.sin_family = AF_INET;
417 if (strcmp(prefix, newprefix)) {
418 ast_copy_string(prefix, newprefix, sizeof(prefix));
419 prefix_len = strlen(prefix);
421 http_server_start(&sin);
425 static int handle_show_http(int fd, int argc, char *argv[])
427 char iabuf[INET_ADDRSTRLEN];
428 struct ast_http_uri *urih;
430 return RESULT_SHOWUSAGE;
431 ast_cli(fd, "HTTP Server Status:\n");
432 ast_cli(fd, "Prefix: %s\n", prefix);
433 if (oldsin.sin_family)
434 ast_cli(fd, "Server Enabled and Bound to %s:%d\n\n",
435 ast_inet_ntoa(iabuf, sizeof(iabuf), oldsin.sin_addr),
436 ntohs(oldsin.sin_port));
438 ast_cli(fd, "Server Disabled\n\n");
439 ast_cli(fd, "Enabled URI's:\n");
442 ast_cli(fd, "/%s/%s%s => %s\n", prefix, urih->uri, (urih->has_subtree ? "/..." : "" ), urih->description);
446 ast_cli(fd, "None.\n");
447 return RESULT_SUCCESS;
450 int ast_http_reload(void)
452 return __ast_http_load(1);
455 static char show_http_help[] =
457 " Shows status of internal HTTP engine\n";
459 static struct ast_cli_entry http_cli[] = {
460 { { "show", "http", NULL }, handle_show_http,
461 "Display HTTP status", show_http_help },
464 int ast_http_init(void)
466 ast_http_uri_link(&statusuri);
467 ast_cli_register_multiple(http_cli, sizeof(http_cli) / sizeof(http_cli[0]));
468 return __ast_http_load(0);