Fri Sep 11 13:45:32 2009

Asterisk developer's documentation


http.h File Reference

Support for Private Asterisk HTTP Servers. More...

#include "asterisk/config.h"

Go to the source code of this file.

Data Structures

struct  ast_http_uri

Typedefs

typedef char *(*) ast_http_callback (struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength)
 HTTP Callbacks take the socket, 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
to provide separation between user headers and content (even if no content is specified).

Functions

char * ast_http_error (int status, const char *title, const char *extra_header, const char *text)
 Return a malloc()'d string containing an HTTP error message.
int ast_http_init (void)
int ast_http_reload (void)
char * ast_http_setcookie (const char *var, const char *val, int expires, char *buf, size_t buflen)
int ast_http_uri_link (struct ast_http_uri *urihandler)
 Link into the Asterisk HTTP server.
void ast_http_uri_unlink (struct ast_http_uri *urihandler)
 Destroy an HTTP server.


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>

Definition in file http.h.


Typedef Documentation

typedef char*(*) ast_http_callback(struct sockaddr_in *requestor, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength)

HTTP Callbacks take the socket, 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
to provide separation between user headers and content (even if no content is specified).

Definition at line 39 of file http.h.


Function Documentation

char* ast_http_error ( int  status,
const char *  title,
const char *  extra_header,
const char *  text 
)

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

Definition at line 231 of file http.c.

References asprintf, ast_log(), errno, and LOG_WARNING.

Referenced by ast_httpd_helper_thread(), handle_uri(), and static_callback().

00232 {
00233    char *c = NULL;
00234    if (asprintf(&c,
00235            "Content-type: text/html\r\n"
00236            "%s"
00237            "\r\n"
00238            "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
00239            "<html><head>\r\n"
00240            "<title>%d %s</title>\r\n"
00241            "</head><body>\r\n"
00242            "<h1>%s</h1>\r\n"
00243            "<p>%s</p>\r\n"
00244            "<hr />\r\n"
00245            "<address>Asterisk Server</address>\r\n"
00246            "</body></html>\r\n",
00247            (extra_header ? extra_header : ""), status, title, title, text) < 0) {
00248       ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00249       c = NULL;
00250    }
00251    return c;
00252 }

int ast_http_init ( void   ) 

Definition at line 745 of file http.c.

References __ast_http_load(), ast_cli_register_multiple(), ast_http_uri_link(), cli_http, staticuri, and statusuri.

Referenced by main().

00746 {
00747    ast_http_uri_link(&statusuri);
00748    ast_http_uri_link(&staticuri);
00749    ast_cli_register_multiple(cli_http, sizeof(cli_http) / sizeof(struct ast_cli_entry));
00750 
00751    return __ast_http_load(0);
00752 }

int ast_http_reload ( void   ) 

Definition at line 730 of file http.c.

References __ast_http_load().

00731 {
00732    return __ast_http_load(1);
00733 }

char* ast_http_setcookie ( const char *  var,
const char *  val,
int  expires,
char *  buf,
size_t  buflen 
)

Definition at line 570 of file http.c.

References ast_build_string().

00571 {
00572    char *c;
00573    c = buf;
00574    ast_build_string(&c, &buflen, "Set-Cookie: %s=\"%s\"; Version=\"1\"", var, val);
00575    if (expires)
00576       ast_build_string(&c, &buflen, "; Max-Age=%d", expires);
00577    ast_build_string(&c, &buflen, "\r\n");
00578    return buf;
00579 }

int ast_http_uri_link ( struct ast_http_uri urihandler  ) 

Link into the Asterisk HTTP server.

Definition at line 254 of file http.c.

References ast_rwlock_unlock(), ast_rwlock_wrlock(), ast_http_uri::next, ast_http_uri::uri, uris, and uris_lock.

Referenced by ast_http_init().

00255 {
00256    struct ast_http_uri *prev;
00257 
00258    ast_rwlock_wrlock(&uris_lock);
00259    prev = uris;
00260    if (!uris || strlen(uris->uri) <= strlen(urih->uri)) {
00261       urih->next = uris;
00262       uris = urih;
00263    } else {
00264       while (prev->next && (strlen(prev->next->uri) > strlen(urih->uri)))
00265          prev = prev->next;
00266       /* Insert it here */
00267       urih->next = prev->next;
00268       prev->next = urih;
00269    }
00270    ast_rwlock_unlock(&uris_lock);
00271 
00272    return 0;
00273 }  

void ast_http_uri_unlink ( struct ast_http_uri urihandler  ) 

Destroy an HTTP server.

Definition at line 275 of file http.c.

References ast_rwlock_unlock(), ast_rwlock_wrlock(), ast_http_uri::next, uris, and uris_lock.

00276 {
00277    struct ast_http_uri *prev;
00278 
00279    ast_rwlock_wrlock(&uris_lock);
00280    if (!uris) {
00281       ast_rwlock_unlock(&uris_lock);
00282       return;
00283    }
00284    prev = uris;
00285    if (uris == urih) {
00286       uris = uris->next;
00287    }
00288    while(prev->next) {
00289       if (prev->next == urih) {
00290          prev->next = urih->next;
00291          break;
00292       }
00293       prev = prev->next;
00294    }
00295    ast_rwlock_unlock(&uris_lock);
00296 }


Generated on Fri Sep 11 13:45:32 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7