~ubuntu-branches/ubuntu/natty/lighttpd/natty

« back to all changes in this revision

Viewing changes to src/base.h

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Marek
  • Date: 2005-11-26 11:48:51 UTC
  • Revision ID: james.westby@ubuntu.com-20051126114851-76t9q0rrwbzjnt2t
Tags: upstream-1.4.8
ImportĀ upstreamĀ versionĀ 1.4.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _BASE_H_
 
2
#define _BASE_H_
 
3
 
 
4
#include <sys/types.h>
 
5
#include <sys/time.h>
 
6
#include <sys/stat.h>
 
7
 
 
8
#ifdef HAVE_CONFIG_H
 
9
#include "config.h"
 
10
#endif
 
11
 
 
12
#include <limits.h>
 
13
#ifdef HAVE_STDINT_H
 
14
# include <stdint.h>
 
15
#endif
 
16
#ifdef HAVE_INTTYPES_H
 
17
# include <inttypes.h>
 
18
#endif
 
19
 
 
20
#include "buffer.h"
 
21
#include "array.h"
 
22
#include "chunk.h"
 
23
#include "keyvalue.h"
 
24
#include "settings.h"
 
25
#include "fdevent.h"
 
26
#include "sys-socket.h"
 
27
#include "splaytree.h"
 
28
 
 
29
 
 
30
#if defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H
 
31
# define USE_OPENSSL
 
32
# include <openssl/ssl.h> 
 
33
#endif
 
34
 
 
35
#ifdef HAVE_FAM_H
 
36
# include <fam.h>
 
37
#endif
 
38
 
 
39
#ifndef O_BINARY
 
40
# define O_BINARY 0
 
41
#endif
 
42
 
 
43
#ifndef O_LARGEFILE
 
44
# define O_LARGEFILE 0
 
45
#endif
 
46
 
 
47
#ifndef SIZE_MAX
 
48
# ifdef SIZE_T_MAX
 
49
#  define SIZE_MAX SIZE_T_MAX
 
50
# else
 
51
#  define SIZE_MAX ((size_t)~0)
 
52
# endif
 
53
#endif
 
54
 
 
55
#ifndef SSIZE_MAX
 
56
# define SSIZE_MAX ((size_t)~0 >> 1)
 
57
#endif
 
58
 
 
59
#ifdef __APPLE__
 
60
#include <crt_externs.h>
 
61
#define environ (* _NSGetEnviron())
 
62
#else
 
63
extern char **environ;
 
64
#endif
 
65
 
 
66
/* for solaris 2.5 and NetBSD 1.3.x */
 
67
#ifndef HAVE_SOCKLEN_T
 
68
typedef int socklen_t;
 
69
#endif
 
70
 
 
71
/* solaris and NetBSD 1.3.x again */
 
72
#if (!defined(HAVE_STDINT_H)) && (!defined(HAVE_INTTYPES_H)) && (!defined(uint32_t))
 
73
# define uint32_t u_int32_t
 
74
#endif
 
75
 
 
76
 
 
77
#ifndef SHUT_WR
 
78
# define SHUT_WR 1
 
79
#endif
 
80
 
 
81
#include "settings.h"
 
82
 
 
83
typedef enum { T_CONFIG_UNSET, 
 
84
                T_CONFIG_STRING, 
 
85
                T_CONFIG_SHORT, 
 
86
                T_CONFIG_BOOLEAN, 
 
87
                T_CONFIG_ARRAY, 
 
88
                T_CONFIG_LOCAL, 
 
89
                T_CONFIG_DEPRECATED
 
90
} config_values_type_t;
 
91
 
 
92
typedef enum { T_CONFIG_SCOPE_UNSET, 
 
93
                T_CONFIG_SCOPE_SERVER, 
 
94
                T_CONFIG_SCOPE_CONNECTION
 
95
} config_scope_type_t;
 
96
 
 
97
typedef struct {
 
98
        const char *key;
 
99
        void *destination;
 
100
        
 
101
        config_values_type_t type;
 
102
        config_scope_type_t scope;
 
103
} config_values_t;
 
104
 
 
105
typedef enum { DIRECT, EXTERNAL } connection_type;
 
106
 
 
107
typedef struct {
 
108
        char *key;
 
109
        connection_type type;
 
110
        char *value;
 
111
} request_handler;
 
