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 |
/* A few convenient defines. */ |
40 |
|
41 |
#ifndef MAX |
42 |
#define MAX(a,b) ((a) > (b) ? (a) : (b)) |
43 |
#endif |
44 |
#ifndef MIN |
45 |
#define MIN(a,b) ((a) < (b) ? (a) : (b)) |
46 |
#endif |
47 |
#define NEW(t,n) ((t*) malloc( sizeof(t) * (n) )) |
48 |
#define RENEW(o,t,n) ((t*) realloc( (void*) o, sizeof(t) * (n) )) |
49 |
|
50 |
|
51 |
/* The httpd structs. */ |
52 |
|
53 |
/* A multi-family sockaddr. */ |
54 |
typedef union { |
55 |
struct sockaddr sa; |
56 |
struct sockaddr_in sa_in; |
57 |
#ifdef HAVE_SOCKADDR_IN6 |
58 |
struct sockaddr_in6 sa_in6; |
59 |
#endif /* HAVE_SOCKADDR_IN6 */ |
60 |
#ifdef HAVE_SOCKADDR_STORAGE |
61 |
struct sockaddr_storage sa_stor; |
62 |
#endif /* HAVE_SOCKADDR_STORAGE */ |
63 |
} httpd_sockaddr; |
64 |
|
65 |
/* A server. */ |
66 |
typedef struct { |
67 |
char* binding_hostname; |
68 |
char* server_hostname; |
69 |
int port; |
70 |
char* cgi_pattern; |
71 |
char* charset; |
72 |
char* cwd; |
73 |
int listen4_fd, listen6_fd; |
74 |
int no_log; |
75 |
FILE* logfp; |
76 |
int no_symlink; |
77 |
int vhost; |
78 |
int global_passwd; |
79 |
char* url_pattern; |
80 |
char* local_pattern; |
81 |
int no_empty_referers; |
82 |
} httpd_server; |
83 |
|
84 |
/* A connection. */ |
85 |
typedef struct { |
86 |
int initialized; |
87 |
httpd_server* hs; |
88 |
httpd_sockaddr client_addr; |
89 |
char* read_buf; |
90 |
int read_size, read_idx, checked_idx; |
91 |
int checked_state; |
92 |
int method; |
93 |
int status; |
94 |
off_t bytes_to_send; |
95 |
off_t bytes_sent; |
96 |
char* encodedurl; |
97 |
char* decodedurl; |
98 |
char* protocol; |
99 |
char* origfilename; |
100 |
char* expnfilename; |
101 |
char* encodings; |
102 |
char* pathinfo; |
103 |
char* query; |
104 |
char* referer; |
105 |
char* useragent; |
106 |
char* accept; |
107 |
char* accepte; |
108 |
char* acceptl; |
109 |
char* cookie; |
110 |
char* contenttype; |
111 |
char* reqhost; |
112 |
char* hdrhost; |
113 |
char* hostdir; |
114 |
char* authorization; |
115 |
char* remoteuser; |
116 |
char* response; |
117 |
int maxdecodedurl, maxorigfilename, maxexpnfilename, maxencodings, |
118 |
maxpathinfo, maxquery, maxaccept, maxaccepte, maxreqhost, maxhostdir, |
119 |
maxremoteuser, maxresponse; |
120 |
#ifdef TILDE_MAP_2 |
121 |
char* altdir; |
122 |
int maxaltdir; |
123 |
#endif /* TILDE_MAP_2 */ |
124 |
int responselen; |
125 |
time_t if_modified_since, range_if; |
126 |
off_t contentlength; |
127 |
char* type; /* not malloc()ed */ |
128 |
char* hostname; /* not malloc()ed */ |
129 |
int mime_flag; |
130 |
int one_one; /* HTTP/1.1 or better */ |
131 |
int got_range; |
132 |
int tildemapped; /* this connection got tilde-mapped */ |
133 |
off_t init_byte_loc, end_byte_loc; |
134 |
int keep_alive; |
135 |
int should_linger; |
136 |
struct stat sb; |
137 |
int conn_fd; |
138 |
char* file_address; |
139 |
#ifdef MMAP_MAX |
140 |
int file_fd; |
141 |
char* write_buf; |
142 |
off_t write_ofs; |
143 |
#endif |
144 |
} httpd_conn; |
145 |
|
146 |
/* Methods. */ |
147 |
#define METHOD_UNKNOWN 0 |
148 |
#define METHOD_GET 1 |
149 |
#define METHOD_HEAD 2 |
150 |
#define METHOD_POST 3 |
151 |
|
152 |
/* States for checked_state. */ |
153 |
#define CHST_FIRSTWORD 0 |
154 |
#define CHST_FIRSTWS 1 |
155 |
#define CHST_SECONDWORD 2 |
156 |
#define CHST_SECONDWS 3 |
157 |
#define CHST_THIRDWORD 4 |
158 |
#define CHST_THIRDWS 5 |
159 |
#define CHST_LINE 6 |
160 |
#define CHST_LF 7 |
161 |
#define CHST_CR 8 |
162 |
#define CHST_CRLF 9 |
163 |
#define CHST_CRLFCR 10 |
164 |
#define CHST_BOGUS 11 |
165 |
|
166 |
|
167 |
/* Initializes. Does the socket(), bind(), and listen(). Returns an |
168 |
** httpd_server* which includes a socket fd that you can select() on. |
169 |
** Return (httpd_server*) 0 on error. |
170 |
*/ |
171 |
extern httpd_server* httpd_initialize( |
172 |
char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port, |
173 |
char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp, |
174 |
int no_symlink, int vhost, int global_passwd, char* url_pattern, |
175 |
char* local_pattern, int no_empty_referers ); |
176 |
|
177 |
/* Change the log file. */ |
178 |
extern void httpd_set_logfp( httpd_server* hs, FILE* logfp ); |
179 |
|
180 |
/* Call to shut down. */ |
181 |
extern void httpd_terminate( httpd_server* hs ); |
182 |
|
183 |
|
184 |
/* When a listen fd is ready to read, call this. It does the accept() and |
185 |
** returns an httpd_conn* which includes the fd to read the request from and |
186 |
** write the response to. Returns an indication of whether the accept() |
187 |
** failed, succeeded, or if there were no more connections to accept. |
188 |
** |
189 |
** In order to minimize malloc()s, the caller passes in the httpd_conn. |
190 |
** The caller is also responsible for setting initialized to zero before the |
191 |
** first call using each different httpd_conn. |
192 |
*/ |
193 |
extern int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc ); |
194 |
#define GC_FAIL 0 |
195 |
#define GC_OK 1 |
196 |
#define GC_NO_MORE 2 |
197 |
|
198 |
/* Checks whether the data in hc->read_buf constitutes a complete request |
199 |
** yet. The caller reads data into hc->read_buf[hc->read_idx] and advances |
200 |
** hc->read_idx. This routine checks what has been read so far, using |
201 |
** hc->checked_idx and hc->checked_state to keep track, and returns an |
202 |
** indication of whether there is no complete request yet, there is a |
203 |
** complete request, or there won't be a valid request due to a syntax error. |
204 |
*/ |
205 |
extern int httpd_got_request( httpd_conn* hc ); |
206 |
#define GR_NO_REQUEST 0 |
207 |
#define GR_GOT_REQUEST 1 |
208 |
#define GR_BAD_REQUEST 2 |
209 |
|
210 |
/* Parses the request in hc->read_buf. Fills in lots of fields in hc, |
211 |
** like the URL and the various headers. |
212 |
** |
213 |
** Returns -1 on error. |
214 |
*/ |
215 |
extern int httpd_parse_request( httpd_conn* hc ); |
216 |
|
217 |
/* Starts sending data back to the client. In some cases (directories, |
218 |
** CGI programs), finishes sending by itself - in those cases, hc->file_fd |
219 |
** is <0. If there is more data to be sent, then hc->file_fd is a file |
220 |
** descriptor for the file to send. If you don't have a current timeval |
221 |
** handy just pass in 0. |
222 |
** |
223 |
** Returns -1 on error. |
224 |
*/ |
225 |
extern int httpd_start_request( httpd_conn* hc, struct timeval* nowP ); |
226 |
|
227 |
/* Actually sends any buffered response text. */ |
228 |
extern void httpd_write_response( httpd_conn* hc ); |
229 |
|
230 |
/* Call this to close down a connection and free the data. A fine point, |
231 |
** if you fork() with a connection open you should still call this in the |
232 |
** parent process - the connection will stay open in the child. |
233 |
** If you don't have a current timeval handy just pass in 0. |
234 |
*/ |
235 |
extern void httpd_close_conn( httpd_conn* hc, struct timeval* nowP ); |
236 |
|
237 |
/* Call this to de-initialize a connection struct and *really* free the |
238 |
** mallocced strings. |
239 |
*/ |
240 |
extern void httpd_destroy_conn( httpd_conn* hc ); |
241 |
|
242 |
|
243 |
/* Send an error message back to the client. */ |
244 |
extern void httpd_send_err( |
245 |
httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ); |
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_ */ |