ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/thttpd/libhttpd.h
Revision: 1.1.2.1
Committed: Mon Jun 18 21:19:51 2001 UTC (23 years ago) by root
Content type: text/plain
Branch: dirpatch
CVS Tags: dp_j
Changes since 1.1: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 root 1.1.2.1 char* autoindex_prog;
73 root 1.1 char* charset;
74     char* cwd;
75     int listen4_fd, listen6_fd;
76     int no_log;
77     FILE* logfp;
78     int no_symlink;
79     int vhost;
80     int global_passwd;
81     char* url_pattern;
82     char* local_pattern;
83     int no_empty_referers;
84     } httpd_server;
85    
86     /* A connection. */
87     typedef struct {
88     int initialized;
89     httpd_server* hs;
90     httpd_sockaddr client_addr;
91     char* read_buf;
92     int read_size, read_idx, checked_idx;
93     int checked_state;
94     int method;
95     int status;
96     off_t bytes_to_send;
97     off_t bytes_sent;
98     char* encodedurl;
99     char* decodedurl;
100     char* protocol;
101     char* origfilename;
102     char* expnfilename;
103     char* encodings;
104     char* pathinfo;
105     char* query;
106     char* referer;
107     char* useragent;
108     char* accept;
109     char* accepte;
110     char* acceptl;
111     char* cookie;
112     char* contenttype;
113     char* reqhost;
114     char* hdrhost;
115     char* hostdir;
116     char* authorization;
117     char* remoteuser;
118     char* response;
119     int maxdecodedurl, maxorigfilename, maxexpnfilename, maxencodings,
120     maxpathinfo, maxquery, maxaccept, maxaccepte, maxreqhost, maxhostdir,
121     maxremoteuser, maxresponse;
122     #ifdef TILDE_MAP_2
123     char* altdir;
124     int maxaltdir;
125     #endif /* TILDE_MAP_2 */
126     int responselen;
127     time_t if_modified_since, range_if;
128     off_t contentlength;
129     char* type; /* not malloc()ed */
130     char* hostname; /* not malloc()ed */
131     int mime_flag;
132     int one_one; /* HTTP/1.1 or better */
133     int got_range;
134     int tildemapped; /* this connection got tilde-mapped */
135     off_t init_byte_loc, end_byte_loc;
136     int keep_alive;
137     int should_linger;
138     struct stat sb;
139     int conn_fd;
140     char* file_address;
141     } httpd_conn;
142    
143     /* Methods. */
144     #define METHOD_UNKNOWN 0
145     #define METHOD_GET 1
146     #define METHOD_HEAD 2
147     #define METHOD_POST 3
148    
149     /* States for checked_state. */
150     #define CHST_FIRSTWORD 0
151     #define CHST_FIRSTWS 1
152     #define CHST_SECONDWORD 2
153     #define CHST_SECONDWS 3
154     #define CHST_THIRDWORD 4
155     #define CHST_THIRDWS 5
156     #define CHST_LINE 6
157     #define CHST_LF 7
158     #define CHST_CR 8
159     #define CHST_CRLF 9
160     #define CHST_CRLFCR 10
161     #define CHST_BOGUS 11
162    
163    
164     /* Initializes. Does the socket(), bind(), and listen(). Returns an
165     ** httpd_server* which includes a socket fd that you can select() on.
166     ** Return (httpd_server*) 0 on error.
167     */
168     extern httpd_server* httpd_initialize(
169     char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port,
170     char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp,
171     int no_symlink, int vhost, int global_passwd, char* url_pattern,
172 root 1.1.2.1 char* local_pattern, int no_empty_referers,
173     char* autoindex_prog );
174 root 1.1
175     /* Change the log file. */
176     extern void httpd_set_logfp( httpd_server* hs, FILE* logfp );
177    
178     /* Call to shut down. */
179     extern void httpd_terminate( httpd_server* hs );
180    
181    
182     /* When a listen fd is ready to read, call this. It does the accept() and
183     ** returns an httpd_conn* which includes the fd to read the request from and
184     ** write the response to. Returns an indication of whether the accept()
185     ** failed, succeeded, or if there were no more connections to accept.
186     **
187     ** In order to minimize malloc()s, the caller passes in the httpd_conn.
188     ** The caller is also responsible for setting initialized to zero before the
189     ** first call using each different httpd_conn.
190     */
191     extern int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc );
192     #define GC_FAIL 0
193     #define GC_OK 1
194     #define GC_NO_MORE 2
195    
196     /* Checks whether the data in hc->read_buf constitutes a complete request
197     ** yet. The caller reads data into hc->read_buf[hc->read_idx] and advances
198     ** hc->read_idx. This routine checks what has been read so far, using
199     ** hc->checked_idx and hc->checked_state to keep track, and returns an
200     ** indication of whether there is no complete request yet, there is a
201     ** complete request, or there won't be a valid request due to a syntax error.
202     */
203     extern int httpd_got_request( httpd_conn* hc );
204     #define GR_NO_REQUEST 0
205     #define GR_GOT_REQUEST 1
206     #define GR_BAD_REQUEST 2
207    
208     /* Parses the request in hc->read_buf. Fills in lots of fields in hc,
209     ** like the URL and the various headers.
210     **
211     ** Returns -1 on error.
212     */
213     extern int httpd_parse_request( httpd_conn* hc );
214    
215     /* Starts sending data back to the client. In some cases (directories,
216     ** CGI programs), finishes sending by itself - in those cases, hc->file_fd
217     ** is <0. If there is more data to be sent, then hc->file_fd is a file
218     ** descriptor for the file to send. If you don't have a current timeval
219     ** handy just pass in 0.
220     **
221     ** Returns -1 on error.
222     */
223     extern int httpd_start_request( httpd_conn* hc, struct timeval* nowP );
224    
225     /* Actually sends any buffered response text. */
226     extern void httpd_write_response( httpd_conn* hc );
227    
228     /* Call this to close down a connection and free the data. A fine point,
229     ** if you fork() with a connection open you should still call this in the
230     ** parent process - the connection will stay open in the child.
231     ** If you don't have a current timeval handy just pass in 0.
232     */
233     extern void httpd_close_conn( httpd_conn* hc, struct timeval* nowP );
234    
235     /* Call this to de-initialize a connection struct and *really* free the
236     ** mallocced strings.
237     */
238     extern void httpd_destroy_conn( httpd_conn* hc );
239    
240    
241     /* Send an error message back to the client. */
242     extern void httpd_send_err(
243     httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg );
244    
245     /* Some error messages. */
246     extern char* httpd_err400title;
247     extern char* httpd_err400form;
248     extern char* httpd_err408title;
249     extern char* httpd_err408form;
250     extern char* httpd_err503title;
251     extern char* httpd_err503form;
252    
253     /* Generate a string representation of a method number. */
254     extern char* httpd_method_str( int method );
255    
256     /* Reallocate a string. */
257     extern void httpd_realloc_str( char** strP, int* maxsizeP, int size );
258    
259     /* Format a network socket to a string representation. */
260     extern char* httpd_ntoa( httpd_sockaddr* saP );
261    
262     /* Set NDELAY mode on a socket. */
263     void httpd_set_ndelay( int fd );
264    
265     /* Clear NDELAY mode on a socket. */
266     void httpd_clear_ndelay( int fd );
267    
268     /* Generate debugging statistics syslog message. */
269     extern void httpd_logstats( long secs );
270    
271     #endif /* _LIBHTTPD_H_ */