112
 
 
113
typedef struct {
 
114
        char *key;
 
115
        char *host;
 
116
        unsigned short port;
 
117
        int used;
 
118
        short factor;
 
119
} fcgi_connections;
 
120
 
 
121
 
 
122
typedef union {
 
123
#ifdef HAVE_IPV6
 
124
        struct sockaddr_in6 ipv6;
 
125
#endif
 
126
        struct sockaddr_in ipv4;
 
127
#ifdef HAVE_SYS_UN_H
 
128
        struct sockaddr_un un;
 
129
#endif
 
130
        struct sockaddr plain;
 
131
} sock_addr;
 
132
 
 
133
/* fcgi_response_header contains ... */
 
134
#define HTTP_STATUS         BV(0)
 
135
#define HTTP_CONNECTION     BV(1)
 
136
#define HTTP_CONTENT_LENGTH BV(2)
 
137
#define HTTP_DATE           BV(3)
 
138
#define HTTP_LOCATION       BV(4)
 
139
 
 
140
typedef struct {
 
141
        /** HEADER */
 
142
        /* the request-line */
 
143
        buffer *request;
 
144
        buffer *uri;
 
145
        
 
146
        buffer *orig_uri;
 
147
        
 
148
        http_method_t  http_method;
 
149
        http_version_t http_version;
 
150
        
 
151
        buffer *request_line;
 
152
        
 
153
        /* strings to the header */
 
154
        buffer *http_host; /* not alloced */
 
155
        const char   *http_range;
 
156
        const char   *http_content_type;
 
157
        const char   *http_if_modified_since;
 
158
        const char   *http_if_none_match;
 
159
        
 
160
        array  *headers;
 
161
        
 
162
        /* CONTENT */
 
163
        size_t content_length; /* returned by strtoul() */
 
164
        
 
165
        /* internal representation */
 
166
        int     accept_encoding;
 
167
        
 
168
        /* internal */
 
169
        buffer *pathinfo;
 
170
} request;
 
171
 
 
172
typedef struct {
 
173
        off_t   content_length;
 
174
        int     keep_alive;               /* used by  the subrequests in proxy, cgi and fcgi to say the subrequest was keep-alive or not */
 
175
        
 
176
        array  *headers;
 
177
        
 
178
        enum { 
 
179
                HTTP_TRANSFER_ENCODING_IDENTITY, HTTP_TRANSFER_ENCODING_CHUNKED
 
180
        } transfer_encoding;
 
181
} response;
 
182
 
 
183
typedef struct {
 
184
        buffer *scheme;
 
185
        buffer *authority;
 
186
        buffer *path;
 
187
        buffer *path_raw;
 
188
        buffer *query;
 
189
} request_uri;
 
190
 
 
191
typedef struct {
 
192
        buffer *path;
 
193
        buffer *basedir; /* path = "(basedir)(.*)" */
 
194
        
 
195
        buffer *doc_root; /* path = doc_root + rel_path */
 
196
        buffer *rel_path;
 
197
        
 
198
        buffer *etag;
 
199
} physical;
 
200
 
 
201
typedef struct {
 
202
        buffer *name;
 
203
        buffer *etag;
 
204
        
 
205
        struct stat st;
 
206
        
 
207
        time_t stat_ts;
 
208
        
 
209
#ifdef HAVE_FAM_H
 
210
        int    dir_version;
 
211
        int    dir_ndx;
 
212
#endif
 
213
 
 
214
        buffer *content_type;
 
215
} stat_cache_entry;
 
216
 
 
217
typedef struct {
 
218
        splay_tree *files; /* the nodes of the tree are stat_cache_entry's */
 
219
        
 
220
        buffer *dir_name; /* for building the dirname from the filename */
 
221
#ifdef HAVE_FAM_H
 
222
        splay_tree *dirs; /* the nodes of the tree are fam_dir_entry */
 
223
 
 
224
        FAMConnection *fam;
 
225
        int    fam_fcce_ndx;
 
226
#endif
 
227
} stat_cache;
 
