libasyncd
ad_http_handler.h
Go to the documentation of this file.
1 /******************************************************************************
2  * libasyncd
3  *
4  * Copyright (c) 2014 Seungyoung Kim.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
28 
29 /**
30  * ad_http_handler header file
31  *
32  * @file ad_http_handler.h
33  */
34 
35 #ifndef _AD_HTTP_HANDLER_H
36 #define _AD_HTTP_HANDLER_H
37 
38 #include "qlibc/qlibc.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /*----------------------------------------------------------------------------*\
45 | HTTP PROTOCOL SPECIFICS |
46 \*----------------------------------------------------------------------------*/
47 
48 /* HTTP PROTOCOL CODES */
49 #define HTTP_PROTOCOL_09 "HTTP/0.9"
50 #define HTTP_PROTOCOL_10 "HTTP/1.0"
51 #define HTTP_PROTOCOL_11 "HTTP/1.1"
52 
53 /* HTTP RESPONSE CODES */
54 #define HTTP_NO_RESPONSE (0)
55 #define HTTP_CODE_CONTINUE (100)
56 #define HTTP_CODE_OK (200)
57 #define HTTP_CODE_CREATED (201)
58 #define HTTP_CODE_NO_CONTENT (204)
59 #define HTTP_CODE_PARTIAL_CONTENT (206)
60 #define HTTP_CODE_MULTI_STATUS (207)
61 #define HTTP_CODE_MOVED_TEMPORARILY (302)
62 #define HTTP_CODE_NOT_MODIFIED (304)
63 #define HTTP_CODE_BAD_REQUEST (400)
64 #define HTTP_CODE_UNAUTHORIZED (401)
65 #define HTTP_CODE_FORBIDDEN (403)
66 #define HTTP_CODE_NOT_FOUND (404)
67 #define HTTP_CODE_METHOD_NOT_ALLOWED (405)
68 #define HTTP_CODE_REQUEST_TIME_OUT (408)
69 #define HTTP_CODE_GONE (410)
70 #define HTTP_CODE_REQUEST_URI_TOO_LONG (414)
71 #define HTTP_CODE_LOCKED (423)
72 #define HTTP_CODE_INTERNAL_SERVER_ERROR (500)
73 #define HTTP_CODE_NOT_IMPLEMENTED (501)
74 #define HTTP_CODE_SERVICE_UNAVAILABLE (503)
75 
76 /* DEFAULT BEHAVIORS */
77 #define HTTP_CRLF "\r\n"
78 #define HTTP_DEF_CONTENTTYPE "application/octet-stream"
79 
80 /*----------------------------------------------------------------------------*\
81 | TYPEDEFS |
82 \*----------------------------------------------------------------------------*/
83 typedef struct ad_http_s ad_http_t;
84 
85 /*!< Hook type */
86 #define AD_HOOK_ALL (0) /*!< call on each and every phases */
87 #define AD_HOOK_ON_CONNECT (1) /*!< call right after the establishment of connection */
88 #define AD_HOOK_AFTER_REQUESTLINE (1 << 2) /*!< call after parsing request line */
89 #define AD_HOOK_AFTER_HEADER (1 << 3) /*!< call after parsing all headers */
90 #define AD_HOOK_ON_BODY (1 << 4) /*!< call on every time body data received */
91 #define AD_HOOK_ON_REQUEST (1 << 5) /*!< call with complete request */
92 #define AD_HOOK_ON_CLOSE (1 << 6) /*!< call right before closing or next request */
93 
95  AD_HTTP_REQ_INIT = 0, /*!< initial state */
96  AD_HTTP_REQ_REQUESTLINE_DONE,/*!< received 1st line */
97  AD_HTTP_REQ_HEADER_DONE, /*!< received headers completely */
98  AD_HTTP_REQ_DONE, /*!< received body completely. no more data expected */
99 
100  AD_HTTP_ERROR, /*!< unrecoverable error found. */
101 };
102 
103 /*----------------------------------------------------------------------------*\
104 | PUBLIC FUNCTIONS |
105 \*----------------------------------------------------------------------------*/
106 extern int ad_http_handler(short event, ad_conn_t *conn, void *userdata);
107 
109 extern struct evbuffer *ad_http_get_inbuf(ad_conn_t *conn);
110 extern struct evbuffer *ad_http_get_outbuf(ad_conn_t *conn);
111 
112 extern const char *ad_http_get_request_header(ad_conn_t *conn, const char *name);
113 extern off_t ad_http_get_content_length(ad_conn_t *conn);
114 extern size_t ad_http_get_content_length_stored(ad_conn_t *conn);
115 extern void *ad_http_get_content(ad_conn_t *conn, size_t maxsize, size_t *storedsize);
116 extern int ad_http_is_keepalive_request(ad_conn_t *conn);
117 
118 extern int ad_http_set_response_header(ad_conn_t *conn, const char *name, const char *value);
119 extern const char *ad_http_get_response_header(ad_conn_t *conn, const char *name);
120 extern int ad_http_set_response_code(ad_conn_t *conn, int code, const char *reason);
121 extern int ad_http_set_response_content(ad_conn_t *conn, const char *contenttype, off_t size);
122 
123 extern size_t ad_http_response(ad_conn_t *conn, int code, const char *contenttype, const void *data, off_t size);
124 extern size_t ad_http_send_header(ad_conn_t *conn);
125 extern size_t ad_http_send_data(ad_conn_t *conn, const void *data, size_t size);
126 extern size_t ad_http_send_chunk(ad_conn_t *conn, const void *data, size_t size);
127 
128 extern const char *ad_http_get_reason(int code);
129 
130 /*---------------------------------------------------------------------------*\
131 | DATA STRUCTURES |
132 \*---------------------------------------------------------------------------*/
133 struct ad_http_s {
134  // HTTP Request
135  struct {
136  enum ad_http_request_status_e status; /*!< request status. */
137  struct evbuffer *inbuf; /*!< input data buffer. */
138 
139  // request line - available on REQ_REQUESTLINE_DONE.
140  char *method; /*!< request method ex) GET */
141  char *uri; /*!< url+query ex) /data%20path?query=the%20value */
142  char *httpver; /*!< version ex) HTTP/1.1 */
143  char *path; /*!< decoded path ex) /data path */
144  char *query; /*!< query string ex) query=the%20value */
145 
146  // request header - available on REQ_HEADER_DONE.
147  qlisttbl_t *headers; /*!< parsed request header entries */
148  char *host; /*!< host ex) www.domain.com or www.domain.com:8080 */
149  char *domain; /*!< domain name ex) www.domain.com (no port number) */
150  off_t contentlength; /*!< value of Content-Length header.*/
151  size_t bodyin; /*!< bytes moved to in-buff */
152  } request;
153 
154  // HTTP Response
155  struct {
156  struct evbuffer *outbuf; /*!< output data buffer. */
157  bool frozen_header; /*!< indicator whether we sent header out or not */
158 
159  // response headers
160  int code; /*!< response status-code */
161  char *reason; /*!< reason-phrase */
162  qlisttbl_t *headers; /*!< response header entries */
163  off_t contentlength; /*!< content length in response */
164  size_t bodyout; /*!< bytes added to out-buffer */
165  } response;
166 };
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /*_AD_HTTP_HANDLER_H */
struct evbuffer * outbuf
int ad_http_handler(short event, ad_conn_t *conn, void *userdata)
HTTP protocol handler hook.
size_t ad_http_get_content_length_stored(ad_conn_t *conn)
Return the actual size of data stored in in-buffer.
const char * ad_http_get_response_header(ad_conn_t *conn, const char *name)
Get response header.
struct evbuffer * ad_http_get_outbuf(ad_conn_t *conn)
enum ad_http_request_status_e ad_http_get_status(ad_conn_t *conn)
Return the request status.
size_t ad_http_send_chunk(ad_conn_t *conn, const void *data, size_t size)
off_t ad_http_get_content_length(ad_conn_t *conn)
Return the size of content from the request.
size_t ad_http_send_header(ad_conn_t *conn)
bool frozen_header
struct ad_http_s::@1 response
char * httpver
struct evbuffer * ad_http_get_inbuf(ad_conn_t *conn)
int ad_http_set_response_header(ad_conn_t *conn, const char *name, const char *value)
Set response header.
size_t ad_http_send_data(ad_conn_t *conn, const void *data, size_t size)
size_t bodyout
enum ad_http_request_status_e status
Connection structure.
Definition: ad_server.h:159
int ad_http_is_keepalive_request(ad_conn_t *conn)
Return whether the request is keep-alive request or not.
qlisttbl_t * headers
int ad_http_set_response_content(ad_conn_t *conn, const char *contenttype, off_t size)
ad_http_request_status_e
size_t ad_http_response(ad_conn_t *conn, int code, const char *contenttype, const void *data, off_t size)
const char * ad_http_get_reason(int code)
const char * ad_http_get_request_header(ad_conn_t *conn, const char *name)
Get request header.
void * ad_http_get_content(ad_conn_t *conn, size_t maxsize, size_t *storedsize)
Remove content from the in-buffer.
off_t contentlength
struct evbuffer * inbuf
struct ad_http_s::@0 request
int ad_http_set_response_code(ad_conn_t *conn, int code, const char *reason)