| Asterisk - The Open Source Telephony Project GIT-master-27fb039
    | 
Support for Private Asterisk HTTP Servers. More...


Go to the source code of this file.
| Data Structures | |
| struct | ast_http_auth | 
| HTTP authentication information.  More... | |
| struct | ast_http_uri | 
| Definition of a URI handler.  More... | |
| Typedefs | |
| typedef int(* | ast_http_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_params, struct ast_variable *headers) | 
| HTTP Callbacks. | |
| Enumerations | |
| enum | ast_http_method { AST_HTTP_UNKNOWN = -1 , AST_HTTP_GET = 0 , AST_HTTP_POST , AST_HTTP_HEAD , AST_HTTP_PUT , AST_HTTP_DELETE , AST_HTTP_OPTIONS , AST_HTTP_MAX_METHOD } | 
| HTTP Request methods known by Asterisk.  More... | |
| Functions | |
| const char * | ast_get_http_method (enum ast_http_method method) attribute_pure | 
| Return http method name string. | |
| enum ast_http_method | ast_get_http_method_from_string (const char *method) | 
| Return http method from string. | |
| 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. | |
| int | ast_http_body_discard (struct ast_tcptls_session_instance *ser) | 
| Read and discard any unread HTTP request body. | |
| void | ast_http_body_read_status (struct ast_tcptls_session_instance *ser, int read_success) | 
| Update the body read success status. | |
| struct ast_variable * | ast_http_create_basic_auth_header (const char *userid, const char *password) | 
| Create an HTTP authorization header. | |
| 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. | |
| void | ast_http_error (struct ast_tcptls_session_instance *ser, int status, const char *title, const char *text) | 
| Send HTTP error message and close socket. | |
| const char * | ast_http_ftype2mtype (const char *ftype) attribute_pure | 
| Return mime type based on extension. | |
| struct ast_http_auth * | ast_http_get_auth (struct ast_variable *headers) | 
| Get HTTP authentication information from headers. | |
| struct ast_variable * | ast_http_get_cookies (struct ast_variable *headers) | 
| Get cookie from Request headers. | |
| 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. | |
| 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. | |
| 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. | |
| 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 be located in the expected value. | |
| int | ast_http_header_parse (char *buf, char **name, char **value) | 
| Parse a header into the given name/value strings. | |
| uint32_t | ast_http_manid_from_vars (struct ast_variable *headers) attribute_pure | 
| Return manager id, if exist, from request headers. | |
| struct ast_variable * | ast_http_parse_post_form (char *buf, int content_length, const char *content_type) | 
| Get post variables from an application/x-www-form-urlencoded buffer. | |
| void | ast_http_prefix (char *buf, int len) | 
| Return the current prefix. | |
| void | ast_http_request_close_on_completion (struct ast_tcptls_session_instance *ser) | 
| Request the HTTP connection be closed after this HTTP request. | |
| int | ast_http_response_status_line (const char *buf, const char *version, int code) | 
| Parse the http response status line. | |
| 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. | |
| int | ast_http_uri_link (struct ast_http_uri *urihandler) | 
| Register a URI handler. | |
| void | ast_http_uri_unlink (struct ast_http_uri *urihandler) | 
| Unregister a URI handler. | |
| void | ast_http_uri_unlink_all_with_key (const char *key) | 
| Unregister all handlers with matching key. | |
Support for Private Asterisk HTTP Servers.
Definition in file http.h.
| typedef int(* ast_http_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_params, struct ast_variable *headers) | 
HTTP Callbacks.
| ser | TCP/TLS session object | 
| urih | Registered URI handler struct for the URI. | 
| uri | Remaining request URI path (also with the get_params removed). | 
| method | enum ast_http_method GET, POST, etc. | 
| get_params | URI argument list passed with the HTTP request. | 
| headers | HTTP request header-name/value pair list | 
Status and status text should be sent as arguments to the ast_http_send() function to reflect the status of the request (200 or 304, for example). Content length is calculated by ast_http_send() automatically.
Static content may be indicated to the ast_http_send() function, to indicate that it may be cached.
For a need authentication response, the ast_http_auth() function should be used.
For an error response, the ast_http_error() function should be used.
| 0 | Continue and process the next HTTP request. | 
| -1 | Fatal HTTP connection error. Force the HTTP connection closed. | 
| enum ast_http_method | 
HTTP Request methods known by Asterisk.
| Enumerator | |
|---|---|
| AST_HTTP_UNKNOWN | Unknown response | 
| AST_HTTP_GET | |
| AST_HTTP_POST | |
| AST_HTTP_HEAD | |
| AST_HTTP_PUT | |
| AST_HTTP_DELETE | |
| AST_HTTP_OPTIONS | |
| AST_HTTP_MAX_METHOD | Last entry in ast_http_method enum | 
Definition at line 58 of file http.h.
| const char * ast_get_http_method | ( | enum ast_http_method | method | ) | 
Return http method name string.
Definition at line 193 of file http.c.
References ARRAY_LEN, ast_http_methods_text, method, NULL, and ast_cfhttp_methods_text::text.
Referenced by add_allow_header(), ast_ari_callback(), ast_ari_invoke(), auth_http_callback(), and handle_options().
| enum ast_http_method ast_get_http_method_from_string | ( | const char * | method | ) | 
Return http method from string.
Definition at line 206 of file http.c.
References ARRAY_LEN, ast_http_methods_text, AST_HTTP_UNKNOWN, ast_strings_equal(), ast_cfhttp_methods_text::method, method, and text.
Referenced by parse_rest_request_msg().
| 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 638 of file http.c.
References ast_http_create_response(), ast_str_create, ast_str_set(), DEFAULT_RESPONSE_HEADER_LENGTH, and text.
| int ast_http_body_discard | ( | struct ast_tcptls_session_instance * | ser | ) | 
Read and discard any unread HTTP request body.
| ser | HTTP TCP/TLS session object. | 
| 0 | on success. | 
| -1 | on error. | 
Definition at line 1135 of file http.c.
References ast_assert, ast_debug, ast_set_flag, ast_test_flag, http_body_check_chunk_sync(), http_body_discard_chunk_trailer_headers(), http_body_discard_contents(), http_body_get_chunk_length(), HTTP_FLAG_BODY_READ, HTTP_FLAG_CLOSE_ON_COMPLETION, HTTP_FLAG_HAS_BODY, ast_tcptls_session_instance::private_data, and request().
Referenced by ast_http_send(), and ast_websocket_uri_cb().
| void ast_http_body_read_status | ( | struct ast_tcptls_session_instance * | ser, | 
| int | read_success | ||
| ) | 
Update the body read success status.
| ser | HTTP TCP/TLS session object. | 
| read_success | TRUE if body was read successfully. | 
Definition at line 914 of file http.c.
References ast_set_flag, ast_test_flag, HTTP_FLAG_BODY_READ, HTTP_FLAG_CLOSE_ON_COMPLETION, HTTP_FLAG_HAS_BODY, ast_tcptls_session_instance::private_data, and request().
Referenced by http_post_callback().
| struct ast_variable * ast_http_create_basic_auth_header | ( | const char * | userid, | 
| const char * | password | ||
| ) | 
Create an HTTP authorization header.
The returned ast_variable must be freed with ast_variables_destroy()
| userid | User ID or "<userid>:<password>". | 
| password | Password if not in userid. | 
| NULL | if memory allocation failed. | 
Definition at line 1668 of file http.c.
References ast_alloca, ast_asprintf, ast_base64encode(), ast_free, ast_strdup, ast_strlen_zero(), ast_variable_new, BASIC_LEN, BASIC_PREFIX, NULL, and RAII_VAR.
Referenced by outbound_session_handler_thread(), and websocket_client_handshake().
| 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.
| ser | TCP/TLS session object | 
| status_code | HTTP response code (200/401/403/404/500) | 
| status_title | English equivalent to the status_code parameter | 
| http_header_data | The formatted text to use in the http header | 
| text | Additional informational text to use in the response | 
The response body is created as HTML content, from the status_code, status_title, and the text parameters.
The http_header_data parameter will be freed as a result of calling function.
Definition at line 583 of file http.c.
References ast_debug, ast_free, ast_http_send(), AST_HTTP_UNKNOWN, ast_str_buffer(), ast_str_create, ast_str_set(), ast_strlen_zero(), ast_tcptls_close_session_file(), ast_xml_escape(), http_server_name, INITIAL_RESPONSE_BODY_BUFFER, MAX_SERVER_NAME_LENGTH, out, and text.
Referenced by ast_http_auth(), and ast_http_error().
| void ast_http_error | ( | struct ast_tcptls_session_instance * | ser, | 
| int | status, | ||
| const char * | title, | ||
| const char * | text | ||
| ) | 
Send HTTP error message and close socket.
Definition at line 664 of file http.c.
References ast_http_create_response(), ast_str_create, ast_str_set(), DEFAULT_RESPONSE_HEADER_LENGTH, and text.
Referenced by ast_ari_callback(), ast_websocket_uri_cb(), auth_http_callback(), generic_http_callback(), handle_uri(), http_callback(), http_post_callback(), http_request_headers_get(), http_request_tracking_setup(), httpd_helper_thread(), httpd_process_request(), httpstatus_callback(), incoming_ws_http_callback(), phoneprov_callback(), static_callback(), and websocket_bad_request().
| const char * ast_http_ftype2mtype | ( | const char * | ftype | ) | 
Return mime type based on extension.
| ftype | filename extension | 
Definition at line 219 of file http.c.
References ARRAY_LEN, ext, mimetypes, and NULL.
Referenced by build_profile(), and static_callback().
| struct ast_http_auth * ast_http_get_auth | ( | struct ast_variable * | headers | ) | 
Get HTTP authentication information from headers.
The returned object is AO2 managed, so clean up with ao2_cleanup().
| headers | HTTP request headers. | 
| NULL | if no supported HTTP auth headers present. | 
Definition at line 1611 of file http.c.
References ast_assert, ast_base64decode(), ast_begins_with(), ast_log, auth_create(), base64, BASIC_LEN, BASIC_PREFIX, LOG_DEBUG, LOG_WARNING, ast_variable::name, ast_variable::next, NULL, strsep(), and ast_variable::value.
Referenced by authenticate_user(), and http_callback().
| struct ast_variable * ast_http_get_cookies | ( | struct ast_variable * | headers | ) | 
Get cookie from Request headers.
Definition at line 1564 of file http.c.
References ast_variables_destroy(), ast_variable::name, ast_variable::next, NULL, parse_cookies(), and ast_variable::value.
Referenced by ast_http_manid_from_vars(), and httpstatus_callback().
| 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.
| ser | TCP/TLS session object | 
| headers | List of HTTP headers | 
| NULL | on error, if no content, or if different content type. | 
Definition at line 1330 of file http.c.
References ast_free, ast_http_get_contents(), ast_json_load_buf(), ast_strlen_zero(), buf, errno, get_content_type(), NULL, RAII_VAR, and type.
Referenced by ast_ari_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 1405 of file http.c.
References ast_free, ast_http_get_contents(), ast_http_parse_post_form(), ast_strlen_zero(), buf, errno, get_content_type(), NULL, RAII_VAR, and type.
Referenced by ast_ari_callback(), auth_http_callback(), and generic_http_callback().
| 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.
| name | header name to check | 
| expected_name | the expected name of the header | 
| value | header value to check | 
| expected_value | the expected value of the header | 
| 0 | if the name and expected name do not match | 
| -1 | if the value and expected value do not match | 
| 1 | if the both the name and value match their expected value | 
Definition at line 1786 of file http.c.
References ast_log, LOG_ERROR, name, and value.
Referenced by websocket_client_handshake_get_response().
| 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 be located in the expected value.
| name | header name to check | 
| expected_name | the expected name of the header | 
| value | header value to check if in expected value | 
| expected_value | the expected value(s) | 
| 0 | if the name and expected name do not match | 
| -1 | if the value and is not in the expected value | 
| 1 | if the name matches expected name and value is in expected value | 
Definition at line 1802 of file http.c.
References ast_log, LOG_ERROR, name, strcasestr(), and value.
Referenced by websocket_client_handshake_get_response().
| int ast_http_header_parse | ( | char * | buf, | 
| char ** | name, | ||
| char ** | value | ||
| ) | 
Parse a header into the given name/value strings.
| buf | a string containing the name/value to point to | |
| [out] | name | header name | 
| [out] | value | header value | 
| -1 | if buf is empty | 
| 0 | if buf could be separated into name and value | 
| 1 | if name or value portion don't exist | 
Definition at line 1764 of file http.c.
References ast_skip_blanks(), ast_strlen_zero(), ast_trim_blanks(), buf, name, remove_excess_lws(), strsep(), and value.
Referenced by websocket_client_handshake_get_response().
| 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 233 of file http.c.
References ast_http_get_cookies(), ast_variables_destroy(), ast_variable::name, ast_variable::next, and ast_variable::value.
Referenced by generic_http_callback(), http_post_callback(), and static_callback().
| struct ast_variable * ast_http_parse_post_form | ( | char * | buf, | 
| int | content_length, | ||
| const char * | content_type | ||
| ) | 
Get post variables from an application/x-www-form-urlencoded buffer.
| buf | input buffer | 
| content_len | Buffer length | 
| content_type | Content type (must be "application/x-www-form-urlencoded") | 
Definition at line 1369 of file http.c.
References ast_strlen_zero(), ast_uri_decode(), ast_uri_http_legacy, ast_variable_new, buf, errno, NULL, strsep(), and var.
Referenced by ast_http_get_post_vars(), and parse_rest_request_msg().
| 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 249 of file http.c.
References ast_copy_string(), buf, len(), and prefix.
| void ast_http_request_close_on_completion | ( | struct ast_tcptls_session_instance * | ser | ) | 
Request the HTTP connection be closed after this HTTP request.
| ser | HTTP TCP/TLS session object. | 
Definition at line 853 of file http.c.
References ast_set_flag, HTTP_FLAG_CLOSE_ON_COMPLETION, ast_tcptls_session_instance::private_data, and request().
Referenced by ast_ari_callback(), ast_ari_invoke(), auth_http_callback(), generic_http_callback(), handle_uri(), http_callback(), http_post_callback(), httpstatus_callback(), static_callback(), and websocket_bad_request().
| int ast_http_response_status_line | ( | const char * | buf, | 
| const char * | version, | ||
| int | code | ||
| ) | 
Parse the http response status line.
| buf | the http response line information | 
| version | the expected http version (e.g. HTTP/1.1) | 
| code | the expected status code | 
| -1 | if version didn't match or status code conversion fails. | 
Definition at line 1712 of file http.c.
References ast_log, buf, LOG_ERROR, and version.
Referenced by websocket_client_handshake_get_response().
| 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.
| 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 472 of file http.c.
References ast_assert, ast_debug, ast_free, ast_http_body_discard(), AST_HTTP_HEAD, ast_iostream_printf(), ast_iostream_write(), ast_localtime(), ast_str_buffer(), ast_str_create, ast_str_set(), ast_str_strlen(), ast_strftime(), ast_strlen_zero(), ast_tcptls_close_session_file(), ast_test_flag, ast_tvnow(), buf, errno, HTTP_FLAG_CLOSE_ON_COMPLETION, http_server_name, len(), MAX_SERVER_NAME_LENGTH, method, out, ast_tcptls_session_instance::private_data, request(), session_keep_alive, and ast_tcptls_session_instance::stream.
Referenced by ast_ari_callback(), ast_http_create_response(), auth_http_callback(), generic_http_callback(), handle_uri(), http_callback(), http_callback(), httpstatus_callback(), phoneprov_callback(), static_callback(), and websocket_bad_request().
| int ast_http_uri_link | ( | struct ast_http_uri * | urih | ) | 
Register a URI handler.
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 689 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, ast_http_uri::entry, len(), ast_http_uri::prefix, prefix, and ast_http_uri::uri.
Referenced by __ast_http_load(), __ast_http_post_load(), __init_manager(), load_module(), load_module(), load_module(), load_module(), load_module(), load_module(), reload_module(), and reload_module().
| void ast_http_uri_unlink | ( | struct ast_http_uri * | urihandler | ) | 
Unregister a URI handler.
Definition at line 721 of file http.c.
References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and ast_http_uri::entry.
Referenced by __ast_http_load(), __init_manager(), load_module(), reload_module(), reload_module(), unload_module(), unload_module(), unload_module(), unload_module(), unload_module(), unload_module(), and unload_module().
| void ast_http_uri_unlink_all_with_key | ( | const char * | key | ) | 
Unregister all handlers with matching key.
Definition at line 728 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, ast_http_uri::entry, ast_http_uri::key, and ast_http_uri::mallocd.
Referenced by __ast_http_post_load(), and unload_module().