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 void *ad_http_get_content(ad_conn_t *conn, size_t maxsize, size_t *storedsize);
115 
116 extern int ad_http_add_response_header(ad_conn_t *conn, const char *name, const char *value);
117 extern int ad_http_set_response_code(ad_conn_t *conn, int code, const char *reason);
118 extern int ad_http_set_response_content(ad_conn_t *conn, const char *contenttype, off_t size);
119 
120 extern size_t ad_http_response(ad_conn_t *conn, int code, const char *contenttype, const void *data, off_t size);
121 extern size_t ad_http_send_header(ad_conn_t *conn);
122 extern size_t ad_http_send_data(ad_conn_t *conn, const void *data, size_t size);
123 extern size_t ad_http_send_chunk(ad_conn_t *conn, const void *data, size_t size);
124 
125 extern const char *ad_http_get_reason(int code);
126 
127 /*---------------------------------------------------------------------------*\
128 | DATA STRUCTURES |
129 \*---------------------------------------------------------------------------*/
130 struct ad_http_s {
131  // HTTP Request
132  struct {
133  enum ad_http_request_status_e status; /*!< request status. */
134  struct evbuffer *inbuf; /*!< input data buffer. */
135 
136  // request line - available on REQ_REQUESTLINE_DONE.
137  char *method; /*!< request method ex) GET */
138  char *uri; /*!< url+query ex) /data%20path?query=the%20value */
139  char *httpver; /*!< version ex) HTTP/1.1 */
140  char *path; /*!< decoded path ex) /data path */
141  char *query; /*!< query string ex) query=the%20value */
142 
143  // request header - available on REQ_HEADER_DONE.
144  qlisttbl_t *headers; /*!< parsed request header entries */
145  char *host; /*!< host ex) www.domain.com or www.domain.com:8080 */
146  char *domain; /*!< domain name ex) www.domain.com (no port number) */
147  off_t contentlength; /*!< value of Content-Length header.*/
148  size_t bodyin; /*!< bytes moved to in-buff */
149  } request;
150 
151  // HTTP Response
152  struct {
153  struct evbuffer *outbuf; /*!< output data buffer. */
154  bool frozen_header; /*!< indicator whether we sent header out or not */
155 
156  // response headers
157  int code; /*!< response status-code */
158  char *reason; /*!< reason-phrase */
159  qlisttbl_t *headers; /*!< response header entries */
160  off_t contentlength; /*!< content length in response */
161  size_t bodyout; /*!< bytes added to out-buffer */
162  } response;
163 };
164 
165 /*---------------------------------------------------------------------------*\
166 | INTERNAL USE ONLY |
167 \*---------------------------------------------------------------------------*/
168 #ifndef _DOXYGEN_SKIP
169 #endif /* _DOXYGEN_SKIP */
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif /*_AD_HTTP_HANDLER_H */