228
 
 
229
typedef struct {
 
230
        array *mimetypes;
 
231
        
 
232
        /* virtual-servers */
 
233
        buffer *document_root;
 
234
        buffer *server_name;
 
235
        buffer *error_handler;
 
236
        buffer *server_tag;
 
237
        buffer *dirlist_encoding;
 
238
        buffer *errorfile_prefix;
 
239
        
 
240
        unsigned short max_keep_alive_requests;
 
241
        unsigned short max_keep_alive_idle;
 
242
        unsigned short max_read_idle;
 
243
        unsigned short max_write_idle;
 
244
        unsigned short use_xattr;
 
245
        unsigned short follow_symlink;
 
246
        unsigned short range_requests;
 
247
        
 
248
        /* debug */
 
249
        
 
250
        unsigned short log_file_not_found;
 
251
        unsigned short log_request_header;
 
252
        unsigned short log_request_handling;
 
253
        unsigned short log_response_header;
 
254
        unsigned short log_condition_handling;
 
255
        
 
256
        
 
257
        /* server wide */
 
258
        buffer *ssl_pemfile;
 
259
        buffer *ssl_ca_file;
 
260
        unsigned short use_ipv6;
 
261
        unsigned short is_ssl;
 
262
        unsigned short allow_http11;
 
263
        unsigned short force_lower_case; /* if the FS is case-insensitive, force all files to lower-case */
 
264
        unsigned short max_request_size;
 
265
 
 
266
        unsigned short kbytes_per_second; /* connection kb/s limit */
 
267
 
 
268
        /* configside */
 
269
        unsigned short global_kbytes_per_second; /*  */
 
270
 
 
271
        off_t  global_bytes_per_second_cnt; 
 
272
        /* server-wide traffic-shaper
 
273
         * 
 
274
         * each context has the counter which is inited once
 
275
         * a second by the global_kbytes_per_second config-var
 
276
         *
 
277
         * as soon as global_kbytes_per_second gets below 0
 
278
         * the connected conns are "offline" a little bit
 
279
         *
 
280
         * the problem:
 
281
         * we somehow have to loose our "we are writable" signal 
 
282
         * on the way.
 
283
         * 
 
284
         */
 
285
        off_t *global_bytes_per_second_cnt_ptr; /*  */
 
286
        
 
287
#ifdef USE_OPENSSL
 
288
        SSL_CTX *ssl_ctx;
 
289
#endif
 
290
} specific_config;
 
291
 
 
292
typedef enum { CON_STATE_CONNECT, CON_STATE_REQUEST_START, CON_STATE_READ, CON_STATE_REQUEST_END, CON_STATE_READ_POST, CON_STATE_HANDLE_REQUEST, CON_STATE_RESPONSE_START, CON_STATE_WRITE, CON_STATE_RESPONSE_END, CON_STATE_ERROR, CON_STATE_CLOSE } connection_state_t;
 
293
 
 
294
typedef enum { COND_RESULT_UNSET, COND_RESULT_FALSE, COND_RESULT_TRUE } cond_result_t;
 
295
typedef struct {
 
296
        cond_result_t result;
 
297
        int patterncount;
 
298
        int matches[3 * 10];
 
299
        buffer *comp_value; /* just a pointer */
 
300
} cond_cache_t;
 
