root/libhttpd.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


   1 /* libhttpd.h - defines for libhttpd
   2 **
   3 ** Copyright (c) 1995,1998,1999,2000,2001
   4 **     Jef Poskanzer <jef@mail.acme.com>.  All rights reserved.
   5 ** Copyright (c) 2023,2024,2025
   6 **     Amelia Zabardast Ziabari <ame@psianesia.org>.  All rights reserved.
   7 **
   8 ** Redistribution and use in source and binary forms, with or without
   9 ** modification, are permitted provided that the following conditions
  10 ** are met:
  11 ** 1. Redistributions of source code must retain the above copyright
  12 **    notice, this list of conditions and the following disclaimer.
  13 ** 2. Redistributions in binary form must reproduce the above copyright
  14 **    notice, this list of conditions and the following disclaimer in the
  15 **    documentation and/or other materials provided with the distribution.
  16 **
  17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27 ** SUCH DAMAGE.
  28 */
  29 
  30 #ifndef _LIBHTTPD_H_
  31 #define _LIBHTTPD_H_
  32 
  33 #include <sys/types.h>
  34 #include <sys/time.h>
  35 #include <sys/param.h>
  36 #include <sys/socket.h>
  37 #include <netinet/in.h>
  38 #include <arpa/inet.h>
  39 #include <netdb.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*) my_realloc( (void*) o, sizeof(t) * (n) ))
  56 
  57 /* Handle common error usage with realloc(), for RENEW() macro */
  58 void* my_realloc(void *old, size_t len);
  59 
  60 /* From The Practice of Programming, by Kernighan and Pike */
  61 #ifndef NELEMS
  62 #define NELEMS(array) (sizeof(array) / sizeof(array[0]))
  63 #endif
  64 
  65 /* Do overlapping strcpy safely, by using memmove. */
  66 #define ol_strcpy(dst,src) memmove(dst,src,strlen(src)+1)
  67 
  68 /* The httpd structs. */
  69 
  70 /* A multi-family sockaddr. */
  71 typedef union {
  72     struct sockaddr sa;
  73     struct sockaddr_in sa_in;
  74 #ifdef USE_IPV6
  75     struct sockaddr_in6 sa_in6;
  76     struct sockaddr_storage sa_stor;
  77 #endif /* USE_IPV6 */
  78     } httpd_sockaddr;
  79 
  80 /* A server. */
  81 typedef struct {
  82     char* binding_hostname;
  83     char* server_hostname;
  84     unsigned short port;
  85     char* cgi_pattern;
  86     char* cgi_wrap;
  87     int cgi_limit, cgi_count;
  88     char* mime_typf;
  89     char* mime_encf;
  90     char* charset;
  91     char* p3p;
  92     int max_age;
  93     char* cwd;
  94     int listen4_fd, listen6_fd;
  95     int no_log;
  96     FILE* logfp;
  97     int no_symlink_check;
  98     int vhost;
  99     int global_passwd;
 100     char* url_pattern;
 101     char* local_pattern;
 102     int no_empty_referrers;
 103     int fwdhdr;
 104     } httpd_server;
 105 
 106 /* A connection. */
 107 typedef struct {
 108     int initialized;
 109     httpd_server* hs;
 110     httpd_sockaddr client_addr;
 111     char* read_buf;
 112     size_t read_size, read_idx, checked_idx;
 113     int checked_state;
 114     int method;
 115     int status;
 116     off_t bytes_to_send;
 117     off_t bytes_sent;
 118     char* encodedurl;
 119     char* decodedurl;
 120     char* protocol;
 121     char* origfilename;
 122     char* expnfilename;
 123     char* encodings;
 124     char* pathinfo;
 125     char* query;
 126     char* referrer;
 127     char* useragent;
 128     char* accept;
 129     char* accepte;
 130     char* acceptl;
 131     char* cookie;
 132     char* contenttype;
 133     char* reqhost;
 134     char* hdrhost;
 135     char* hostdir;
 136     char* authorization;
 137     char* remoteuser;
 138     char* response;
 139     size_t maxdecodedurl, maxorigfilename, maxexpnfilename, maxencodings,
 140         maxpathinfo, maxquery, maxaccept, maxaccepte, maxreqhost, maxhostdir,
 141         maxremoteuser, maxresponse;
 142 #ifdef TILDE_MAP_2
 143     char* altdir;
 144     size_t maxaltdir;
 145 #endif /* TILDE_MAP_2 */
 146     size_t responselen;
 147     time_t if_modified_since, range_if;
 148     size_t contentlength;
 149     char* type;         /* not malloc()ed */
 150     char* hostname;     /* not malloc()ed */
 151     int mime_flag;
 152     int one_one;        /* HTTP/1.1 or better */
 153     int got_range;
 154     int tildemapped;    /* this connection got tilde-mapped */
 155     off_t first_byte_index, last_byte_index;
 156     int keep_alive;     /* whether the client asks for keep-alive */
 157     int do_keep_alive;  /* whether we actually want to keep-alive */
 158     int should_linger;
 159     struct stat sb;
 160     int conn_fd;
 161     char* file_address;
 162 #ifndef DISABLE_GZ
 163     int dotgz;
 164 #endif
 165     } httpd_conn;
 166 
 167 /* Methods. */
 168 #define METHOD_UNKNOWN 0
 169 #define METHOD_GET 1
 170 #define METHOD_HEAD 2
 171 #define METHOD_POST 3
 172 #define METHOD_PUT 4
 173 #define METHOD_DELETE 5
 174 #define METHOD_TRACE 6
 175 
 176 /* States for checked_state. */
 177 #define CHST_FIRSTWORD 0
 178 #define CHST_FIRSTWS 1
 179 #define CHST_SECONDWORD 2
 180 #define CHST_SECONDWS 3
 181 #define CHST_THIRDWORD 4
 182 #define CHST_THIRDWS 5
 183 #define CHST_LINE 6
 184 #define CHST_LF 7
 185 #define CHST_CR 8
 186 #define CHST_CRLF 9
 187 #define CHST_CRLFCR 10
 188 #define CHST_BOGUS 11
 189 
 190 
 191 /* Initializes.  Does the socket(), bind(), and listen().   Returns an
 192 ** httpd_server* which includes a socket fd that you can select() on.
 193 ** Return (httpd_server*) 0 on error.
 194 */
 195 httpd_server* httpd_initialize(
 196     char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P,
 197     unsigned short port, char* cgi_pattern, int cgi_limit, char* charset,
 198     char* p3p, int max_age, char* cwd, int no_log, FILE* logfp,
 199     int no_symlink_check, int vhost, int global_passwd, char* url_pattern,
 200     char* local_pattern, int no_empty_referrers, int fwdhdr, char* cgi_wrap,
 201     char* mime_encf, char* mime_typf );
 202 
 203 /* Change the log file. */
 204 void httpd_set_logfp( httpd_server* hs, FILE* logfp );
 205 
 206 /* Call to unlisten/close socket(s) listening for new connections. */
 207 void httpd_unlisten( httpd_server* hs );
 208 
 209 /* Used to reinitialize the connection for pipelined keep-alive requets */
 210 void httpd_init_conn_mem( httpd_conn *hc );
 211 void httpd_init_conn_content( httpd_conn *hc );
 212 
 213 /* Call to shut down. */
 214 void httpd_terminate( httpd_server* hs );
 215 
 216 
 217 /* When a listen fd is ready to read, call this.  It does the accept() and
 218 ** returns an httpd_conn* which includes the fd to read the request from and
 219 ** write the response to.  Returns an indication of whether the accept()
 220 ** failed, succeeded, or if there were no more connections to accept.
 221 **
 222 ** In order to minimize malloc()s, the caller passes in the httpd_conn.
 223 ** The caller is also responsible for setting initialized to zero before the
 224 ** first call using each different httpd_conn.
 225 */
 226 int httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc );
 227 #define GC_FAIL 0
 228 #define GC_OK 1
 229 #define GC_NO_MORE 2
 230 
 231 /* Checks whether the data in hc->read_buf constitutes a complete request
 232 ** yet.  The caller reads data into hc->read_buf[hc->read_idx] and advances
 233 ** hc->read_idx.  This routine checks what has been read so far, using
 234 ** hc->checked_idx and hc->checked_state to keep track, and returns an
 235 ** indication of whether there is no complete request yet, there is a
 236 ** complete request, or there won't be a valid request due to a syntax error.
 237 */
 238 int httpd_got_request( httpd_conn* hc );
 239 #define GR_NO_REQUEST 0
 240 #define GR_GOT_REQUEST 1
 241 #define GR_BAD_REQUEST 2
 242 
 243 /* Parses the request in hc->read_buf.  Fills in lots of fields in hc,
 244 ** like the URL and the various headers.
 245 **
 246 ** Returns -1 on error.
 247 */
 248 int httpd_parse_request( httpd_conn* hc );
 249 
 250 /* Starts sending data back to the client.  In some cases (directories,
 251 ** CGI programs), finishes sending by itself - in those cases, hc->file_fd
 252 ** is <0.  If there is more data to be sent, then hc->file_fd is a file
 253 ** descriptor for the file to send.  If you don't have a current timeval
 254 ** handy just pass in 0.
 255 **
 256 ** Returns -1 on error.
 257 */
 258 int httpd_start_request( httpd_conn* hc, struct timeval* nowP );
 259 
 260 /* Actually sends any buffered response text. */
 261 void httpd_write_response( httpd_conn* hc );
 262 
 263 /* Call this to close down a connection and free the data.  A fine point,
 264 ** if you fork() with a connection open you should still call this in the
 265 ** parent process - the connection will stay open in the child.
 266 ** If you don't have a current timeval handy just pass in 0.
 267 */
 268 void httpd_close_conn( httpd_conn* hc, struct timeval* nowP );
 269 
 270 /* Call this to de-initialize a connection struct and *really* free the
 271 ** mallocced strings.
 272 */
 273 void httpd_destroy_conn( httpd_conn* hc );
 274 
 275 
 276 /* Send an error message back to the client. */
 277 void httpd_send_err(
 278     httpd_conn* hc, int status, char* title, char* extraheads, char* form,
 279     char* arg );
 280 
 281 /* Some error messages. */
 282 extern char* httpd_err400title;
 283 extern char* httpd_err400form;
 284 extern char* httpd_err408title;
 285 extern char* httpd_err408form;
 286 extern char* httpd_err503title;
 287 extern char* httpd_err503form;
 288 
 289 /* Generate a string representation of a method number. */
 290 char* httpd_method_str( int method );
 291 
 292 /* Reallocate a string. */
 293 void httpd_realloc_str( char** strP, size_t* maxsizeP, size_t size );
 294 
 295 /* Format a network socket to a string representation. */
 296 char* httpd_ntoa( httpd_sockaddr* saP );
 297 
 298 /* Set NDELAY mode on a socket. */
 299 void httpd_set_ndelay( int fd );
 300 
 301 /* Clear NDELAY mode on a socket. */
 302 void httpd_clear_ndelay( int fd );
 303 
 304 /* Read the requested buffer completely, accounting for interruptions. */
 305 int httpd_read_fully( int fd, void* buf, size_t nbytes );
 306 
 307 /* Write the requested buffer completely, accounting for interruptions. */
 308 int httpd_write_fully( int fd, const char* buf, size_t nbytes );
 309 
 310 /* Generate debugging statistics syslog message. */
 311 void httpd_logstats( long secs );
 312 
 313 #endif /* _LIBHTTPD_H_ */

/* [previous][next][first][last][top][bottom][index][help] */