Asterisk - The Open Source Telephony Project  GIT-master-932eae6
http.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*!
20  * \file
21  * \brief http server for AMI access
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * This program implements a tiny http server
26  * and was inspired by micro-httpd by Jef Poskanzer
27  *
28  * GMime http://spruce.sourceforge.net/gmime/
29  *
30  * \ref AstHTTP - AMI over the http protocol
31  */
32 
33 /*! \li \ref http.c uses the configuration file \ref http.conf
34  * \addtogroup configuration_file
35  */
36 
37 /*! \page http.conf http.conf
38  * \verbinclude http.conf.sample
39  */
40 
41 /*** MODULEINFO
42  <support_level>core</support_level>
43  ***/
44 
45 #include "asterisk.h"
46 
47 #include <time.h>
48 #include <sys/time.h>
49 #include <sys/stat.h>
50 #include <signal.h>
51 #include <fcntl.h>
52 
53 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
54 #include "asterisk/cli.h"
55 #include "asterisk/tcptls.h"
56 #include "asterisk/http.h"
57 #include "asterisk/utils.h"
58 #include "asterisk/strings.h"
59 #include "asterisk/config.h"
60 #include "asterisk/stringfields.h"
61 #include "asterisk/ast_version.h"
62 #include "asterisk/manager.h"
63 #include "asterisk/module.h"
64 #include "asterisk/astobj2.h"
65 #include "asterisk/netsock2.h"
66 #include "asterisk/json.h"
67 
68 #define MAX_PREFIX 80
69 #define DEFAULT_PORT 8088
70 #define DEFAULT_TLS_PORT 8089
71 #define DEFAULT_SESSION_LIMIT 100
72 /*! (ms) Idle time waiting for data. */
73 #define DEFAULT_SESSION_INACTIVITY 30000
74 /*! (ms) Min timeout for initial HTTP request to start coming in. */
75 #define MIN_INITIAL_REQUEST_TIMEOUT 10000
76 /*! (ms) Idle time between HTTP requests */
77 #define DEFAULT_SESSION_KEEP_ALIVE 15000
78 /*! Max size for the http server name */
79 #define MAX_SERVER_NAME_LENGTH 128
80 /*! Max size for the http response header */
81 #define DEFAULT_RESPONSE_HEADER_LENGTH 512
82 
83 /*! Maximum application/json or application/x-www-form-urlencoded body content length. */
84 #if !defined(LOW_MEMORY)
85 #define MAX_CONTENT_LENGTH 40960
86 #else
87 #define MAX_CONTENT_LENGTH 1024
88 #endif /* !defined(LOW_MEMORY) */
89 
90 /*! Initial response body length. */
91 #if !defined(LOW_MEMORY)
92 #define INITIAL_RESPONSE_BODY_BUFFER 1024
93 #else
94 #define INITIAL_RESPONSE_BODY_BUFFER 512
95 #endif /* !defined(LOW_MEMORY) */
96 
97 /*! Maximum line length for HTTP requests. */
98 #if !defined(LOW_MEMORY)
99 #define MAX_HTTP_LINE_LENGTH 4096
100 #else
101 #define MAX_HTTP_LINE_LENGTH 1024
102 #endif /* !defined(LOW_MEMORY) */
103 
105 
109 static int session_count = 0;
110 
112 
113 static void *httpd_helper_thread(void *arg);
114 
115 /*!
116  * we have up to two accepting threads, one for http, one for https
117  */
119  .accept_fd = -1,
120  .master = AST_PTHREADT_NULL,
121  .tls_cfg = NULL,
122  .poll_timeout = -1,
123  .name = "http server",
124  .accept_fn = ast_tcptls_server_root,
125  .worker_fn = httpd_helper_thread,
126 };
127 
129  .accept_fd = -1,
130  .master = AST_PTHREADT_NULL,
131  .tls_cfg = &http_tls_cfg,
132  .poll_timeout = -1,
133  .name = "https server",
134  .accept_fn = ast_tcptls_server_root,
135  .worker_fn = httpd_helper_thread,
136 };
137 
138 static AST_RWLIST_HEAD_STATIC(uris, ast_http_uri); /*!< list of supported handlers */
139 
140 /* all valid URIs must be prepended by the string in prefix. */
141 static char prefix[MAX_PREFIX];
144 
145 /*! \brief Limit the kinds of files we're willing to serve up */
146 static struct {
147  const char *ext;
148  const char *mtype;
149 } mimetypes[] = {
150  { "png", "image/png" },
151  { "xml", "text/xml" },
152  { "jpg", "image/jpeg" },
153  { "js", "application/x-javascript" },
154  { "wav", "audio/x-wav" },
155  { "mp3", "audio/mpeg" },
156  { "svg", "image/svg+xml" },
157  { "svgz", "image/svg+xml" },
158  { "gif", "image/gif" },
159  { "html", "text/html" },
160  { "htm", "text/html" },
161  { "css", "text/css" },
162  { "cnf", "text/plain" },
163  { "cfg", "text/plain" },
164  { "bin", "application/octet-stream" },
165  { "sbn", "application/octet-stream" },
166  { "ld", "application/octet-stream" },
167 };
168 
171  char *dest;
172  char target[0];
173 };
174 
176 
177 static const struct ast_cfhttp_methods_text {
179  const char *text;
180 } ast_http_methods_text[] = {
181  { AST_HTTP_UNKNOWN, "UNKNOWN" },
182  { AST_HTTP_GET, "GET" },
183  { AST_HTTP_POST, "POST" },
184  { AST_HTTP_HEAD, "HEAD" },
185  { AST_HTTP_PUT, "PUT" },
186  { AST_HTTP_DELETE, "DELETE" },
187  { AST_HTTP_OPTIONS, "OPTIONS" },
188 };
189 
191 {
192  int x;
193 
194  for (x = 0; x < ARRAY_LEN(ast_http_methods_text); x++) {
195  if (ast_http_methods_text[x].method == method) {
196  return ast_http_methods_text[x].text;
197  }
198  }
199 
200  return NULL;
201 }
202 
203 const char *ast_http_ftype2mtype(const char *ftype)
204 {
205  int x;
206 
207  if (ftype) {
208  for (x = 0; x < ARRAY_LEN(mimetypes); x++) {
209  if (!strcasecmp(ftype, mimetypes[x].ext)) {
210  return mimetypes[x].mtype;
211  }
212  }
213  }
214  return NULL;
215 }
216 
217 uint32_t ast_http_manid_from_vars(struct ast_variable *headers)
218 {
219  uint32_t mngid = 0;
220  struct ast_variable *v, *cookies;
221 
222  cookies = ast_http_get_cookies(headers);
223  for (v = cookies; v; v = v->next) {
224  if (!strcasecmp(v->name, "mansession_id")) {
225  sscanf(v->value, "%30x", &mngid);
226  break;
227  }
228  }
229  ast_variables_destroy(cookies);
230  return mngid;
231 }
232 
233 void ast_http_prefix(char *buf, int len)
234 {
235  if (buf) {
236  ast_copy_string(buf, prefix, len);
237  }
238 }
239 
241  const struct ast_http_uri *urih, const char *uri,
242  enum ast_http_method method, struct ast_variable *get_vars,
243  struct ast_variable *headers)
244 {
245  char *path;
246  const char *ftype;
247  const char *mtype;
248  char wkspace[80];
249  struct stat st;
250  int len;
251  int fd;
252  struct ast_str *http_header;
253  struct timeval tv;
254  struct ast_tm tm;
255  char timebuf[80], etag[23];
256  struct ast_variable *v;
257  int not_modified = 0;
258 
259  if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
260  ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
261  return 0;
262  }
263 
264  /* Yuck. I'm not really sold on this, but if you don't deliver static content it
265  * makes your configuration substantially more challenging, but this seems like a
266  * rather irritating feature creep on Asterisk.
267  *
268  * XXX: It is not clear to me what this comment means or if it is any longer
269  * relevant. */
270  if (ast_strlen_zero(uri)) {
271  goto out403;
272  }
273 
274  /* Disallow any funny filenames at all (checking first character only??) */
275  if ((uri[0] < 33) || strchr("./|~@#$%^&*() \t", uri[0])) {
276  goto out403;
277  }
278 
279  if (strstr(uri, "/..")) {
280  goto out403;
281  }
282 
283  if ((ftype = strrchr(uri, '.'))) {
284  ftype++;
285  }
286 
287  if (!(mtype = ast_http_ftype2mtype(ftype))) {
288  snprintf(wkspace, sizeof(wkspace), "text/%s", S_OR(ftype, "plain"));
289  mtype = wkspace;
290  }
291 
292  /* Cap maximum length */
293  if ((len = strlen(uri) + strlen(ast_config_AST_DATA_DIR) + strlen("/static-http/") + 5) > 1024) {
294  goto out403;
295  }
296 
297  path = ast_alloca(len);
298  sprintf(path, "%s/static-http/%s", ast_config_AST_DATA_DIR, uri);
299  if (stat(path, &st)) {
300  goto out404;
301  }
302 
303  if (S_ISDIR(st.st_mode)) {
304  goto out404;
305  }
306 
307  if (strstr(path, "/private/") && !astman_is_authed(ast_http_manid_from_vars(headers))) {
308  goto out403;
309  }
310 
311  fd = open(path, O_RDONLY);
312  if (fd < 0) {
313  goto out403;
314  }
315 
316  /* make "Etag:" http header value */
317  snprintf(etag, sizeof(etag), "\"%ld\"", (long)st.st_mtime);
318 
319  /* make "Last-Modified:" http header value */
320  tv.tv_sec = st.st_mtime;
321  tv.tv_usec = 0;
322  ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", ast_localtime(&tv, &tm, "GMT"));
323 
324  /* check received "If-None-Match" request header and Etag value for file */
325  for (v = headers; v; v = v->next) {
326  if (!strcasecmp(v->name, "If-None-Match")) {
327  if (!strcasecmp(v->value, etag)) {
328  not_modified = 1;
329  }
330  break;
331  }
332  }
333 
334  http_header = ast_str_create(255);
335  if (!http_header) {
337  ast_http_error(ser, 500, "Server Error", "Out of memory");
338  close(fd);
339  return 0;
340  }
341 
342  ast_str_set(&http_header, 0, "Content-type: %s\r\n"
343  "ETag: %s\r\n"
344  "Last-Modified: %s\r\n",
345  mtype,
346  etag,
347  timebuf);
348 
349  /* ast_http_send() frees http_header, so we don't need to do it before returning */
350  if (not_modified) {
351  ast_http_send(ser, method, 304, "Not Modified", http_header, NULL, 0, 1);
352  } else {
353  ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 1); /* static content flag is set */
354  }
355  close(fd);
356  return 0;
357 
358 out404:
359  ast_http_error(ser, 404, "Not Found", "The requested URL was not found on this server.");
360  return 0;
361 
362 out403:
364  ast_http_error(ser, 403, "Access Denied", "You do not have permission to access the requested URL.");
365  return 0;
366 }
367 
369  const struct ast_http_uri *urih, const char *uri,
370  enum ast_http_method method, struct ast_variable *get_vars,
371  struct ast_variable *headers)
372 {
373  struct ast_str *out;
374  struct ast_variable *v, *cookies = NULL;
375 
376  if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
377  ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
378  return 0;
379  }
380 
381  out = ast_str_create(512);
382  if (!out) {
384  ast_http_error(ser, 500, "Server Error", "Out of memory");
385  return 0;
386  }
387 
388  ast_str_append(&out, 0,
389  "<html><title>Asterisk HTTP Status</title>\r\n"
390  "<body bgcolor=\"#ffffff\">\r\n"
391  "<table bgcolor=\"#f1f1f1\" align=\"center\"><tr><td bgcolor=\"#e0e0ff\" colspan=\"2\" width=\"500\">\r\n"
392  "<h2>&nbsp;&nbsp;Asterisk&trade; HTTP Status</h2></td></tr>\r\n");
393 
394  ast_str_append(&out, 0, "<tr><td><i>Server</i></td><td><b>%s</b></td></tr>\r\n", http_server_name);
395  ast_str_append(&out, 0, "<tr><td><i>Prefix</i></td><td><b>%s</b></td></tr>\r\n", prefix);
396  ast_str_append(&out, 0, "<tr><td><i>Bind Address</i></td><td><b>%s</b></td></tr>\r\n",
398  ast_str_append(&out, 0, "<tr><td><i>Bind Port</i></td><td><b>%s</b></td></tr>\r\n",
400  if (http_tls_cfg.enabled) {
401  ast_str_append(&out, 0, "<tr><td><i>SSL Bind Port</i></td><td><b>%s</b></td></tr>\r\n",
403  }
404  ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
405  for (v = get_vars; v; v = v->next) {
406  ast_str_append(&out, 0, "<tr><td><i>Submitted GET Variable '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
407  }
408  ast_str_append(&out, 0, "<tr><td colspan=\"2\"><hr></td></tr>\r\n");
409 
410  cookies = ast_http_get_cookies(headers);
411  for (v = cookies; v; v = v->next) {
412  ast_str_append(&out, 0, "<tr><td><i>Cookie '%s'</i></td><td>%s</td></tr>\r\n", v->name, v->value);
413  }
414  ast_variables_destroy(cookies);
415 
416  ast_str_append(&out, 0, "</table><center><font size=\"-1\"><i>Asterisk and Digium are registered trademarks of Digium, Inc.</i></font></center></body></html>\r\n");
417  ast_http_send(ser, method, 200, NULL, NULL, out, 0, 0);
418  return 0;
419 }
420 
421 static struct ast_http_uri status_uri = {
423  .description = "Asterisk HTTP General Status",
424  .uri = "httpstatus",
425  .has_subtree = 0,
426  .data = NULL,
427  .key = __FILE__,
428 };
429 
430 static struct ast_http_uri static_uri = {
432  .description = "Asterisk HTTP Static Delivery",
433  .uri = "static",
434  .has_subtree = 1,
435  .data = NULL,
436  .key= __FILE__,
437 };
438 
440  /*! TRUE if the HTTP request has a body. */
441  HTTP_FLAG_HAS_BODY = (1 << 0),
442  /*! TRUE if the HTTP request body has been read. */
444  /*! TRUE if the HTTP request must close when completed. */
446 };
447 
448 /*! HTTP tcptls worker_fn private data. */
450  /*! Body length or -1 if chunked. Valid if HTTP_FLAG_HAS_BODY is TRUE. */
452  /*! HTTP body tracking flags */
453  struct ast_flags flags;
454 };
455 
457  enum ast_http_method method, int status_code, const char *status_title,
458  struct ast_str *http_header, struct ast_str *out, int fd,
459  unsigned int static_content)
460 {
461  struct timeval now = ast_tvnow();
462  struct ast_tm tm;
463  char timebuf[80];
464  char buf[256];
465  int len;
466  int content_length = 0;
467  int close_connection;
468  struct ast_str *server_header_field = ast_str_create(MAX_SERVER_NAME_LENGTH);
469  int send_content;
470 
471  if (!ser || !server_header_field) {
472  /* The connection is not open. */
473  ast_free(http_header);
474  ast_free(out);
475  ast_free(server_header_field);
476  return;
477  }
478 
480  ast_str_set(&server_header_field,
481  0,
482  "Server: %s\r\n",
484  }
485 
486  /*
487  * We shouldn't be sending non-final status codes to this
488  * function because we may close the connection before
489  * returning.
490  */
491  ast_assert(200 <= status_code);
492 
493  if (session_keep_alive <= 0) {
494  close_connection = 1;
495  } else {
497 
498  request = ser->private_data;
499  if (!request
501  || ast_http_body_discard(ser)) {
502  close_connection = 1;
503  } else {
504  close_connection = 0;
505  }
506  }
507 
508  ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", ast_localtime(&now, &tm, "GMT"));
509 
510  /* calc content length */
511  if (out) {
512  content_length += ast_str_strlen(out);
513  }
514 
515  if (fd) {
516  content_length += lseek(fd, 0, SEEK_END);
517  lseek(fd, 0, SEEK_SET);
518  }
519 
520  send_content = method != AST_HTTP_HEAD || status_code >= 400;
521 
522  /* send http header */
523  if (ast_iostream_printf(ser->stream,
524  "HTTP/1.1 %d %s\r\n"
525  "%s"
526  "Date: %s\r\n"
527  "%s"
528  "%s"
529  "%s"
530  "Content-Length: %d\r\n"
531  "\r\n"
532  "%s",
533  status_code, status_title ? status_title : "OK",
534  ast_str_buffer(server_header_field),
535  timebuf,
536  close_connection ? "Connection: close\r\n" : "",
537  static_content ? "" : "Cache-Control: no-cache, no-store\r\n",
538  http_header ? ast_str_buffer(http_header) : "",
539  content_length,
540  send_content && out && ast_str_strlen(out) ? ast_str_buffer(out) : ""
541  ) <= 0) {
542  ast_debug(1, "ast_iostream_printf() failed: %s\n", strerror(errno));
543  close_connection = 1;
544  } else if (send_content && fd) {
545  /* send file content */
546  while ((len = read(fd, buf, sizeof(buf))) > 0) {
547  if (ast_iostream_write(ser->stream, buf, len) != len) {
548  ast_debug(1, "ast_iostream_write() failed: %s\n", strerror(errno));
549  close_connection = 1;
550  break;
551  }
552  }
553  }
554 
555  ast_free(http_header);
556  ast_free(out);
557  ast_free(server_header_field);
558 
559  if (close_connection) {
560  ast_debug(1, "HTTP closing session. status_code:%d\n", status_code);
562  } else {
563  ast_debug(1, "HTTP keeping session open. status_code:%d\n", status_code);
564  }
565 }
566 
567 void ast_http_create_response(struct ast_tcptls_session_instance *ser, int status_code,
568  const char *status_title, struct ast_str *http_header_data, const char *text)
569 {
570  char server_name[MAX_SERVER_NAME_LENGTH];
571  struct ast_str *server_address = ast_str_create(MAX_SERVER_NAME_LENGTH);
573 
574  if (!http_header_data || !server_address || !out) {
575  ast_free(http_header_data);
576  ast_free(server_address);
577  ast_free(out);
578  if (ser) {
579  ast_debug(1, "HTTP closing session. OOM.\n");
581  }
582  return;
583  }
584 
586  ast_xml_escape(http_server_name, server_name, sizeof(server_name));
587  ast_str_set(&server_address,
588  0,
589  "<address>%s</address>\r\n",
590  server_name);
591  }
592 
593  ast_str_set(&out,
594  0,
595  "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
596  "<html><head>\r\n"
597  "<title>%d %s</title>\r\n"
598  "</head><body>\r\n"
599  "<h1>%s</h1>\r\n"
600  "<p>%s</p>\r\n"
601  "<hr />\r\n"
602  "%s"
603  "</body></html>\r\n",
604  status_code,
605  status_title,
606  status_title,
607  text ? text : "",
608  ast_str_buffer(server_address));
609 
610  ast_free(server_address);
611 
612  ast_http_send(ser,
614  status_code,
615  status_title,
616  http_header_data,
617  out,
618  0,
619  0);
620 }
621 
622 void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm,
623  const unsigned long nonce, const unsigned long opaque, int stale,
624  const char *text)
625 {
626  int status_code = 401;
627  char *status_title = "Unauthorized";
628  struct ast_str *http_header_data = ast_str_create(DEFAULT_RESPONSE_HEADER_LENGTH);
629 
630  if (http_header_data) {
631  ast_str_set(&http_header_data,
632  0,
633  "WWW-authenticate: Digest algorithm=MD5, realm=\"%s\", nonce=\"%08lx\", qop=\"auth\", opaque=\"%08lx\"%s\r\n"
634  "Content-type: text/html\r\n",
635  realm ? realm : "Asterisk",
636  nonce,
637  opaque,
638  stale ? ", stale=true" : "");
639  }
640 
642  status_code,
643  status_title,
644  http_header_data,
645  text);
646 }
647 
648 void ast_http_error(struct ast_tcptls_session_instance *ser, int status_code,
649  const char *status_title, const char *text)
650 {
651  struct ast_str *http_header_data = ast_str_create(DEFAULT_RESPONSE_HEADER_LENGTH);
652 
653  if (http_header_data) {
654  ast_str_set(&http_header_data, 0, "Content-type: text/html\r\n");
655  }
656 
658  status_code,
659  status_title,
660  http_header_data,
661  text);
662 }
663 
664 /*!
665  * \brief Link the new uri into the list.
666  *
667  * They are sorted by length of
668  * the string, not alphabetically. Duplicate entries are not replaced,
669  * but the insertion order (using <= and not just <) makes sure that
670  * more recent insertions hide older ones.
671  * On a lookup, we just scan the list and stop at the first matching entry.
672  */
674 {
675  struct ast_http_uri *uri;
676  int len = strlen(urih->uri);
677 
679 
680  urih->prefix = prefix;
681 
682  if ( AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len ) {
685  return 0;
686  }
687 
688  AST_RWLIST_TRAVERSE(&uris, uri, entry) {
689  if (AST_RWLIST_NEXT(uri, entry) &&
690  strlen(AST_RWLIST_NEXT(uri, entry)->uri) <= len) {
691  AST_RWLIST_INSERT_AFTER(&uris, uri, urih, entry);
693 
694  return 0;
695  }
696  }
697 
699 
701 
702  return 0;
703 }
704 
706 {
708  AST_RWLIST_REMOVE(&uris, urih, entry);
710 }
711 
713 {
714  struct ast_http_uri *urih;
717  if (!strcmp(urih->key, key)) {
719  if (urih->dmallocd) {
720  ast_free(urih->data);
721  }
722  if (urih->mallocd) {
723  ast_free(urih);
724  }
725  }
726  }
729 }
730 
731 /*!
732  * \brief Retrieves the header with the given field name.
733  *
734  * \param headers Headers to search.
735  * \param field_name Name of the header to find.
736  * \return Associated header value.
737  * \return \c NULL if header is not present.
738  */
739 static const char *get_header(struct ast_variable *headers, const char *field_name)
740 {
741  struct ast_variable *v;
742 
743  for (v = headers; v; v = v->next) {
744  if (!strcasecmp(v->name, field_name)) {
745  return v->value;
746  }
747  }
748  return NULL;
749 }
750 
751 /*!
752  * \brief Retrieves the content type specified in the "Content-Type" header.
753  *
754  * This function only returns the "type/subtype" and any trailing parameter is
755  * not included.
756  *
757  * \note the return value is an allocated string that needs to be freed.
758  *
759  * \retval the content type/subtype or NULL if the header is not found.
760  */
761 static char *get_content_type(struct ast_variable *headers)
762 {
763  const char *content_type = get_header(headers, "Content-Type");
764  const char *param;
765  size_t size;
766 
767  if (!content_type) {
768  return NULL;
769  }
770 
771  param = strchr(content_type, ';');
772  size = param ? param - content_type : strlen(content_type);
773 
774  return ast_strndup(content_type, size);
775 }
776 
777 /*!
778  * \brief Returns the value of the Content-Length header.
779  *
780  * \param headers HTTP headers.
781  *
782  * \retval length Value of the Content-Length header.
783  * \retval 0 if header is not present.
784  * \retval -1 if header is invalid.
785  */
786 static int get_content_length(struct ast_variable *headers)
787 {
788  const char *content_length = get_header(headers, "Content-Length");
789  int length;
790 
791  if (!content_length) {
792  /* Missing content length; assume zero */
793  return 0;
794  }
795 
796  length = 0;
797  if (sscanf(content_length, "%30d", &length) != 1) {
798  /* Invalid Content-Length value */
799  length = -1;
800  }
801  return length;
802 }
803 
804 /*!
805  * \brief Returns the value of the Transfer-Encoding header.
806  *
807  * \param headers HTTP headers.
808  * \retval string Value of the Transfer-Encoding header.
809  * \retval NULL if header is not present.
810  */
811 static const char *get_transfer_encoding(struct ast_variable *headers)
812 {
813  return get_header(headers, "Transfer-Encoding");
814 }
815 
816 /*!
817  * \internal
818  * \brief Determine if the HTTP peer wants the connection closed.
819  *
820  * \param headers List of HTTP headers
821  *
822  * \retval 0 keep connection open.
823  * \retval -1 close connection.
824  */
825 static int http_check_connection_close(struct ast_variable *headers)
826 {
827  const char *connection = get_header(headers, "Connection");
828  int close_connection = 0;
829 
830  if (connection && !strcasecmp(connection, "close")) {
831  close_connection = -1;
832  }
833  return close_connection;
834 }
835 
837 {
839 
841 }
842 
843 /*!
844  * \internal
845  * \brief Initialize the request tracking information in case of early failure.
846  * \since 12.4.0
847  *
848  * \param request Request tracking information.
849  *
850  * \return Nothing
851  */
853 {
854  ast_set_flags_to(&request->flags,
856  /* Assume close in case request fails early */
858 }
859 
860 /*!
861  * \internal
862  * \brief Setup the HTTP request tracking information.
863  * \since 12.4.0
864  *
865  * \param ser HTTP TCP/TLS session object.
866  * \param headers List of HTTP headers.
867  *
868  * \retval 0 on success.
869  * \retval -1 on error.
870  */
872 {
874  const char *transfer_encoding;
875 
876  ast_set_flags_to(&request->flags,
879 
880  transfer_encoding = get_transfer_encoding(headers);
881  if (transfer_encoding && !strcasecmp(transfer_encoding, "chunked")) {
882  request->body_length = -1;
884  return 0;
885  }
886 
887  request->body_length = get_content_length(headers);
888  if (0 < request->body_length) {
890  } else if (request->body_length < 0) {
891  /* Invalid Content-Length */
892  ast_set_flag(&request->flags, HTTP_FLAG_CLOSE_ON_COMPLETION);
893  ast_http_error(ser, 400, "Bad Request", "Invalid Content-Length in request!");
894  return -1;
895  }
896  return 0;
897 }
898 
899 void ast_http_body_read_status(struct ast_tcptls_session_instance *ser, int read_success)
900 {
902 
903  request = ser->private_data;
904  if (!ast_test_flag(&request->flags, HTTP_FLAG_HAS_BODY)
905  || ast_test_flag(&request->flags, HTTP_FLAG_BODY_READ)) {
906  /* No body to read. */
907  return;
908  }
910  if (!read_success) {
912  }
913 }
914 
915 /*!
916  * \internal
917  * \brief Read the next length bytes from the HTTP body.
918  * \since 12.4.0
919  *
920  * \param ser HTTP TCP/TLS session object.
921  * \param buf Where to put the contents reading.
922  * \param length How much contents to read.
923  * \param what_getting Name of the contents reading.
924  *
925  * \retval 0 on success.
926  * \retval -1 on error.
927  */
928 static int http_body_read_contents(struct ast_tcptls_session_instance *ser, char *buf, int length, const char *what_getting)
929 {
930  int res;
931  int total = 0;
932 
933  /* Stream is in exclusive mode so we get it all if possible. */
934  while (total != length) {
935  res = ast_iostream_read(ser->stream, buf + total, length - total);
936  if (res <= 0) {
937  break;
938  }
939 
940  total += res;
941  }
942 
943  if (total != length) {
944  ast_log(LOG_WARNING, "Wrong HTTP content read. Request %s (Wanted %d, Read %d)\n",
945  what_getting, length, res);
946  return -1;
947  }
948 
949  return 0;
950 }
951 
952 /*!
953  * \internal
954  * \brief Read and discard the next length bytes from the HTTP body.
955  * \since 12.4.0
956  *
957  * \param ser HTTP TCP/TLS session object.
958  * \param length How much contents to discard
959  * \param what_getting Name of the contents discarding.
960  *
961  * \retval 0 on success.
962  * \retval -1 on error.
963  */
964 static int http_body_discard_contents(struct ast_tcptls_session_instance *ser, int length, const char *what_getting)
965 {
966  ssize_t res;
967 
968  res = ast_iostream_discard(ser->stream, length);
969  if (res < length) {
970  ast_log(LOG_WARNING, "Short HTTP request %s (Wanted %d but got %zd)\n",
971  what_getting, length, res);
972  return -1;
973  }
974  return 0;
975 }
976 
977 /*!
978  * \internal
979  * \brief decode chunked mode hexadecimal value
980  *
981  * \param s string to decode
982  * \param len length of string
983  *
984  * \retval length on success.
985  * \retval -1 on error.
986  */
987 static int chunked_atoh(const char *s, int len)
988 {
989  int value = 0;
990  char c;
991 
992  if (*s < '0') {
993  /* zero value must be 0\n not just \n */
994  return -1;
995  }
996 
997  while (len--) {
998  c = *s++;
999  if (c == '\x0D') {
1000  return value;
1001  }
1002  if (c == ';') {
1003  /* We have a chunk-extension that we don't care about. */
1004  while (len--) {
1005  if (*s++ == '\x0D') {
1006  return value;
1007  }
1008  }
1009  break;
1010  }
1011  value <<= 4;
1012  if (c >= '0' && c <= '9') {
1013  value += c - '0';
1014  continue;
1015  }
1016  if (c >= 'a' && c <= 'f') {
1017  value += 10 + c - 'a';
1018  continue;
1019  }
1020  if (c >= 'A' && c <= 'F') {
1021  value += 10 + c - 'A';
1022  continue;
1023  }
1024  /* invalid character */
1025  return -1;
1026  }
1027  /* end of string */
1028  return -1;
1029 }
1030 
1031 /*!
1032  * \internal
1033  * \brief Read and convert the chunked body header length.
1034  * \since 12.4.0
1035  *
1036  * \param ser HTTP TCP/TLS session object.
1037  *
1038  * \retval length Size of chunk to expect.
1039  * \retval -1 on error.
1040  */
1042 {
1043  int length;
1044  char header_line[MAX_HTTP_LINE_LENGTH];
1045 
1046  /* get the line of hexadecimal giving chunk-size w/ optional chunk-extension */
1047  if (ast_iostream_gets(ser->stream, header_line, sizeof(header_line)) <= 0) {
1048  ast_log(LOG_WARNING, "Short HTTP read of chunked header\n");
1049  return -1;
1050  }
1051  length = chunked_atoh(header_line, strlen(header_line));
1052  if (length < 0) {
1053  ast_log(LOG_WARNING, "Invalid HTTP chunk size\n");
1054  return -1;
1055  }
1056  return length;
1057 }
1058 
1059 /*!
1060  * \internal
1061  * \brief Read and check the chunk contents line termination.
1062  * \since 12.4.0
1063  *
1064  * \param ser HTTP TCP/TLS session object.
1065  *
1066  * \retval 0 on success.
1067  * \retval -1 on error.
1068  */
1070 {
1071  int res;
1072  char chunk_sync[2];
1073 
1074  /* Stay in fread until get the expected CRLF or timeout. */
1075  res = ast_iostream_read(ser->stream, chunk_sync, sizeof(chunk_sync));
1076  if (res < sizeof(chunk_sync)) {
1077  ast_log(LOG_WARNING, "Short HTTP chunk sync read (Wanted %zu)\n",
1078  sizeof(chunk_sync));
1079  return -1;
1080  }
1081  if (chunk_sync[0] != 0x0D || chunk_sync[1] != 0x0A) {
1082  ast_log(LOG_WARNING, "HTTP chunk sync bytes wrong (0x%02hhX, 0x%02hhX)\n",
1083  (unsigned char) chunk_sync[0], (unsigned char) chunk_sync[1]);
1084  return -1;
1085  }
1086 
1087  return 0;
1088 }
1089 
1090 /*!
1091  * \internal
1092  * \brief Read and discard any chunked trailer entity-header lines.
1093  * \since 12.4.0
1094  *
1095  * \param ser HTTP TCP/TLS session object.
1096  *
1097  * \retval 0 on success.
1098  * \retval -1 on error.
1099  */
1101 {
1102  char header_line[MAX_HTTP_LINE_LENGTH];
1103 
1104  for (;;) {
1105  if (ast_iostream_gets(ser->stream, header_line, sizeof(header_line)) <= 0) {
1106  ast_log(LOG_WARNING, "Short HTTP read of chunked trailer header\n");
1107  return -1;
1108  }
1109 
1110  /* Trim trailing whitespace */
1111  ast_trim_blanks(header_line);
1112  if (ast_strlen_zero(header_line)) {
1113  /* A blank line ends the chunked-body */
1114  break;
1115  }
1116  }
1117  return 0;
1118 }
1119 
1121 {
1123 
1124  request = ser->private_data;
1125  if (!ast_test_flag(&request->flags, HTTP_FLAG_HAS_BODY)
1126  || ast_test_flag(&request->flags, HTTP_FLAG_BODY_READ)) {
1127  /* No body to read or it has already been read. */
1128  return 0;
1129  }
1131 
1132  ast_debug(1, "HTTP discarding unused request body\n");
1133 
1134  ast_assert(request->body_length != 0);
1135  if (0 < request->body_length) {
1136  if (http_body_discard_contents(ser, request->body_length, "body")) {
1138  return -1;
1139  }
1140  return 0;
1141  }
1142 
1143  /* parse chunked-body */
1144  for (;;) {
1145  int length;
1146 
1147  length = http_body_get_chunk_length(ser);
1148  if (length < 0) {
1150  return -1;
1151  }
1152  if (length == 0) {
1153  /* parsed last-chunk */
1154  break;
1155  }
1156 
1157  if (http_body_discard_contents(ser, length, "chunk-data")
1158  || http_body_check_chunk_sync(ser)) {
1160  return -1;
1161  }
1162  }
1163 
1164  /* Read and discard any trailer entity-header lines. */
1167  return -1;
1168  }
1169  return 0;
1170 }
1171 
1172 /*!
1173  * \brief Returns the contents (body) of the HTTP request
1174  *
1175  * \param return_length ptr to int that returns content length
1176  * \param ser HTTP TCP/TLS session object
1177  * \param headers List of HTTP headers
1178  * \return ptr to content (zero terminated) or NULL on failure
1179  * \note Since returned ptr is malloc'd, it should be free'd by caller
1180  */
1181 static char *ast_http_get_contents(int *return_length,
1182  struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
1183 {
1185  int content_length;
1186  int bufsize;
1187  char *buf;
1188 
1189  request = ser->private_data;
1190  if (!ast_test_flag(&request->flags, HTTP_FLAG_HAS_BODY)) {
1191  /* no content - not an error */
1192  return NULL;
1193  }
1194  if (ast_test_flag(&request->flags, HTTP_FLAG_BODY_READ)) {
1195  /* Already read the body. Cannot read again. Assume no content. */
1196  ast_assert(0);
1197  return NULL;
1198  }
1200 
1201  ast_debug(2, "HTTP consuming request body\n");
1202 
1203  ast_assert(request->body_length != 0);
1204  if (0 < request->body_length) {
1205  /* handle regular non-chunked content */
1206  content_length = request->body_length;
1207  if (content_length > MAX_CONTENT_LENGTH) {
1208  ast_log(LOG_WARNING, "Excessively long HTTP content. (%d > %d)\n",
1209  content_length, MAX_CONTENT_LENGTH);
1211  errno = EFBIG;
1212  return NULL;
1213  }
1214  buf = ast_malloc(content_length + 1);
1215  if (!buf) {
1216  /* Malloc sets ENOMEM */
1218  return NULL;
1219  }
1220 
1221  if (http_body_read_contents(ser, buf, content_length, "body")) {
1223  errno = EIO;
1224  ast_free(buf);
1225  return NULL;
1226  }
1227 
1228  buf[content_length] = 0;
1229  *return_length = content_length;
1230  return buf;
1231  }
1232 
1233  /* pre-allocate buffer */
1234  bufsize = 250;
1235  buf = ast_malloc(bufsize);
1236  if (!buf) {
1238  return NULL;
1239  }
1240 
1241  /* parse chunked-body */
1242  content_length = 0;
1243  for (;;) {
1244  int chunk_length;
1245 
1246  chunk_length = http_body_get_chunk_length(ser);
1247  if (chunk_length < 0) {
1249  errno = EIO;
1250  ast_free(buf);
1251  return NULL;
1252  }
1253  if (chunk_length == 0) {
1254  /* parsed last-chunk */
1255  break;
1256  }
1257  if (content_length + chunk_length > MAX_CONTENT_LENGTH) {
1259  "Excessively long HTTP accumulated chunked body. (%d + %d > %d)\n",
1260  content_length, chunk_length, MAX_CONTENT_LENGTH);
1262  errno = EFBIG;
1263  ast_free(buf);
1264  return NULL;
1265  }
1266 
1267  /* insure buffer is large enough +1 */
1268  if (content_length + chunk_length >= bufsize) {
1269  char *new_buf;
1270 
1271  /* Increase bufsize until it can handle the expected data. */
1272  do {
1273  bufsize *= 2;
1274  } while (content_length + chunk_length >= bufsize);
1275 
1276  new_buf = ast_realloc(buf, bufsize);
1277  if (!new_buf) {
1279  ast_free(buf);
1280  return NULL;
1281  }
1282  buf = new_buf;
1283  }
1284 
1285  if (http_body_read_contents(ser, buf + content_length, chunk_length, "chunk-data")
1286  || http_body_check_chunk_sync(ser)) {
1288  errno = EIO;
1289  ast_free(buf);
1290  return NULL;
1291  }
1292  content_length += chunk_length;
1293  }
1294 
1295  /*
1296  * Read and discard any trailer entity-header lines
1297  * which we don't care about.
1298  *
1299  * XXX In the future we may need to add the trailer headers
1300  * to the passed in headers list rather than discarding them.
1301  */
1304  errno = EIO;
1305  ast_free(buf);
1306  return NULL;
1307  }
1308 
1309  buf[content_length] = 0;
1310  *return_length = content_length;
1311  return buf;
1312 }
1313 
1315  struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
1316 {
1317  int content_length = 0;
1318  struct ast_json *body;
1319  RAII_VAR(char *, buf, NULL, ast_free);
1320  RAII_VAR(char *, type, get_content_type(headers), ast_free);
1321 
1322  /* Use errno to distinguish errors from no body */
1323  errno = 0;
1324 
1325  if (ast_strlen_zero(type) || strcasecmp(type, "application/json")) {
1326  /* Content type is not JSON. Don't read the body. */
1327  return NULL;
1328  }
1329 
1330  buf = ast_http_get_contents(&content_length, ser, headers);
1331  if (!buf || !content_length) {
1332  /*
1333  * errno already set
1334  * or it is not an error to have zero content
1335  */
1336  return NULL;
1337  }
1338 
1339  body = ast_json_load_buf(buf, content_length, NULL);
1340  if (!body) {
1341  /* Failed to parse JSON; treat as an I/O error */
1342  errno = EIO;
1343  return NULL;
1344  }
1345 
1346  return body;
1347 }
1348 
1349 /*
1350  * get post variables from client Request Entity-Body, if content type is
1351  * application/x-www-form-urlencoded
1352  */
1354  struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
1355 {
1356  int content_length = 0;
1357  struct ast_variable *v, *post_vars=NULL, *prev = NULL;
1358  char *var, *val;
1359  RAII_VAR(char *, buf, NULL, ast_free);
1360  RAII_VAR(char *, type, get_content_type(headers), ast_free);
1361 
1362  /* Use errno to distinguish errors from no params */
1363  errno = 0;
1364 
1365  if (ast_strlen_zero(type) ||
1366  strcasecmp(type, "application/x-www-form-urlencoded")) {
1367  /* Content type is not form data. Don't read the body. */
1368  return NULL;
1369  }
1370 
1371  buf = ast_http_get_contents(&content_length, ser, headers);
1372  if (!buf || !content_length) {
1373  /*
1374  * errno already set
1375  * or it is not an error to have zero content
1376  */
1377  return NULL;
1378  }
1379 
1380  while ((val = strsep(&buf, "&"))) {
1381  var = strsep(&val, "=");
1382  if (val) {
1384  } else {
1385  val = "";
1386  }
1388  if ((v = ast_variable_new(var, val, ""))) {
1389  if (post_vars) {
1390  prev->next = v;
1391  } else {
1392  post_vars = v;
1393  }
1394  prev = v;
1395  }
1396  }
1397 
1398  return post_vars;
1399 }
1400 
1401 static int handle_uri(struct ast_tcptls_session_instance *ser, char *uri,
1402  enum ast_http_method method, struct ast_variable *headers)
1403 {
1404  char *c;
1405  int res = 0;
1406  char *params = uri;
1407  struct ast_http_uri *urih = NULL;
1408  int l;
1409  struct ast_variable *get_vars = NULL, *v, *prev = NULL;
1410  struct http_uri_redirect *redirect;
1411 
1412  ast_debug(2, "HTTP Request URI is %s \n", uri);
1413 
1414  strsep(&params, "?");
1415  /* Extract arguments from the request and store them in variables. */
1416  if (params) {
1417  char *var, *val;
1418 
1419  while ((val = strsep(&params, "&"))) {
1420  var = strsep(&val, "=");
1421  if (val) {
1423  } else {
1424  val = "";
1425  }
1427  if ((v = ast_variable_new(var, val, ""))) {
1428  if (get_vars) {
1429  prev->next = v;
1430  } else {
1431  get_vars = v;
1432  }
1433  prev = v;
1434  }
1435  }
1436  }
1437 
1439  AST_RWLIST_TRAVERSE(&uri_redirects, redirect, entry) {
1440  if (!strcasecmp(uri, redirect->target)) {
1441  struct ast_str *http_header = ast_str_create(128);
1442 
1443  if (!http_header) {
1445  ast_http_error(ser, 500, "Server Error", "Out of memory");
1446  break;
1447  }
1448  ast_str_set(&http_header, 0, "Location: %s\r\n", redirect->dest);
1449  ast_http_send(ser, method, 302, "Moved Temporarily", http_header, NULL, 0, 0);
1450  break;
1451  }
1452  }
1454  if (redirect) {
1455  goto cleanup;
1456  }
1457 
1458  /* We want requests to start with the (optional) prefix and '/' */
1459  l = strlen(prefix);
1460  if (!strncasecmp(uri, prefix, l) && uri[l] == '/') {
1461  uri += l + 1;
1462  /* scan registered uris to see if we match one. */
1464  AST_RWLIST_TRAVERSE(&uris, urih, entry) {
1465  l = strlen(urih->uri);
1466  c = uri + l; /* candidate */
1467  ast_debug(2, "match request [%s] with handler [%s] len %d\n", uri, urih->uri, l);
1468  if (strncasecmp(urih->uri, uri, l) /* no match */
1469  || (*c && *c != '/')) { /* substring */
1470  continue;
1471  }
1472  if (*c == '/') {
1473  c++;
1474  }
1475  if (!*c || urih->has_subtree) {
1476  uri = c;
1477  break;
1478  }
1479  }
1481  }
1482  if (urih) {
1483  ast_debug(1, "Match made with [%s]\n", urih->uri);
1484  if (!urih->no_decode_uri) {
1486  }
1487  res = urih->callback(ser, urih, uri, method, get_vars, headers);
1488  } else {
1489  ast_debug(1, "Requested URI [%s] has no handler\n", uri);
1490  ast_http_error(ser, 404, "Not Found", "The requested URL was not found on this server.");
1491  }
1492 
1493 cleanup:
1494  ast_variables_destroy(get_vars);
1495  return res;
1496 }
1497 
1498 static struct ast_variable *parse_cookies(const char *cookies)
1499 {
1500  char *parse = ast_strdupa(cookies);
1501  char *cur;
1502  struct ast_variable *vars = NULL, *var;
1503 
1504  while ((cur = strsep(&parse, ";"))) {
1505  char *name, *val;
1506 
1507  name = val = cur;
1508  strsep(&val, "=");
1509 
1510  if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
1511  continue;
1512  }
1513 
1514  name = ast_strip(name);
1515  val = ast_strip_quoted(val, "\"", "\"");
1516 
1517  if (ast_strlen_zero(name) || ast_strlen_zero(val)) {
1518  continue;
1519  }
1520 
1521  ast_debug(1, "HTTP Cookie, Name: '%s' Value: '%s'\n", name, val);
1522 
1523  var = ast_variable_new(name, val, __FILE__);
1524  var->next = vars;
1525  vars = var;
1526  }
1527 
1528  return vars;
1529 }
1530 
1531 /* get cookie from Request headers */
1533 {
1534  struct ast_variable *v, *cookies = NULL;
1535 
1536  for (v = headers; v; v = v->next) {
1537  if (!strcasecmp(v->name, "Cookie")) {
1538  ast_variables_destroy(cookies);
1539  cookies = parse_cookies(v->value);
1540  }
1541  }
1542  return cookies;
1543 }
1544 
1545 static struct ast_http_auth *auth_create(const char *userid, const char *password)
1546 {
1547  struct ast_http_auth *auth;
1548  size_t userid_len;
1549  size_t password_len;
1550 
1551  if (!userid || !password) {
1552  ast_log(LOG_ERROR, "Invalid userid/password\n");
1553  return NULL;
1554  }
1555 
1556  userid_len = strlen(userid) + 1;
1557  password_len = strlen(password) + 1;
1558 
1559  /* Allocate enough room to store everything in one memory block */
1560  auth = ao2_alloc(sizeof(*auth) + userid_len + password_len, NULL);
1561  if (!auth) {
1562  return NULL;
1563  }
1564 
1565  /* Put the userid right after the struct */
1566  auth->userid = (char *)(auth + 1);
1567  strcpy(auth->userid, userid);
1568 
1569  /* Put the password right after the userid */
1570  auth->password = auth->userid + userid_len;
1571  strcpy(auth->password, password);
1572 
1573  return auth;
1574 }
1575 
1576 #define BASIC_PREFIX "Basic "
1577 #define BASIC_LEN 6 /*!< strlen(BASIC_PREFIX) */
1578 
1580 {
1581  struct ast_variable *v;
1582 
1583  for (v = headers; v; v = v->next) {
1584  const char *base64;
1585  char decoded[256] = {};
1586  char *username;
1587  char *password;
1588 #ifdef AST_DEVMODE
1589  int cnt;
1590 #endif /* AST_DEVMODE */
1591 
1592  if (strcasecmp("Authorization", v->name) != 0) {
1593  continue;
1594  }
1595 
1596  if (!ast_begins_with(v->value, BASIC_PREFIX)) {
1598  "Unsupported Authorization scheme\n");
1599  continue;
1600  }
1601 
1602  /* Basic auth header parsing. RFC 2617, section 2.
1603  * credentials = "Basic" basic-credentials
1604  * basic-credentials = base64-user-pass
1605  * base64-user-pass = <base64 encoding of user-pass,
1606  * except not limited to 76 char/line>
1607  * user-pass = userid ":" password
1608  */
1609 
1610  base64 = v->value + BASIC_LEN;
1611 
1612  /* This will truncate "userid:password" lines to
1613  * sizeof(decoded). The array is long enough that this shouldn't
1614  * be a problem */
1615 #ifdef AST_DEVMODE
1616  cnt =
1617 #endif /* AST_DEVMODE */
1618  ast_base64decode((unsigned char*)decoded, base64,
1619  sizeof(decoded) - 1);
1620  ast_assert(cnt < sizeof(decoded));
1621 
1622  /* Split the string at the colon */
1623  password = decoded;
1624  username = strsep(&password, ":");
1625  if (!password) {
1626  ast_log(LOG_WARNING, "Invalid Authorization header\n");
1627  return NULL;
1628  }
1629 
1630  return auth_create(username, password);
1631  }
1632 
1633  return NULL;
1634 }
1635 
1636 int ast_http_response_status_line(const char *buf, const char *version, int code)
1637 {
1638  int status_code;
1639  size_t size = strlen(version);
1640 
1641  if (strncmp(buf, version, size) || buf[size] != ' ') {
1642  ast_log(LOG_ERROR, "HTTP version not supported - "
1643  "expected %s\n", version);
1644  return -1;
1645  }
1646 
1647  /* skip to status code (version + space) */
1648  buf += size + 1;
1649 
1650  if (sscanf(buf, "%d", &status_code) != 1) {
1651  ast_log(LOG_ERROR, "Could not read HTTP status code - "
1652  "%s\n", buf);
1653  return -1;
1654  }
1655 
1656  return status_code;
1657 }
1658 
1659 static void remove_excess_lws(char *s)
1660 {
1661  char *p, *res = s;
1662  char *buf = ast_malloc(strlen(s) + 1);
1663  char *buf_end;
1664 
1665  if (!buf) {
1666  return;
1667  }
1668 
1669  buf_end = buf;
1670 
1671  while (*s && *(s = ast_skip_blanks(s))) {
1672  p = s;
1673  s = ast_skip_nonblanks(s);
1674 
1675  if (buf_end != buf) {
1676  *buf_end++ = ' ';
1677  }
1678 
1679  memcpy(buf_end, p, s - p);
1680  buf_end += s - p;
1681  }
1682  *buf_end = '\0';
1683  /* safe since buf will always be less than or equal to res */
1684  strcpy(res, buf);
1685  ast_free(buf);
1686 }
1687 
1688 int ast_http_header_parse(char *buf, char **name, char **value)
1689 {
1690  ast_trim_blanks(buf);
1691  if (ast_strlen_zero(buf)) {
1692  return -1;
1693  }
1694 
1695  *value = buf;
1696  *name = strsep(value, ":");
1697  if (!*value) {
1698  return 1;
1699  }
1700 
1701  *value = ast_skip_blanks(*value);
1702  if (ast_strlen_zero(*value) || ast_strlen_zero(*name)) {
1703  return 1;
1704  }
1705 
1706  remove_excess_lws(*value);
1707  return 0;
1708 }
1709 
1710 int ast_http_header_match(const char *name, const char *expected_name,
1711  const char *value, const char *expected_value)
1712 {
1713  if (strcasecmp(name, expected_name)) {
1714  /* no value to validate if names don't match */
1715  return 0;
1716  }
1717 
1718  if (strcasecmp(value, expected_value)) {
1719  ast_log(LOG_ERROR, "Invalid header value - expected %s "
1720  "received %s", value, expected_value);
1721  return -1;
1722  }
1723  return 1;
1724 }
1725 
1726 int ast_http_header_match_in(const char *name, const char *expected_name,
1727  const char *value, const char *expected_value)
1728 {
1729  if (strcasecmp(name, expected_name)) {
1730  /* no value to validate if names don't match */
1731  return 0;
1732  }
1733 
1734  if (!strcasestr(expected_value, value)) {
1735  ast_log(LOG_ERROR, "Header '%s' - could not locate '%s' "
1736  "in '%s'\n", name, value, expected_value);
1737  return -1;
1738 
1739  }
1740  return 1;
1741 }
1742 
1743 /*! Limit the number of request headers in case the sender is being ridiculous. */
1744 #define MAX_HTTP_REQUEST_HEADERS 100
1745 
1746 /*!
1747  * \internal
1748  * \brief Read the request headers.
1749  * \since 12.4.0
1750  *
1751  * \param ser HTTP TCP/TLS session object.
1752  * \param headers Where to put the request headers list pointer.
1753  *
1754  * \retval 0 on success.
1755  * \retval -1 on error.
1756  */
1757 static int http_request_headers_get(struct ast_tcptls_session_instance *ser, struct ast_variable **headers)
1758 {
1759  struct ast_variable *tail = *headers;
1760  int remaining_headers;
1761  char header_line[MAX_HTTP_LINE_LENGTH];
1762 
1763  remaining_headers = MAX_HTTP_REQUEST_HEADERS;
1764  for (;;) {
1765  ssize_t len;
1766  char *name;
1767  char *value;
1768 
1769  len = ast_iostream_gets(ser->stream, header_line, sizeof(header_line));
1770  if (len <= 0) {
1771  ast_http_error(ser, 400, "Bad Request", "Timeout");
1772  return -1;
1773  }
1774  if (header_line[len - 1] != '\n') {
1775  /* We didn't get a full line */
1776  ast_http_error(ser, 400, "Bad Request",
1777  (len == sizeof(header_line) - 1) ? "Header line too long" : "Timeout");
1778  return -1;
1779  }
1780 
1781  /* Trim trailing characters */
1782  ast_trim_blanks(header_line);
1783  if (ast_strlen_zero(header_line)) {
1784  /* A blank line ends the request header section. */
1785  break;
1786  }
1787 
1788  value = header_line;
1789  name = strsep(&value, ":");
1790  if (!value) {
1791  continue;
1792  }
1793 
1794  value = ast_skip_blanks(value);
1795  if (ast_strlen_zero(value) || ast_strlen_zero(name)) {
1796  continue;
1797  }
1798 
1799  ast_trim_blanks(name);
1800 
1801  if (!remaining_headers--) {
1802  /* Too many headers. */
1803  ast_http_error(ser, 413, "Request Entity Too Large", "Too many headers");
1804  return -1;
1805  }
1806  if (!*headers) {
1807  *headers = ast_variable_new(name, value, __FILE__);
1808  tail = *headers;
1809  } else {
1810  tail->next = ast_variable_new(name, value, __FILE__);
1811  tail = tail->next;
1812  }
1813  if (!tail) {
1814  /*
1815  * Variable allocation failure.
1816  * Try to make some room.
1817  */
1818  ast_variables_destroy(*headers);
1819  *headers = NULL;
1820 
1821  ast_http_error(ser, 500, "Server Error", "Out of memory");
1822  return -1;
1823  }
1824  }
1825 
1826  return 0;
1827 }
1828 
1829 /*!
1830  * \internal
1831  * \brief Process a HTTP request.
1832  * \since 12.4.0
1833  *
1834  * \param ser HTTP TCP/TLS session object.
1835  *
1836  * \retval 0 Continue and process the next HTTP request.
1837  * \retval -1 Fatal HTTP connection error. Force the HTTP connection closed.
1838  */
1840 {
1841  RAII_VAR(struct ast_variable *, headers, NULL, ast_variables_destroy);
1842  char *uri;
1843  char *method;
1844  const char *transfer_encoding;
1846  enum ast_http_method http_method = AST_HTTP_UNKNOWN;
1847  int res;
1848  ssize_t len;
1849  char request_line[MAX_HTTP_LINE_LENGTH];
1850 
1851  len = ast_iostream_gets(ser->stream, request_line, sizeof(request_line));
1852  if (len <= 0) {
1853  return -1;
1854  }
1855 
1856  /* Re-initialize the request body tracking data. */
1857  request = ser->private_data;
1858  http_request_tracking_init(request);
1859 
1860  if (request_line[len - 1] != '\n') {
1861  /* We didn't get a full line */
1862  ast_http_error(ser, 400, "Bad Request",
1863  (len == sizeof(request_line) - 1) ? "Request line too long" : "Timeout");
1864  return -1;
1865  }
1866 
1867  /* Get method */
1868  method = ast_skip_blanks(request_line);
1869  uri = ast_skip_nonblanks(method);
1870  if (*uri) {
1871  *uri++ = '\0';
1872  }
1873 
1874  if (!strcasecmp(method,"GET")) {
1875  http_method = AST_HTTP_GET;
1876  } else if (!strcasecmp(method,"POST")) {
1877  http_method = AST_HTTP_POST;
1878  } else if (!strcasecmp(method,"HEAD")) {
1879  http_method = AST_HTTP_HEAD;
1880  } else if (!strcasecmp(method,"PUT")) {
1881  http_method = AST_HTTP_PUT;
1882  } else if (!strcasecmp(method,"DELETE")) {
1883  http_method = AST_HTTP_DELETE;
1884  } else if (!strcasecmp(method,"OPTIONS")) {
1885  http_method = AST_HTTP_OPTIONS;
1886  }
1887 
1888  uri = ast_skip_blanks(uri); /* Skip white space */
1889  if (*uri) { /* terminate at the first blank */
1890  char *c = ast_skip_nonblanks(uri);
1891 
1892  if (*c) {
1893  *c = '\0';
1894  }
1895  } else {
1896  ast_http_error(ser, 400, "Bad Request", "Invalid Request");
1897  return -1;
1898  }
1899 
1900  if (ast_shutdown_final()) {
1901  ast_http_error(ser, 503, "Service Unavailable", "Shutdown in progress");
1902  return -1;
1903  }
1904 
1905  /* process "Request Headers" lines */
1906  if (http_request_headers_get(ser, &headers)) {
1907  return -1;
1908  }
1909 
1910  transfer_encoding = get_transfer_encoding(headers);
1911  /* Transfer encoding defaults to identity */
1912  if (!transfer_encoding) {
1913  transfer_encoding = "identity";
1914  }
1915 
1916  /*
1917  * RFC 2616, section 3.6, we should respond with a 501 for any transfer-
1918  * codings we don't understand.
1919  */
1920  if (strcasecmp(transfer_encoding, "identity") != 0 &&
1921  strcasecmp(transfer_encoding, "chunked") != 0) {
1922  /* Transfer encodings not supported */
1923  ast_http_error(ser, 501, "Unimplemented", "Unsupported Transfer-Encoding.");
1924  return -1;
1925  }
1926 
1927  if (http_request_tracking_setup(ser, headers)
1928  || handle_uri(ser, uri, http_method, headers)
1930  res = -1;
1931  } else {
1932  res = 0;
1933  }
1934  return res;
1935 }
1936 
1937 static void *httpd_helper_thread(void *data)
1938 {
1939  struct ast_tcptls_session_instance *ser = data;
1940  int timeout;
1941  int arg = 1;
1942 
1943  if (!ser) {
1944  ao2_cleanup(ser);
1945  return NULL;
1946  }
1947 
1949  ast_log(LOG_WARNING, "HTTP session count exceeded %d sessions.\n",
1950  session_limit);
1951  goto done;
1952  }
1953  ast_debug(1, "HTTP opening session. Top level\n");
1954 
1955  /*
1956  * Here we set TCP_NODELAY on the socket to disable Nagle's algorithm.
1957  * This is necessary to prevent delays (caused by buffering) as we
1958  * write to the socket in bits and pieces.
1959  */
1960  if (setsockopt(ast_iostream_get_fd(ser->stream), IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg)) < 0) {
1961  ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection: %s\n", strerror(errno));
1962  }
1964 
1965  /* Setup HTTP worker private data to keep track of request body reading. */
1966  ao2_cleanup(ser->private_data);
1969  if (!ser->private_data) {
1970  ast_http_error(ser, 500, "Server Error", "Out of memory");
1971  goto done;
1972  }
1974 
1975  /* Determine initial HTTP request wait timeout. */
1976  timeout = session_keep_alive;
1977  if (timeout <= 0) {
1978  /* Persistent connections not enabled. */
1979  timeout = session_inactivity;
1980  }
1981  if (timeout < MIN_INITIAL_REQUEST_TIMEOUT) {
1982  timeout = MIN_INITIAL_REQUEST_TIMEOUT;
1983  }
1984 
1985  /* We can let the stream wait for data to arrive. */
1987 
1988  for (;;) {
1989  /* Wait for next potential HTTP request message. */
1991  if (httpd_process_request(ser)) {
1992  /* Break the connection or the connection closed */
1993  break;
1994  }
1995  if (!ser->stream) {
1996  /* Web-socket or similar that took the connection */
1997  break;
1998  }
1999 
2000  timeout = session_keep_alive;
2001  if (timeout <= 0) {
2002  /* Persistent connections not enabled. */
2003  break;
2004  }
2005  }
2006 
2007 done:
2009 
2010  ast_debug(1, "HTTP closing session. Top level\n");
2012 
2013  ao2_ref(ser, -1);
2014  return NULL;
2015 }
2016 
2017 /*!
2018  * \brief Add a new URI redirect
2019  * The entries in the redirect list are sorted by length, just like the list
2020  * of URI handlers.
2021  */
2022 static void add_redirect(const char *value)
2023 {
2024  char *target, *dest;
2025  struct http_uri_redirect *redirect, *cur;
2026  unsigned int target_len;
2027  unsigned int total_len;
2028  size_t dest_len;
2029 
2030  dest = ast_strdupa(value);
2031  dest = ast_skip_blanks(dest);
2032  target = strsep(&dest, " ");
2033  target = ast_skip_blanks(target);
2034  target = strsep(&target, " "); /* trim trailing whitespace */
2035 
2036  if (!dest) {
2037  ast_log(LOG_WARNING, "Invalid redirect '%s'\n", value);
2038  return;
2039  }
2040 
2041  target_len = strlen(target) + 1;
2042  dest_len = strlen(dest) + 1;
2043  total_len = sizeof(*redirect) + target_len + dest_len;
2044 
2045  if (!(redirect = ast_calloc(1, total_len))) {
2046  return;
2047  }
2048  redirect->dest = redirect->target + target_len;
2049  strcpy(redirect->target, target);
2050  ast_copy_string(redirect->dest, dest, dest_len);
2051 
2053 
2054  target_len--; /* So we can compare directly with strlen() */
2056  || strlen(AST_RWLIST_FIRST(&uri_redirects)->target) <= target_len ) {
2059 
2060  return;
2061  }
2062 
2064  if (AST_RWLIST_NEXT(cur, entry)
2065  && strlen(AST_RWLIST_NEXT(cur, entry)->target) <= target_len ) {
2066  AST_RWLIST_INSERT_AFTER(&uri_redirects, cur, redirect, entry);
2068  return;
2069  }
2070  }
2071 
2073 
2075 }
2076 
2077 static int __ast_http_load(int reload)
2078 {
2079  struct ast_config *cfg;
2080  struct ast_variable *v;
2081  int enabled = 0;
2082  int new_static_uri_enabled = 0;
2083  int new_status_uri_enabled = 1; /* Default to enabled for BC */
2084  char newprefix[MAX_PREFIX] = "";
2085  char server_name[MAX_SERVER_NAME_LENGTH];
2086  struct http_uri_redirect *redirect;
2087  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2088  uint32_t bindport = DEFAULT_PORT;
2089  RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
2090  int num_addrs = 0;
2091  int http_tls_was_enabled = 0;
2092 
2093  cfg = ast_config_load2("http.conf", "http", config_flags);
2094  if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
2095  return 0;
2096  }
2097 
2098  /* Even if the http.conf hasn't been updated, the TLS certs/keys may have been */
2099  if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
2100  if (http_tls_cfg.enabled && ast_ssl_setup(https_desc.tls_cfg)) {
2101  ast_tcptls_server_start(&https_desc);
2102  }
2103  return 0;
2104  }
2105 
2106  http_tls_was_enabled = (reload && http_tls_cfg.enabled);
2107 
2108  http_tls_cfg.enabled = 0;
2109 
2112 
2115 
2118 
2119  /* Apply modern intermediate settings according to the Mozilla OpSec team as of July 30th, 2015 but disable TLSv1 */
2121 
2123  http_tls_cfg.cipher = ast_strdup("ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA");
2124 
2126  while ((redirect = AST_RWLIST_REMOVE_HEAD(&uri_redirects, entry))) {
2127  ast_free(redirect);
2128  }
2130 
2131  ast_sockaddr_setnull(&https_desc.local_address);
2132 
2136 
2137  snprintf(server_name, sizeof(server_name), "Asterisk/%s", ast_get_version());
2138 
2139  v = ast_variable_browse(cfg, "general");
2140  for (; v; v = v->next) {
2141  /* read tls config options while preventing unsupported options from being set */
2142  if (strcasecmp(v->name, "tlscafile")
2143  && strcasecmp(v->name, "tlscapath")
2144  && strcasecmp(v->name, "tlscadir")
2145  && strcasecmp(v->name, "tlsverifyclient")
2146  && strcasecmp(v->name, "tlsdontverifyserver")
2147  && strcasecmp(v->name, "tlsclientmethod")
2148  && strcasecmp(v->name, "sslclientmethod")
2149  && !ast_tls_read_conf(&http_tls_cfg, &https_desc, v->name, v->value)) {
2150  continue;
2151  }
2152 
2153  if (!strcasecmp(v->name, "servername")) {
2154  if (!ast_strlen_zero(v->value)) {
2155  ast_copy_string(server_name, v->value, sizeof(server_name));
2156  } else {
2157  server_name[0] = '\0';
2158  }
2159  } else if (!strcasecmp(v->name, "enabled")) {
2160  enabled = ast_true(v->value);
2161  } else if (!strcasecmp(v->name, "enablestatic") || !strcasecmp(v->name, "enable_static")) {
2162  new_static_uri_enabled = ast_true(v->value);
2163  } else if (!strcasecmp(v->name, "enable_status")) {
2164  new_status_uri_enabled = ast_true(v->value);
2165  } else if (!strcasecmp(v->name, "bindport")) {
2167  &bindport, DEFAULT_PORT, 0, 65535)) {
2168  ast_log(LOG_WARNING, "Invalid port %s specified. Using default port %" PRId32 "\n",
2169  v->value, DEFAULT_PORT);
2170  }
2171  } else if (!strcasecmp(v->name, "bindaddr")) {
2172  if (!(num_addrs = ast_sockaddr_resolve(&addrs, v->value, 0, AST_AF_UNSPEC))) {
2173  ast_log(LOG_WARNING, "Invalid bind address %s\n", v->value);
2174  }
2175  } else if (!strcasecmp(v->name, "prefix")) {
2176  if (!ast_strlen_zero(v->value)) {
2177  newprefix[0] = '/';
2178  ast_copy_string(newprefix + 1, v->value, sizeof(newprefix) - 1);
2179  } else {
2180  newprefix[0] = '\0';
2181  }
2182  } else if (!strcasecmp(v->name, "redirect")) {
2183  add_redirect(v->value);
2184  } else if (!strcasecmp(v->name, "sessionlimit")) {
2186  &session_limit, DEFAULT_SESSION_LIMIT, 1, INT_MAX)) {
2187  ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of http.conf\n",
2188  v->name, v->value, v->lineno);
2189  }
2190  } else if (!strcasecmp(v->name, "session_inactivity")) {
2193  ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of http.conf\n",
2194  v->name, v->value, v->lineno);
2195  }
2196  } else if (!strcasecmp(v->name, "session_keep_alive")) {
2197  if (sscanf(v->value, "%30d", &session_keep_alive) != 1
2198  || session_keep_alive < 0) {
2200  ast_log(LOG_WARNING, "Invalid %s '%s' at line %d of http.conf\n",
2201  v->name, v->value, v->lineno);
2202  }
2203  } else {
2204  ast_log(LOG_WARNING, "Ignoring unknown option '%s' in http.conf\n", v->name);
2205  }
2206  }
2207 
2208  ast_config_destroy(cfg);
2209 
2210  if (strcmp(prefix, newprefix)) {
2211  ast_copy_string(prefix, newprefix, sizeof(prefix));
2212  }
2213 
2214  ast_copy_string(http_server_name, server_name, sizeof(http_server_name));
2215 
2216  if (num_addrs && enabled) {
2217  int i;
2218  for (i = 0; i < num_addrs; ++i) {
2219  ast_sockaddr_copy(&http_desc.local_address, &addrs[i]);
2220  if (!ast_sockaddr_port(&http_desc.local_address)) {
2221  ast_sockaddr_set_port(&http_desc.local_address, bindport);
2222  }
2223  ast_tcptls_server_start(&http_desc);
2224  if (http_desc.accept_fd == -1) {
2225  ast_log(LOG_WARNING, "Failed to start HTTP server for address %s\n", ast_sockaddr_stringify(&addrs[i]));
2226  ast_sockaddr_setnull(&http_desc.local_address);
2227  } else {
2228  ast_verb(1, "Bound HTTP server to address %s\n", ast_sockaddr_stringify(&addrs[i]));
2229  break;
2230  }
2231  }
2232  /* When no specific TLS bindaddr is specified, we just use
2233  * the non-TLS bindaddress here.
2234  */
2235  if (ast_sockaddr_isnull(&https_desc.local_address) && http_desc.accept_fd != -1) {
2236  ast_sockaddr_copy(&https_desc.local_address, &http_desc.local_address);
2237  /* Of course, we can't use the same port though.
2238  * Since no bind address was specified, we just use the
2239  * default TLS port
2240  */
2242  }
2243  }
2244  if (http_tls_was_enabled && !http_tls_cfg.enabled) {
2245  ast_tcptls_server_stop(&https_desc);
2246  } else if (http_tls_cfg.enabled && !ast_sockaddr_isnull(&https_desc.local_address)) {
2247  /* We can get here either because a TLS-specific address was specified
2248  * or because we copied the non-TLS address here. In the case where
2249  * we read an explicit address from the config, there may have been
2250  * no port specified, so we'll just use the default TLS port.
2251  */
2252  if (!ast_sockaddr_port(&https_desc.local_address)) {
2254  }
2255  if (ast_ssl_setup(https_desc.tls_cfg)) {
2256  ast_tcptls_server_start(&https_desc);
2257  }
2258  }
2259 
2260  if (static_uri_enabled && !new_static_uri_enabled) {
2261  ast_http_uri_unlink(&static_uri);
2262  } else if (!static_uri_enabled && new_static_uri_enabled) {
2263  ast_http_uri_link(&static_uri);
2264  }
2265 
2266  static_uri_enabled = new_static_uri_enabled;
2267 
2268  if (status_uri_enabled && !new_status_uri_enabled) {
2269  ast_http_uri_unlink(&status_uri);
2270  } else if (!status_uri_enabled && new_status_uri_enabled) {
2271  ast_http_uri_link(&status_uri);
2272  }
2273 
2274  status_uri_enabled = new_status_uri_enabled;
2275 
2276  return 0;
2277 }
2278 
2279 static char *handle_show_http(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2280 {
2281  struct ast_http_uri *urih;
2282  struct http_uri_redirect *redirect;
2283 
2284  switch (cmd) {
2285  case CLI_INIT:
2286  e->command = "http show status";
2287  e->usage =
2288  "Usage: http show status\n"
2289  " Lists status of internal HTTP engine\n";
2290  return NULL;
2291  case CLI_GENERATE:
2292  return NULL;
2293  }
2294 
2295  if (a->argc != 3) {
2296  return CLI_SHOWUSAGE;
2297  }
2298  ast_cli(a->fd, "HTTP Server Status:\n");
2299  ast_cli(a->fd, "Prefix: %s\n", prefix);
2300  ast_cli(a->fd, "Server: %s\n", http_server_name);
2301  if (ast_sockaddr_isnull(&http_desc.old_address)) {
2302  ast_cli(a->fd, "Server Disabled\n\n");
2303  } else {
2304  ast_cli(a->fd, "Server Enabled and Bound to %s\n\n",
2305  ast_sockaddr_stringify(&http_desc.old_address));
2306  if (http_tls_cfg.enabled) {
2307  ast_cli(a->fd, "HTTPS Server Enabled and Bound to %s\n\n",
2308  ast_sockaddr_stringify(&https_desc.old_address));
2309  }
2310  }
2311 
2312  ast_cli(a->fd, "Enabled URI's:\n");
2314  if (AST_RWLIST_EMPTY(&uris)) {
2315  ast_cli(a->fd, "None.\n");
2316  } else {
2317  AST_RWLIST_TRAVERSE(&uris, urih, entry)
2318  ast_cli(a->fd, "%s/%s%s => %s\n", prefix, urih->uri, (urih->has_subtree ? "/..." : "" ), urih->description);
2319  }
2321 
2322  ast_cli(a->fd, "\nEnabled Redirects:\n");
2325  ast_cli(a->fd, " %s => %s\n", redirect->target, redirect->dest);
2327  ast_cli(a->fd, " None.\n");
2328  }
2330 
2331  return CLI_SUCCESS;
2332 }
2333 
2334 static int reload_module(void)
2335 {
2336  return __ast_http_load(1);
2337 }
2338 
2339 static struct ast_cli_entry cli_http[] = {
2340  AST_CLI_DEFINE(handle_show_http, "Display HTTP server status"),
2341 };
2342 
2343 static int unload_module(void)
2344 {
2345  struct http_uri_redirect *redirect;
2346  ast_cli_unregister_multiple(cli_http, ARRAY_LEN(cli_http));
2347 
2348  ast_tcptls_server_stop(&http_desc);
2349  if (http_tls_cfg.enabled) {
2350  ast_tcptls_server_stop(&https_desc);
2351  }
2356 
2357  if (status_uri_enabled) {
2358  ast_http_uri_unlink(&status_uri);
2359  }
2360 
2361  if (static_uri_enabled) {
2362  ast_http_uri_unlink(&static_uri);
2363  }
2364 
2366  while ((redirect = AST_RWLIST_REMOVE_HEAD(&uri_redirects, entry))) {
2367  ast_free(redirect);
2368  }
2370 
2371  return 0;
2372 }
2373 
2374 static int load_module(void)
2375 {
2376  ast_cli_register_multiple(cli_http, ARRAY_LEN(cli_http));
2377 
2379 }
2380 
2382  .support_level = AST_MODULE_SUPPORT_CORE,
2383  .load = load_module,
2384  .unload = unload_module,
2385  .reload = reload_module,
2386  .load_pri = AST_MODPRI_CORE,
2387  .requires = "extconfig",
2388 );
void ast_iostream_set_exclusive_input(struct ast_iostream *stream, int exclusive_input)
Set the iostream if it can exclusively depend upon the set timeouts.
Definition: iostream.c:148
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:290
void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
Definition: main/utils.c:487
struct ast_variable * next
static const char type[]
Definition: chan_ooh323.c:109
static char * ast_http_get_contents(int *return_length, struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Returns the contents (body) of the HTTP request.
Definition: http.c:1181
#define AST_RWLIST_NEXT
Definition: linkedlists.h:440
static struct ast_variable * parse_cookies(const char *cookies)
Definition: http.c:1498
char * pvtfile
Definition: tcptls.h:90
#define AST_CERTFILE
Definition: tcptls.h:62
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
Asterisk main include file. File version handling, generic pbx functions.
#define ast_realloc(p, len)
A wrapper for realloc()
Definition: astmm.h:228
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
Definition: linkedlists.h:332
ast_http_callback callback
Definition: http.h:105
void ast_tcptls_server_start(struct ast_tcptls_session_args *desc)
This is a generic (re)start routine for a TCP server, which does the socket/bind/listen and starts a ...
Definition: tcptls.c:685
String manipulation functions.
static int http_request_headers_get(struct ast_tcptls_session_instance *ser, struct ast_variable **headers)
Definition: http.c:1757
char * userid
Definition: http.h:125
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
Definition: json.c:564
int ast_http_response_status_line(const char *buf, const char *version, int code)
Parse the http response status line.
Definition: http.c:1636
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
Definition: ast_expr2.c:325
int ast_ssl_setup(struct ast_tls_config *cfg)
Set up an SSL server.
Definition: tcptls.c:570
#define AST_RWLIST_INSERT_AFTER
Definition: linkedlists.h:701
Asterisk version information.
struct ast_variable * ast_http_get_cookies(struct ast_variable *headers)
Get cookie from Request headers.
Definition: http.c:1532
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define ast_test_flag(p, flag)
Definition: utils.h:63
uint32_t ast_http_manid_from_vars(struct ast_variable *headers)
Return manager id, if exist, from request headers.
Definition: http.c:217
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
static int static_uri_enabled
Definition: http.c:142
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:171
ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t count)
Write data to an iostream.
Definition: iostream.c:374
Time-related functions and macros.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * ast_get_version(void)
Retrieve the Asterisk version string.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define ast_set_flag(p, flag)
Definition: utils.h:70
static char * get_content_type(struct ast_variable *headers)
Retrieves the content type specified in the "Content-Type" header.
Definition: http.c:761
#define DEFAULT_PORT
Definition: http.c:69
descriptor for a cli entry.
Definition: cli.h:171
const int argc
Definition: cli.h:160
static struct ast_cli_entry cli_http[]
Definition: http.c:2339
#define LOG_WARNING
Definition: logger.h:274
static int status_uri_enabled
Definition: http.c:143
static int chunked_atoh(const char *s, int len)
Definition: http.c:987
ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t count)
Discard the specified number of bytes from an iostream.
Definition: iostream.c:357
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define CONFIG_STATUS_FILEINVALID
void ast_http_uri_unlink(struct ast_http_uri *urih)
Unregister a URI handler.
Definition: http.c:705
#define DEFAULT_TLS_PORT
Definition: http.c:70
static int timeout
Definition: cdr_mysql.c:86
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
unsigned int flags
Definition: utils.h:200
#define DEFAULT_SESSION_LIMIT
Definition: http.c:71
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3154
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
Definition: cli.h:152
#define INITIAL_RESPONSE_BODY_BUFFER
Definition: http.c:92
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
const char * key
Definition: http.h:116
int ast_iostream_get_fd(struct ast_iostream *stream)
Get an iostream&#39;s file descriptor.
Definition: iostream.c:84
char * dest
Definition: http.c:171
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
arguments for the accepting thread
Definition: tcptls.h:129
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:650
static int get_content_length(struct ast_variable *headers)
Returns the value of the Content-Length header.
Definition: http.c:786
static struct test_val c
const char * text
Definition: http.c:179
char * text
Definition: app_queue.c:1511
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
unsigned int has_subtree
Definition: http.h:106
#define NULL
Definition: resample.c:96
static int http_request_tracking_setup(struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Definition: http.c:871
int value
Definition: syslog.c:37
Generic support for tcp/tls servers in Asterisk.
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define LOG_DEBUG
Definition: logger.h:241
int ast_xml_escape(const char *string, char *outbuf, size_t buflen)
Escape reserved characters for use in XML.
Definition: main/utils.c:589
void ast_iostream_set_timeout_idle_inactivity(struct ast_iostream *stream, int timeout, int timeout_reset)
Set the iostream inactivity & idle timeout timers.
Definition: iostream.c:130
const char * ast_get_http_method(enum ast_http_method method)
Return http method name string.
Definition: http.c:190
#define MAX_PREFIX
Definition: http.c:68
const char * ext
Definition: http.c:147
Socket address structure.
Definition: netsock2.h:97
static struct ast_str * password
Definition: cdr_mysql.c:77
#define ast_verb(level,...)
Definition: logger.h:455
char * ast_skip_nonblanks(const char *str)
Gets a pointer to first whitespace character in a string.
Definition: strings.h:200
void ast_http_uri_unlink_all_with_key(const char *key)
Unregister all handlers with matching key.
Definition: http.c:712
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
#define ast_set_flags_to(p, flag, value)
Definition: utils.h:104
#define DEFAULT_SESSION_KEEP_ALIVE
Definition: http.c:77
Utility functions.
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:140
static int 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 *get_vars, struct ast_variable *headers)
Definition: http.c:368
unsigned int mallocd
Definition: http.h:108
int done
Definition: test_amihooks.c:48
ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count)
Read data from an iostream.
Definition: iostream.c:273
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:521
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1065
#define MAX_SERVER_NAME_LENGTH
Definition: http.c:79
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:362
Configuration File Parser.
Support for Private Asterisk HTTP Servers.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: main/utils.c:287
int ast_http_header_parse(char *buf, char **name, char **value)
Parse a header into the given name/value strings.
Definition: http.c:1688
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:717
http_private_flags
Definition: http.c:439
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: main/utils.c:1510
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
int astman_is_authed(uint32_t ident)
Determinie if a manager session ident is authenticated.
Definition: manager.c:7551
void * ast_tcptls_server_root(void *)
Definition: tcptls.c:280
int ast_http_body_discard(struct ast_tcptls_session_instance *ser)
Read and discard any unread HTTP request body.
Definition: http.c:1120
Asterisk JSON abstraction layer.
Asterisk file paths, configured in asterisk.conf.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:851
const int fd
Definition: cli.h:159
#define AST_PTHREADT_NULL
Definition: lock.h:66
static int http_body_read_contents(struct ast_tcptls_session_instance *ser, char *buf, int length, const char *what_getting)
Definition: http.c:928
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
static struct ast_tls_config http_tls_cfg
Definition: http.c:111
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:569
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
const char * method
Definition: res_pjsip.c:4220
ssize_t ast_iostream_printf(struct ast_iostream *stream, const char *format,...)
Write a formatted string to an iostream.
Definition: iostream.c:491
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
#define BASIC_LEN
Definition: http.c:1577
#define ast_variable_new(name, value, filename)
void ast_http_body_read_status(struct ast_tcptls_session_instance *ser, int read_success)
Update the body read success status.
Definition: http.c:899
static struct @381 mimetypes[]
Limit the kinds of files we&#39;re willing to serve up.
Network socket handling.
static struct ast_tcptls_session_args http_desc
Definition: http.c:118
describes a server instance
Definition: tcptls.h:149
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:544
#define CONFIG_STATUS_FILEUNCHANGED
static char http_server_name[MAX_SERVER_NAME_LENGTH]
Definition: http.c:104
static void * httpd_helper_thread(void *arg)
Definition: http.c:1937
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int unload_module(void)
Definition: http.c:2343
const char * ast_config_AST_DATA_DIR
Definition: options.c:158
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
static int httpd_process_request(struct ast_tcptls_session_instance *ser)
Definition: http.c:1839
#define AST_RWLIST_EMPTY
Definition: linkedlists.h:451
int ast_shutdown_final(void)
Definition: asterisk.c:1829
static int __ast_http_load(int reload)
Definition: http.c:2077
static void add_redirect(const char *value)
Add a new URI redirect The entries in the redirect list are sorted by length, just like the list of U...
Definition: http.c:2022
static int session_limit
Definition: http.c:106
#define LOG_ERROR
Definition: logger.h:285
const struct ast_flags ast_uri_http_legacy
Definition: main/utils.c:445
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1822
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
const char * mtype
Definition: http.c:148
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:537
static const char * get_header(struct ast_variable *headers, const char *field_name)
Retrieves the header with the given field name.
Definition: http.c:739
void ast_http_request_close_on_completion(struct ast_tcptls_session_instance *ser)
Request the HTTP connection be closed after this HTTP request.
Definition: http.c:836
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
struct ast_flags flags
Definition: http.c:453
int errno
const char * description
Definition: http.h:102
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:260
int ast_http_header_match(const char *name, const char *expected_name, const char *value, const char *expected_value)
Check if the header and value match (case insensitive) their associated expected values.
Definition: http.c:1710
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:157
void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method, int status_code, const char *status_title, struct ast_str *http_header, struct ast_str *out, int fd, unsigned int static_content)
Generic function for sending HTTP/1.1 response.
Definition: http.c:456
#define ast_strndup(str, len)
A wrapper for strndup()
Definition: astmm.h:258
static const struct ast_cfhttp_methods_text ast_http_methods_text[]
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
char * password
Definition: http.h:127
char * ast_trim_blanks(char *str)
Trims trailing whitespace characters from a string.
Definition: strings.h:182
char * strcasestr(const char *, const char *)
static void http_request_tracking_init(struct http_worker_private_data *request)
Definition: http.c:852
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
#define ast_strlen_zero(a)
Definition: muted.c:73
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
char target[0]
Definition: http.c:172
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
char * command
Definition: cli.h:186
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static const char * get_transfer_encoding(struct ast_variable *headers)
Returns the value of the Transfer-Encoding header.
Definition: http.c:811
static int reload(void)
Definition: cdr_mysql.c:741
void ast_http_prefix(char *buf, int len)
Return the current prefix.
Definition: http.c:233
static struct ast_http_auth * auth_create(const char *userid, const char *password)
Definition: http.c:1545
Module could not be loaded properly.
Definition: module.h:102
struct ast_variable * ast_http_get_post_vars(struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Get post variables from client Request Entity-Body, if content type is application/x-www-form-urlenco...
Definition: http.c:1353
static int session_inactivity
Definition: http.c:107
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *result,...)
The argument parsing routine.
Definition: main/config.c:3657
struct ast_http_auth * ast_http_get_auth(struct ast_variable *headers)
Get HTTP authentication information from headers.
Definition: http.c:1579
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:843
static int http_body_get_chunk_length(struct ast_tcptls_session_instance *ser)
Definition: http.c:1041
struct ast_sockaddr old_address
Definition: tcptls.h:131
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
#define MIN_INITIAL_REQUEST_TIMEOUT
Definition: http.c:75
static int request(void *obj)
Definition: chan_pjsip.c:2559
const char * prefix
Definition: http.h:104
static int http_body_discard_chunk_trailer_headers(struct ast_tcptls_session_instance *ser)
Definition: http.c:1100
#define DEFAULT_SESSION_INACTIVITY
Definition: http.c:73
static int session_count
Definition: http.c:109
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
Structure used to handle boolean flags.
Definition: utils.h:199
char * certfile
Definition: tcptls.h:89
const char * name
Definition: tcptls.h:142
struct ast_iostream * stream
Definition: tcptls.h:160
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct ast_json * ast_http_get_json(struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
Get JSON from client Request Entity-Body, if content type is application/json.
Definition: http.c:1314
#define MAX_CONTENT_LENGTH
Definition: http.c:85
const char * usage
Definition: cli.h:177
static int handle_uri(struct ast_tcptls_session_instance *ser, char *uri, enum ast_http_method method, struct ast_variable *headers)
Definition: http.c:1401
static int http_check_connection_close(struct ast_variable *headers)
Definition: http.c:825
ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size)
Read a LF-terminated string from an iostream.
Definition: iostream.c:300
#define CLI_SUCCESS
Definition: cli.h:44
void ast_http_create_response(struct ast_tcptls_session_instance *ser, int status_code, const char *status_title, struct ast_str *http_header_data, const char *text)
Creates and sends a formatted http response message.
Definition: http.c:567
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:740
#define DEFAULT_RESPONSE_HEADER_LENGTH
Definition: http.c:81
char * strsep(char **str, const char *delims)
static int load_module(void)
Definition: http.c:2374
int ast_http_uri_link(struct ast_http_uri *urih)
Link the new uri into the list.
Definition: http.c:673
FILE * out
Definition: utils/frame.c:33
static int http_body_discard_contents(struct ast_tcptls_session_instance *ser, int length, const char *what_getting)
Definition: http.c:964
HTTP authentication information.
Definition: http.h:123
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Standard Command Line Interface.
static struct ast_http_uri static_uri
Definition: http.c:430
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
void ast_tcptls_close_session_file(struct ast_tcptls_session_instance *tcptls_session)
Closes a tcptls session instance&#39;s file and/or file descriptor. The tcptls_session will be set to NUL...
Definition: tcptls.c:839
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
void ast_http_auth(struct ast_tcptls_session_instance *ser, const char *realm, const unsigned long nonce, const unsigned long opaque, int stale, const char *text)
Send http "401 Unauthorized" response and close socket.
Definition: http.c:622
Definition of a URI handler.
Definition: http.h:100
static char * handle_show_http(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: http.c:2279
int ast_http_header_match_in(const char *name, const char *expected_name, const char *value, const char *expected_value)
Check if the header name matches the expected header name. If so, then check to see if the value can ...
Definition: http.c:1726
static void remove_excess_lws(char *s)
Definition: http.c:1659
static int 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 *get_vars, struct ast_variable *headers)
Definition: http.c:240
void ast_iostream_nonblock(struct ast_iostream *stream)
Make an iostream non-blocking.
Definition: iostream.c:103
int ast_tls_read_conf(struct ast_tls_config *tls_cfg, struct ast_tcptls_session_args *tls_desc, const char *varname, const char *value)
Used to parse conf files containing tls/ssl options.
Definition: tcptls.c:875
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94
static int total
Definition: res_adsi.c:968
unsigned int dmallocd
Definition: http.h:110
static int http_body_check_chunk_sync(struct ast_tcptls_session_instance *ser)
Definition: http.c:1069
Abstract JSON element (object, array, string, int, ...).
struct ast_flags flags
Definition: tcptls.h:94
#define BASIC_PREFIX
Definition: http.c:1576
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:884
Definition: search.h:40
#define MAX_HTTP_REQUEST_HEADERS
Definition: http.c:1744
char * capath
Definition: tcptls.h:93
void ast_tcptls_server_stop(struct ast_tcptls_session_args *desc)
Shutdown a running server if there is one.
Definition: tcptls.c:849
struct ast_tls_config * tls_cfg
Definition: tcptls.h:134
void ast_http_error(struct ast_tcptls_session_instance *ser, int status_code, const char *status_title, const char *text)
Send HTTP error message and close socket.
Definition: http.c:648
Definition: http.c:138
const char * uri
Definition: http.h:103
struct ast_sockaddr local_address
Definition: tcptls.h:130
ast_http_method
HTTP Request methods known by Asterisk.
Definition: http.h:56
#define AST_RWLIST_FIRST
Definition: linkedlists.h:422
static int reload_module(void)
Definition: http.c:2334
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
static int session_keep_alive
Definition: http.c:108
Asterisk module definitions.
static struct ast_tcptls_session_args https_desc
Definition: http.c:128
const char * ast_http_ftype2mtype(const char *ftype)
Return mime type based on extension.
Definition: http.c:203
static struct ast_http_uri status_uri
Definition: http.c:421
char * cipher
Definition: tcptls.h:91
void * data
Definition: http.h:114
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:616
unsigned int no_decode_uri
Definition: http.h:112
#define MAX_HTTP_LINE_LENGTH
Definition: http.c:99
static char base64[64]
Definition: main/utils.c:73
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:280
static char prefix[MAX_PREFIX]
Definition: http.c:141
static int enabled
Definition: dnsmgr.c:91
static struct test_val a
int enabled
Definition: tcptls.h:88