1 |
/* |
2 |
http-server.h - generic HTTP server |
3 |
Copyright (C) 2008 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#ifndef HTTP_SERVER_H_ |
21 |
#define HTTP_SERVER_H_ |
22 |
|
23 |
#include <stdbool.h> |
24 |
#include <stdint.h> |
25 |
#include <stdlib.h> |
26 |
|
27 |
#ifdef _WIN32 |
28 |
#include <winsock2.h> |
29 |
#else |
30 |
#include <netinet/in.h> |
31 |
typedef int SOCKET; |
32 |
#endif |
33 |
|
34 |
#include "stream.h" |
35 |
|
36 |
#define HTTP_ACCEPT "Accept" |
37 |
#define HTTP_ACCEPT_CHARSET "Accept-Charset" |
38 |
#define HTTP_ACCEPT_ENCODING "Accept-Encoding" |
39 |
#define HTTP_ACCEPT_LANGUAGE "Accept-Language" |
40 |
#define HTTP_ACCEPT_RANGES "Accept-Ranges" |
41 |
#define HTTP_AGE "Age" |
42 |
#define HTTP_ALLOW "Allow" |
43 |
#define HTTP_AUTHORIZATION "Authorization" |
44 |
#define HTTP_CACHE_CONTROL "Cache-Control" |
45 |
#define HTTP_CONNECTION "Connection" |
46 |
#define HTTP_CONTENT_ENCODING "Content-Encoding" |
47 |
#define HTTP_CONTENT_LANGUAGE "Content-Language" |
48 |
#define HTTP_CONTENT_LENGTH "Content-Length" |
49 |
#define HTTP_CONTENT_LOCATION "Content-Location" |
50 |
#define HTTP_CONTENT_MD5 "Content-MD5" |
51 |
#define HTTP_CONTENT_RANGE "Content-Range" |
52 |
#define HTTP_CONTENT_TYPE "Content-Type" |
53 |
#define HTTP_DATE "Date" |
54 |
#define HTTP_ETAG "ETag" |
55 |
#define HTTP_EXPECT "Expect" |
56 |
#define HTTP_EXPIRES "Expires" |
57 |
#define HTTP_FROM "From" |
58 |
#define HTTP_HOST "Host" |
59 |
#define HTTP_IF_MATCH "If-Match" |
60 |
#define HTTP_IF_MODIFIED_SINCE "If-Modified-Since" |
61 |
#define HTTP_IF_NONE_MATCH "If-None-Match" |
62 |
#define HTTP_IF_RANGE "If-Range" |
63 |
#define HTTP_IF_UNMODIFIED_SINCE "If-Unmodified-Since" |
64 |
#define HTTP_LAST_MODIFIED "Last-Modified" |
65 |
#define HTTP_LOCATION "Location" |
66 |
#define HTTP_MAX_FORWARDS "Max-Forwards" |
67 |
#define HTTP_PRAGMA "Pragma" |
68 |
#define HTTP_PROXY_AUTHENTICATE "Proxy-Authenticate" |
69 |
#define HTTP_PROXY_AUTHORIZATION "Proxy-Authorization" |
70 |
#define HTTP_RANGE "Range" |
71 |
#define HTTP_REFERER "Referer" |
72 |
#define HTTP_RETRY_AFTER "Retry-After" |
73 |
#define HTTP_SERVER "Server" |
74 |
#define HTTP_TE "TE" |
75 |
#define HTTP_TRAILER "Trailer" |
76 |
#define HTTP_TRANSFER_ENCODING "Transfer-Encoding" |
77 |
#define HTTP_UPGRADE "Upgrade" |
78 |
#define HTTP_USER_AGENT "User-Agent" |
79 |
#define HTTP_VARY "Vary" |
80 |
#define HTTP_VIA "Via" |
81 |
#define HTTP_WARNING "Warning" |
82 |
#define HTTP_WWW_AUTHENTICATE "WWW-Authenticate" |
83 |
|
84 |
typedef struct HTTPHeader { |
85 |
char * name; |
86 |
char * value; |
87 |
struct HTTPHeader * next; |
88 |
} HTTPHeader; |
89 |
|
90 |
typedef struct HTTPMessage HTTPMessage; |
91 |
|
92 |
typedef struct HTTPExchange HTTPExchange; |
93 |
|
94 |
typedef struct HTTPConnection HTTPConnection; |
95 |
|
96 |
typedef void (*HTTPServerHandler)(HTTPExchange * exchange); |
97 |
|
98 |
/* HTTPConnection */ |
99 |
HTTPConnection * HTTPConnection_new_server(SOCKET s); |
100 |
HTTPConnection * HTTPConnection_new_client(const char * host, uint16_t port) __attribute__((warn_unused_result)); |
101 |
int HTTPConnection_delete(HTTPConnection * connection) __attribute__((warn_unused_result)); |
102 |
int HTTPConnection_get_peer(HTTPConnection * connection, struct sockaddr_in * peer) __attribute__((warn_unused_result)); |
103 |
int HTTPConnection_read_octet(HTTPConnection * connection, int * octet) __attribute__((warn_unused_result)); |
104 |
int HTTPConnection_peek_octet(HTTPConnection * connection, int * octet) __attribute__((warn_unused_result)); |
105 |
int HTTPConnection_write(HTTPConnection * connection, const void * p, size_t size) __attribute__((warn_unused_result)); |
106 |
int HTTPConnection_flush(HTTPConnection * connection) __attribute__((warn_unused_result)); |
107 |
|
108 |
/* HTTPMessage */ |
109 |
HTTPMessage * HTTPMessage_new(HTTPConnection * connection); |
110 |
void HTTPMessage_delete(HTTPMessage * message); |
111 |
HTTPConnection * HTTPMessage_get_connection(const HTTPMessage * message); |
112 |
const char * HTTPMessage_get_start_line(const HTTPMessage * message); |
113 |
void HTTPMessage_set_start_line(HTTPMessage * message, const char * start_line); |
114 |
const HTTPHeader * HTTPMessage_get_headers(const HTTPMessage * message); |
115 |
const char * HTTPMessage_find_header(const HTTPMessage * message, const char * name); |
116 |
void HTTPMessage_add_header(HTTPMessage * message, const char * name, const char * value); |
117 |
void HTTPMessage_set_header(HTTPMessage * message, const char * name, const char * value); |
118 |
void HTTPMessage_set_content_length(HTTPMessage * message, size_t value); |
119 |
int HTTPMessage_read_start_line_and_headers(HTTPMessage * message) __attribute__((warn_unused_result)); |
120 |
int HTTPMessage_write_start_line_and_headers(HTTPMessage * message) __attribute__((warn_unused_result)); |
121 |
bool HTTPMessage_has_sent_headers(const HTTPMessage * message); |
122 |
int HTTPMessage_write(HTTPMessage * message, const void * p, size_t size) __attribute__((warn_unused_result)); |
123 |
int HTTPMessage_flush(HTTPMessage * message) __attribute__((warn_unused_result)); |
124 |
|
125 |
/* |
126 |
This function reads the entire entity body from a message. If the message uses |
127 |
the "chunked" Transfer-Encoding, this function will decode it, so that the |
128 |
result will be the original entity. |
129 |
*/ |
130 |
int HTTPMessage_read_entire_entity_body(HTTPMessage * message, Stream * input_stream) __attribute__((warn_unused_result)); |
131 |
|
132 |
/* |
133 |
This function makes no attempt to decode the Transfer-Encoding. |
134 |
*/ |
135 |
int HTTPMessage_read_message_body(HTTPMessage * message, void * p, size_t capacity, size_t * bytes_read) __attribute__((warn_unused_result)); |
136 |
|
137 |
/* HTTPExchange */ |
138 |
HTTPExchange * HTTPExchange_new(HTTPConnection * connection); |
139 |
void HTTPExchange_delete(HTTPExchange * exchange); |
140 |
int HTTPExchange_get_peer(const HTTPExchange * exchange, struct sockaddr_in * peer) __attribute__((warn_unused_result)); |
141 |
|
142 |
HTTPMessage * HTTPExchange_get_request_message(const HTTPExchange * exchange); |
143 |
|
144 |
const char * HTTPExchange_get_request_line(const HTTPExchange * exchange); |
145 |
const char * HTTPExchange_get_method(const HTTPExchange * exchange); |
146 |
const char * HTTPExchange_get_request_uri(const HTTPExchange * exchange); |
147 |
const char * HTTPExchange_get_request_http_version(const HTTPExchange * exchange); |
148 |
const char * HTTPExchange_get_host(const HTTPExchange * exchange); |
149 |
uint16_t HTTPExchange_get_port(const HTTPExchange * exchange); |
150 |
const char * HTTPExchange_get_abs_path(const HTTPExchange * exchange); |
151 |
void HTTPExchange_set_method(HTTPExchange * exchange, const char * method); |
152 |
void HTTPExchange_set_request_uri(HTTPExchange * exchange, const char * request_uri); |
153 |
|
154 |
const HTTPHeader * HTTPExchange_get_request_headers(const HTTPExchange * exchange); |
155 |
const char * HTTPExchange_find_request_header(const HTTPExchange * exchange, const char * name); |
156 |
void HTTPExchange_add_request_header(HTTPExchange * exchange, const char * name, const char * value); |
157 |
void HTTPExchange_set_request_header(HTTPExchange * exchange, const char * name, const char * value); |
158 |
void HTTPExchange_set_request_content_length(HTTPExchange * exchange, size_t value); |
159 |
|
160 |
int HTTPExchange_read_request_headers(HTTPExchange * exchange) __attribute__((warn_unused_result)); |
161 |
int HTTPExchange_write_request_headers(HTTPExchange * exchange) __attribute__((warn_unused_result)); |
162 |
bool HTTPExchange_request_has_body(const HTTPExchange * exchange); |
163 |
int HTTPExchange_read_entire_request_entity_body(HTTPExchange * exchange, Stream * stream) __attribute__((warn_unused_result)); |
164 |
int HTTPExchange_flush_request(HTTPExchange * exchange) __attribute__((warn_unused_result)); |
165 |
|
166 |
HTTPMessage * HTTPExchange_get_response_message(const HTTPExchange * exchange); |
167 |
|
168 |
uint16_t HTTPExchange_get_status_code(const HTTPExchange * exchange); |
169 |
const char * HTTPExchange_get_response_http_version(const HTTPExchange * exchange); |
170 |
void HTTPExchange_set_status_code(HTTPExchange * exchange, uint16_t status_code); |
171 |
|
172 |
const HTTPHeader * HTTPExchange_get_response_headers(const HTTPExchange * exchange); |
173 |
const char * HTTPExchange_find_response_header(const HTTPExchange * exchange, const char * name); |
174 |
void HTTPExchange_add_response_header(HTTPExchange * exchange, const char * name, const char * value); |
175 |
void HTTPExchange_set_response_header(HTTPExchange * exchange, const char * name, const char * value); |
176 |
void HTTPExchange_set_response_content_length(HTTPExchange * exchange, size_t value); |
177 |
|
178 |
int HTTPExchange_read_response_headers(HTTPExchange * exchange) __attribute__((warn_unused_result)); |
179 |
int HTTPExchange_write_response_headers(HTTPExchange * exchange) __attribute__((warn_unused_result)); |
180 |
bool HTTPExchange_response_has_body(const HTTPExchange * exchange); |
181 |
int HTTPExchange_read_entire_response_entity_body(HTTPExchange * exchange, Stream * stream) __attribute__((warn_unused_result)); |
182 |
int HTTPExchange_write_response(HTTPExchange * exchange, const void * p, size_t size) __attribute__((warn_unused_result)); |
183 |
int HTTPExchange_flush_response(HTTPExchange * exchange) __attribute__((warn_unused_result)); |
184 |
|
185 |
void HTTPServer_run(const char * ip_address, uint16_t port, HTTPServerHandler handler); |
186 |
void HTTPServer_shutdown(void); |
187 |
void HTTPServer_log_out(const char * format, ...) __attribute__((__format__(printf, 1, 2))); |
188 |
void HTTPServer_log_err(const char * format, ...) __attribute__((__format__(printf, 1, 2))); |
189 |
|
190 |
int URL_parse(const char * url, char ** host, uint16_t * port, char ** abs_path, char ** query) __attribute__((warn_unused_result)); |
191 |
int URL_parse_host_and_port(const char * s, char ** host, uint16_t * port) __attribute__((warn_unused_result)); |
192 |
int URL_parse_abs_path_and_query(const char * s, char ** abs_path, char ** query) __attribute__((warn_unused_result)); |
193 |
|
194 |
int xgethostbyname(const char * host, struct in_addr * result) __attribute__((warn_unused_result)); |
195 |
|
196 |
#endif /* HTTP_SERVER_H_ */ |