1 /* libhttpd.h - defines for libhttpd 2 ** 3 ** Copyright © 1995,1998,1999,2000,2001 by Jef Poskanzer <jef@mail.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 <sys/stat.h> 36 #include <netinet/in.h> 37 #include <arpa/inet.h> 38 #include <netdb.h> 39 #include <stdio.h> 40 41 #if defined(AF_INET6) && defined(IN6_IS_ADDR_V4MAPPED) 42 #define USE_IPV6 43 #endif 44 45 46 /* A few convenient defines. */ 47 48 #ifndef MAX 49 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 50 #endif 51 #ifndef MIN 52 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 53 #endif 54 #define NEW(t,n) ((t*) malloc( sizeof(t) * (n) )) 55 #define RENEW(o,t,n) ((t*) realloc( (void*) o, sizeof(t) * (n) )) 56 57 58 /* The httpd structs. */ 59 60 /* A multi-family sockaddr. */ 61 typedef union { 62 struct sockaddr sa; 63 struct sockaddr_in sa_in; 64 #ifdef USE_IPV6 65 struct sockaddr_in6 sa_in6; 66 struct sockaddr_storage sa_stor; 67 #endif /* USE_IPV6 */ 68 } httpd_sockaddr; 69 70 /* A server. */ 71 typedef struct { 72 char* binding_hostname; 73 char* server_hostname; 74 unsigned short port; 75 char* cgi_pattern; 76 int cgi_limit, cgi_count; 77 char* charset; 78 char* p3p; 79 int max_age; 80 char* cwd; 81 int listen4_fd, listen6_fd; 82 int no_log; 83 FILE* logfp; 84 int no_symlink_check; 85 int vhost; 86 int global_passwd; 87 char* url_pattern; 88 char* local_pattern; 89 int no_empty_referers; 90 //added for poorman 91 int do_list_dir; 92 char* index_name; 93 } httpd_server; 94 95 /* A connection. */ 96 typedef struct { 97 int initialized; 98 httpd_server* hs; 99 httpd_sockaddr client_addr; 100 char* read_buf; 101 size_t read_size, read_idx, checked_idx; 102 int checked_state; 103 int method; 104 int status; 105 off_t bytes_to_send; 106 off_t bytes_sent; 107 char* encodedurl; 108 char* decodedurl; 109 char* protocol; 110 char* origfilename; 111 char* expnfilename; 112 char* encodings; 113 char* pathinfo; 114 char* query; 115 char* referer; 116 char* useragent; 117 char* accept; 118 char* accepte; 119 char* acceptl; 120 char* cookie; 121 char* contenttype; 122 char* reqhost; 123 char* hdrhost; 124 char* hostdir; 125 char* authorization; 126 char* remoteuser; 127 char* response; 128 size_t maxdecodedurl, maxorigfilename, maxexpnfilename, maxencodings, 129 maxpathinfo, maxquery, maxaccept, maxaccepte, maxreqhost, maxhostdir, 130 maxremoteuser, maxresponse; 131 #ifdef TILDE_MAP_2 132 char* altdir; 133 size_t maxaltdir; 134 #endif /* TILDE_MAP_2 */ 135 size_t responselen; 136 time_t if_modified_since, range_if; 137 size_t contentlength; 138 char* type; /* not malloc()ed */ 139 char* hostname; /* not malloc()ed */ 140 int mime_flag; 141 int one_one; /* HTTP/1.1 or better */ 142 int got_range; 143 int tildemapped; /* this connection got tilde-mapped */ 144 off_t first_byte_index, last_byte_index; 145 int keep_alive; 146 int should_linger; 147 struct stat sb; 148 int conn_fd; 149 int processed_directory_index; 150 } httpd_conn; 151 152 /* Methods. */ 153 #define METHOD_UNKNOWN 0 154 #define METHOD_GET 1 155 #define METHOD_HEAD 2 156 #define METHOD_POST 3 157 158 /* States for checked_state. */ 159 #define CHST_FIRSTWORD 0 160 #define CHST_FIRSTWS 1 161 #define CHST_SECONDWORD 2 162 #define CHST_SECONDWS 3 163 #define CHST_THIRDWORD 4 164 #define CHST_THIRDWS 5 165 #define CHST_LINE 6 166 #define CHST_LF 7 167 #define CHST_CR 8 168 #define CHST_CRLF 9 169 #define CHST_CRLFCR 10 170 #define CHST_BOGUS 11 171 172 #ifdef __cplusplus 173 extern "C" { 174 #endif 175 176 /* Initializes. Does the socket(), bind(), and listen(). Returns an 177 ** httpd_server* which includes a socket fd that you can select() on. 178 ** Return (httpd_server*) 0 on error. 179 */ 180 extern httpd_server* httpd_initialize( 181 char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, 182 unsigned short port, char* cgi_pattern, int cgi_limit, char* charset, 183 char* p3p, int max_age, char* cwd, int no_log, FILE* logfp, 184 int no_symlink_check, int vhost, int global_passwd, char* url_pattern, 185 char* local_pattern, int no_empty_referers ); 186 187 /* PoorMan: Initialize_listen_socket() is changed from static to extern. 188 ** httpd_unlisten() needs an opposite operation that can be accessed from 189 ** outside libhttpd. So this is it. 190 */ 191 extern int httpd_initialize_listen_socket( httpd_sockaddr* saP ); 192 193 /* Change the log file. */ 194 extern void httpd_set_logfp( httpd_server* hs, FILE* logfp ); 195 196 /* Call to unlisten/close socket(s) listening for new connections. */ 197 extern void httpd_unlisten( httpd_server* hs ); 198 199 /* Call to shut down. */ 200 extern void httpd_terminate( httpd_server* hs ); 201 202 203 /* When a listen fd is ready to read, call this. It does the accept() and 204 ** returns an httpd_conn* which includes the fd to read the request from and 205 ** write the response to. Returns an indication of whether the accept() 206 ** failed, succeeded, or if there were no more connections to accept. 207 ** 208 ** In order to minimize malloc()s, the caller passes in the httpd_conn. 209 ** The caller is also responsible for setting initialized to zero before the 210 ** first call using each different httpd_conn. 211 */ 212 extern int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc ); 213 #define GC_FAIL 0 214 #define GC_OK 1 215 #define GC_NO_MORE 2 216 217 /* Checks whether the data in hc->read_buf constitutes a complete request 218 ** yet. The caller reads data into hc->read_buf[hc->read_idx] and advances 219 ** hc->read_idx. This routine checks what has been read so far, using 220 ** hc->checked_idx and hc->checked_state to keep track, and returns an 221 ** indication of whether there is no complete request yet, there is a 222 ** complete request, or there won't be a valid request due to a syntax error. 223 */ 224 extern int httpd_got_request( httpd_conn* hc ); 225 #define GR_NO_REQUEST 0 226 #define GR_GOT_REQUEST 1 227 #define GR_BAD_REQUEST 2 228 229 /* Parses the request in hc->read_buf. Fills in lots of fields in hc, 230 ** like the URL and the various headers. 231 ** 232 ** Returns -1 on error. 233 */ 234 extern int httpd_parse_request( httpd_conn* hc ); 235 236 /* Starts sending data back to the client. In some cases (directories, 237 ** CGI programs), finishes sending by itself - in those cases, hc->file_fd 238 ** is <0. If there is more data to be sent, then hc->file_fd is a file 239 ** descriptor for the file to send. If you don't have a current timeval 240 ** handy just pass in 0. 241 ** 242 ** Returns -1 on error. 243 */ 244 extern int httpd_start_request( httpd_conn* hc, struct timeval* nowP ); 245 246 /* Actually sends any buffered response text. */ 247 extern void httpd_write_response( httpd_conn* hc ); 248 249 /* Call this to close down a connection and free the data. A fine point, 250 ** if you fork() with a connection open you should still call this in the 251 ** parent process - the connection will stay open in the child. 252 ** If you don't have a current timeval handy just pass in 0. 253 */ 254 extern void httpd_close_conn( httpd_conn* hc, struct timeval* nowP ); 255 256 /* Call this to de-initialize a connection struct and *really* free the 257 ** mallocced strings. 258 */ 259 extern void httpd_destroy_conn( httpd_conn* hc ); 260 261 262 /* Send an error message back to the client. */ 263 extern void httpd_send_err( 264 httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ); 265 266 /* Some error messages. */ 267 extern char* httpd_err400title; 268 extern char* httpd_err400form; 269 extern char* httpd_err408title; 270 extern char* httpd_err408form; 271 extern char* httpd_err503title; 272 extern char* httpd_err503form; 273 274 /* Generate a string representation of a method number. */ 275 extern char* httpd_method_str( int method ); 276 277 /* Reallocate a string. */ 278 extern void httpd_realloc_str( char** strP, size_t* maxsizeP, size_t size ); 279 280 /* Format a network socket to a string representation. */ 281 extern char* httpd_ntoa( httpd_sockaddr* saP ); 282 283 /* Set NDELAY mode on a socket. */ 284 extern void httpd_set_ndelay( int fd ); 285 286 /* Clear NDELAY mode on a socket. */ 287 extern void httpd_clear_ndelay( int fd ); 288 289 /* Read the requested buffer completely, accounting for interruptions. */ 290 extern int httpd_read_fully( int fd, void* buf, size_t nbytes ); 291 292 /* Write the requested buffer completely, accounting for interruptions. */ 293 extern int httpd_write_fully( int fd, const void* buf, size_t nbytes ); 294 295 /* Generate debugging statistics syslog message. */ 296 extern void httpd_logstats( long secs ); 297 298 #ifdef __cplusplus 299 } 300 #endif 301 302 #endif /* _LIBHTTPD_H_ */ 303