#include "asterisk/config.h"
#include "asterisk/tcptls.h"
#include "asterisk/linkedlists.h"
Go to the source code of this file.
Data Structures | |
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 } |
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. | |
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. | |
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. | |
ast_variable * | ast_http_get_cookies (struct ast_variable *headers) |
Get cookie from Request headers. | |
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. | |
uint32_t | ast_http_manid_from_vars (struct ast_variable *headers) attribute_pure |
Return manager id, if exist, from request headers. | |
void | ast_http_prefix (char *buf, int len) |
Return the current prefix. | |
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. | |
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. |
: the ssl-support variables (ssl_ctx, do_ssl, certfile, cipher) and their setup should be moved to a more central place, e.g. asterisk.conf and the source files that processes it. Similarly, ssl_setup() should be run earlier in the startup process so modules have it available.
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.
Static content may be indicated to the ast_http_send() function, to indicate that it may be cached.
* The return value may include additional headers at the front and MUST * include a blank line with \r\n to provide separation between user headers * and content (even if no content is specified) *
For an error response, the ast_http_error() function may be used.
enum ast_http_method |
HTTP Request methods known by Asterisk.
AST_HTTP_UNKNOWN | Unknown response |
AST_HTTP_GET | |
AST_HTTP_POST | |
AST_HTTP_HEAD | |
AST_HTTP_PUT | Not supported in Asterisk |
Definition at line 56 of file http.h.
00056 { 00057 AST_HTTP_UNKNOWN = -1, /*!< Unknown response */ 00058 AST_HTTP_GET = 0, 00059 AST_HTTP_POST, 00060 AST_HTTP_HEAD, 00061 AST_HTTP_PUT, /*!< Not supported in Asterisk */ 00062 };
const char* ast_get_http_method | ( | enum ast_http_method | method | ) |
Return http method name string.
Definition at line 151 of file http.c.
References ARRAY_LEN, ast_http_methods_text, and ast_cfhttp_methods_text::text.
00152 { 00153 int x; 00154 00155 for (x = 0; x < ARRAY_LEN(ast_http_methods_text); x++) { 00156 if (ast_http_methods_text[x].method == method) { 00157 return ast_http_methods_text[x].text; 00158 } 00159 } 00160 00161 return NULL; 00162 }
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 465 of file http.c.
References ast_free, ast_http_send(), AST_HTTP_UNKNOWN, ast_str_create(), and ast_str_set().
00468 { 00469 struct ast_str *http_headers = ast_str_create(128); 00470 struct ast_str *out = ast_str_create(512); 00471 00472 if (!http_headers || !out) { 00473 ast_free(http_headers); 00474 ast_free(out); 00475 return; 00476 } 00477 00478 ast_str_set(&http_headers, 0, 00479 "WWW-authenticate: Digest algorithm=MD5, realm=\"%s\", nonce=\"%08lx\", qop=\"auth\", opaque=\"%08lx\"%s\r\n" 00480 "Content-type: text/html\r\n", 00481 realm ? realm : "Asterisk", 00482 nonce, 00483 opaque, 00484 stale ? ", stale=true" : ""); 00485 00486 ast_str_set(&out, 0, 00487 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n" 00488 "<html><head>\r\n" 00489 "<title>401 Unauthorized</title>\r\n" 00490 "</head><body>\r\n" 00491 "<h1>401 Unauthorized</h1>\r\n" 00492 "<p>%s</p>\r\n" 00493 "<hr />\r\n" 00494 "<address>Asterisk Server</address>\r\n" 00495 "</body></html>\r\n", 00496 text ? text : ""); 00497 00498 ast_http_send(ser, AST_HTTP_UNKNOWN, 401, "Unauthorized", http_headers, out, 0, 0); 00499 return; 00500 }
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 503 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(), phoneprov_callback(), and static_callback().
00504 { 00505 struct ast_str *http_headers = ast_str_create(40); 00506 struct ast_str *out = ast_str_create(256); 00507 00508 if (!http_headers || !out) { 00509 ast_free(http_headers); 00510 ast_free(out); 00511 return; 00512 } 00513 00514 ast_str_set(&http_headers, 0, "Content-type: text/html\r\n"); 00515 00516 ast_str_set(&out, 0, 00517 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n" 00518 "<html><head>\r\n" 00519 "<title>%d %s</title>\r\n" 00520 "</head><body>\r\n" 00521 "<h1>%s</h1>\r\n" 00522 "<p>%s</p>\r\n" 00523 "<hr />\r\n" 00524 "<address>Asterisk Server</address>\r\n" 00525 "</body></html>\r\n", 00526 status_code, status_title, status_title, text); 00527 00528 ast_http_send(ser, AST_HTTP_UNKNOWN, status_code, status_title, http_headers, out, 0, 0); 00529 return; 00530 }
const char* ast_http_ftype2mtype | ( | const char * | ftype | ) |
Return mime type based on extension.
ftype | filename extension |
Definition at line 164 of file http.c.
References ARRAY_LEN, ext, and mimetypes.
Referenced by static_callback().
00165 { 00166 int x; 00167 00168 if (ftype) { 00169 for (x = 0; x < ARRAY_LEN(mimetypes); x++) { 00170 if (!strcasecmp(ftype, mimetypes[x].ext)) { 00171 return mimetypes[x].mtype; 00172 } 00173 } 00174 } 00175 return NULL; 00176 }
struct ast_variable* ast_http_get_cookies | ( | struct ast_variable * | headers | ) |
Get cookie from Request headers.
Definition at line 839 of file http.c.
References ast_strdupa, 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().
00840 { 00841 struct ast_variable *v, *cookies=NULL; 00842 00843 for (v = headers; v; v = v->next) { 00844 if (!strncasecmp(v->name, "Cookie", 6)) { 00845 char *tmp = ast_strdupa(v->value); 00846 if (cookies) { 00847 ast_variables_destroy(cookies); 00848 } 00849 00850 cookies = parse_cookies(tmp); 00851 } 00852 } 00853 return cookies; 00854 }
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 619 of file http.c.
References ast_variable::name, ast_variable::next, ast_variable::value, and var.
00621 { 00622 int content_length = 0; 00623 struct ast_variable *v, *post_vars=NULL, *prev = NULL; 00624 char *buf, *var, *val; 00625 00626 for (v = headers; v; v = v->next) { 00627 if (!strcasecmp(v->name, "Content-Type")) { 00628 if (strcasecmp(v->value, "application/x-www-form-urlencoded")) { 00629 return NULL; 00630 } 00631 break; 00632 } 00633 } 00634 00635 for (v = headers; v; v = v->next) { 00636 if (!strcasecmp(v->name, "Content-Length")) { 00637 content_length = atoi(v->value) + 1; 00638 break; 00639 } 00640 } 00641 00642 if (!content_length) { 00643 return NULL; 00644 } 00645 00646 if (!(buf = alloca(content_length))) { 00647 return NULL; 00648 } 00649 if (!fgets(buf, content_length, ser->f)) { 00650 return NULL; 00651 } 00652 00653 while ((val = strsep(&buf, "&"))) { 00654 var = strsep(&val, "="); 00655 if (val) { 00656 http_decode(val); 00657 } else { 00658 val = ""; 00659 } 00660 http_decode(var); 00661 if ((v = ast_variable_new(var, val, ""))) { 00662 if (post_vars) { 00663 prev->next = v; 00664 } else { 00665 post_vars = v; 00666 } 00667 prev = v; 00668 } 00669 } 00670 return post_vars; 00671 }
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 178 of file http.c.
References ast_http_get_cookies(), ast_variable::name, ast_variable::next, and ast_variable::value.
Referenced by http_post_callback(), and static_callback().
00179 { 00180 uint32_t mngid = 0; 00181 struct ast_variable *v, *cookies; 00182 00183 cookies = ast_http_get_cookies(headers); 00184 for (v = cookies; v; v = v->next) { 00185 if (!strcasecmp(v->name, "mansession_id")) { 00186 sscanf(v->value, "%30x", &mngid); 00187 break; 00188 } 00189 } 00190 if (cookies) { 00191 ast_variables_destroy(cookies); 00192 } 00193 return mngid; 00194 }
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().
00197 { 00198 if (buf) { 00199 ast_copy_string(buf, prefix, len); 00200 } 00201 }
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 |
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 391 of file http.c.
References ast_free, ast_get_version(), AST_HTTP_HEAD, ast_localtime(), ast_log(), ast_str_buffer(), ast_strftime(), ast_tvnow(), errno, ast_tcptls_session_instance::f, len(), and LOG_WARNING.
Referenced by ast_http_auth(), ast_http_error(), handle_uri(), httpstatus_callback(), and phoneprov_callback().
00395 { 00396 struct timeval now = ast_tvnow(); 00397 struct ast_tm tm; 00398 char timebuf[80]; 00399 int content_length = 0; 00400 00401 if (!ser || 0 == ser->f) { 00402 return; 00403 } 00404 00405 ast_strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", ast_localtime(&now, &tm, "GMT")); 00406 00407 /* calc content length */ 00408 if (out) { 00409 content_length += strlen(ast_str_buffer(out)); 00410 } 00411 00412 if (fd) { 00413 content_length += lseek(fd, 0, SEEK_END); 00414 lseek(fd, 0, SEEK_SET); 00415 } 00416 00417 /* send http header */ 00418 fprintf(ser->f, "HTTP/1.1 %d %s\r\n" 00419 "Server: Asterisk/%s\r\n" 00420 "Date: %s\r\n" 00421 "Connection: close\r\n" 00422 "%s" 00423 "Content-Length: %d\r\n" 00424 "%s" 00425 "\r\n", 00426 status_code, status_title ? status_title : "OK", 00427 ast_get_version(), 00428 timebuf, 00429 static_content ? "" : "Cache-Control: no-cache, no-store\r\n", 00430 content_length, 00431 http_header ? ast_str_buffer(http_header) : "" 00432 ); 00433 00434 /* send content */ 00435 if (method != AST_HTTP_HEAD || status_code >= 400) { 00436 if (out) { 00437 fprintf(ser->f, "%s", ast_str_buffer(out)); 00438 } 00439 00440 if (fd) { 00441 char buf[256]; 00442 int len; 00443 while ((len = read(fd, buf, sizeof(buf))) > 0) { 00444 if (fwrite(buf, len, 1, ser->f) != 1) { 00445 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno)); 00446 break; 00447 } 00448 } 00449 } 00450 } 00451 00452 if (http_header) { 00453 ast_free(http_header); 00454 } 00455 if (out) { 00456 ast_free(out); 00457 } 00458 00459 fclose(ser->f); 00460 ser->f = 0; 00461 return; 00462 }
int ast_http_uri_link | ( | struct ast_http_uri * | urih | ) |
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 541 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(), and ast_http_uri::uri.
Referenced by __ast_http_post_load(), ast_http_init(), and load_module().
00542 { 00543 struct ast_http_uri *uri; 00544 int len = strlen(urih->uri); 00545 00546 AST_RWLIST_WRLOCK(&uris); 00547 00548 if ( AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len ) { 00549 AST_RWLIST_INSERT_HEAD(&uris, urih, entry); 00550 AST_RWLIST_UNLOCK(&uris); 00551 return 0; 00552 } 00553 00554 AST_RWLIST_TRAVERSE(&uris, uri, entry) { 00555 if (AST_RWLIST_NEXT(uri, entry) && 00556 strlen(AST_RWLIST_NEXT(uri, entry)->uri) <= len) { 00557 AST_RWLIST_INSERT_AFTER(&uris, uri, urih, entry); 00558 AST_RWLIST_UNLOCK(&uris); 00559 00560 return 0; 00561 } 00562 } 00563 00564 AST_RWLIST_INSERT_TAIL(&uris, urih, entry); 00565 00566 AST_RWLIST_UNLOCK(&uris); 00567 00568 return 0; 00569 }
void ast_http_uri_unlink | ( | struct ast_http_uri * | urihandler | ) |
Unregister a URI handler.
Definition at line 571 of file http.c.
References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and ast_http_uri::entry.
Referenced by unload_module().
00572 { 00573 AST_RWLIST_WRLOCK(&uris); 00574 AST_RWLIST_REMOVE(&uris, urih, entry); 00575 AST_RWLIST_UNLOCK(&uris); 00576 }
void ast_http_uri_unlink_all_with_key | ( | const char * | key | ) |
Unregister all handlers with matching key.
Definition at line 578 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().
00579 { 00580 struct ast_http_uri *urih; 00581 AST_RWLIST_WRLOCK(&uris); 00582 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&uris, urih, entry) { 00583 if (!strcmp(urih->key, key)) { 00584 AST_RWLIST_REMOVE_CURRENT(entry); 00585 if (urih->dmallocd) { 00586 ast_free(urih->data); 00587 } 00588 if (urih->mallocd) { 00589 ast_free(urih); 00590 } 00591 } 00592 } 00593 AST_RWLIST_TRAVERSE_SAFE_END; 00594 AST_RWLIST_UNLOCK(&uris); 00595 }