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