libasyncd
ad_http_handler.h File Reference

ad_http_handler header file More...

Go to the source code of this file.

Data Structures

struct  ad_http_s
 

Macros

#define HTTP_PROTOCOL_09   "HTTP/0.9"
 
#define HTTP_PROTOCOL_10   "HTTP/1.0"
 
#define HTTP_PROTOCOL_11   "HTTP/1.1"
 
#define HTTP_NO_RESPONSE   (0)
 
#define HTTP_CODE_CONTINUE   (100)
 
#define HTTP_CODE_OK   (200)
 
#define HTTP_CODE_CREATED   (201)
 
#define HTTP_CODE_NO_CONTENT   (204)
 
#define HTTP_CODE_PARTIAL_CONTENT   (206)
 
#define HTTP_CODE_MULTI_STATUS   (207)
 
#define HTTP_CODE_MOVED_TEMPORARILY   (302)
 
#define HTTP_CODE_NOT_MODIFIED   (304)
 
#define HTTP_CODE_BAD_REQUEST   (400)
 
#define HTTP_CODE_UNAUTHORIZED   (401)
 
#define HTTP_CODE_FORBIDDEN   (403)
 
#define HTTP_CODE_NOT_FOUND   (404)
 
#define HTTP_CODE_METHOD_NOT_ALLOWED   (405)
 
#define HTTP_CODE_REQUEST_TIME_OUT   (408)
 
#define HTTP_CODE_GONE   (410)
 
#define HTTP_CODE_REQUEST_URI_TOO_LONG   (414)
 
#define HTTP_CODE_LOCKED   (423)
 
#define HTTP_CODE_INTERNAL_SERVER_ERROR   (500)
 
#define HTTP_CODE_NOT_IMPLEMENTED   (501)
 
#define HTTP_CODE_SERVICE_UNAVAILABLE   (503)
 
#define HTTP_CRLF   "\r\n"
 
#define HTTP_DEF_CONTENTTYPE   "application/octet-stream"
 
#define AD_HOOK_ALL   (0)
 
#define AD_HOOK_ON_CONNECT   (1)
 
#define AD_HOOK_AFTER_REQUESTLINE   (1 << 2)
 
#define AD_HOOK_AFTER_HEADER   (1 << 3)
 
#define AD_HOOK_ON_BODY   (1 << 4)
 
#define AD_HOOK_ON_REQUEST   (1 << 5)
 
#define AD_HOOK_ON_CLOSE   (1 << 6)
 

Typedefs

typedef struct ad_http_s ad_http_t
 

Enumerations

enum  ad_http_request_status_e {
  AD_HTTP_REQ_INIT = 0, AD_HTTP_REQ_REQUESTLINE_DONE, AD_HTTP_REQ_HEADER_DONE, AD_HTTP_REQ_DONE,
  AD_HTTP_ERROR
}
 

Functions

int ad_http_handler (short event, ad_conn_t *conn, void *userdata)
 HTTP protocol handler hook. More...
 
enum ad_http_request_status_e ad_http_get_status (ad_conn_t *conn)
 Return the request status. More...
 
struct evbuffer * ad_http_get_inbuf (ad_conn_t *conn)
 
struct evbuffer * ad_http_get_outbuf (ad_conn_t *conn)
 
const char * ad_http_get_request_header (ad_conn_t *conn, const char *name)
 Get request header. More...
 
off_t ad_http_get_content_length (ad_conn_t *conn)
 Return the size of content from the request. More...
 
size_t ad_http_get_content_length_stored (ad_conn_t *conn)
 Return the actual size of data stored in in-buffer. More...
 
void * ad_http_get_content (ad_conn_t *conn, size_t maxsize, size_t *storedsize)
 Remove content from the in-buffer. More...
 
int ad_http_is_keepalive_request (ad_conn_t *conn)
 Return whether the request is keep-alive request or not. More...
 
int ad_http_set_response_header (ad_conn_t *conn, const char *name, const char *value)
 Set response header. More...
 
const char * ad_http_get_response_header (ad_conn_t *conn, const char *name)
 Get response header. More...
 
int ad_http_set_response_code (ad_conn_t *conn, int code, const char *reason)
 
int ad_http_set_response_content (ad_conn_t *conn, const char *contenttype, off_t size)
 
size_t ad_http_response (ad_conn_t *conn, int code, const char *contenttype, const void *data, off_t size)
 
size_t ad_http_send_header (ad_conn_t *conn)
 
