http server for AMI access More...
#include "asterisk.h"
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <fcntl.h>
#include "asterisk/paths.h"
#include "asterisk/network.h"
#include "asterisk/cli.h"
#include "asterisk/tcptls.h"
#include "asterisk/http.h"
#include "asterisk/utils.h"
#include "asterisk/strings.h"
#include "asterisk/config.h"
#include "asterisk/stringfields.h"
#include "asterisk/ast_version.h"
#include "asterisk/manager.h"
#include "asterisk/_private.h"
#include "asterisk/astobj2.h"
Go to the source code of this file.
Data Structures | |
struct | ast_cfhttp_methods_text |
struct | http_uri_redirect |
struct | uri_redirects |
struct | uris |
Macros | |
#define | DEFAULT_HTTP_PORT 8088 |
#define | DEFAULT_HTTPS_PORT 8089 |
#define | DEFAULT_SESSION_INACTIVITY 30000 /* (ms) Idle time waiting for data. */ |
#define | DEFAULT_SESSION_LIMIT 100 |
#define | DO_SSL /* comment in/out if you want to support ssl */ |
#define | HOOK_T ssize_t |
#define | LEN_T size_t |
#define | MAX_HTTP_REQUEST_HEADERS 100 |
#define | MAX_POST_CONTENT 1025 |
#define | MAX_PREFIX 80 |
Functions | |
static int | __ast_http_load (int reload) |
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 URI handlers. More... | |
const char * | ast_get_http_method (enum ast_http_method method) |
Return http method name string. More... | |
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. More... | |
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. More... | |
const char * | ast_http_ftype2mtype (const char *ftype) |
Return mime type based on extension. More... | |
struct ast_variable * | ast_http_get_cookies (struct ast_variable *headers) |
Get cookie from Request headers. More... | |
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-urlencoded. More... | |
int | ast_http_init (void) |
uint32_t | ast_http_manid_from_vars (struct ast_variable *headers) |
Return manager id, if exist, from request headers. More... | |
void | ast_http_prefix (char *buf, int len) |
Return the current prefix. More... | |
int | ast_http_reload (void) |
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, const int fd, unsigned int static_content) |
Generic function for sending http/1.1 response. More... | |
int | ast_http_uri_link (struct ast_http_uri *urih) |
Link the new uri into the list. More... | |
void | ast_http_uri_unlink (struct ast_http_uri *urih) |
Unregister a URI handler. More... | |
void | ast_http_uri_unlink_all_with_key (const char *key) |
Unregister all handlers with matching key. More... | |
static char * | handle_show_http (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static int | handle_uri (struct ast_tcptls_session_instance *ser, char *uri, enum ast_http_method method, struct ast_variable *headers) |
static void | http_decode (char *s) |
static void | http_shutdown (void) |
static void * | httpd_helper_thread (void *arg) |
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) |
static struct ast_variable * | parse_cookies (const char *cookies) |
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) |
Variables | |
static struct ast_cfhttp_methods_text | ast_http_methods_text [] |
static struct ast_cli_entry | cli_http [] |
static int | enablestatic |
static struct ast_tcptls_session_args | http_desc |
static struct ast_tls_config | http_tls_cfg |
static struct ast_tcptls_session_args | https_desc |
struct { | |
const char * ext | |
const char * mtype | |
} | mimetypes [] |
Limit the kinds of files we're willing to serve up. More... | |
static char | prefix [MAX_PREFIX] |
static int | session_count = 0 |
static int | session_inactivity = DEFAULT_SESSION_INACTIVITY |
static int | session_limit = DEFAULT_SESSION_LIMIT |
static struct ast_http_uri | staticuri |
static struct ast_http_uri | statusuri |
static struct uri_redirects | uri_redirects = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 } , } |
static struct uris | uris = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 } , } |
http server for AMI access
This program implements a tiny http server and was inspired by micro-httpd by Jef Poskanzer
AMI over HTTP support - AMI over the http protocol
Definition in file http.c.
#define DEFAULT_HTTP_PORT 8088 |
Definition at line 65 of file http.c.
Referenced by __ast_http_load().
#define DEFAULT_HTTPS_PORT 8089 |
Definition at line 66 of file http.c.
Referenced by __ast_http_load().
#define DEFAULT_SESSION_INACTIVITY 30000 /* (ms) Idle time waiting for data. */ |
Definition at line 63 of file http.c.
Referenced by __ast_http_load().
#define DEFAULT_SESSION_LIMIT 100 |
Definition at line 62 of file http.c.
Referenced by __ast_http_load().
#define MAX_HTTP_REQUEST_HEADERS 100 |
Limit the number of request headers in case the sender is being ridiculous.
Definition at line 876 of file http.c.
Referenced by httpd_helper_thread().
#define MAX_POST_CONTENT 1025 |
Definition at line 618 of file http.c.
Referenced by ast_http_get_post_vars().
#define MAX_PREFIX 80 |
Definition at line 61 of file http.c.
Referenced by __ast_http_load().
|
static |
Definition at line 1084 of file http.c.
References add_redirect(), AST_CERTFILE, ast_config_destroy(), ast_config_load2(), ast_copy_string(), ast_free, ast_gethostbyname(), ast_log(), ast_parse_arg(), AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_sockaddr_from_sin, ast_sockaddr_set_port, ast_sockaddr_setnull(), ast_sockaddr_to_sin, ast_ssl_setup(), ast_strdup, ast_strlen_zero(), ast_tcptls_server_start(), ast_tcptls_server_stop(), ast_tls_read_conf(), ast_true(), ast_variable_browse(), ast_tls_config::certfile, ast_tls_config::cipher, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT, DEFAULT_SESSION_INACTIVITY, DEFAULT_SESSION_LIMIT, ast_tls_config::enabled, enabled, http_uri_redirect::entry, hp, http_tls_cfg, ast_variable::lineno, ast_tcptls_session_args::local_address, LOG_WARNING, MAX_PREFIX, ast_variable::name, ast_variable::next, PARSE_DEFAULT, PARSE_IN_RANGE, PARSE_INT32, ast_tls_config::pvtfile, ast_tcptls_session_args::tls_cfg, and ast_variable::value.
Referenced by ast_http_init(), and ast_http_reload().
|
static |
Add a new URI redirect The entries in the redirect list are sorted by length, just like the list of URI handlers.
Definition at line 1031 of file http.c.
References ast_calloc, ast_log(), AST_RWLIST_EMPTY, AST_RWLIST_FIRST, AST_RWLIST_INSERT_AFTER, AST_RWLIST_INSERT_HEAD, AST_RWLIST_INSERT_TAIL, AST_RWLIST_NEXT, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_skip_blanks(), ast_strdupa, http_uri_redirect::dest, http_uri_redirect::entry, LOG_WARNING, strsep(), and http_uri_redirect::target.
Referenced by __ast_http_load().
const char* ast_get_http_method | ( | enum ast_http_method | method | ) |
Return http method name string.
Definition at line 153 of file http.c.
References ARRAY_LEN, ast_http_methods_text, and ast_cfhttp_methods_text::text.
Referenced by auth_http_callback().
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 at line 468 of file http.c.
References ast_free, ast_http_send(), AST_HTTP_UNKNOWN, ast_str_create(), and ast_str_set().
Referenced by auth_http_callback().
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 at line 506 of file http.c.
References ast_free, ast_http_send(), AST_HTTP_UNKNOWN, ast_str_create(), and ast_str_set().
Referenced by auth_http_callback(), generic_http_callback(), handle_uri(), http_post_callback(), httpd_helper_thread(), httpstatus_callback(), and static_callback().
const char* ast_http_ftype2mtype | ( | const char * | ftype | ) |
struct ast_variable* ast_http_get_cookies | ( | struct ast_variable * | headers | ) |
Get cookie from Request headers.
Definition at line 862 of file http.c.
References ast_variables_destroy(), ast_variable::name, ast_variable::next, parse_cookies(), and ast_variable::value.
Referenced by ast_http_manid_from_vars(), generic_http_callback(), http_post_callback(), and httpstatus_callback().
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-urlencoded.
ser | TCP/TLS session object |
headers | List of HTTP headers |
Definition at line 624 of file http.c.
References ast_free, AST_HTTP_POST, ast_http_send(), ast_log(), ast_malloc, ast_variable_new(), ast_tcptls_session_instance::f, http_decode(), LOG_WARNING, MAX_POST_CONTENT, ast_variable::name, ast_variable::next, strsep(), ast_variable::value, and var.
Referenced by auth_http_callback(), and generic_http_callback().
int ast_http_init | ( | void | ) |
Provided by http.c
Definition at line 1319 of file http.c.
References __ast_http_load(), ARRAY_LEN, ast_cli_register_multiple(), ast_http_uri_link(), ast_register_atexit(), cli_http, http_shutdown(), staticuri, and statusuri.
Referenced by main().
uint32_t ast_http_manid_from_vars | ( | struct ast_variable * | headers | ) |
Return manager id, if exist, from request headers.
headers | List of HTTP headers |
Definition at line 180 of file http.c.
References ast_http_get_cookies(), ast_variables_destroy(), ast_variable::name, ast_variable::next, and ast_variable::value.
Referenced by http_post_callback(), and static_callback().
void ast_http_prefix | ( | char * | buf, |
int | len | ||
) |
Return the current prefix.
[out] | buf | destination buffer for previous |
[in] | len | length of prefix to copy |
Definition at line 196 of file http.c.
References ast_copy_string().
int ast_http_reload | ( | void | ) |
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, | ||
const int | fd, | ||
unsigned int | static_content | ||
) |
Generic function for sending http/1.1 response.
ser | TCP/TLS session object |
method | GET/POST/HEAD |
status_code | HTTP response code (200/401/403/404/500) |
status_title | English equivalent to the status_code parameter |
http_header | An ast_str object containing all headers |
out | An ast_str object containing the body of the response |
fd | If out is NULL, a file descriptor where the body of the response is held (otherwise -1) |
static_content | Zero if the content is dynamically generated and should not be cached; nonzero otherwise |
Extra HTTP headers MUST be present only in the http_header argument. The argument "out" should contain only content of the response (no headers!).
HTTP content can be constructed from the argument "out", if it is not NULL; otherwise, the function will read content from FD.
This function calculates the content-length http header itself.
Both the http_header and out arguments will be freed by this function; however, if FD is open, it will remain open.
Definition at line 393 of file http.c.
References ast_free, ast_get_version(), AST_HTTP_HEAD, ast_localtime(), ast_log(), ast_str_buffer(), ast_str_strlen(), ast_strftime(), ast_tcptls_close_session_file(), ast_tvnow(), errno, ast_tcptls_session_instance::f, len(), LOG_ERROR, and LOG_WARNING.
Referenced by ast_http_auth(), ast_http_error(), ast_http_get_post_vars(), auth_http_callback(), generic_http_callback(), handle_uri(), httpstatus_callback(), and static_callback().
int ast_http_uri_link | ( | struct ast_http_uri * | urih | ) |
Link the new uri into the list.
Register a URI handler.
They are sorted by length of the string, not alphabetically. Duplicate entries are not replaced, but the insertion order (using <= and not just <) makes sure that more recent insertions hide older ones. On a lookup, we just scan the list and stop at the first matching entry.
Definition at line 544 of file http.c.
References AST_RWLIST_EMPTY, AST_RWLIST_FIRST, AST_RWLIST_INSERT_AFTER, AST_RWLIST_INSERT_HEAD, AST_RWLIST_INSERT_TAIL, AST_RWLIST_NEXT, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, http_uri_redirect::entry, len(), and ast_http_uri::uri.
Referenced by __ast_http_post_load(), __init_manager(), and ast_http_init().
void ast_http_uri_unlink | ( | struct ast_http_uri * | urih | ) |
Unregister a URI handler.
Definition at line 574 of file http.c.
References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and http_uri_redirect::entry.
Referenced by __init_manager(), and http_shutdown().
void ast_http_uri_unlink_all_with_key | ( | const char * | key | ) |
Unregister all handlers with matching key.
Definition at line 581 of file http.c.
References ast_free, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_http_uri::data, ast_http_uri::dmallocd, http_uri_redirect::entry, ast_http_uri::key, and ast_http_uri::mallocd.
Referenced by __ast_http_post_load(), and unload_module().
|
static |
Definition at line 1229 of file http.c.
References ast_cli_args::argc, ast_cli(), ast_inet_ntoa(), AST_RWLIST_EMPTY, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_sockaddr_to_sin, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_http_uri::description, http_uri_redirect::dest, ast_tls_config::enabled, http_uri_redirect::entry, ast_cli_args::fd, ast_http_uri::has_subtree, http_tls_cfg, ast_tcptls_session_args::old_address, http_uri_redirect::target, ast_http_uri::uri, and ast_cli_entry::usage.
|
static |
Definition at line 695 of file http.c.
References ast_debug, ast_http_error(), ast_http_send(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_str_create(), ast_str_set(), ast_variable_new(), ast_variables_destroy(), ast_http_uri::callback, cleanup(), http_uri_redirect::dest, http_uri_redirect::entry, ast_http_uri::has_subtree, http_decode(), strsep(), http_uri_redirect::target, ast_http_uri::uri, and var.
Referenced by httpd_helper_thread().
|
static |
Definition at line 605 of file http.c.
References ast_uri_decode().
Referenced by ast_http_get_post_vars(), and handle_uri().
|
static |
Definition at line 1296 of file http.c.
References ARRAY_LEN, ast_cli_unregister_multiple(), ast_free, ast_http_uri_unlink(), AST_RWLIST_REMOVE_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_tcptls_server_stop(), ast_tls_config::certfile, ast_tls_config::cipher, cli_http, ast_tls_config::enabled, http_uri_redirect::entry, http_tls_cfg, ast_tls_config::pvtfile, staticuri, and statusuri.
Referenced by ast_http_init().
|
static |
Definition at line 878 of file http.c.
References ao2_ref, ast_atomic_fetchadd_int(), ast_http_error(), AST_HTTP_GET, AST_HTTP_HEAD, AST_HTTP_POST, AST_HTTP_PUT, AST_HTTP_UNKNOWN, ast_log(), ast_skip_blanks(), ast_skip_nonblanks(), ast_strlen_zero(), ast_tcptls_close_session_file(), ast_tcptls_stream_set_exclusive_input(), ast_tcptls_stream_set_timeout_inactivity(), ast_trim_blanks(), ast_variable_new(), ast_variables_destroy(), errno, ast_tcptls_session_instance::f, ast_tcptls_session_instance::fd, handle_uri(), LOG_WARNING, MAX_HTTP_REQUEST_HEADERS, name, ast_variable::next, ast_tcptls_session_instance::stream_cookie, strsep(), and value.
|
static |
Definition at line 323 of file http.c.
References ast_http_error(), AST_HTTP_GET, ast_http_get_cookies(), AST_HTTP_HEAD, ast_http_send(), ast_sockaddr_stringify_addr(), ast_sockaddr_stringify_port(), ast_str_append(), ast_str_create(), ast_variables_destroy(), ast_tls_config::enabled, http_tls_cfg, ast_variable::name, ast_variable::next, ast_tcptls_session_args::old_address, and ast_variable::value.
|
static |
replacement read/write functions for SSL support. We use wrappers rather than SSL_read/SSL_write directly so we can put in some debugging.
Definition at line 828 of file http.c.
References ast_debug, ast_strdupa, ast_strip(), ast_strip_quoted(), ast_strlen_zero(), ast_variable_new(), name, parse(), strsep(), and var.
Referenced by ast_http_get_cookies().
|
static |
Definition at line 203 of file http.c.
References ast_alloca, ast_config_AST_DATA_DIR, ast_http_error(), ast_http_ftype2mtype(), AST_HTTP_GET, AST_HTTP_HEAD, ast_http_manid_from_vars(), ast_http_send(), ast_localtime(), ast_str_create(), ast_str_set(), ast_strftime(), ast_strlen_zero(), astman_is_authed(), len(), mtype, ast_variable::name, ast_variable::next, S_OR, and ast_variable::value.
|
static |
Referenced by ast_get_http_method().
|
static |
Definition at line 1292 of file http.c.
Referenced by ast_http_init(), and http_shutdown().
const char* ext |
Definition at line 112 of file http.c.
Referenced by ast_ext_ctx(), ast_filehelper(), ast_http_ftype2mtype(), ast_rtp_read(), cli_console_dial(), common_exec(), console_transfer(), do_directory(), exts_compare(), handle_cli_dialplan_save(), iax_park_thread(), misdn_call(), misdn_request(), mixmonitor_thread(), moh_scan_files(), oh323_request(), pbx_load_config(), pbx_load_users(), pvalGotoSetTarget(), record_exec(), register_exten(), register_peer_exten(), sig_pri_call(), sig_pri_extract_called_num_subaddr(), sip_park_thread(), sip_request_call(), and unregister_exten().
|
static |
|
static |
Definition at line 77 of file http.c.
Referenced by __ast_http_load(), handle_show_http(), http_shutdown(), and httpstatus_callback().
|
static |
struct { ... } mimetypes[] |
Limit the kinds of files we're willing to serve up.
Referenced by ast_http_ftype2mtype().
const char* mtype |
Definition at line 113 of file http.c.
Referenced by static_callback().
|
static |
Definition at line 107 of file http.c.
Referenced by _while_exec(), aoc_d_event(), aoc_e_event(), aoc_s_event(), ast_db_deltree(), ast_db_gettree(), ast_remotecontrol(), exec_clearhash(), handle_cli_database_show(), hashkeys_read(), hashkeys_read2(), shared_read(), shared_write(), sip_show_settings(), and while_continue_exec().
|
static |
Definition at line 75 of file http.c.
Referenced by cli_fax_show_sessions().
|
static |
|
static |
|
static |
Definition at line 381 of file http.c.
Referenced by ast_http_init(), and http_shutdown().
|
static |
Definition at line 372 of file http.c.
Referenced by ast_http_init(), and http_shutdown().
|
static |