Annotation of embedaddon/thttpd/libhttpd.h, revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>