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"
53 #include "asterisk/astobj2.h"
57 /* See http.h for more information about the SSL implementation */
58 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
59 #define DO_SSL /* comment in/out if you want to support ssl */
62 static struct ast_tls_config http_tls_cfg;
64 static void *httpd_helper_thread(void *arg);
67 * we have up to two accepting threads, one for http, one for https
69 static struct ast_tcptls_session_args http_desc = {
71 .master = AST_PTHREADT_NULL,
74 .name = "http server",
75 .accept_fn = ast_tcptls_server_root,
76 .worker_fn = httpd_helper_thread,
79 static struct ast_tcptls_session_args https_desc = {
81 .master = AST_PTHREADT_NULL,
82 .tls_cfg = &http_tls_cfg,
84 .name = "https server",
85 .accept_fn = ast_tcptls_server_root,
86 .worker_fn = httpd_helper_thread,
89 static AST_RWLIST_HEAD_STATIC(uris, ast_http_uri); /*!< list of supported handlers */
91 /* all valid URIs must be prepended by the string in prefix. */
92 static char prefix[MAX_PREFIX];
93 static int enablestatic;
95 /*! \brief Limit the kinds of files we're willing to serve up */
100 { "png", "image/png" },
101 { "jpg", "image/jpeg" },
102 { "js", "application/x-javascript" },
103 { "wav", "audio/x-wav" },
104 { "mp3", "audio/mpeg" },
105 { "svg", "image/svg+xml" },
106 { "svgz", "image/svg+xml" },
107 { "gif", "image/gif" },
110 struct http_uri_redirect {
111 AST_LIST_ENTRY(http_uri_redirect) entry;
116 static AST_RWLIST_HEAD_STATIC(uri_redirects, http_uri_redirect);
118 static const char *ftype2mtype(const char *ftype, char *wkspace, int wkspacelen)
123 for (x = 0; x < ARRAY_LEN(mimetypes); x++) {
124 if (!strcasecmp(ftype, mimetypes[x].ext)) {
125 return mimetypes[x].mtype;
130 snprintf(wkspace, wkspacelen, "text/%s", S_OR(ftype, "plain"));
135 static uint32_t manid_from_vars(struct ast_variable *sid) {
138 while (sid && strcmp(sid->name, "mansession_id"))
141 if (!sid || sscanf(sid->value, "%x", &mngid) != 1)
147 void ast_http_prefix(char *buf, int len)
150 ast_copy_string(buf, prefix, len);
154 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)
163 struct timeval now = ast_tvnow();
167 /* Yuck. I'm not really sold on this, but if you don't deliver static content it makes your configuration
168 substantially more challenging, but this seems like a rather irritating feature creep on Asterisk. */
169 if (!enablestatic || ast_strlen_zero(uri)) {
173 /* Disallow any funny filenames at all */
174 if ((uri[0] < 33) || strchr("./|~@#$%^&*() \t", uri[0])) {
178 if (strstr(uri, "/..")) {
182 if ((ftype = strrchr(uri, '.'))) {
186 mtype = ftype2mtype(ftype, wkspace, sizeof(wkspace));
188 /* Cap maximum length */
189 if ((len = strlen(uri) + strlen(ast_config_AST_DATA_DIR) + strlen("/static-http/") + 5) > 1024) {
194 sprintf(path, "%s/static-http/%s", ast_config_AST_DATA_DIR, uri);
195 if (stat(path, &st)) {
199 if (S_ISDIR(st.st_mode)) {
203 if ((fd = open(path, O_RDONLY)) < 0) {
207 if (strstr(path, "/private/") && !astman_is_authed(manid_from_vars(vars))) {
211 ast_strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&now, &tm, "GMT"));
212 fprintf(ser->f, "HTTP/1.1 200 OK\r\n"
213 "Server: Asterisk/%s\r\n"
215 "Connection: close\r\n"
216 "Cache-Control: no-cache, no-store\r\n"
217 "Content-Length: %d\r\n"
218 "Content-type: %s\r\n\r\n",
219 ast_get_version(), buf, (int) st.st_size, mtype);
221 while ((len = read(fd, buf, sizeof(buf))) > 0) {
222 if (fwrite(buf, 1, len, ser->f) != len) {
223 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
232 return ast_http_error((*status = 404),
233 (*title = ast_strdup("Not Found")),
234 NULL, "The requested URL was not found on this server.");
237 return ast_http_error((*status = 403),
238 (*title = ast_strdup("Access Denied")),
239 NULL, "You do not have permission to access the requested URL.");
243 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)
245 struct ast_str *out = ast_str_create(512);
246 struct ast_variable *v;
252 ast_str_append(&out, 0,
254 "<title>Asterisk HTTP Status</title>\r\n"
255 "<body bgcolor=\"#ffffff\">\r\n"
256 "<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
257 "<h2> Asterisk™ HTTP Status</h2></td></tr>\r\n");
258 ast_str_append(&out, 0, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
259 ast_str_append(&out, 0, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
260 ast_inet_ntoa(http_desc.old_local_address.sin_addr));
261 ast_str_append(&out, 0, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
262 ntohs(http_desc.old_local_address.sin_port));
264 if (http_tls_cfg.enabled) {
265 ast_str_append(&out, 0, "<tr><td><i>SSL Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
266 ntohs(https_desc.old_local_address.sin_port));
269 ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
271 for (v = vars; v; v = v->next) {
272 if (strncasecmp(v->name, "cookie_", 7)) {
273 ast_str_append(&out, 0, "<tr><td><i>Submitted Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
277 ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
279 for (v = vars; v; v = v->next) {
280 if (!strncasecmp(v->name, "cookie_", 7)) {
281 ast_str_append(&out, 0, "<tr><td><i>Cookie '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
285 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");
289 static struct ast_http_uri statusuri = {
290 .callback = httpstatus_callback,
291 .description = "Asterisk HTTP General Status",
298 static struct ast_http_uri staticuri = {
299 .callback = static_callback,
300 .description = "Asterisk HTTP Static Delivery",
309 struct ast_str *ast_http_error(int status, const char *title, const char *extra_header, const char *text)
311 struct ast_str *out = ast_str_create(512);
318 "Content-type: text/html\r\n"
321 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
323 "<title>%d %s</title>\r\n"
328 "<address>Asterisk Server</address>\r\n"
329 "</body></html>\r\n",
330 (extra_header ? extra_header : ""), status, title, title, text);
336 * Link the new uri into the list.
338 * They are sorted by length of
339 * the string, not alphabetically. Duplicate entries are not replaced,
340 * but the insertion order (using <= and not just <) makes sure that
341 * more recent insertions hide older ones.
342 * On a lookup, we just scan the list and stop at the first matching entry.
344 int ast_http_uri_link(struct ast_http_uri *urih)
346 struct ast_http_uri *uri;
347 int len = strlen(urih->uri);
349 if (!(urih->supports_get || urih->supports_post)) {
350 ast_log(LOG_WARNING, "URI handler does not provide either GET or POST method: %s (%s)\n", urih->uri, urih->description);
354 AST_RWLIST_WRLOCK(&uris);
356 if (AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len) {
357 AST_RWLIST_INSERT_HEAD(&uris, urih, entry);
358 AST_RWLIST_UNLOCK(&uris);
363 AST_RWLIST_TRAVERSE(&uris, uri, entry) {
364 if (AST_RWLIST_NEXT(uri, entry) &&
365 strlen(AST_RWLIST_NEXT(uri, entry)->uri) <= len) {
366 AST_RWLIST_INSERT_AFTER(&uris, uri, urih, entry);
367 AST_RWLIST_UNLOCK(&uris);
373 AST_RWLIST_INSERT_TAIL(&uris, urih, entry);
375 AST_RWLIST_UNLOCK(&uris);
380 void ast_http_uri_unlink(struct ast_http_uri *urih)
382 AST_RWLIST_WRLOCK(&uris);
383 AST_RWLIST_REMOVE(&uris, urih, entry);
384 AST_RWLIST_UNLOCK(&uris);
387 void ast_http_uri_unlink_all_with_key(const char *key)
389 struct ast_http_uri *urih;
390 AST_RWLIST_WRLOCK(&uris);
391 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&uris, urih, entry) {
392 if (!strcmp(urih->key, key)) {
393 AST_RWLIST_REMOVE_CURRENT(entry);
395 if (urih->dmallocd) {
396 ast_free(urih->data);
402 AST_RWLIST_TRAVERSE_SAFE_END
403 AST_RWLIST_UNLOCK(&uris);
407 * Decode special characters in http uri.
408 * We have ast_uri_decode to handle %XX sequences, but spaces
409 * are encoded as a '+' so we need to replace them beforehand.
411 static void http_decode(char *s)
415 for (t = s; *t; t++) {
423 static struct ast_str *handle_uri(struct ast_tcptls_session_instance *ser, char *uri, enum ast_http_method method,
424 int *status, char **title, int *contentlength, struct ast_variable **cookies, struct ast_variable *headers,
425 unsigned int *static_content)
428 struct ast_str *out = NULL;
430 struct ast_http_uri *urih = NULL;
432 struct ast_variable *vars = NULL, *v, *prev = NULL;
433 struct http_uri_redirect *redirect;
436 /* preserve previous behavior of only support URI parameters on GET requests */
437 if (method == AST_HTTP_GET) {
438 strsep(¶ms, "?");
440 /* Extract arguments from the request and store them in variables.
441 * Note that a request can have multiple arguments with the same
442 * name, and we store them all in the list of variables.
443 * It is up to the application to handle multiple values.
448 while ((val = strsep(¶ms, "&"))) {
449 var = strsep(&val, "=");
456 if ((v = ast_variable_new(var, val, ""))) {
469 * Append the cookies to the list of variables.
470 * This saves a pass in the cookies list, but has the side effect
471 * that a variable might mask a cookie with the same name if the
472 * application stops at the first match.
473 * Note that this is the same behaviour as $_REQUEST variables in PHP.
476 prev->next = *cookies;
484 AST_RWLIST_RDLOCK(&uri_redirects);
485 AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
486 if (!strcasecmp(uri, redirect->target)) {
489 snprintf(buf, sizeof(buf), "Location: %s\r\n", redirect->dest);
490 out = ast_http_error((*status = 302),
491 (*title = ast_strdup("Moved Temporarily")),
492 buf, "Redirecting...");
497 AST_RWLIST_UNLOCK(&uri_redirects);
503 /* We want requests to start with the (optional) prefix and '/' */
505 if (!strncasecmp(uri, prefix, l) && uri[l] == '/') {
507 /* scan registered uris to see if we match one. */
508 AST_RWLIST_RDLOCK(&uris);
509 AST_RWLIST_TRAVERSE(&uris, urih, entry) {
510 ast_debug(2, "match request [%s] with handler [%s] len %d\n", uri, urih->uri, l);
514 if (urih->supports_get) {
519 if (urih->supports_post) {
526 l = strlen(urih->uri);
527 c = uri + l; /* candidate */
529 if (strncasecmp(urih->uri, uri, l) || /* no match */
530 (*c && *c != '/')) { /* substring */
538 if (!*c || urih->has_subtree) {
539 if (((method == AST_HTTP_GET) && urih->supports_get) ||
540 ((method == AST_HTTP_POST) && urih->supports_post)) {
549 AST_RWLIST_UNLOCK(&uris);
553 if (method == AST_HTTP_POST && !astman_is_authed(manid_from_vars(vars))) {
554 out = ast_http_error((*status = 403),
555 (*title = ast_strdup("Access Denied")),
556 NULL, "You do not have permission to access the requested URL.");
558 *static_content = urih->static_content;
559 out = urih->callback(ser, urih, uri, method, vars, headers, status, title, contentlength);
560 AST_RWLIST_UNLOCK(&uris);
561 } else if (saw_method) {
562 out = ast_http_error((*status = 404),
563 (*title = ast_strdup("Not Found")), NULL,
564 "The requested URL was not found on this server.");
566 out = ast_http_error((*status = 501),
567 (*title = ast_strdup("Not Implemented")), NULL,
568 "Attempt to use unimplemented / unsupported method");
572 ast_variables_destroy(vars);
578 #if defined(HAVE_FUNOPEN)
582 #define HOOK_T ssize_t
587 * replacement read/write functions for SSL support.
588 * We use wrappers rather than SSL_read/SSL_write directly so
589 * we can put in some debugging.
591 /*static HOOK_T ssl_read(void *cookie, char *buf, LEN_T len)
593 int i = SSL_read(cookie, buf, len-1);
597 ast_verbose("ssl read size %d returns %d <%s>\n", (int)len, i, buf);
602 static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
605 char *s = alloca(len+1);
606 strncpy(s, buf, len);
608 ast_verbose("ssl write size %d <%s>\n", (int)len, s);
610 return SSL_write(cookie, buf, len);
613 static int ssl_close(void *cookie)
615 close(SSL_get_fd(cookie));
616 SSL_shutdown(cookie);
622 static struct ast_variable *parse_cookies(char *cookies)
625 struct ast_variable *vars = NULL, *var;
630 while ((cur = strsep(&cookies, ";"))) {
636 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
640 name = ast_strip(name);
641 val = ast_strip_quoted(val, "\"", "\"");
643 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
648 ast_log(LOG_DEBUG, "mmm ... cookie! Name: '%s' Value: '%s'\n", name, val);
651 var = ast_variable_new(name, val, __FILE__);
659 static void *httpd_helper_thread(void *data)
663 struct ast_tcptls_session_instance *ser = data;
664 struct ast_variable *vars=NULL, *headers = NULL;
665 char *uri, *title=NULL;
666 int status = 200, contentlength = 0;
667 struct ast_str *out = NULL;
668 unsigned int static_content = 0;
669 struct ast_variable *tail = headers;
671 if (!fgets(buf, sizeof(buf), ser->f)) {
675 uri = ast_skip_nonblanks(buf); /* Skip method */
680 uri = ast_skip_blanks(uri); /* Skip white space */
682 if (*uri) { /* terminate at the first blank */
683 char *c = ast_skip_nonblanks(uri);
690 /* process "Cookie: " lines */
691 while (fgets(cookie, sizeof(cookie), ser->f)) {
692 /* Trim trailing characters */
693 ast_trim_blanks(cookie);
694 if (ast_strlen_zero(cookie)) {
697 if (!strncasecmp(cookie, "Cookie: ", 8)) {
698 vars = parse_cookies(cookie);
703 name = strsep(&val, ":");
704 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
707 ast_trim_blanks(name);
708 val = ast_skip_blanks(val);
711 headers = ast_variable_new(name, val, __FILE__);
714 tail->next = ast_variable_new(name, val, __FILE__);
721 out = ast_http_error(400, "Bad Request", NULL, "Invalid Request");
722 } else if (strcasecmp(buf, "post") && strcasecmp(buf, "get")) {
723 out = ast_http_error(501, "Not Implemented", NULL,
724 "Attempt to use unimplemented / unsupported method");
725 } else { /* try to serve it */
726 out = handle_uri(ser, uri, (!strcasecmp(buf, "get")) ? AST_HTTP_GET : AST_HTTP_POST,
727 &status, &title, &contentlength, &vars, headers, &static_content);
730 /* If they aren't mopped up already, clean up the cookies */
732 ast_variables_destroy(vars);
734 /* Clean up all the header information pulled as well */
736 ast_variables_destroy(headers);
740 struct timeval now = ast_tvnow();
744 ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&now, &tm, "GMT"));
747 "Server: Asterisk/%s\r\n"
749 "Connection: close\r\n"
751 status, title ? title : "OK", ast_get_version(), timebuf,
752 static_content ? "" : "Cache-Control: no-cache, no-store\r\n");
753 /* We set the no-cache headers only for dynamic content.
754 * If you want to make sure the static file you requested is not from cache,
755 * append a random variable to your GET request. Ex: 'something.html?r=109987734'
757 if (!contentlength) { /* opaque body ? just dump it hoping it is properly formatted */
758 fprintf(ser->f, "%s", ast_str_buffer(out));
760 char *tmp = strstr(ast_str_buffer(out), "\r\n\r\n");
763 fprintf(ser->f, "Content-length: %d\r\n", contentlength);
764 /* first write the header, then the body */
765 if (fwrite(ast_str_buffer(out), 1, (tmp + 4 - ast_str_buffer(out)), ser->f) != tmp + 4 - ast_str_buffer(out)) {
766 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
768 if (fwrite(tmp + 4, 1, contentlength, ser->f) != contentlength ) {
769 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
789 * \brief Add a new URI redirect
790 * The entries in the redirect list are sorted by length, just like the list
793 static void add_redirect(const char *value)
796 struct http_uri_redirect *redirect, *cur;
797 unsigned int target_len;
798 unsigned int total_len;
800 dest = ast_strdupa(value);
801 dest = ast_skip_blanks(dest);
802 target = strsep(&dest, " ");
803 target = ast_skip_blanks(target);
804 target = strsep(&target, " "); /* trim trailing whitespace */
807 ast_log(LOG_WARNING, "Invalid redirect '%s'\n", value);
811 target_len = strlen(target) + 1;
812 total_len = sizeof(*redirect) + target_len + strlen(dest) + 1;
814 if (!(redirect = ast_calloc(1, total_len))) {
818 redirect->dest = redirect->target + target_len;
819 strcpy(redirect->target, target);
820 strcpy(redirect->dest, dest);
822 AST_RWLIST_WRLOCK(&uri_redirects);
824 target_len--; /* So we can compare directly with strlen() */
825 if (AST_RWLIST_EMPTY(&uri_redirects)
826 || strlen(AST_RWLIST_FIRST(&uri_redirects)->target) <= target_len) {
827 AST_RWLIST_INSERT_HEAD(&uri_redirects, redirect, entry);
828 AST_RWLIST_UNLOCK(&uri_redirects);
833 AST_RWLIST_TRAVERSE(&uri_redirects, cur, entry) {
834 if (AST_RWLIST_NEXT(cur, entry)
835 && strlen(AST_RWLIST_NEXT(cur, entry)->target) <= target_len) {
836 AST_RWLIST_INSERT_AFTER(&uri_redirects, cur, redirect, entry);
837 AST_RWLIST_UNLOCK(&uri_redirects);
843 AST_RWLIST_INSERT_TAIL(&uri_redirects, redirect, entry);
845 AST_RWLIST_UNLOCK(&uri_redirects);
848 static int __ast_http_load(int reload)
850 struct ast_config *cfg;
851 struct ast_variable *v;
853 int newenablestatic=0;
855 struct ast_hostent ahp;
856 char newprefix[MAX_PREFIX] = "";
857 int have_sslbindaddr = 0;
858 struct http_uri_redirect *redirect;
859 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
861 cfg = ast_config_load2("http.conf", "http", config_flags);
862 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
867 memset(&http_desc.local_address, 0, sizeof(http_desc.local_address));
868 http_desc.local_address.sin_port = htons(8088);
870 memset(&https_desc.local_address, 0, sizeof(https_desc.local_address));
871 https_desc.local_address.sin_port = htons(8089);
873 http_tls_cfg.enabled = 0;
874 if (http_tls_cfg.certfile) {
875 ast_free(http_tls_cfg.certfile);
877 http_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
878 if (http_tls_cfg.cipher) {
879 ast_free(http_tls_cfg.cipher);
881 http_tls_cfg.cipher = ast_strdup("");
883 AST_RWLIST_WRLOCK(&uri_redirects);
884 while ((redirect = AST_RWLIST_REMOVE_HEAD(&uri_redirects, entry))) {
887 AST_RWLIST_UNLOCK(&uri_redirects);
890 v = ast_variable_browse(cfg, "general");
891 for (; v; v = v->next) {
892 if (!strcasecmp(v->name, "enabled")) {
893 enabled = ast_true(v->value);
894 } else if (!strcasecmp(v->name, "sslenable")) {
895 http_tls_cfg.enabled = ast_true(v->value);
896 } else if (!strcasecmp(v->name, "sslbindport")) {
897 https_desc.local_address.sin_port = htons(atoi(v->value));
898 } else if (!strcasecmp(v->name, "sslcert")) {
899 ast_free(http_tls_cfg.certfile);
900 http_tls_cfg.certfile = ast_strdup(v->value);
901 } else if (!strcasecmp(v->name, "sslcipher")) {
902 ast_free(http_tls_cfg.cipher);
903 http_tls_cfg.cipher = ast_strdup(v->value);
904 } else if (!strcasecmp(v->name, "enablestatic")) {
905 newenablestatic = ast_true(v->value);
906 } else if (!strcasecmp(v->name, "bindport")) {
907 http_desc.local_address.sin_port = htons(atoi(v->value));
908 } else if (!strcasecmp(v->name, "sslbindaddr")) {
909 if ((hp = ast_gethostbyname(v->value, &ahp))) {
910 memcpy(&https_desc.local_address.sin_addr, hp->h_addr, sizeof(https_desc.local_address.sin_addr));
911 have_sslbindaddr = 1;
913 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
915 } else if (!strcasecmp(v->name, "bindaddr")) {
916 if ((hp = ast_gethostbyname(v->value, &ahp))) {
917 memcpy(&http_desc.local_address.sin_addr, hp->h_addr, sizeof(http_desc.local_address.sin_addr));
919 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
921 } else if (!strcasecmp(v->name, "prefix")) {
922 if (!ast_strlen_zero(v->value)) {
924 ast_copy_string(newprefix + 1, v->value, sizeof(newprefix) - 1);
928 } else if (!strcasecmp(v->name, "redirect")) {
929 add_redirect(v->value);
931 ast_log(LOG_WARNING, "Ignoring unknown option '%s' in http.conf\n", v->name);
935 ast_config_destroy(cfg);
938 if (!have_sslbindaddr) {
939 https_desc.local_address.sin_addr = http_desc.local_address.sin_addr;
942 http_desc.local_address.sin_family = https_desc.local_address.sin_family = AF_INET;
944 if (strcmp(prefix, newprefix)) {
945 ast_copy_string(prefix, newprefix, sizeof(prefix));
947 enablestatic = newenablestatic;
948 ast_tcptls_server_start(&http_desc);
949 if (ast_ssl_setup(https_desc.tls_cfg)) {
950 ast_tcptls_server_start(&https_desc);
956 static char *handle_show_http(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
958 struct ast_http_uri *urih;
959 struct http_uri_redirect *redirect;
963 e->command = "http show status";
965 "Usage: http show status\n"
966 " Lists status of internal HTTP engine\n";
973 return CLI_SHOWUSAGE;
975 ast_cli(a->fd, "HTTP Server Status:\n");
976 ast_cli(a->fd, "Prefix: %s\n", prefix);
977 if (!http_desc.old_local_address.sin_family) {
978 ast_cli(a->fd, "Server Disabled\n\n");
980 ast_cli(a->fd, "Server Enabled and Bound to %s:%d\n\n",
981 ast_inet_ntoa(http_desc.old_local_address.sin_addr),
982 ntohs(http_desc.old_local_address.sin_port));
983 if (http_tls_cfg.enabled) {
984 ast_cli(a->fd, "HTTPS Server Enabled and Bound to %s:%d\n\n",
985 ast_inet_ntoa(https_desc.old_local_address.sin_addr),
986 ntohs(https_desc.old_local_address.sin_port));
990 ast_cli(a->fd, "Enabled URI's:\n");
991 AST_RWLIST_RDLOCK(&uris);
992 if (AST_RWLIST_EMPTY(&uris)) {
993 ast_cli(a->fd, "None.\n");
995 AST_RWLIST_TRAVERSE(&uris, urih, entry) {
996 ast_cli(a->fd, "%s/%s%s => %s\n", prefix, urih->uri, (urih->has_subtree ? "/..." : ""), urih->description);
999 AST_RWLIST_UNLOCK(&uris);
1001 ast_cli(a->fd, "\nEnabled Redirects:\n");
1002 AST_RWLIST_RDLOCK(&uri_redirects);
1003 AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
1004 ast_cli(a->fd, " %s => %s\n", redirect->target, redirect->dest);
1006 if (AST_RWLIST_EMPTY(&uri_redirects)) {
1007 ast_cli(a->fd, " None.\n");
1009 AST_RWLIST_UNLOCK(&uri_redirects);
1014 int ast_http_reload(void)
1016 return __ast_http_load(1);
1019 static struct ast_cli_entry cli_http[] = {
1020 AST_CLI_DEFINE(handle_show_http, "Display HTTP server status"),
1023 int ast_http_init(void)
1025 ast_http_uri_link(&statusuri);
1026 ast_http_uri_link(&staticuri);
1027 ast_cli_register_multiple(cli_http, ARRAY_LEN(cli_http));
1029 return __ast_http_load(0);