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 * \extref GMime http://spruce.sourceforge.net/gmime/
30 * \ref AstHTTP - AMI over the http protocol
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include <sys/signal.h>
43 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
44 #include "asterisk/network.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/tcptls.h"
47 #include "asterisk/http.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/strings.h"
50 #include "asterisk/config.h"
51 #include "asterisk/stringfields.h"
52 #include "asterisk/ast_version.h"
53 #include "asterisk/manager.h"
54 #include "asterisk/_private.h"
55 #include "asterisk/astobj2.h"
59 /* See http.h for more information about the SSL implementation */
60 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
61 #define DO_SSL /* comment in/out if you want to support ssl */
64 static struct ast_tls_config http_tls_cfg;
66 static void *httpd_helper_thread(void *arg);
69 * we have up to two accepting threads, one for http, one for https
71 static struct ast_tcptls_session_args http_desc = {
73 .master = AST_PTHREADT_NULL,
76 .name = "http server",
77 .accept_fn = ast_tcptls_server_root,
78 .worker_fn = httpd_helper_thread,
81 static struct ast_tcptls_session_args https_desc = {
83 .master = AST_PTHREADT_NULL,
84 .tls_cfg = &http_tls_cfg,
86 .name = "https server",
87 .accept_fn = ast_tcptls_server_root,
88 .worker_fn = httpd_helper_thread,
91 static AST_RWLIST_HEAD_STATIC(uris, ast_http_uri); /*!< list of supported handlers */
93 /* all valid URIs must be prepended by the string in prefix. */
94 static char prefix[MAX_PREFIX];
95 static int enablestatic;
97 /*! \brief Limit the kinds of files we're willing to serve up */
102 { "png", "image/png" },
103 { "xml", "text/xml" },
104 { "jpg", "image/jpeg" },
105 { "js", "application/x-javascript" },
106 { "wav", "audio/x-wav" },
107 { "mp3", "audio/mpeg" },
108 { "svg", "image/svg+xml" },
109 { "svgz", "image/svg+xml" },
110 { "gif", "image/gif" },
113 struct http_uri_redirect {
114 AST_LIST_ENTRY(http_uri_redirect) entry;
119 static AST_RWLIST_HEAD_STATIC(uri_redirects, http_uri_redirect);
121 static const struct ast_cfhttp_methods_text {
122 enum ast_http_method method;
124 } ast_http_methods_text[] = {
125 { AST_HTTP_UNKNOWN, "UNKNOWN" },
126 { AST_HTTP_GET, "GET" },
127 { AST_HTTP_POST, "POST" },
128 { AST_HTTP_HEAD, "HEAD" },
129 { AST_HTTP_PUT, "PUT" },
132 const char *ast_get_http_method(enum ast_http_method method)
134 return ast_http_methods_text[method].text;
137 const char *ast_http_ftype2mtype(const char *ftype)
142 for (x = 0; x < ARRAY_LEN(mimetypes); x++) {
143 if (!strcasecmp(ftype, mimetypes[x].ext)) {
144 return mimetypes[x].mtype;
151 uint32_t ast_http_manid_from_vars(struct ast_variable *headers)
154 struct ast_variable *v, *cookies;
156 cookies = ast_http_get_cookies(headers);
157 for (v = cookies; v; v = v->next) {
158 if (!strcasecmp(v->name, "mansession_id")) {
159 sscanf(v->value, "%30x", &mngid);
164 ast_variables_destroy(cookies);
169 void ast_http_prefix(char *buf, int len)
172 ast_copy_string(buf, prefix, len);
176 static int static_callback(struct ast_tcptls_session_instance *ser,
177 const struct ast_http_uri *urih, const char *uri,
178 enum ast_http_method method, struct ast_variable *get_vars,
179 struct ast_variable *headers)
188 struct ast_str *http_header;
191 char timebuf[80], etag[23];
192 struct ast_variable *v;
193 int not_modified = 0;
195 if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
196 ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
200 /* Yuck. I'm not really sold on this, but if you don't deliver static content it makes your configuration
201 substantially more challenging, but this seems like a rather irritating feature creep on Asterisk. */
202 if (!enablestatic || ast_strlen_zero(uri)) {
206 /* Disallow any funny filenames at all */
207 if ((uri[0] < 33) || strchr("./|~@#$%^&*() \t", uri[0])) {
211 if (strstr(uri, "/..")) {
215 if ((ftype = strrchr(uri, '.'))) {
219 if (!(mtype = ast_http_ftype2mtype(ftype))) {
220 snprintf(wkspace, sizeof(wkspace), "text/%s", S_OR(ftype, "plain"));
223 /* Cap maximum length */
224 if ((len = strlen(uri) + strlen(ast_config_AST_DATA_DIR) + strlen("/static-http/") + 5) > 1024) {
229 sprintf(path, "%s/static-http/%s", ast_config_AST_DATA_DIR, uri);
230 if (stat(path, &st)) {
234 if (S_ISDIR(st.st_mode)) {
238 fd = open(path, O_RDONLY);
243 if (strstr(path, "/private/") && !astman_is_authed(ast_http_manid_from_vars(headers))) {
247 /* make "Etag:" http header value */
248 snprintf(etag, sizeof(etag), "\"%ld\"", (long)st.st_mtime);
250 /* make "Last-Modified:" http header value */
251 tv.tv_sec = st.st_mtime;
253 ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", ast_localtime(&tv, &tm, "GMT"));
255 /* check received "If-None-Match" request header and Etag value for file */
256 for (v = headers; v; v = v->next) {
257 if (!strcasecmp(v->name, "If-None-Match")) {
258 if (!strcasecmp(v->value, etag)) {
265 if ( (http_header = ast_str_create(255)) == NULL) {
269 ast_str_set(&http_header, 0, "Content-type: %s\r\n"
276 /* ast_http_send() frees http_header, so we don't need to do it before returning */
278 ast_http_send(ser, method, 304, "Not Modified", http_header, NULL, 0, 1);
280 ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 1); /* static content flag is set */
286 ast_http_error(ser, 404, "Not Found", "The requested URL was not found on this server.");
290 ast_http_error(ser, 403, "Access Denied", "You do not have permission to access the requested URL.");
294 static int httpstatus_callback(struct ast_tcptls_session_instance *ser,
295 const struct ast_http_uri *urih, const char *uri,
296 enum ast_http_method method, struct ast_variable *get_vars,
297 struct ast_variable *headers)
300 struct ast_variable *v, *cookies = NULL;
302 if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
303 ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
307 if ( (out = ast_str_create(512)) == NULL) {
311 ast_str_append(&out, 0,
312 "<title>Asterisk HTTP Status</title>\r\n"
313 "<body bgcolor=\"#ffffff\">\r\n"
314 "<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
315 "<h2> Asterisk™ HTTP Status</h2></td></tr>\r\n");
317 ast_str_append(&out, 0, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
318 ast_str_append(&out, 0, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
319 ast_inet_ntoa(http_desc.old_address.sin_addr));
320 ast_str_append(&out, 0, "<tr><td><i>Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
321 ntohs(http_desc.old_address.sin_port));
322 if (http_tls_cfg.enabled) {
323 ast_str_append(&out, 0, "<tr><td><i>SSL Bind Port</i></td><td><b>%d</b></td></tr>\r\n",
324 ntohs(https_desc.old_address.sin_port));
326 ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
327 for (v = get_vars; v; v = v->next) {
328 ast_str_append(&out, 0, "<tr><td><i>Submitted GET Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
330 ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
332 cookies = ast_http_get_cookies(headers);
333 for (v = cookies; v; v = v->next) {
334 ast_str_append(&out, 0, "<tr><td><i>Cookie '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
336 ast_variables_destroy(cookies);
338 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");
339 ast_http_send(ser, method, 200, NULL, NULL, out, 0, 0);
343 static struct ast_http_uri statusuri = {
344 .callback = httpstatus_callback,
345 .description = "Asterisk HTTP General Status",
352 static struct ast_http_uri staticuri = {
353 .callback = static_callback,
354 .description = "Asterisk HTTP Static Delivery",
362 /* send http/1.1 responce */
363 /* free content variable and close socket*/
364 void ast_http_send(struct ast_tcptls_session_instance *ser,
365 enum ast_http_method method, int status_code, const char *status_title,
366 struct ast_str *http_header, struct ast_str *out, const int fd,
367 unsigned int static_content)
369 struct timeval now = ast_tvnow();
372 int content_length = 0;
374 if (!ser || 0 == ser->f) {
378 ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", ast_localtime(&now, &tm, "GMT"));
380 /* calc conetnt length */
382 content_length += strlen(ast_str_buffer(out));
386 content_length += lseek(fd, 0, SEEK_END);
387 lseek(fd, 0, SEEK_SET);
390 /* send http header */
391 fprintf(ser->f, "HTTP/1.1 %d %s\r\n"
392 "Server: Asterisk/%s\r\n"
394 "Connection: close\r\n"
396 "Content-Length: %d\r\n"
398 status_code, status_title ? status_title : "OK",
401 static_content ? "" : "Cache-Control: no-cache, no-store\r\n",
403 http_header ? ast_str_buffer(http_header) : ""
407 if (method != AST_HTTP_HEAD || status_code >= 400) {
409 fprintf(ser->f, "%s", ast_str_buffer(out));
415 while ((len = read(fd, buf, sizeof(buf))) > 0) {
416 if (fwrite(buf, len, 1, ser->f) != len) {
417 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
424 ast_free(http_header);
435 /* Send http "401 Unauthorized" responce and close socket*/
436 void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm,
437 const unsigned long nonce, const unsigned long opaque, int stale,
440 struct ast_str *http_headers = ast_str_create(128);
441 struct ast_str *out = ast_str_create(512);
443 if (!http_headers || !out) {
444 ast_free(http_headers);
449 ast_str_set(&http_headers, 0,
450 "WWW-authenticate: Digest algorithm=MD5, realm=\"%s\", nonce=\"%08lx\", qop=\"auth\", opaque=\"%08lx\"%s\r\n"
451 "Content-type: text/html",
452 realm ? realm : "Asterisk",
455 stale ? ", stale=true" : "");
458 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
460 "<title>401 Unauthorized</title>\r\n"
462 "<h1>401 Unauthorized</h1>\r\n"
465 "<address>Asterisk Server</address>\r\n"
466 "</body></html>\r\n",
469 ast_http_send(ser, AST_HTTP_UNKNOWN, 401, "Unauthorized", http_headers, out, 0, 0);
473 /* send http error responce and close socket*/
474 void ast_http_error(struct ast_tcptls_session_instance *ser, int status_code, const char *status_title, const char *text)
476 struct ast_str *http_headers = ast_str_create(40);
477 struct ast_str *out = ast_str_create(256);
479 if (!http_headers || !out) {
480 ast_free(http_headers);
485 ast_str_set(&http_headers, 0, "Content-type: text/html");
488 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
490 "<title>%d %s</title>\r\n"
495 "<address>Asterisk Server</address>\r\n"
496 "</body></html>\r\n",
497 status_code, status_title, status_title, text);
499 ast_http_send(ser, AST_HTTP_UNKNOWN, status_code, status_title, http_headers, out, 0, 0);
504 * Link the new uri into the list.
506 * They are sorted by length of
507 * the string, not alphabetically. Duplicate entries are not replaced,
508 * but the insertion order (using <= and not just <) makes sure that
509 * more recent insertions hide older ones.
510 * On a lookup, we just scan the list and stop at the first matching entry.
512 int ast_http_uri_link(struct ast_http_uri *urih)
514 struct ast_http_uri *uri;
515 int len = strlen(urih->uri);
517 AST_RWLIST_WRLOCK(&uris);
519 if ( AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len ) {
520 AST_RWLIST_INSERT_HEAD(&uris, urih, entry);
521 AST_RWLIST_UNLOCK(&uris);
525 AST_RWLIST_TRAVERSE(&uris, uri, entry) {
526 if (AST_RWLIST_NEXT(uri, entry) &&
527 strlen(AST_RWLIST_NEXT(uri, entry)->uri) <= len) {
528 AST_RWLIST_INSERT_AFTER(&uris, uri, urih, entry);
529 AST_RWLIST_UNLOCK(&uris);
535 AST_RWLIST_INSERT_TAIL(&uris, urih, entry);
537 AST_RWLIST_UNLOCK(&uris);
542 void ast_http_uri_unlink(struct ast_http_uri *urih)
544 AST_RWLIST_WRLOCK(&uris);
545 AST_RWLIST_REMOVE(&uris, urih, entry);
546 AST_RWLIST_UNLOCK(&uris);
549 void ast_http_uri_unlink_all_with_key(const char *key)
551 struct ast_http_uri *urih;
552 AST_RWLIST_WRLOCK(&uris);
553 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&uris, urih, entry) {
554 if (!strcmp(urih->key, key)) {
555 AST_RWLIST_REMOVE_CURRENT(entry);
557 if (urih->dmallocd) {
558 ast_free(urih->data);
564 AST_RWLIST_TRAVERSE_SAFE_END;
565 AST_RWLIST_UNLOCK(&uris);
569 * Decode special characters in http uri.
570 * We have ast_uri_decode to handle %XX sequences, but spaces
571 * are encoded as a '+' so we need to replace them beforehand.
573 static void http_decode(char *s)
577 for (t = s; *t; t++) {
587 * get post variables from client Request Entity-Body, if content type is
588 * application/x-www-form-urlencoded
590 struct ast_variable *ast_http_get_post_vars(
591 struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
593 int content_length = 0;
594 struct ast_variable *v, *post_vars=NULL, *prev = NULL;
595 char *buf, *var, *val;
597 for (v = headers; v; v = v->next) {
598 if (!strcasecmp(v->name, "Content-Type")) {
599 if (strcasecmp(v->value, "application/x-www-form-urlencoded")) {
606 for (v = headers; v; v = v->next) {
607 if (!strcasecmp(v->name, "Content-Length")) {
608 content_length = atoi(v->value) + 1;
613 if (!content_length) {
617 if (!(buf = alloca(content_length))) {
620 if (!fgets(buf, content_length, ser->f)) {
624 while ((val = strsep(&buf, "&"))) {
625 var = strsep(&val, "=");
632 if ((v = ast_variable_new(var, val, ""))) {
644 static int handle_uri(struct ast_tcptls_session_instance *ser, char *uri,
645 enum ast_http_method method, struct ast_variable *headers)
650 struct ast_http_uri *urih = NULL;
652 struct ast_variable *get_vars = NULL, *v, *prev = NULL;
653 struct http_uri_redirect *redirect;
655 strsep(¶ms, "?");
656 /* Extract arguments from the request and store them in variables. */
660 while ((val = strsep(¶ms, "&"))) {
661 var = strsep(&val, "=");
668 if ((v = ast_variable_new(var, val, ""))) {
680 AST_RWLIST_RDLOCK(&uri_redirects);
681 AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
682 if (!strcasecmp(uri, redirect->target)) {
683 struct ast_str *http_header = ast_str_create(128);
684 ast_str_set(&http_header, 0, "Location: %s\r\n", redirect->dest);
685 ast_http_send(ser, method, 302, "Moved Temporarily", http_header, NULL, 0, 0);
690 AST_RWLIST_UNLOCK(&uri_redirects);
695 /* We want requests to start with the (optional) prefix and '/' */
697 if (!strncasecmp(uri, prefix, l) && uri[l] == '/') {
699 /* scan registered uris to see if we match one. */
700 AST_RWLIST_RDLOCK(&uris);
701 AST_RWLIST_TRAVERSE(&uris, urih, entry) {
702 ast_debug(2, "match request [%s] with handler [%s] len %d\n", uri, urih->uri, l);
703 l = strlen(urih->uri);
704 c = uri + l; /* candidate */
705 if (strncasecmp(urih->uri, uri, l) /* no match */
706 || (*c && *c != '/')) { /* substring */
712 if (!*c || urih->has_subtree) {
717 AST_RWLIST_UNLOCK(&uris);
720 res = urih->callback(ser, urih, uri, method, get_vars, headers);
722 ast_http_error(ser, 404, "Not Found", "The requested URL was not found on this server.");
726 ast_variables_destroy(get_vars);
731 #if defined(HAVE_FUNOPEN)
735 #define HOOK_T ssize_t
740 * replacement read/write functions for SSL support.
741 * We use wrappers rather than SSL_read/SSL_write directly so
742 * we can put in some debugging.
744 /*static HOOK_T ssl_read(void *cookie, char *buf, LEN_T len)
746 int i = SSL_read(cookie, buf, len-1);
750 ast_verbose("ssl read size %d returns %d <%s>\n", (int)len, i, buf);
755 static HOOK_T ssl_write(void *cookie, const char *buf, LEN_T len)
758 char *s = alloca(len+1);
759 strncpy(s, buf, len);
761 ast_verbose("ssl write size %d <%s>\n", (int)len, s);
763 return SSL_write(cookie, buf, len);
766 static int ssl_close(void *cookie)
768 close(SSL_get_fd(cookie));
769 SSL_shutdown(cookie);
775 static struct ast_variable *parse_cookies(char *cookies)
778 struct ast_variable *vars = NULL, *var;
780 while ((cur = strsep(&cookies, ";"))) {
786 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
790 name = ast_strip(name);
791 val = ast_strip_quoted(val, "\"", "\"");
793 if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
798 ast_log(LOG_DEBUG, "mmm ... cookie! Name: '%s' Value: '%s'\n", name, val);
801 var = ast_variable_new(name, val, __FILE__);
809 /* get cookie from Request headers */
810 struct ast_variable *ast_http_get_cookies(struct ast_variable *headers)
812 struct ast_variable *v, *cookies=NULL;
814 for (v = headers; v; v = v->next) {
815 if (!strncasecmp(v->name, "Cookie", 6)) {
816 char *tmp = ast_strdupa(v->value);
818 ast_variables_destroy(cookies);
821 cookies = parse_cookies(tmp);
828 static void *httpd_helper_thread(void *data)
831 char header_line[4096];
832 struct ast_tcptls_session_instance *ser = data;
833 struct ast_variable *headers = NULL;
834 struct ast_variable *tail = headers;
836 enum ast_http_method http_method = AST_HTTP_UNKNOWN;
838 if (!fgets(buf, sizeof(buf), ser->f)) {
843 method = ast_skip_blanks(buf);
844 uri = ast_skip_nonblanks(method);
849 if (!strcasecmp(method,"GET")) {
850 http_method = AST_HTTP_GET;
851 } else if (!strcasecmp(method,"POST")) {
852 http_method = AST_HTTP_POST;
853 } else if (!strcasecmp(method,"HEAD")) {
854 http_method = AST_HTTP_HEAD;
855 } else if (!strcasecmp(method,"PUT")) {
856 http_method = AST_HTTP_PUT;
859 uri = ast_skip_blanks(uri); /* Skip white space */
861 if (*uri) { /* terminate at the first blank */
862 char *c = ast_skip_nonblanks(uri);
869 /* process "Request Headers" lines */
870 while (fgets(header_line, sizeof(header_line), ser->f)) {
873 /* Trim trailing characters */
874 ast_trim_blanks(header_line);
875 if (ast_strlen_zero(header_line)) {
880 name = strsep(&value, ":");
885 value = ast_skip_blanks(value);
886 if (ast_strlen_zero(value) || ast_strlen_zero(name)) {
890 ast_trim_blanks(name);
893 headers = ast_variable_new(name, value, __FILE__);
896 tail->next = ast_variable_new(name, value, __FILE__);
902 ast_http_error(ser, 400, "Bad Request", "Invalid Request");
906 handle_uri(ser, uri, http_method, headers);
908 /* Clean up all the header information pulled as well */
910 ast_variables_destroy(headers);
923 * \brief Add a new URI redirect
924 * The entries in the redirect list are sorted by length, just like the list
927 static void add_redirect(const char *value)
930 struct http_uri_redirect *redirect, *cur;
931 unsigned int target_len;
932 unsigned int total_len;
934 dest = ast_strdupa(value);
935 dest = ast_skip_blanks(dest);
936 target = strsep(&dest, " ");
937 target = ast_skip_blanks(target);
938 target = strsep(&target, " "); /* trim trailing whitespace */
941 ast_log(LOG_WARNING, "Invalid redirect '%s'\n", value);
945 target_len = strlen(target) + 1;
946 total_len = sizeof(*redirect) + target_len + strlen(dest) + 1;
948 if (!(redirect = ast_calloc(1, total_len))) {
951 redirect->dest = redirect->target + target_len;
952 strcpy(redirect->target, target);
953 strcpy(redirect->dest, dest);
955 AST_RWLIST_WRLOCK(&uri_redirects);
957 target_len--; /* So we can compare directly with strlen() */
958 if (AST_RWLIST_EMPTY(&uri_redirects)
959 || strlen(AST_RWLIST_FIRST(&uri_redirects)->target) <= target_len ) {
960 AST_RWLIST_INSERT_HEAD(&uri_redirects, redirect, entry);
961 AST_RWLIST_UNLOCK(&uri_redirects);
966 AST_RWLIST_TRAVERSE(&uri_redirects, cur, entry) {
967 if (AST_RWLIST_NEXT(cur, entry)
968 && strlen(AST_RWLIST_NEXT(cur, entry)->target) <= target_len ) {
969 AST_RWLIST_INSERT_AFTER(&uri_redirects, cur, redirect, entry);
970 AST_RWLIST_UNLOCK(&uri_redirects);
975 AST_RWLIST_INSERT_TAIL(&uri_redirects, redirect, entry);
977 AST_RWLIST_UNLOCK(&uri_redirects);
980 static int __ast_http_load(int reload)
982 struct ast_config *cfg;
983 struct ast_variable *v;
985 int newenablestatic=0;
987 struct ast_hostent ahp;
988 char newprefix[MAX_PREFIX] = "";
989 struct http_uri_redirect *redirect;
990 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
992 cfg = ast_config_load2("http.conf", "http", config_flags);
993 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
998 memset(&http_desc.local_address, 0, sizeof(http_desc.local_address));
999 http_desc.local_address.sin_port = htons(8088);
1001 memset(&https_desc.local_address, 0, sizeof(https_desc.local_address));
1002 https_desc.local_address.sin_port = htons(8089);
1004 http_tls_cfg.enabled = 0;
1005 if (http_tls_cfg.certfile) {
1006 ast_free(http_tls_cfg.certfile);
1008 http_tls_cfg.certfile = ast_strdup(AST_CERTFILE);
1010 if (http_tls_cfg.pvtfile) {
1011 ast_free(http_tls_cfg.pvtfile);
1013 http_tls_cfg.pvtfile = ast_strdup("");
1015 if (http_tls_cfg.cipher) {
1016 ast_free(http_tls_cfg.cipher);
1018 http_tls_cfg.cipher = ast_strdup("");
1020 AST_RWLIST_WRLOCK(&uri_redirects);
1021 while ((redirect = AST_RWLIST_REMOVE_HEAD(&uri_redirects, entry))) {
1024 AST_RWLIST_UNLOCK(&uri_redirects);
1027 v = ast_variable_browse(cfg, "general");
1028 for (; v; v = v->next) {
1030 /* handle tls conf */
1031 if (!ast_tls_read_conf(&http_tls_cfg, &https_desc, v->name, v->value)) {
1035 if (!strcasecmp(v->name, "enabled")) {
1036 enabled = ast_true(v->value);
1037 } else if (!strcasecmp(v->name, "enablestatic")) {
1038 newenablestatic = ast_true(v->value);
1039 } else if (!strcasecmp(v->name, "bindport")) {
1040 http_desc.local_address.sin_port = htons(atoi(v->value));
1041 } else if (!strcasecmp(v->name, "bindaddr")) {
1042 if ((hp = ast_gethostbyname(v->value, &ahp))) {
1043 memcpy(&http_desc.local_address.sin_addr, hp->h_addr, sizeof(http_desc.local_address.sin_addr));
1045 ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
1047 } else if (!strcasecmp(v->name, "prefix")) {
1048 if (!ast_strlen_zero(v->value)) {
1050 ast_copy_string(newprefix + 1, v->value, sizeof(newprefix) - 1);
1052 newprefix[0] = '\0';
1054 } else if (!strcasecmp(v->name, "redirect")) {
1055 add_redirect(v->value);
1057 ast_log(LOG_WARNING, "Ignoring unknown option '%s' in http.conf\n", v->name);
1061 ast_config_destroy(cfg);
1063 /* if the https addres has not been set, default is the same as non secure http */
1064 if (!https_desc.local_address.sin_addr.s_addr) {
1065 https_desc.local_address.sin_addr = http_desc.local_address.sin_addr;
1068 http_desc.local_address.sin_family = https_desc.local_address.sin_family = AF_INET;
1070 if (strcmp(prefix, newprefix)) {
1071 ast_copy_string(prefix, newprefix, sizeof(prefix));
1073 enablestatic = newenablestatic;
1074 ast_tcptls_server_start(&http_desc);
1075 if (ast_ssl_setup(https_desc.tls_cfg)) {
1076 ast_tcptls_server_start(&https_desc);
1082 static char *handle_show_http(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1084 struct ast_http_uri *urih;
1085 struct http_uri_redirect *redirect;
1089 e->command = "http show status";
1091 "Usage: http show status\n"
1092 " Lists status of internal HTTP engine\n";
1099 return CLI_SHOWUSAGE;
1101 ast_cli(a->fd, "HTTP Server Status:\n");
1102 ast_cli(a->fd, "Prefix: %s\n", prefix);
1103 if (!http_desc.old_address.sin_family) {
1104 ast_cli(a->fd, "Server Disabled\n\n");
1106 ast_cli(a->fd, "Server Enabled and Bound to %s:%d\n\n",
1107 ast_inet_ntoa(http_desc.old_address.sin_addr),
1108 ntohs(http_desc.old_address.sin_port));
1109 if (http_tls_cfg.enabled) {
1110 ast_cli(a->fd, "HTTPS Server Enabled and Bound to %s:%d\n\n",
1111 ast_inet_ntoa(https_desc.old_address.sin_addr),
1112 ntohs(https_desc.old_address.sin_port));
1116 ast_cli(a->fd, "Enabled URI's:\n");
1117 AST_RWLIST_RDLOCK(&uris);
1118 if (AST_RWLIST_EMPTY(&uris)) {
1119 ast_cli(a->fd, "None.\n");
1121 AST_RWLIST_TRAVERSE(&uris, urih, entry)
1122 ast_cli(a->fd, "%s/%s%s => %s\n", prefix, urih->uri, (urih->has_subtree ? "/..." : "" ), urih->description);
1124 AST_RWLIST_UNLOCK(&uris);
1126 ast_cli(a->fd, "\nEnabled Redirects:\n");
1127 AST_RWLIST_RDLOCK(&uri_redirects);
1128 AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry)
1129 ast_cli(a->fd, " %s => %s\n", redirect->target, redirect->dest);
1130 if (AST_RWLIST_EMPTY(&uri_redirects)) {
1131 ast_cli(a->fd, " None.\n");
1133 AST_RWLIST_UNLOCK(&uri_redirects);
1138 int ast_http_reload(void)
1140 return __ast_http_load(1);
1143 static struct ast_cli_entry cli_http[] = {
1144 AST_CLI_DEFINE(handle_show_http, "Display HTTP server status"),
1147 int ast_http_init(void)
1149 ast_http_uri_link(&statusuri);
1150 ast_http_uri_link(&staticuri);
1151 ast_cli_register_multiple(cli_http, ARRAY_LEN(cli_http));
1153 return __ast_http_load(0);