size_t ad_http_send_data (ad_conn_t *conn, const void *data, size_t size)
 
size_t ad_http_send_chunk (ad_conn_t *conn, const void *data, size_t size)
 
const char * ad_http_get_reason (int code)
 

Detailed Description

ad_http_handler header file

Definition in file ad_http_handler.h.

Macro Definition Documentation

#define HTTP_PROTOCOL_09   "HTTP/0.9"

Definition at line 49 of file ad_http_handler.h.

#define HTTP_PROTOCOL_10   "HTTP/1.0"

Definition at line 50 of file ad_http_handler.h.

#define HTTP_PROTOCOL_11   "HTTP/1.1"

Definition at line 51 of file ad_http_handler.h.

#define HTTP_NO_RESPONSE   (0)

Definition at line 54 of file ad_http_handler.h.

#define HTTP_CODE_CONTINUE   (100)

Definition at line 55 of file ad_http_handler.h.

#define HTTP_CODE_OK   (200)

Definition at line 56 of file ad_http_handler.h.

#define HTTP_CODE_CREATED   (201)

Definition at line 57 of file ad_http_handler.h.

#define HTTP_CODE_NO_CONTENT   (204)

Definition at line 58 of file ad_http_handler.h.

#define HTTP_CODE_PARTIAL_CONTENT   (206)

Definition at line 59 of file ad_http_handler.h.

#define HTTP_CODE_MULTI_STATUS   (207)

Definition at line 60 of file ad_http_handler.h.

#define HTTP_CODE_MOVED_TEMPORARILY   (302)

Definition at line 61 of file ad_http_handler.h.

#define HTTP_CODE_NOT_MODIFIED   (304)

Definition at line 62 of file ad_http_handler.h.

#define HTTP_CODE_BAD_REQUEST   (400)

Definition at line 63 of file ad_http_handler.h.

#define HTTP_CODE_UNAUTHORIZED   (401)

Definition at line 64 of file ad_http_handler.h.

#define HTTP_CODE_FORBIDDEN   (403)

Definition at line 65 of file ad_http_handler.h.

#define HTTP_CODE_NOT_FOUND   (404)

Definition at line 66 of file ad_http_handler.h.

#define HTTP_CODE_METHOD_NOT_ALLOWED   (405)

Definition at line 67 of file ad_http_handler.h.

#define HTTP_CODE_REQUEST_TIME_OUT   (408)

Definition at line 68 of file ad_http_handler.h.

#define HTTP_CODE_GONE   (410)

Definition at line 69 of file ad_http_handler.h.

#define HTTP_CODE_REQUEST_URI_TOO_LONG   (414)

Definition at line 70 of file ad_http_handler.h.

#define HTTP_CODE_LOCKED   (423)

Definition at line 71 of file ad_http_handler.h.

#define HTTP_CODE_INTERNAL_SERVER_ERROR   (500)

Definition at line 72 of file ad_http_handler.h.

#define HTTP_CODE_NOT_IMPLEMENTED   (501)

Definition at line 73 of file ad_http_handler.h.

#define HTTP_CODE_SERVICE_UNAVAILABLE   (503)

Definition at line 74 of file ad_http_handler.h.

#define HTTP_CRLF   "\r\n"

Definition at line 77 of file ad_http_handler.h.

#define HTTP_DEF_CONTENTTYPE   "application/octet-stream"

Definition at line 78 of file ad_http_handler.h.

#define AD_HOOK_ALL   (0)

call on each and every phases

Definition at line 86 of file ad_http_handler.h.

#define AD_HOOK_ON_CONNECT   (1)

call right after the establishment of connection

Definition at line 87 of file ad_http_handler.h.

#define AD_HOOK_AFTER_REQUESTLINE   (1 << 2)

call after parsing request line

Definition at line 88 of file ad_http_handler.h.

#define AD_HOOK_AFTER_HEADER   (1 << 3)

call after parsing all headers

Definition at line 89 of file ad_http_handler.h.

#define AD_HOOK_ON_BODY   (1 << 4)

call on every time body data received

Definition at line 90 of file ad_http_handler.h.

#define AD_HOOK_ON_REQUEST   (1 << 5)

call with complete request

Definition at line 91 of file ad_http_handler.h.

#define AD_HOOK_ON_CLOSE   (1 << 6)

call right before closing or next request

Definition at line 92 of file ad_http_handler.h.

Typedef Documentation

