| 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* autoindex_prog; |
| 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 |
#ifdef MMAP_MAX |
| 141 |
int file_fd; |
| 142 |
char* write_buf; |
| 143 |
off_t write_ofs; |
| 144 |
#endif |
| 145 |
} httpd_conn; |
| 146 |
|
| 147 |
/* Methods. */ |
| 148 |
#define METHOD_UNKNOWN 0 |
| 149 |
#define METHOD_GET 1 |
| 150 |
#define METHOD_HEAD 2 |
| 151 |
#define METHOD_POST 3 |
| 152 |
|
| 153 |
/* States for checked_state. */ |
| 154 |
#define CHST_FIRSTWORD 0 |
| 155 |
#define CHST_FIRSTWS 1 |
| 156 |
#define CHST_SECONDWORD 2 |
| 157 |
#define CHST_SECONDWS 3 |
| 158 |
#define CHST_THIRDWORD 4 |
| 159 |
#define CHST_THIRDWS 5 |
| 160 |
#define CHST_LINE 6 |
| 161 |
#define CHST_LF 7 |
| 162 |
#define CHST_CR 8 |
| 163 |
#define CHST_CRLF 9 |
| 164 |
#define CHST_CRLFCR 10 |
| 165 |
#define CHST_BOGUS 11 |
| 166 |
|
| 167 |
|
| 168 |
/* Initializes. Does the socket(), bind(), and listen(). Returns an |
| 169 |
** httpd_server* which includes a socket fd that you can select() on. |
| 170 |
** Return (httpd_server*) 0 on error. |
| 171 |
*/ |
| 172 |
extern httpd_server* httpd_initialize( |
| 173 |
char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port, |
| 174 |
char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp, |
| 175 |
int no_symlink, int vhost, int global_passwd, char* url_pattern, |
| 176 |
char* local_pattern, int no_empty_referers, |
| 177 |
char* autoindex_prog ); |
| 178 |
|
| 179 |
/* Change the log file. */ |
| 180 |
extern void httpd_set_logfp( httpd_server* hs, FILE* logfp ); |
| 181 |
|
| 182 |
/* Call to shut down. */ |
| 183 |
extern void httpd_terminate( httpd_server* hs ); |
| 184 |
|
| 185 |
|
| 186 |
/* When a listen fd is ready to read, call this. It does the accept() and |
| 187 |
** returns an httpd_conn* which includes the fd to read the request from and |
| 188 |
** write the response to. Returns an indication of whether the accept() |
| 189 |
** failed, succeeded, or if there were no more connections to accept. |
| 190 |
** |
| 191 |
** In order to minimize malloc()s, the caller passes in the httpd_conn. |
| 192 |
** The caller is also responsible for setting initialized to zero before the |
| 193 |
** first call using each different httpd_conn. |
| 194 |
*/ |
| 195 |
extern int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc ); |
| 196 |
#define GC_FAIL 0 |
| 197 |
#define GC_OK 1 |
| 198 |
#define GC_NO_MORE 2 |
| 199 |
|
| 200 |
/* Checks whether the data in hc->read_buf constitutes a complete request |
| 201 |
** yet. The caller reads data into hc->read_buf[hc->read_idx] and advances |
| 202 |
** hc->read_idx. This routine checks what has been read so far, using |
| 203 |
** hc->checked_idx and hc->checked_state to keep track, and returns an |
| 204 |
** indication of whether there is no complete request yet, there is a |
| 205 |
** complete request, or there won't be a valid request due to a syntax error. |
| 206 |
*/ |
| 207 |
extern int httpd_got_request( httpd_conn* hc ); |
| 208 |
#define GR_NO_REQUEST 0 |
| 209 |
#define GR_GOT_REQUEST 1 |
| 210 |
#define GR_BAD_REQUEST 2 |
| 211 |
|
| 212 |
/* Parses the request in hc->read_buf. Fills in lots of fields in hc, |
| 213 |
** like the URL and the various headers. |
| 214 |
** |
| 215 |
** Returns -1 on error. |
| 216 |
*/ |
| 217 |
extern int httpd_parse_request( httpd_conn* hc ); |
| 218 |
|
| 219 |
/* Starts sending data back to the client. In some cases (directories, |
| 220 |
** CGI programs), finishes sending by itself - in those cases, hc->file_fd |
| 221 |
** is <0. If there is more data to be sent, then hc->file_fd is a file |
| 222 |
** descriptor for the file to send. If you don't have a current timeval |
| 223 |
** handy just pass in 0. |
| 224 |
** |
| 225 |
** Returns -1 on error. |
| 226 |
*/ |
| 227 |
extern int httpd_start_request( httpd_conn* hc, struct timeval* nowP ); |
| 228 |
|
| 229 |
/* Actually sends any buffered response text. */ |
| 230 |
extern void httpd_write_response( httpd_conn* hc ); |
| 231 |
|
| 232 |
/* Call this to close down a connection and free the data. A fine point, |
| 233 |
** if you fork() with a connection open you should still call this in the |
| 234 |
** parent process - the connection will stay open in the child. |
| 235 |
** If you don't have a current timeval handy just pass in 0. |
| 236 |
*/ |
| 237 |
extern void httpd_close_conn( httpd_conn* hc, struct timeval* nowP ); |
| 238 |
|
| 239 |
/* Call this to de-initialize a connection struct and *really* free the |
| 240 |
** mallocced strings. |
| 241 |
*/ |
| 242 |
extern void httpd_destroy_conn( httpd_conn* hc ); |
| 243 |
|
| 244 |
|
| 245 |
/* Send an error message back to the client. */ |
| 246 |
extern void httpd_send_err( |
| 247 |
httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ); |
| 248 |
|
| 249 |
extern void httpd_send_err_blocked( |
| 250 |
httpd_conn* hc); |
| 251 |
|
| 252 |
|
| 253 |
/* Some error messages. */ |
| 254 |
extern char* httpd_err400title; |
| 255 |
extern char* httpd_err400form; |
| 256 |
extern char* httpd_err408title; |
| 257 |
extern char* httpd_err408form; |
| 258 |
extern char* httpd_err503title; |
| 259 |
extern char* httpd_err503form; |
| 260 |
|
| 261 |
/* Generate a string representation of a method number. */ |
| 262 |
extern char* httpd_method_str( int method ); |
| 263 |
|
| 264 |
/* Reallocate a string. */ |
| 265 |
extern void httpd_realloc_str( char** strP, int* maxsizeP, int size ); |
| 266 |
|
| 267 |
/* Format a network socket to a string representation. */ |
| 268 |
extern char* httpd_ntoa( httpd_sockaddr* saP ); |
| 269 |
|
| 270 |
/* Set NDELAY mode on a socket. */ |
| 271 |
void httpd_set_ndelay( int fd ); |
| 272 |
|
| 273 |
/* Clear NDELAY mode on a socket. */ |
| 274 |
void httpd_clear_ndelay( int fd ); |
| 275 |
|
| 276 |
/* Generate debugging statistics syslog message. */ |
| 277 |
extern void httpd_logstats( long secs ); |
| 278 |
|
| 279 |
#endif /* _LIBHTTPD_H_ */ |