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.
21 * \brief http server for AMI access
23 * \author Mark Spencer <markster@digium.com>
25 * This program implements a tiny http server
26 * and was inspired by micro-httpd by Jef Poskanzer
28 * \ref AstHTTP - AMI over the http protocol
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <sys/signal.h>
41 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
42 #include "asterisk/network.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/tcptls.h"
45 #include "asterisk/http.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/strings.h"
48 #include "asterisk/config.h"
49 #include "asterisk/stringfields.h"
50 #include "asterisk/ast_version.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/_private.h"
56 /* See http.h for more information about the SSL implementation */
57 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
58 #define DO_SSL /* comment in/out if you want to support ssl */
61 static struct ast_tls_config http_tls_cfg;
63 static void *httpd_helper_thread(void *arg);
66 * we have up to two accepting threads, one for http, one for https
68 static struct server_args http_desc = {
70 .master = AST_PTHREADT_NULL,
73 .name = "http server",
74 .accept_fn = ast_tcptls_server_root,
75 .worker_fn = httpd_helper_thread,
78 static struct server_args https_desc = {
80 .master = AST_PTHREADT_NULL,
81 .tls_cfg = &http_tls_cfg,
83 .name = "https server",
84 .accept_fn = ast_tcptls_server_root,
85 .worker_fn = httpd_helper_thread,
88 static AST_RWLIST_HEAD_STATIC(uris, ast_http_uri); /*!< list of supported handlers */
90 /* all valid URIs must be prepended by the string in prefix. */
91 static char prefix[MAX_PREFIX];
92 static int enablestatic;
94 /*! \brief Limit the kinds of files we're willing to serve up */
99 { "png", "image/png" },
100 { "jpg", "image/jpeg" },
101 { "js", "application/x-javascript" },
102 { "wav", "audio/x-wav" },
103 { "mp3", "audio/mpeg" },
104 { "svg", "image/svg+xml" },
105 { "svgz", "image/svg+xml" },
106 { "gif", "image/gif" },
109 struct http_uri_redirect {
110 AST_LIST_ENTRY(http_uri_redirect) entry;
115 static AST_RWLIST_HEAD_STATIC(uri_redirects, http_uri_redirect);
117 static const char *ftype2mtype(const char *ftype, char *wkspace, int wkspacelen)
122 for (x = 0; x < ARRAY_LEN(mimetypes); x++) {
123 if (!strcasecmp(ftype, mimetypes[x].ext)) {
124 return mimetypes[x].mtype;
129 snprintf(wkspace, wkspacelen, "text/%s", S_OR(ftype, "plain"));
134 static uint32_t manid_from_vars(struct ast_variable *sid) {
137 while (sid && strcmp(sid->name, "mansession_id"))
140 if (!sid || sscanf(sid->value, "%x", &mngid) != 1)
146 void ast_http_prefix(char *buf, int len)
149 ast_copy_string(buf, prefix, len);
153 static struct ast_str *static_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *vars, struct ast_variable *headers, int *status, char **title, int *contentlength)
162 struct timeval now = ast_tvnow();
166 /* Yuck. I'm not really sold on this, but if you don't deliver static content it makes your configuration
167 substantially more challenging, but this seems like a rather irritating feature creep on Asterisk. */
168 if (!enablestatic || ast_strlen_zero(uri)) {
172 /* Disallow any funny filenames at all */
173 if ((uri[0] < 33) || strchr("./|~@#$%^&*() \t", uri[0])) {
177 if (strstr(uri, "/..")) {
181 if ((ftype = strrchr(uri, '.'))) {
185 mtype = ftype2mtype(ftype, wkspace, sizeof(wkspace));
187 /* Cap maximum length */
188 if ((len = strlen(uri) + strlen(ast_config_AST_DATA_DIR) + strlen("/static-http/") + 5) > 1024) {
193 sprintf(path, "%s/static-http/%s", ast_config_AST_DATA_DIR, uri);
194 if (stat(path, &st)) {
198 if (S_ISDIR(st.st_mode)) {
202 if ((fd = open(path, O_RDONLY)) < 0) {
206 if (strstr(path, "/private/") && !astman_is_authed(manid_from_vars(vars))) {
210 ast_strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&now, &tm, "GMT"));
211 fprintf(ser->f, "HTTP/1.1 200 OK\r\n"
212 "Server: Asterisk/%s\r\n"
214 "Connection: close\r\n"
215 "Cache-Control: no-cache, no-store\r\n"
216 "Content-Length: %d\r\n"
217 "Content-type: %s\r\n\r\n",
218 ast_get_version(), buf, (int) st.st_size, mtype);
220 while ((len = read(fd, buf, sizeof(buf))) > 0) {
221 fwrite(buf, 1, len, ser->f);
229 return ast_http_error((*status = 404),
230 (*title = ast_strdup("Not Found")),
231 NULL, "The requested URL was not found on this server.");
234 return ast_http_error((*status = 403),
235 (*title = ast_strdup("Access Denied")),
236 NULL, "You do not have permission to access the requested URL.");
240 static struct ast_str *httpstatus_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *vars, struct ast_variable *headers, int *status, char **title, int *contentlength)
242 struct ast_str *out = ast_str_create(512);
243 struct ast_variable *v;
249 ast_str_append(&out, 0,
251 "<title>Asterisk HTTP Status</title>\r\n"
252 "<body bgcolor=\"#ffffff\">\r\n"
253 "<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
254 "<h2> Asterisk™ HTTP Status</h2></td></tr>\r\n");
255 ast_str_append(&out, 0, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
256 ast_str_append(&out, 0, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
257 ast_inet_ntoa(http_desc.oldsin.sin_addr));
258 ast_str_append(&out, 0, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
259 ntohs(http_desc.oldsin.sin_port));
261 if (http_tls_cfg.enabled) {
262 ast_str_append(&out, 0, "<tr><td><i>SSL Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
263 ntohs(https_desc.oldsin.sin_port));
266 ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
268 for (v = vars; v; v = v->next) {
269 if (strncasecmp(v->name, "cookie_", 7)) {
270 ast_str_append(&out, 0, "<tr><td><i>Submitted Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
274 ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
276 for (v = vars; v; v = v->next) {
277 if (!strncasecmp(v->name, "cookie_", 7)) {
278 ast_str_append(&out, 0, "<tr><td><i>Cookie '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
282 ast_str_append(&out, 0, "</table><center><font size=\"-1\"><i>Asterisk and Digium are registered trademarks of Digium, Inc.</i></font></center></body>\r\n");
286 static struct ast_http_uri statusuri = {
287 .callback = httpstatus_callback,
288 .description = "Asterisk HTTP General Status",
295 static struct ast_http_uri staticuri = {
296 .callback = static_callback,
297 .description = "Asterisk HTTP Static Delivery",
306 struct ast_str *ast_http_error(int status, const char *title, const char *extra_header, const char *text)
308 struct ast_str *out = ast_str_create(512);
315 "Content-type: text/html\r\n"
318 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
320 "<title>%d %s</title>\r\n"
325 "<address>Asterisk Server</address>\r\n"
326 "</body></html>\r\n",
327 (extra_header ? extra_header : ""), status, title, title, text);
333 * Link the new uri into the list.
335 * They are sorted by length of
336 * the string, not alphabetically. Duplicate entries are not replaced,
337 * but the insertion order (using <= and not just <) makes sure that
338 * more recent insertions hide older ones.
339 * On a lookup, we just scan the list and stop at the first matching entry.
341 int ast_http_uri_link(struct ast_http_uri *urih)
343 struct ast_http_uri *uri;
344 int len = strlen(urih->uri);
346 if (!(urih->supports_get || urih->supports_post)) {
347 ast_log(LOG_WARNING, "URI handler does not provide either GET or POST method: %s (%s)\n", urih->uri, urih->description);
351 AST_RWLIST_WRLOCK(&uris);
353 if (AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len) {
354 AST_RWLIST_INSERT_HEAD(&uris, urih, entry);
355 AST_RWLIST_UNLOCK(&uris);
360 AST_RWLIST_TRAVERSE(&uris, uri, entry) {
361 if (AST_RWLIST_NEXT(uri, entry) &&
362 strlen(AST_RWLIST_NEXT(uri, entry)->uri) <= len) {
363 AST_RWLIST_INSERT_AFTER(&uris, uri, urih, entry);
364 AST_RWLIST_UNLOCK(&uris);
370 AST_RWLIST_INSERT_TAIL(&uris, urih, entry);
372 AST_RWLIST_UNLOCK(&uris);
377 void ast_http_uri_unlink(struct ast_http_uri *urih)
379 AST_RWLIST_WRLOCK(&uris);
380 AST_RWLIST_REMOVE(&uris, urih, entry);
381 AST_RWLIST_UNLOCK(&uris);
384 void ast_http_uri_unlink_all_with_key(const char *key)
386 struct ast_http_uri *urih;
387 AST_RWLIST_WRLOCK(&uris);
388 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&uris, urih, entry) {
389 if (!strcmp(urih->key, key)) {
390 AST_RWLIST_REMOVE_CURRENT(entry);
392 if (urih->dmallocd) {
393 ast_free(urih->data);
399 AST_RWLIST_TRAVERSE_SAFE_END
400 AST_RWLIST_UNLOCK(&uris);
404 * Decode special characters in http uri.
405 * We have ast_uri_decode to handle %XX sequences, but spaces
406 * are encoded as a '+' so we need to replace them beforehand.
408 static void http_decode(char *s)
412 for (t = s; *t; t++) {
420 static struct ast_str *handle_uri(struct ast_tcptls_session_instance *ser, char *uri, enum ast_http_method method,
421 int *status, char **title, int *contentlength, struct ast_variable **cookies, struct ast_variable *headers,
422 unsigned int *static_content)
425 struct ast_str *out = NULL;
427 struct ast_http_uri *urih = NULL;
429 struct ast_variable *vars = NULL, *v, *prev = NULL;
430 struct http_uri_redirect *redirect;
433 /* preserve previous behavior of only support URI parameters on GET requests */
434 if (method == AST_HTTP_GET) {
435 strsep(¶ms, "?");
437 /* Extract arguments from the request and store them in variables.
438 * Note that a request can have multiple arguments with the same
439 * name, and we store them all in the list of variables.
440 * It is up to the application to handle multiple values.
445 while ((val = strsep(¶ms, "&"))) {
446 var = strsep(&val, "=");
453 if ((v = ast_variable_new(var, val, ""))) {
466 * Append the cookies to the list of variables.
467 * This saves a pass in the cookies list, but has the side effect
468 * that a variable might mask a cookie with the same name if the
469 * application stops at the first match.
470 * Note that this is the same behaviour as $_REQUEST variables in PHP.
473 prev->next = *cookies;
481 AST_RWLIST_RDLOCK(&uri_redirects);
482 AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
483 if (!strcasecmp(uri, redirect->target)) {
486 snprintf(buf, sizeof(buf), "Location: %s\r\n", redirect->dest);
487 out = ast_http_error((*status = 302),
488 (*title = ast_strdup("Moved Temporarily")),
489 buf, "Redirecting...");
494 AST_RWLIST_UNLOCK(&uri_redirects);
500 /* We want requests to start with the (optional) prefix and '/' */
502 if (!strncasecmp(uri, prefix, l) && uri[l] == '/') {
504 /* scan registered uris to see if we match one. */
505 AST_RWLIST_RDLOCK(&uris);
506 AST_RWLIST_TRAVERSE(&uris, urih, entry) {
507 ast_debug(2, "match request [%s] with handler [%s] len %d\n", uri, urih->uri, l);
511 if (urih->supports_get) {
516 if (urih->supports_post) {
523 l = strlen(urih->uri);
524 c = uri + l; /* candidate */
526 if (strncasecmp(urih->uri, uri, l) || /* no match */
527 (*c && *c != '/')) { /* substring */
535 if (!*c || urih->has_subtree) {
536 if (((method == AST_HTTP_GET) && urih->supports_get) ||
537 ((method == AST_HTTP_POST) && urih->supports_post)) {
546 AST_RWLIST_UNLOCK(&uris);
550 if (method == AST_HTTP_POST && !astman_is_authed(manid_from_vars(vars))) {
551 out = ast_http_error((*status = 403),
552 (*title = ast_strdup("Access Denied")),
553 NULL, "You do not have permission to access the requested URL.");
555 *static_content = urih->static_content;
556 out = urih->callback(ser, urih, uri, method, vars, headers, status, title, contentlength);
557 AST_RWLIST_UNLOCK(&uris);
558 } else if (saw_method) {
559 out = ast_http_error((*status = 404),
560 (*title = ast_strdup("Not Found")), NULL,
561 "The requested URL was not found on this server.");
563 out = ast_http_error((*status = 501),
564 (*title = ast_strdup("Not Implemented")), NULL,
565 "Attempt to use unimplemented / unsupported method");
569 ast_variables_destroy(vars);
575 #if defined(HAVE_FUNOPEN)
579 #define HOOK_T ssize_t
584 * replacement read/write functions for SSL support.
585 * We use wrappers rather than SSL_read/SSL_write directly so
586 * we can put in some debugging.
588 /*static HOOK_T ssl_read(void *cookie, char *buf, LEN_T len)
590 int i = SSL_read(cookie, buf, len-1);
594 ast_verbose("ssl read size %d returns %d <%s>\n", (int)len, i, buf);
599 static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
602 char *s = alloca(len+1);
603 strncpy(s, buf, len);
605 ast_verbose("ssl write size %d <%s>\n", (int)len, s);
607 return SSL_write(cookie, buf, len);
610 static int ssl_close(void *cookie)
612 close(SSL_get_fd(cookie));
613 SSL_shutdown(cookie);
619 static struct ast_variable *parse_cookies(char *cookies)
622 struct ast_variable *vars = NULL, *var;
627 while ((cur = strsep(&cookies, ";"))) {
633 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
637 name = ast_strip(name);
638 val = ast_strip_quoted(val, "\"", "\"");
640 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
645 ast_log(LOG_DEBUG, "mmm ... cookie! Name: '%s' Value: '%s'\n", name, val);
648 var = ast_variable_new(name, val, __FILE__);
656 static void *httpd_helper_thread(void *data)
660 struct ast_tcptls_session_instance *ser = data;
661 struct ast_variable *vars=NULL, *headers = NULL;
662 char *uri, *title=NULL;
663 int status = 200, contentlength = 0;
664 struct ast_str *out = NULL;
665 unsigned int static_content = 0;
666 struct ast_variable *tail = headers;
668 if (!fgets(buf, sizeof(buf), ser->f)) {
672 uri = ast_skip_nonblanks(buf); /* Skip method */
677 uri = ast_skip_blanks(uri); /* Skip white space */
679 if (*uri) { /* terminate at the first blank */
680 char *c = ast_skip_nonblanks(uri);
687 /* process "Cookie: " lines */
688 while (fgets(cookie, sizeof(cookie), ser->f)) {
689 /* Trim trailing characters */
690 ast_trim_blanks(cookie);
691 if (ast_strlen_zero(cookie)) {
694 if (!strncasecmp(cookie, "Cookie: ", 8)) {
695 vars = parse_cookies(cookie);
700 name = strsep(&val, ":");
701 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
704 ast_trim_blanks(name);
705 val = ast_skip_blanks(val);
708 headers = ast_variable_new(name, val, __FILE__);
711 tail->next = ast_variable_new(name, val, __FILE__);
718 out = ast_http_error(400, "Bad Request", NULL, "Invalid Request");
719 } else if (strcasecmp(buf, "post") && strcasecmp(buf, "get")) {
720 out = ast_http_error(501, "Not Implemented", NULL,
721 "Attempt to use unimplemented / unsupported method");
722 } else { /* try to serve it */
723 out = handle_uri(ser, uri, (!strcasecmp(buf, "get")) ? AST_HTTP_GET : AST_HTTP_POST,
724 &status, &title, &contentlength, &vars, headers, &static_content);
727 /* If they aren't mopped up already, clean up the cookies */
729 ast_variables_destroy(vars);
731 /* Clean up all the header information pulled as well */
733 ast_variables_destroy(headers);
737 struct timeval now = ast_tvnow();
741 ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&now, &tm, "GMT"));
744 "Server: Asterisk/%s\r\n"
746 "Connection: close\r\n"
748 status, title ? title : "OK", ast_get_version(), timebuf,
749 static_content ? "" : "Cache-Control: no-cache, no-store\r\n");
750 /* We set the no-cache headers only for dynamic content.
751 * If you want to make sure the static file you requested is not from cache,
752 * append a random variable to your GET request. Ex: 'something.html?r=109987734'
754 if (!contentlength) { /* opaque body ? just dump it hoping it is properly formatted */
755 fprintf(ser->f, "%s", out->str);
757 char *tmp = strstr(out->str, "\r\n\r\n");
760 fprintf(ser->f, "Content-length: %d\r\n", contentlength);
761 /* first write the header, then the body */
762 fwrite(out->str, 1, (tmp + 4 - out->str), ser->f);
763 fwrite(tmp + 4, 1, contentlength, ser->f);
782 * \brief Add a new URI redirect
783 * The entries in the redirect list are sorted by length, just like the list
786 static void add_redirect(const char *value)
789 struct http_uri_redirect *redirect, *cur;
790 unsigned int target_len;
791 unsigned int total_len;
793 dest = ast_strdupa(value);
794 dest = ast_skip_blanks(dest);
795 target = strsep(&dest, " ");
796 target = ast_skip_blanks(target);
797 target = strsep(&target, " "); /* trim trailing whitespace */
800 ast_log(LOG_WARNING, "Invalid redirect '%s'\n", value);
804 target_len = strlen(target) + 1;
805 total_len = sizeof(*redirect) + target_len + strlen(dest) + 1;
807 if (!(redirect = ast_calloc(1, total_len))) {
811 redirect->dest = redirect->target + target_len;
812 strcpy(redirect->target, target);
813 strcpy(redirect->dest, dest);
815 AST_RWLIST_WRLOCK(&uri_redirects);
817 target_len--; /* So we can compare directly with strlen() */
818 if (AST_RWLIST_EMPTY(&uri_redirects)
819 || strlen(AST_RWLIST_FIRST(&uri_redirects)->target) <= target_len) {
820 AST_RWLIST_INSERT_HEAD(&uri_redirects, redirect, entry);
821 AST_RWLIST_UNLOCK(&uri_redirects);
826 AST_RWLIST_TRAVERSE(&uri_redirects, cur, entry) {
827 if (AST_RWLIST_NEXT(cur, entry)
828 && strlen(AST_RWLIST_NEXT(cur, entry)->target) <= target_len) {
829 AST_RWLIST_INSERT_AFTER(&uri_redirects, cur, redirect, entry);
830 AST_RWLIST_UNLOCK(&uri_redirects);
836 AST_RWLIST_INSERT_TAIL(&uri_redirects, redirect, entry);
838 AST_RWLIST_UNLOCK(&uri_redirects);
841 static int __ast_http_load(int reload)
843 struct ast_config *cfg;
844 struct ast_variable *v;
846 int newenablestatic=0;
848 struct ast_hostent ahp;
849 char newprefix[MAX_PREFIX] = "";
850 int have_sslbindaddr = 0;
851 struct http_uri_redirect *redirect;
852 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
854 cfg = ast_config_load2("http.conf", "http", config_flags);
855 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
860 memset(&http_desc.sin, 0, sizeof(http_desc.sin));
861 http_desc.sin.sin_port = htons(8088);
863 memset(&https_desc.sin, 0, sizeof(https_desc.sin));
864 https_desc.sin.sin_port = htons(8089);
866 http_tls_cfg.enabled = 0;
867 if (http_tls_cfg.certfile) {
868 ast_free(http_tls_cfg.certfile);
870 http_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
871 if (http_tls_cfg.cipher) {
872 ast_free(http_tls_cfg.cipher);
874 http_tls_cfg.cipher = ast_strdup("");
876 AST_RWLIST_WRLOCK(&uri_redirects);
877 while ((redirect = AST_RWLIST_REMOVE_HEAD(&uri_redirects, entry))) {
880 AST_RWLIST_UNLOCK(&uri_redirects);
883 v = ast_variable_browse(cfg, "general");
884 for (; v; v = v->next) {
885 if (!strcasecmp(v->name, "enabled")) {
886 enabled = ast_true(v->value);
887 } else if (!strcasecmp(v->name, "sslenable")) {
888 http_tls_cfg.enabled = ast_true(v->value);
889 } else if (!strcasecmp(v->name, "sslbindport")) {
890 https_desc.sin.sin_port = htons(atoi(v->value));
891 } else if (!strcasecmp(v->name, "sslcert")) {
892 ast_free(http_tls_cfg.certfile);
893 http_tls_cfg.certfile = ast_strdup(v->value);
894 } else if (!strcasecmp(v->name, "sslcipher")) {
895 ast_free(http_tls_cfg.cipher);
896 http_tls_cfg.cipher = ast_strdup(v->value);
897 } else if (!strcasecmp(v->name, "enablestatic")) {
898 newenablestatic = ast_true(v->value);
899 } else if (!strcasecmp(v->name, "bindport")) {
900 http_desc.sin.sin_port = htons(atoi(v->value));
901 } else if (!strcasecmp(v->name, "sslbindaddr")) {
902 if ((hp = ast_gethostbyname(v->value, &ahp))) {
903 memcpy(&https_desc.sin.sin_addr, hp->h_addr, sizeof(https_desc.sin.sin_addr));
904 have_sslbindaddr = 1;
906 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
908 } else if (!strcasecmp(v->name, "bindaddr")) {
909 if ((hp = ast_gethostbyname(v->value, &ahp))) {
910 memcpy(&http_desc.sin.sin_addr, hp->h_addr, sizeof(http_desc.sin.sin_addr));
912 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
914 } else if (!strcasecmp(v->name, "prefix")) {
915 if (!ast_strlen_zero(v->value)) {
917 ast_copy_string(newprefix + 1, v->value, sizeof(newprefix) - 1);
921 } else if (!strcasecmp(v->name, "redirect")) {
922 add_redirect(v->value);
924 ast_log(LOG_WARNING, "Ignoring unknown option '%s' in http.conf\n", v->name);
928 ast_config_destroy(cfg);
931 if (!have_sslbindaddr) {
932 https_desc.sin.sin_addr = http_desc.sin.sin_addr;
935 http_desc.sin.sin_family = https_desc.sin.sin_family = AF_INET;
937 if (strcmp(prefix, newprefix)) {
938 ast_copy_string(prefix, newprefix, sizeof(prefix));
940 enablestatic = newenablestatic;
941 ast_tcptls_server_start(&http_desc);
942 if (ast_ssl_setup(https_desc.tls_cfg)) {
943 ast_tcptls_server_start(&https_desc);
949 static char *handle_show_http(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
951 struct ast_http_uri *urih;
952 struct http_uri_redirect *redirect;
956 e->command = "http show status";
958 "Usage: http show status\n"
959 " Lists status of internal HTTP engine\n";
966 return CLI_SHOWUSAGE;
968 ast_cli(a->fd, "HTTP Server Status:\n");
969 ast_cli(a->fd, "Prefix: %s\n", prefix);
970 if (!http_desc.oldsin.sin_family) {
971 ast_cli(a->fd, "Server Disabled\n\n");
973 ast_cli(a->fd, "Server Enabled and Bound to %s:%d\n\n",
974 ast_inet_ntoa(http_desc.oldsin.sin_addr),
975 ntohs(http_desc.oldsin.sin_port));
976 if (http_tls_cfg.enabled) {
977 ast_cli(a->fd, "HTTPS Server Enabled and Bound to %s:%d\n\n",
978 ast_inet_ntoa(https_desc.oldsin.sin_addr),
979 ntohs(https_desc.oldsin.sin_port));
983 ast_cli(a->fd, "Enabled URI's:\n");
984 AST_RWLIST_RDLOCK(&uris);
985 if (AST_RWLIST_EMPTY(&uris)) {
986 ast_cli(a->fd, "None.\n");
988 AST_RWLIST_TRAVERSE(&uris, urih, entry) {
989 ast_cli(a->fd, "%s/%s%s => %s\n", prefix, urih->uri, (urih->has_subtree ? "/..." : ""), urih->description);
992 AST_RWLIST_UNLOCK(&uris);
994 ast_cli(a->fd, "\nEnabled Redirects:\n");
995 AST_RWLIST_RDLOCK(&uri_redirects);
996 AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
997 ast_cli(a->fd, " %s => %s\n", redirect->target, redirect->dest);
999 if (AST_RWLIST_EMPTY(&uri_redirects)) {
1000 ast_cli(a->fd, " None.\n");
1002 AST_RWLIST_UNLOCK(&uri_redirects);
1007 int ast_http_reload(void)
1009 return __ast_http_load(1);
1012 static struct ast_cli_entry cli_http[] = {
1013 AST_CLI_DEFINE(handle_show_http, "Display HTTP server status"),
1016 int ast_http_init(void)
1018 ast_http_uri_link(&statusuri);
1019 ast_http_uri_link(&staticuri);
1020 ast_cli_register_multiple(cli_http, sizeof(cli_http) / sizeof(struct ast_cli_entry));
1022 return __ast_http_load(0);