Thu Apr 28 2011 17:16:07

Asterisk developer's documentation


http.h File Reference

Support for Private Asterisk HTTP Servers. More...

#include "asterisk/config.h"
#include "asterisk/tcptls.h"
#include "asterisk/linkedlists.h"
Include dependency graph for http.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_http_uri
 Definition of a URI handler. More...

Typedefs

typedef struct ast_str *(* 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 *params, struct ast_variable *headers, int *status, char **title, int *contentlength)

Enumerations

enum  ast_http_method { AST_HTTP_GET = 0, AST_HTTP_POST }
 

HTTP Callbacks take the socket.

More...

Functions

struct ast_strast_http_error (int status, const char *title, const char *extra_header, const char *text)
 Return an ast_str malloc()'d string containing an HTTP error message.
void ast_http_prefix (char *buf, int len)
 Return the current prefix.
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.

Detailed Description

Support for Private Asterisk HTTP Servers.

Note:
Note: The Asterisk HTTP servers are extremely simple and minimal and only support the "GET" method.
Author:
Mark Spencer <markster@digium.com>
Note:
In order to have TLS/SSL support, we need the openssl libraries. Still we can decide whether or not to use them by commenting in or out the DO_SSL macro. TLS/SSL support is basically implemented by reading from a config file (currently http.conf) the names of the certificate and cipher to use, and then run ssl_setup() to create an appropriate SSL_CTX (ssl_ctx) If we support multiple domains, presumably we need to read multiple certificates. When we are requested to open a TLS socket, we run make_file_from_fd() on the socket, to do the necessary setup. At the moment the context's name is hardwired in the function, but we can certainly make it into an extra parameter to the function. We declare most of ssl support variables unconditionally, because their number is small and this simplifies the code.
: 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 Documentation

typedef struct ast_str*(* 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 *params, struct ast_variable *headers, int *status, char **title, int *contentlength)

Definition at line 75 of file http.h.


Enumeration Type Documentation

HTTP Callbacks take the socket.

Note:
The method and the path as arguments and should return the content, allocated with malloc(). Status should be changed to reflect the status of the request if it isn't 200 and title may be set to a malloc()'d string to an appropriate title for non-200 responses. Content length may also be specified.
   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)
Enumerator:
AST_HTTP_GET 
AST_HTTP_POST 

Definition at line 69 of file http.h.


Function Documentation

struct ast_str* ast_http_error ( int  status,
const char *  title,
const char *  extra_header,
const char *  text 
) [read]

Return an ast_str malloc()'d string containing an HTTP error message.

Definition at line 322 of file http.c.

References ast_str_create(), and ast_str_set().

Referenced by generic_http_callback(), handle_uri(), http_post_callback(), httpd_helper_thread(), phoneprov_callback(), and static_callback().

{
   struct ast_str *out = ast_str_create(512);

   if (out == NULL) {
      return out;
   }

   ast_str_set(&out, 0,
          "Content-type: text/html\r\n"
          "%s"
          "\r\n"
          "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
          "<html><head>\r\n"
          "<title>%d %s</title>\r\n"
          "</head><body>\r\n"
          "<h1>%s</h1>\r\n"
          "<p>%s</p>\r\n"
          "<hr />\r\n"
          "<address>Asterisk Server</address>\r\n"
          "</body></html>\r\n",
          (extra_header ? extra_header : ""), status, title, title, text);

   return out;
}
void ast_http_prefix ( char *  buf,
int  len 
)

Return the current prefix.

Parameters:
buf[out]destination buffer for previous
len[in]length of prefix to copy
Since:
1.6.1

Definition at line 160 of file http.c.

References ast_copy_string().

{
   if (buf) {
      ast_copy_string(buf, prefix, len);
   }
}
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 357 of file http.c.

References 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_http_uri::description, http_uri_redirect::entry, len(), LOG_WARNING, ast_http_uri::supports_get, ast_http_uri::supports_post, and ast_http_uri::uri.

Referenced by __ast_http_post_load(), __init_manager(), ast_http_init(), and load_module().

{
   struct ast_http_uri *uri;
   int len = strlen(urih->uri);

   if (!(urih->supports_get || urih->supports_post)) {
      ast_log(LOG_WARNING, "URI handler does not provide either GET or POST method: %s (%s)\n", urih->uri, urih->description);
      return -1;
   }

   AST_RWLIST_WRLOCK(&uris);

   if (AST_RWLIST_EMPTY(&uris) || strlen(AST_RWLIST_FIRST(&uris)->uri) <= len) {
      AST_RWLIST_INSERT_HEAD(&uris, urih, entry);
      AST_RWLIST_UNLOCK(&uris);

      return 0;
   }

   AST_RWLIST_TRAVERSE(&uris, uri, entry) {
      if (AST_RWLIST_NEXT(uri, entry) &&
          strlen(AST_RWLIST_NEXT(uri, entry)->uri) <= len) {
         AST_RWLIST_INSERT_AFTER(&uris, uri, urih, entry);
         AST_RWLIST_UNLOCK(&uris); 

         return 0;
      }
   }

   AST_RWLIST_INSERT_TAIL(&uris, urih, entry);

   AST_RWLIST_UNLOCK(&uris);
   
   return 0;
}  
void ast_http_uri_unlink ( struct ast_http_uri urihandler)

Unregister a URI handler.

Definition at line 393 of file http.c.

References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and http_uri_redirect::entry.

Referenced by __init_manager(), and unload_module().

void ast_http_uri_unlink_all_with_key ( const char *  key)