1 /* libhttpd.h - defines for libhttpd
3 ** Copyright © 1995,1998,1999,2000,2001 by Jef Poskanzer <jef@mail.acme.com>.
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions
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.
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
31 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
39 #if defined(AF_INET6) && defined(IN6_IS_ADDR_V4MAPPED)
44 /* A few convenient defines. */
47 #define MAX(a,b) ((a) > (b) ? (a) : (b))
50 #define MIN(a,b) ((a) < (b) ? (a) : (b))
52 #define NEW(t,n) ((t*) malloc( sizeof(t) * (n) ))
53 #define RENEW(o,t,n) ((t*) realloc( (void*) o, sizeof(t) * (n) ))
55 /* Do overlapping strcpy safely, by using memmove. */
56 #define ol_strcpy(dst,src) memmove(dst,src,strlen(src)+1)
59 /* The httpd structs. */
61 /* A multi-family sockaddr. */
64 struct sockaddr_in sa_in;
66 struct sockaddr_in6 sa_in6;
67 struct sockaddr_storage sa_stor;
73 char* binding_hostname;
74 char* server_hostname;
77 int cgi_limit, cgi_count;
82 int listen4_fd, listen6_fd;
90 int no_empty_referrers;
97 httpd_sockaddr client_addr;
99 size_t read_size, read_idx, checked_idx;
126 size_t maxdecodedurl, maxorigfilename, maxexpnfilename, maxencodings,
127 maxpathinfo, maxquery, maxaccept, maxaccepte, maxreqhost, maxhostdir,
128 maxremoteuser, maxresponse;
132 #endif /* TILDE_MAP_2 */
134 time_t if_modified_since, range_if;
135 size_t contentlength;
136 char* type; /* not malloc()ed */
137 char* hostname; /* not malloc()ed */
139 int one_one; /* HTTP/1.1 or better */
141 int tildemapped; /* this connection got tilde-mapped */
142 off_t first_byte_index, last_byte_index;
151 #define METHOD_UNKNOWN 0
153 #define METHOD_HEAD 2
154 #define METHOD_POST 3
156 #define METHOD_DELETE 5
157 #define METHOD_TRACE 6
159 /* States for checked_state. */
160 #define CHST_FIRSTWORD 0
161 #define CHST_FIRSTWS 1
162 #define CHST_SECONDWORD 2
163 #define CHST_SECONDWS 3
164 #define CHST_THIRDWORD 4
165 #define CHST_THIRDWS 5
170 #define CHST_CRLFCR 10
171 #define CHST_BOGUS 11
174 /* Initializes. Does the socket(), bind(), and listen(). Returns an
175 ** httpd_server* which includes a socket fd that you can select() on.
176 ** Return (httpd_server*) 0 on error.
178 httpd_server* httpd_initialize(
179 char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P,
180 unsigned short port, char* cgi_pattern, int cgi_limit, char* charset,
181 char* p3p, int max_age, char* cwd, int no_log, FILE* logfp,
182 int no_symlink_check, int vhost, int global_passwd, char* url_pattern,
183 char* local_pattern, int no_empty_referrers );
185 /* Change the log file. */
186 void httpd_set_logfp( httpd_server* hs, FILE* logfp );
188 /* Call to unlisten/close socket(s) listening for new connections. */
189 void httpd_unlisten( httpd_server* hs );
191 /* Call to shut down. */
192 void httpd_terminate( httpd_server* hs );
195 /* When a listen fd is ready to read, call this. It does the accept() and
196 ** returns an httpd_conn* which includes the fd to read the request from and
197 ** write the response to. Returns an indication of whether the accept()
198 ** failed, succeeded, or if there were no more connections to accept.
200 ** In order to minimize malloc()s, the caller passes in the httpd_conn.
201 ** The caller is also responsible for setting initialized to zero before the
202 ** first call using each different httpd_conn.
204 int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc );
209 /* Checks whether the data in hc->read_buf constitutes a complete request
210 ** yet. The caller reads data into hc->read_buf[hc->read_idx] and advances
211 ** hc->read_idx. This routine checks what has been read so far, using
212 ** hc->checked_idx and hc->checked_state to keep track, and returns an
213 ** indication of whether there is no complete request yet, there is a
214 ** complete request, or there won't be a valid request due to a syntax error.
216 int httpd_got_request( httpd_conn* hc );
217 #define GR_NO_REQUEST 0
218 #define GR_GOT_REQUEST 1
219 #define GR_BAD_REQUEST 2
221 /* Parses the request in hc->read_buf. Fills in lots of fields in hc,
222 ** like the URL and the various headers.
224 ** Returns -1 on error.
226 int httpd_parse_request( httpd_conn* hc );
228 /* Starts sending data back to the client. In some cases (directories,
229 ** CGI programs), finishes sending by itself - in those cases, hc->file_fd
230 ** is <0. If there is more data to be sent, then hc->file_fd is a file
231 ** descriptor for the file to send. If you don't have a current timeval
232 ** handy just pass in 0.
234 ** Returns -1 on error.
236 int httpd_start_request( httpd_conn* hc, struct timeval* nowP );
238 /* Actually sends any buffered response text. */
239 void httpd_write_response( httpd_conn* hc );
241 /* Call this to close down a connection and free the data. A fine point,
242 ** if you fork() with a connection open you should still call this in the
243 ** parent process - the connection will stay open in the child.
244 ** If you don't have a current timeval handy just pass in 0.
246 void httpd_close_conn( httpd_conn* hc, struct timeval* nowP );
248 /* Call this to de-initialize a connection struct and *really* free the
249 ** mallocced strings.
251 void httpd_destroy_conn( httpd_conn* hc );
254 /* Send an error message back to the client. */
256 httpd_conn* hc, int status, char* title, char* extraheads, char* form,
259 /* Some error messages. */
260 extern char* httpd_err400title;
261 extern char* httpd_err400form;
262 extern char* httpd_err408title;
263 extern char* httpd_err408form;
264 extern char* httpd_err503title;
265 extern char* httpd_err503form;
267 /* Generate a string representation of a method number. */
268 char* httpd_method_str( int method );
270 /* Reallocate a string. */
271 void httpd_realloc_str( char** strP, size_t* maxsizeP, size_t size );
273 /* Format a network socket to a string representation. */
274 char* httpd_ntoa( httpd_sockaddr* saP );
276 /* Set NDELAY mode on a socket. */
277 void httpd_set_ndelay( int fd );
279 /* Clear NDELAY mode on a socket. */
280 void httpd_clear_ndelay( int fd );
282 /* Read the requested buffer completely, accounting for interruptions. */
283 int httpd_read_fully( int fd, void* buf, size_t nbytes );
285 /* Write the requested buffer completely, accounting for interruptions. */
286 int httpd_write_fully( int fd, const char* buf, size_t nbytes );
288 /* Generate debugging statistics syslog message. */
289 void httpd_logstats( long secs );
291 #endif /* _LIBHTTPD_H_ */