typedef struct ad_http_s ad_http_t

Hook type

Definition at line 83 of file ad_http_handler.h.

Enumeration Type Documentation

Enumerator
AD_HTTP_REQ_INIT 

initial state

AD_HTTP_REQ_REQUESTLINE_DONE 

received 1st line

AD_HTTP_REQ_HEADER_DONE 

received headers completely

AD_HTTP_REQ_DONE 

received body completely. no more data expected

AD_HTTP_ERROR 

unrecoverable error found.

Definition at line 94 of file ad_http_handler.h.

Function Documentation

int ad_http_handler ( short  event,
ad_conn_t conn,
void *  userdata 
)

HTTP protocol handler hook.

This hook provides an easy way to handle HTTP request/response.

Note
This hook must be registered at the top of hook chain.

Definition at line 84 of file ad_http_handler.c.

enum ad_http_request_status_e ad_http_get_status ( ad_conn_t conn)

Return the request status.

Definition at line 116 of file ad_http_handler.c.

struct evbuffer* ad_http_get_inbuf ( ad_conn_t conn)

Definition at line 123 of file ad_http_handler.c.

struct evbuffer* ad_http_get_outbuf ( ad_conn_t conn)

Definition at line 128 of file ad_http_handler.c.

const char* ad_http_get_request_header ( ad_conn_t conn,
const char *  name 
)

Get request header.

Parameters
namename of header.
Returns
value of string if found, otherwise NULL.

Definition at line 140 of file ad_http_handler.c.

off_t ad_http_get_content_length ( ad_conn_t conn)

Return the size of content from the request.

Definition at line 148 of file ad_http_handler.c.

size_t ad_http_get_content_length_stored ( ad_conn_t conn)

Return the actual size of data stored in in-buffer.

Definition at line 157 of file ad_http_handler.c.

void* ad_http_get_content ( ad_conn_t conn,
size_t  maxsize,
size_t *  storedsize 
)

Remove content from the in-buffer.

The return data gets null terminated for convenience. For an example, if it reads 3 bytes, it will allocate 4 bytes and the 4th byte will be set to null terminator. storedsized will still return 3.

Parameters
maxsizemaximum length of data to read. 0 to read everything.
storedsizethe size of data read and stored in the return.

Definition at line 172 of file ad_http_handler.c.

int ad_http_is_keepalive_request ( ad_conn_t conn)

Return whether the request is keep-alive request or not.

Returns
1 if keep-alive request, otherwise 0.

Definition at line 199 of file ad_http_handler.c.

int ad_http_set_response_header ( ad_conn_t conn,
const char *  name,
const char *  value 
)

Set response header.

Parameters
namename of header.
valuevalue string to set. NULL to remove the header.
Returns
0 on success, -1 if we already sent it out.

Definition at line 231 of file ad_http_handler.c.

const char* ad_http_get_response_header ( ad_conn_t conn,
const char *  name 
)

Get response header.

Parameters
namename of header.
Returns
value of string if found, otherwise NULL.

Definition at line 254 of file ad_http_handler.c.

int ad_http_set_response_code ( ad_conn_t conn,
int  code,
const char *  reason 
)
Returns
0 on success, -1 if we already sent it out.

Definition at line 263 of file ad_http_handler.c.

int ad_http_set_response_content ( ad_conn_t conn,
const char *  contenttype,
off_t  size 
)
Parameters
sizecontent size. -1 for chunked transfer encoding.
Returns
0 on success, -1 if we already sent it out.

Definition at line 281 of file ad_http_handler.c.

size_t ad_http_response ( ad_conn_t conn,
int  code,
const char *  contenttype,
const void *  data,
off_t  size 
)
Returns
total bytes sent, 0 on error.

Definition at line 308 of file ad_http_handler.c.

size_t ad_http_send_header ( ad_conn_t conn)
Returns
0 total bytes put in out buffer, -1 if we already sent it out.

Definition at line 331 of file ad_http_handler.c.

size_t ad_http_send_data ( ad_conn_t conn,
const void *  data,
size_t  size 
)
Returns
0 on success, -1 if we already sent it out.

Definition at line 367 of file ad_http_handler.c.

size_t ad_http_send_chunk ( ad_conn_t conn,
const void *  data,
size_t  size 
)

Definition at line 394 of file ad_http_handler.c.

const char* ad_http_get_reason ( int  code)

Definition at line 428 of file ad_http_handler.c.