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 if (!(ser = ast_calloc(1, sizeof(*ser)))) {
305 if ((ser->f = fdopen(ser->fd, "w+"))) {
306 if (ast_pthread_create(&launched, NULL, ast_httpd_helper_thread, ser)) {
307 ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
312 ast_log(LOG_WARNING, "fdopen failed!\n");
320 static void http_server_start(struct sockaddr_in *sin)
322 char iabuf[INET_ADDRSTRLEN];
326 /* Do nothing if nothing has changed */
327 if (!memcmp(&oldsin, sin, sizeof(oldsin))) {
328 ast_log(LOG_DEBUG, "Nothing changed in http\n");
332 memcpy(&oldsin, sin, sizeof(oldsin));
334 /* Shutdown a running server if there is one */
335 if (master != AST_PTHREADT_NULL) {
336 pthread_cancel(master);
337 pthread_kill(master, SIGURG);
338 pthread_join(master, NULL);
344 /* If there's no new server, stop here */
345 if (!sin->sin_family)
349 httpfd = socket(AF_INET, SOCK_STREAM, 0);
351 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
355 setsockopt(httpfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
356 if (bind(httpfd, (struct sockaddr *)sin, sizeof(*sin))) {
357 ast_log(LOG_NOTICE, "Unable to bind http server to %s:%d: %s\n",
358 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port),
364 if (listen(httpfd, 10)) {
365 ast_log(LOG_NOTICE, "Unable to listen!\n");
370 flags = fcntl(httpfd, F_GETFL);
371 fcntl(httpfd, F_SETFL, flags | O_NONBLOCK);
372 if (ast_pthread_create(&master, NULL, http_root, NULL)) {
373 ast_log(LOG_NOTICE, "Unable to launch http server on %s:%d: %s\n",
374 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr), ntohs(sin->sin_port),
381 static int __ast_http_load(int reload)
383 struct ast_config *cfg;
384 struct ast_variable *v;
386 struct sockaddr_in sin;
388 struct ast_hostent ahp;
389 char newprefix[MAX_PREFIX];
390 memset(&sin, 0, sizeof(sin));
392 strcpy(newprefix, DEFAULT_PREFIX);
393 cfg = ast_config_load("http.conf");
395 v = ast_variable_browse(cfg, "general");
397 if (!strcasecmp(v->name, "enabled"))
398 enabled = ast_true(v->value);
399 else if (!strcasecmp(v->name, "bindport"))
400 sin.sin_port = ntohs(atoi(v->value));
401 else if (!strcasecmp(v->name, "bindaddr")) {
402 if ((hp = ast_gethostbyname(v->value, &ahp))) {
403 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
405 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
407 } else if (!strcasecmp(v->name, "prefix"))
408 ast_copy_string(newprefix, v->value, sizeof(newprefix));
411 ast_config_destroy(cfg);
414 sin.sin_family = AF_INET;
415 if (strcmp(prefix, newprefix)) {
416 ast_copy_string(prefix, newprefix, sizeof(prefix));
417 prefix_len = strlen(prefix);
419 http_server_start(&sin);
423 static int handle_show_http(int fd, int argc, char *argv[])
425 char iabuf[INET_ADDRSTRLEN];
426 struct ast_http_uri *urih;
428 return RESULT_SHOWUSAGE;
429 ast_cli(fd, "HTTP Server Status:\n");
430 ast_cli(fd, "Prefix: %s\n", prefix);
431 if (oldsin.sin_family)
432 ast_cli(fd, "Server Enabled and Bound to %s:%d\n\n",
433 ast_inet_ntoa(iabuf, sizeof(iabuf), oldsin.sin_addr),
434 ntohs(oldsin.sin_port));
436 ast_cli(fd, "Server Disabled\n\n");
437 ast_cli(fd, "Enabled URI's:\n");
440 ast_cli(fd, "/%s/%s%s => %s\n", prefix, urih->uri, (urih->has_subtree ? "/..." : "" ), urih->description);
444 ast_cli(fd, "None.\n");
445 return RESULT_SUCCESS;
448 int ast_http_reload(void)
450 return __ast_http_load(1);
453 static char show_http_help[] =
455 " Shows status of internal HTTP engine\n";
457 static struct ast_cli_entry http_cli[] = {
458 { { "show", "http", NULL }, handle_show_http,
459 "Display HTTP status", show_http_help },
462 int ast_http_init(void)
464 ast_http_uri_link(&statusuri);
465 ast_cli_register_multiple(http_cli, sizeof(http_cli) / sizeof(http_cli[0]));
466 return __ast_http_load(0);