301
 
 
302
typedef struct {
 
303
        connection_state_t state;
 
304
        
 
305
        /* timestamps */
 
306
        time_t read_idle_ts;
 
307
        time_t close_timeout_ts;
 
308
        time_t write_request_ts;
 
309
        
 
310
        time_t connection_start;
 
311
        time_t request_start;
 
312
        
 
313
        struct timeval start_tv;
 
314
        
 
315
        size_t request_count;        /* number of requests handled in this connection */
 
316
        size_t loops_per_request;    /* to catch endless loops in a single request
 
317
                                      * 
 
318
                                      * used by mod_rewrite, mod_fastcgi, ... and others
 
319
                                      * this is self-protection
 
320
                                      */
 
321
        
 
322
        int fd;                      /* the FD for this connection */
 
323
        int fde_ndx;                 /* index for the fdevent-handler */
 
324
        int ndx;                     /* reverse mapping to server->connection[ndx] */
 
325
        
 
326
        /* fd states */
 
327
        int is_readable;
 
328
        int is_writable;
 
329
        
 
330
        int     keep_alive;           /* only request.c can enable it, all other just disable */
 
331
        
 
332
        int file_started;
 
333
        int file_finished;
 
334
        
 
335
        chunkqueue *write_queue;      /* a large queue for low-level write ( HTTP response ) [ file, mem ] */
 
336
        chunkqueue *read_queue;       /* a small queue for low-level read ( HTTP request ) [ mem ] */
 
337
        chunkqueue *request_content_queue; /* takes request-content into tempfile if necessary [ tempfile, mem ]*/
 
338
        
 
339
        int traffic_limit_reached;
 
340
        
 
341
        off_t bytes_written;          /* used by mod_accesslog, mod_rrd */
 
342
        off_t bytes_written_cur_second; /* used by mod_accesslog, mod_rrd */
 
343
        off_t bytes_read;             /* used by mod_accesslog, mod_rrd */
 
344
        off_t bytes_header;
 
345
        
 
346
        int http_status;
 
347
        
 
348
        sock_addr dst_addr;
 
349
        buffer *dst_addr_buf;
 
350
 
 
351
        /* request */
 
352
        buffer *parse_request;
 
353
        unsigned int parsed_response; /* bitfield which contains the important header-fields of the parsed response header */
 
354
        
 
355
        request  request;
 
356
        request_uri uri;
 
357
        physical physical; 
 
358
        response response;
 
359
        
 
360
        size_t header_len;
 
361
        
 
362
        buffer *authed_user;
 
363
        array  *environment; /* used to pass lighttpd internal stuff to the FastCGI/CGI apps, setenv does that */
 
364
        
 
365
        /* response */
 
366
        int    got_response;
 
367
        
 
368
        int    in_joblist;
 
369
        
 
370
        connection_type mode;
 
371
        
 
372
        void **plugin_ctx;           /* plugin connection specific config */
 
373
        
 
374
        specific_config conf;        /* global connection specific config */
 
375
        cond_cache_t *cond_cache;
 
376
        
 
377
        buffer *server_name;
 
378
        
 
379
        /* error-handler */
 
380
        buffer *error_handler;
 
381
        int error_handler_saved_status;
 
382
        int in_error_handler;
 
383
        
 
384
        void *srv_socket;   /* reference to the server-socket (typecast to server_socket) */
 
385
        
 
386
#ifdef USE_OPENSSL
 
387
        SSL *ssl;
 
388
#endif
 
389
} connection;
 
390
 
 
391
typedef struct {
 
392
        connection **ptr;
 
393
        size_t size;
 
394
        size_t used;
 
395
} connections;
 
396
 
 
397
 
 
398
#ifdef HAVE_IPV6
 
399
typedef struct {
 
400
        int family;
 
401
        union {
 
402
                struct in6_addr ipv6;
 
403
                struct in_addr  ipv4;
 
404
        } addr;
 
405
        char b2[INET6_ADDRSTRLEN + 1];
 
406
        time_t ts;
 
407
} inet_ntop_cache_type;
 
408
#endif
 
409
 
 
410
 
 
411
typedef struct {
 
412
        buffer *uri;
 
413
        time_t mtime;
 
414
        int http_status;
 
415
} realpath_cache_type;
 
416
 
 
417
typedef struct {
 
418
        time_t  mtime;  /* the key */
 
419
        buffer *str;    /* a buffer for the string represenation */
 
420
} mtime_cache_type;
 
421
 
 
422
typedef struct {
 
423
        void  *ptr;
 
424
        size_t used;
 
425
        size_t size;
 
426
} buffer_plugin;
 
427
 
 
428
typedef struct {
 
429
        unsigned short port;
 
430
        buffer *bindhost;
 
431
        
 
432
        buffer *errorlog_file;
 
433
        unsigned short errorlog_use_syslog;
 
434
        
 
435
        unsigned short dont_daemonize;
 
436
        buffer *changeroot;
 
437
        buffer *username;
 
438
        buffer *groupname;
 
439
        
 
440
        buffer *pid_file;
 
441
        
 
442
        buffer *event_handler;
 
443
        
 
444
        buffer *modules_dir;
 
445
        buffer *network_backend;
 
446
        array *modules;
 
447
        array *upload_tempdirs;
 
448
        
 
449
        unsigned short max_worker;
 
450
        unsigned short max_fds;
 
451
        unsigned short max_conns;
 
452
        unsigned short max_request_size;
 
453
        
 
454
        unsigned short log_request_header_on_error;
 
455
        unsigned short log_state_handling;
 
456
        
 
457
        enum { STAT_CACHE_ENGINE_UNSET, 
 
458
                        STAT_CACHE_ENGINE_NONE, 
 
459
                        STAT_CACHE_ENGINE_SIMPLE, 
 
460
                        STAT_CACHE_ENGINE_FAM 
 
461
        } stat_cache_engine;
 
462
} server_config;
 
