1 |
/* libhttpd.h - defines for libhttpd |
2 |
** |
3 |
** Copyright © 1995,1998,1999,2000,2001 by Jef Poskanzer <jef@acme.com>. |
4 |
** All rights reserved. |
5 |
** |
6 |
** Redistribution and use in source and binary forms, with or without |
7 |
** modification, are permitted provided that the following conditions |
8 |
** are met: |
9 |
** 1. Redistributions of source code must retain the above copyright |
10 |
** notice, this list of conditions and the following disclaimer. |
11 |
** 2. Redistributions in binary form must reproduce the above copyright |
12 |
** notice, this list of conditions and the following disclaimer in the |
13 |
** documentation and/or other materials provided with the distribution. |
14 |
** |
15 |
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
** SUCH DAMAGE. |
26 |
*/ |
27 |
|
28 |
#ifndef _LIBHTTPD_H_ |
29 |
#define _LIBHTTPD_H_ |
30 |
|
31 |
#include <sys/types.h> |
32 |
#include <sys/time.h> |
33 |
#include <sys/param.h> |
34 |
#include <sys/socket.h> |
35 |
#include <netinet/in.h> |
36 |
#include <arpa/inet.h> |
37 |
#include <netdb.h> |
38 |
|
39 |
|
40 |
/* A few convenient defines. */ |
41 |
|
42 |
#ifndef MAX |
43 |
#define MAX(a,b) ((a) > (b) ? (a) : (b)) |
44 |
#endif |
45 |
#ifndef MIN |
46 |
#define MIN(a,b) ((a) < (b) ? (a) : (b)) |
47 |
#endif |
48 |
#define NEW(t,n) ((t*) malloc( sizeof(t) * (n) )) |
49 |
#define RENEW(o,t,n) ((t*) realloc( (void*) o, sizeof(t) * (n) )) |
50 |
|
51 |
|
52 |
/* The httpd structs. */ |
53 |
|
54 |
/* A multi-family sockaddr. */ |
55 |
typedef union { |
56 |
struct sockaddr sa; |
57 |
struct sockaddr_in sa_in; |
58 |
#ifdef HAVE_SOCKADDR_IN6 |
59 |
struct sockaddr_in6 sa_in6; |
60 |
#endif /* HAVE_SOCKADDR_IN6 */ |
61 |
#ifdef HAVE_SOCKADDR_STORAGE |
62 |
struct sockaddr_storage sa_stor; |
63 |
#endif /* HAVE_SOCKADDR_STORAGE */ |
64 |
} httpd_sockaddr; |
65 |
|
66 |
/* A server. */ |
67 |
typedef struct { |
68 |
char* binding_hostname; |
69 |
char* server_hostname; |
70 |
int port; |
71 |
char* cgi_pattern; |
72 |
char* charset; |
73 |
char* cwd; |
74 |
int listen4_fd, listen6_fd; |
75 |
int no_log; |
76 |
FILE* logfp; |
77 |
int no_symlink; |
78 |
int vhost; |
79 |
int global_passwd; |
80 |
char* url_pattern; |
81 |
char* local_pattern; |
82 |
int no_empty_referers; |
83 |
} httpd_server; |
84 |
|
85 |
/* A connection. */ |
86 |
typedef struct { |
87 |
int initialized; |
88 |
httpd_server* hs; |
89 |
httpd_sockaddr client_addr; |
90 |
char* read_buf; |
91 |
int read_size, read_idx, checked_idx; |
92 |
int checked_state; |
93 |
int method; |
94 |
int status; |
95 |
off_t bytes_to_send; |
96 |
off_t bytes_sent; |
97 |
char* encodedurl; |
98 |
char* decodedurl; |
99 |
char* protocol; |
100 |
char* origfilename; |
101 |
char* expnfilename; |
102 |
char* encodings; |
103 |
char* pathinfo; |
104 |
char* query; |
105 |
char* referer; |
106 |
char* useragent; |
107 |
char* accept; |
108 |
char* accepte; |
109 |
char* acceptl; |
110 |
char* cookie; |
111 |
char* contenttype; |
112 |
char* reqhost; |
113 |
char* hdrhost; |
114 |
char* hostdir; |
115 |
char* authorization; |
116 |
char* remoteuser; |
117 |
char* response; |
118 |
int maxdecodedurl, maxorigfilename, maxexpnfilename, maxencodings, |
119 |
maxpathinfo, maxquery, maxaccept, maxaccepte, maxreqhost, maxhostdir, |
120 |
maxremoteuser, maxresponse; |
121 |
#ifdef TILDE_MAP_2 |
122 |
char* altdir; |
123 |
int maxaltdir; |
124 |
#endif /* TILDE_MAP_2 */ |
125 |
int responselen; |
126 |
time_t if_modified_since, range_if; |
127 |
off_t contentlength; |
128 |
char* type; /* not malloc()ed */ |
129 |
char* hostname; /* not malloc()ed */ |
130 |
int mime_flag; |
131 |
int one_one; /* HTTP/1.1 or better */ |
132 |
int got_range; |
133 |
int tildemapped; /* this connection got tilde-mapped */ |
134 |
off_t init_byte_loc, end_byte_loc; |
135 |
int keep_alive; |
136 |
int should_linger; |
137 |
struct stat sb; |
138 |
int conn_fd; |
139 |
char* file_address; |
140 |
} httpd_conn; |
141 |
|
142 |
/* Methods. */ |
143 |
#define METHOD_UNKNOWN 0 |
144 |
#define METHOD_GET 1 |
145 |
#define METHOD_HEAD 2 |
146 |
#define METHOD_POST 3 |
147 |
|
148 |
/* States for checked_state. */ |
149 |
#define CHST_FIRSTWORD 0 |
150 |
#define CHST_FIRSTWS 1 |
151 |
#define CHST_SECONDWORD 2 |
152 |
#define CHST_SECONDWS 3 |
153 |
#define CHST_THIRDWORD 4 |
154 |
#define CHST_THIRDWS 5 |
155 |
#define CHST_LINE 6 |
156 |
#define CHST_LF 7 |
157 |
#define CHST_CR 8 |
158 |
#define CHST_CRLF 9 |
159 |
#define CHST_CRLFCR 10 |
160 |
#define CHST_BOGUS 11 |
161 |
|
162 |
|
163 |
/* Initializes. Does the socket(), bind(), and listen(). Returns an |
164 |
** httpd_server* which includes a socket fd that you can select() on. |
165 |
** Return (httpd_server*) 0 on error. |
166 |
*/ |
167 |
extern httpd_server* httpd_initialize( |
168 |
char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port, |
169 |
char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp, |
170 |
int no_symlink, int vhost, int global_passwd, char* url_pattern, |
171 |
char* local_pattern, int no_empty_referers ); |
172 |
|
173 |
/* Change the log file. */ |
174 |
extern void httpd_set_logfp( httpd_server* hs, FILE* logfp ); |
175 |
|
176 |
/* Call to shut down. */ |
177 |
extern void httpd_terminate( httpd_server* hs ); |
178 |
|
179 |
|
180 |
/* When a listen fd is ready to read, call this. It does the accept() and |
181 |
** returns an httpd_conn* which includes the fd to read the request from and |
182 |
** write the response to. Returns an indication of whether the accept() |
183 |
** failed, succeeded, or if there were no more connections to accept. |
184 |
** |
185 |
** In order to minimize malloc()s, the caller passes in the httpd_conn. |
186 |
** The caller is also responsible for setting initialized to zero before the |
187 |
** first call using each different httpd_conn. |
188 |
*/ |
189 |
extern int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc ); |
190 |
#define GC_FAIL 0 |
191 |
#define GC_OK 1 |
192 |
#define GC_NO_MORE 2 |
193 |
|
194 |
/* Checks whether the data in hc->read_buf constitutes a complete request |
195 |
** yet. The caller reads data into hc->read_buf[hc->read_idx] and advances |
196 |
** hc->read_idx. This routine checks what has been read so far, using |
197 |
** hc->checked_idx and hc->checked_state to keep track, and returns an |
198 |
** indication of whether there is no complete request yet, there is a |
199 |
** complete request, or there won't be a valid request due to a syntax error. |
200 |
*/ |
201 |
extern int httpd_got_request( httpd_conn* hc ); |
202 |
#define GR_NO_REQUEST 0 |
203 |
#define GR_GOT_REQUEST 1 |
204 |
#define GR_BAD_REQUEST 2 |
205 |
|
206 |
/* Parses the request in hc->read_buf. Fills in lots of fields in hc, |
207 |
** like the URL and the various headers. |
208 |
** |
209 |
** Returns -1 on error. |
210 |
*/ |
211 |
extern int httpd_parse_request( httpd_conn* hc ); |
212 |
|
213 |
/* Starts sending data back to the client. In some cases (directories, |
214 |
** CGI programs), finishes sending by itself - in those cases, hc->file_fd |
215 |
** is <0. If there is more data to be sent, then hc->file_fd is a file |
216 |
** descriptor for the file to send. If you don't have a current timeval |
217 |
** handy just pass in 0. |
218 |
** |
219 |
** Returns -1 on error. |
220 |
*/ |
221 |
extern int httpd_start_request( httpd_conn* hc, struct timeval* nowP ); |
222 |
|
223 |
/* Actually sends any buffered response text. */ |
224 |
extern void httpd_write_response( httpd_conn* hc ); |
225 |
|
226 |
/* Call this to close down a connection and free the data. A fine point, |
227 |
** if you fork() with a connection open you should still call this in the |
228 |
** parent process - the connection will stay open in the child. |
229 |
** If you don't have a current timeval handy just pass in 0. |
230 |
*/ |
231 |
extern void httpd_close_conn( httpd_conn* hc, struct timeval* nowP ); |
232 |
|
233 |
/* Call this to de-initialize a connection struct and *really* free the |
234 |
** mallocced strings. |
235 |
*/ |
236 |
extern void httpd_destroy_conn( httpd_conn* hc ); |
237 |
|
238 |
|
239 |
/* Send an error message back to the client. */ |
240 |
extern void httpd_send_err( |
241 |
httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ); |
242 |
|
243 |
extern void httpd_send_err_blocked( |
244 |
httpd_conn* hc); |
245 |
|
246 |
|
247 |
/* Some error messages. */ |
248 |
extern char* httpd_err400title; |
249 |
extern char* httpd_err400form; |
250 |
extern char* httpd_err408title; |
251 |
extern char* httpd_err408form; |
252 |
extern char* httpd_err503title; |
253 |
extern char* httpd_err503form; |
254 |
|
255 |
/* Generate a string representation of a method number. */ |
256 |
extern char* httpd_method_str( int method ); |
257 |
|
258 |
/* Reallocate a string. */ |
259 |
extern void httpd_realloc_str( char** strP, int* maxsizeP, int size ); |
260 |
|
261 |
/* Format a network socket to a string representation. */ |
262 |
extern char* httpd_ntoa( httpd_sockaddr* saP ); |
263 |
|
264 |
/* Set NDELAY mode on a socket. */ |
265 |
void httpd_set_ndelay( int fd ); |
266 |
|
267 |
/* Clear NDELAY mode on a socket. */ |
268 |
void httpd_clear_ndelay( int fd ); |
269 |
|
270 |
/* Generate debugging statistics syslog message. */ |
271 |
extern void httpd_logstats( long secs ); |
272 |
|
273 |
#endif /* _LIBHTTPD_H_ */ |