463
 
 
464
typedef struct {
 
465
        sock_addr addr;
 
466
        int       fd;
 
467
        int       fde_ndx;
 
468
        
 
469
        buffer *ssl_pemfile;
 
470
        buffer *ssl_ca_file;
 
471
        unsigned short use_ipv6;
 
472
        unsigned short is_ssl;
 
473
        
 
474
        buffer *srv_token;
 
475
        
 
476
#ifdef USE_OPENSSL
 
477
        SSL_CTX *ssl_ctx;
 
478
#endif
 
479
} server_socket;
 
480
 
 
481
typedef struct {
 
482
        server_socket **ptr;
 
483
        
 
484
        size_t size;
 
485
        size_t used;
 
486
} server_socket_array;
 
487
 
 
488
typedef struct server {
 
489
        server_socket_array srv_sockets;
 
490
        
 
491
        /* the errorlog */
 
492
        int errorlog_fd;
 
493
        enum { ERRORLOG_STDERR, ERRORLOG_FILE, ERRORLOG_SYSLOG } errorlog_mode;
 
494
        buffer *errorlog_buf;
 
495
        
 
496
        fdevents *ev, *ev_ins;
 
497
        
 
498
        buffer_plugin plugins;
 
499
        void *plugin_slots;
 
500
        
 
501
        /* counters */
 
502
        int con_opened;
 
503
        int con_read;
 
504
        int con_written;
 
505
        int con_closed;
 
506
        
 
507
        int ssl_is_init;
 
508
        
 
509
        int max_fds;    /* max possible fds */
 
510
        int cur_fds;    /* currently used fds */
 
511
        int want_fds;   /* waiting fds */
 
512
        int sockets_disabled;
 
513
        
 
514
        size_t max_conns;
 
515
 
 
516
        /* buffers */
 
517
        buffer *parse_full_path;
 
518
        buffer *response_header;
 
519
        buffer *response_range;
 
520
        buffer *tmp_buf;
 
521
        
 
522
        buffer *tmp_chunk_len;
 
523
        
 
524
        buffer *empty_string; /* is necessary for cond_match */
 
525
 
 
526
        buffer *cond_check_buf;
 
527
        
 
528
        /* caches */
 
529
#ifdef HAVE_IPV6
 
530
        inet_ntop_cache_type inet_ntop_cache[INET_NTOP_CACHE_MAX];
 
531
#endif
 
532
        mtime_cache_type mtime_cache[FILE_CACHE_MAX];
 
533
 
 
534
        array *split_vals;
 
535
        
 
536
        /* Timestamps */
 
537
        time_t cur_ts;
 
538
        time_t last_generated_date_ts;
 
539
        time_t last_generated_debug_ts;
 
540
        time_t startup_ts;
 
541
        
 
542
        buffer *ts_debug_str;
 
543
        buffer *ts_date_str;
 
544
        
 
545
        /* config-file */
 
546
        array *config;
 
547
        array *config_touched;
 
548
        
 
549
        array *config_context;
 
550
        specific_config **config_storage;
 
551
        
 
552
        server_config  srvconf;
 
553
        
 
554
        int config_deprecated;
 
555
        
 
556
        connections *conns;
 
557
        connections *joblist;
 
558
        connections *fdwaitqueue;
 
559
        
 
560
        stat_cache  *stat_cache;
 
561
        
 
562
        fdevent_handler_t event_handler;
 
563
 
 
564
        int (* network_backend_write)(struct server *srv, connection *con, int fd, chunkqueue *cq);
 
565
        int (* network_backend_read)(struct server *srv, connection *con, int fd, chunkqueue *cq);
 
566
#ifdef USE_OPENSSL
 
567
        int (* network_ssl_backend_write)(struct server *srv, connection *con, SSL *ssl, chunkqueue *cq);
 
568
        int (* network_ssl_backend_read)(struct server *srv, connection *con, SSL *ssl, chunkqueue *cq);
 
569
#endif
 
570
 
 
571
        uid_t uid;
 
572
        gid_t gid;
 
573
} server;
 
574
 
 
575
 
 
576
#endif