7ad18edd242b62da4de5ebc5f2dcc1b2d202cb2f
[nginx.git] / nginx / src / http / ngx_http_core_module.h
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #ifndef _NGX_HTTP_CORE_H_INCLUDED_
8 #define _NGX_HTTP_CORE_H_INCLUDED_
9
10
11 #include <ngx_config.h>
12 #include <ngx_core.h>
13 #include <ngx_http.h>
14
15
16 #define NGX_HTTP_GZIP_PROXIED_OFF       0x0002
17 #define NGX_HTTP_GZIP_PROXIED_EXPIRED   0x0004
18 #define NGX_HTTP_GZIP_PROXIED_NO_CACHE  0x0008
19 #define NGX_HTTP_GZIP_PROXIED_NO_STORE  0x0010
20 #define NGX_HTTP_GZIP_PROXIED_PRIVATE   0x0020
21 #define NGX_HTTP_GZIP_PROXIED_NO_LM     0x0040
22 #define NGX_HTTP_GZIP_PROXIED_NO_ETAG   0x0080
23 #define NGX_HTTP_GZIP_PROXIED_AUTH      0x0100
24 #define NGX_HTTP_GZIP_PROXIED_ANY       0x0200
25
26
27 #define NGX_HTTP_SATISFY_ALL            0
28 #define NGX_HTTP_SATISFY_ANY            1
29
30
31 #define NGX_HTTP_IMS_OFF                0
32 #define NGX_HTTP_IMS_EXACT              1
33 #define NGX_HTTP_IMS_BEFORE             2
34
35
36 typedef struct ngx_http_location_tree_node_s  ngx_http_location_tree_node_t;
37 typedef struct ngx_http_core_loc_conf_s  ngx_http_core_loc_conf_t;
38
39
40 typedef struct {
41     unsigned                   default_server:1;
42     unsigned                   bind:1;
43     unsigned                   wildcard:1;
44 #if (NGX_HTTP_SSL)
45     unsigned                   ssl:1;
46 #endif
47
48     int                        backlog;
49     int                        rcvbuf;
50     int                        sndbuf;
51
52 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
53     char                      *accept_filter;
54 #endif
55 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
56     ngx_uint_t                 deferred_accept;
57 #endif
58
59     u_char                     addr[NGX_SOCKADDR_STRLEN + 1];
60 } ngx_http_listen_conf_t;
61
62
63 typedef struct {
64     u_char                     sockaddr[NGX_SOCKADDRLEN];
65     socklen_t                  socklen;
66
67     u_char                    *file_name;
68     ngx_uint_t                 line;
69
70     ngx_http_listen_conf_t     conf;
71 } ngx_http_listen_t;
72
73
74 typedef enum {
75     NGX_HTTP_POST_READ_PHASE = 0,
76
77     NGX_HTTP_SERVER_REWRITE_PHASE,
78
79     NGX_HTTP_FIND_CONFIG_PHASE,
80     NGX_HTTP_REWRITE_PHASE,
81     NGX_HTTP_POST_REWRITE_PHASE,
82
83     NGX_HTTP_PREACCESS_PHASE,
84
85     NGX_HTTP_ACCESS_PHASE,
86     NGX_HTTP_POST_ACCESS_PHASE,
87
88     NGX_HTTP_TRY_FILES_PHASE,
89     NGX_HTTP_CONTENT_PHASE,
90
91     NGX_HTTP_LOG_PHASE
92 } ngx_http_phases;
93
94 typedef struct ngx_http_phase_handler_s  ngx_http_phase_handler_t;
95
96 typedef ngx_int_t (*ngx_http_phase_handler_pt)(ngx_http_request_t *r,
97     ngx_http_phase_handler_t *ph);
98
99 struct ngx_http_phase_handler_s {
100     ngx_http_phase_handler_pt  checker;
101     ngx_http_handler_pt        handler;
102     ngx_uint_t                 next;
103 };
104
105
106 typedef struct {
107     ngx_http_phase_handler_t  *handlers;
108     ngx_uint_t                 server_rewrite_index;
109     ngx_uint_t                 location_rewrite_index;
110 } ngx_http_phase_engine_t;
111
112
113 typedef struct {
114     ngx_array_t                handlers;
115 } ngx_http_phase_t;
116
117
118 typedef struct {
119     ngx_array_t                servers;         /* ngx_http_core_srv_conf_t */
120
121     ngx_http_phase_engine_t    phase_engine;
122
123     ngx_hash_t                 headers_in_hash;
124
125     ngx_hash_t                 variables_hash;
126
127     ngx_array_t                variables;       /* ngx_http_variable_t */
128
129     ngx_uint_t                 server_names_hash_max_size;
130     ngx_uint_t                 server_names_hash_bucket_size;
131
132     ngx_uint_t                 variables_hash_max_size;
133     ngx_uint_t                 variables_hash_bucket_size;
134
135     ngx_hash_keys_arrays_t    *variables_keys;
136
137     ngx_uint_t                 try_files;       /* unsigned  try_files:1 */
138
139     ngx_http_phase_t           phases[NGX_HTTP_LOG_PHASE + 1];
140 } ngx_http_core_main_conf_t;
141
142
143 typedef struct {
144     /* array of the ngx_http_listen_t, "listen" directive */
145     ngx_array_t                 listen;
146
147     /* array of the ngx_http_server_name_t, "server_name" directive */
148     ngx_array_t                 server_names;
149
150     /* server ctx */
151     ngx_http_conf_ctx_t        *ctx;
152
153     ngx_http_virtual_names_t   *virtual_names;
154
155     ngx_str_t                   server_name;
156
157     size_t                      connection_pool_size;
158     size_t                      request_pool_size;
159     size_t                      client_header_buffer_size;
160
161     ngx_bufs_t                  large_client_header_buffers;
162
163     ngx_msec_t                  client_header_timeout;
164
165     ngx_flag_t                  ignore_invalid_headers;
166     ngx_flag_t                  merge_slashes;
167     ngx_flag_t                  underscores_in_headers;
168
169     ngx_http_core_loc_conf_t  **named_locations;
170 } ngx_http_core_srv_conf_t;
171
172
173 /* list of structures to find core_srv_conf quickly at run time */
174
175
176 typedef struct {
177     /* the default server configuration for this address:port */
178     ngx_http_core_srv_conf_t  *core_srv_conf;
179
180 #if (NGX_HTTP_SSL)
181     ngx_uint_t                 ssl;   /* unsigned  ssl:1; */
182 #endif
183 } ngx_http_addr_conf_t;
184
185
186 typedef struct {
187     in_addr_t                  addr;
188     ngx_http_addr_conf_t       conf;
189 } ngx_http_in_addr_t;
190
191
192 #if (NGX_HAVE_INET6)
193
194 typedef struct {
195     struct in6_addr            addr6;
196     ngx_http_addr_conf_t       conf;
197 } ngx_http_in6_addr_t;
198
199 #endif
200
201
202 typedef struct {
203     /* ngx_http_in_addr_t or ngx_http_in6_addr_t */
204     void                      *addrs;
205     ngx_uint_t                 naddrs;
206 } ngx_http_port_t;
207
208
209 typedef struct {
210     ngx_int_t                  family;
211     in_port_t                  port;
212     ngx_array_t                addrs;     /* array of ngx_http_conf_addr_t */
213 } ngx_http_conf_port_t;
214
215
216 typedef struct {
217     struct sockaddr           *sockaddr;
218     socklen_t                  socklen;
219
220     ngx_hash_t                 hash;
221     ngx_hash_wildcard_t       *wc_head;
222     ngx_hash_wildcard_t       *wc_tail;
223
224     ngx_array_t                names;      /* array of ngx_http_server_name_t */
225
226 #if (NGX_PCRE)
227     ngx_uint_t                 nregex;
228     ngx_http_server_name_t    *regex;
229 #endif
230
231     /* the default server configuration for this address:port */
232     ngx_http_core_srv_conf_t  *core_srv_conf;
233
234     unsigned                   default_server:1;
235     unsigned                   bind:1;
236     unsigned                   wildcard:1;
237 #if (NGX_HTTP_SSL)
238     unsigned                   ssl:1;
239 #endif
240
241     ngx_http_listen_conf_t    *listen_conf;
242 } ngx_http_conf_addr_t;
243
244
245 struct ngx_http_server_name_s {
246 #if (NGX_PCRE)
247     ngx_regex_t               *regex;
248     ngx_uint_t                 captures;      /* unsigned  captures:1; */
249 #endif
250     ngx_http_core_srv_conf_t  *core_srv_conf; /* virtual name server conf */
251     ngx_str_t                  name;
252 };
253
254
255 typedef struct {
256     ngx_int_t                  status;
257     ngx_int_t                  overwrite;
258     ngx_str_t                  uri;
259     ngx_str_t                  args;
260     ngx_array_t               *uri_lengths;
261     ngx_array_t               *uri_values;
262 } ngx_http_err_page_t;
263
264
265 typedef struct {
266     ngx_array_t               *lengths;
267     ngx_array_t               *values;
268     ngx_str_t                  name;
269     ngx_uint_t                 test_dir;   /* unsigned  test_dir:1; */
270 } ngx_http_try_file_t;
271
272
273 struct ngx_http_core_loc_conf_s {
274     ngx_str_t     name;          /* location name */
275
276 #if (NGX_PCRE)
277     ngx_regex_t  *regex;
278
279     unsigned      captures:1;
280 #endif
281
282     unsigned      noname:1;   /* "if () {}" block or limit_except */
283     unsigned      named:1;
284
285     unsigned      exact_match:1;
286     unsigned      noregex:1;
287
288     unsigned      auto_redirect:1;
289     unsigned      alias:1;
290 #if (NGX_HTTP_GZIP)
291     unsigned      gzip_disable_msie6:2;
292 #endif
293
294     ngx_http_location_tree_node_t   *static_locations;
295 #if (NGX_PCRE)
296     ngx_http_core_loc_conf_t       **regex_locations;
297 #endif
298
299     /* pointer to the modules' loc_conf */
300     void        **loc_conf;
301
302     uint32_t      limit_except;
303     void        **limit_except_loc_conf;
304
305     ngx_http_handler_pt  handler;
306
307     ngx_str_t     root;                    /* root, alias */
308     ngx_str_t     post_action;
309
310     ngx_array_t  *root_lengths;
311     ngx_array_t  *root_values;
312
313     ngx_array_t  *types;
314     ngx_hash_t    types_hash;
315     ngx_str_t     default_type;
316
317     off_t         client_max_body_size;    /* client_max_body_size */
318     off_t         directio;                /* directio */
319
320     size_t        client_body_buffer_size; /* client_body_buffer_size */
321     size_t        send_lowat;              /* send_lowat */
322     size_t        postpone_output;         /* postpone_output */
323     size_t        limit_rate;              /* limit_rate */
324     size_t        sendfile_max_chunk;      /* sendfile_max_chunk */
325
326     ngx_msec_t    client_body_timeout;     /* client_body_timeout */
327     ngx_msec_t    send_timeout;            /* send_timeout */
328     ngx_msec_t    keepalive_timeout;       /* keepalive_timeout */
329     ngx_msec_t    lingering_time;          /* lingering_time */
330     ngx_msec_t    lingering_timeout;       /* lingering_timeout */
331     ngx_msec_t    resolver_timeout;        /* resolver_timeout */
332
333     ngx_resolver_t  *resolver;             /* resolver */
334
335     time_t        keepalive_header;        /* keepalive_timeout */
336
337     ngx_uint_t    satisfy;                 /* satisfy */
338     ngx_uint_t    if_modified_since;       /* if_modified_since */
339
340     ngx_flag_t    internal;                /* internal */
341     ngx_flag_t    client_body_in_file_only; /* client_body_in_file_only */
342     ngx_flag_t    sendfile;                /* sendfile */
343     ngx_flag_t    tcp_nopush;              /* tcp_nopush */
344     ngx_flag_t    tcp_nodelay;             /* tcp_nodelay */
345     ngx_flag_t    reset_timedout_connection; /* reset_timedout_connection */
346     ngx_flag_t    server_name_in_redirect; /* server_name_in_redirect */
347     ngx_flag_t    port_in_redirect;        /* port_in_redirect */
348     ngx_flag_t    msie_padding;            /* msie_padding */
349     ngx_flag_t    msie_refresh;            /* msie_refresh */
350     ngx_flag_t    log_not_found;           /* log_not_found */
351     ngx_flag_t    log_subrequest;          /* log_subrequest */
352     ngx_flag_t    recursive_error_pages;   /* recursive_error_pages */
353     ngx_flag_t    server_tokens;           /* server_tokens */
354
355 #if (NGX_HTTP_GZIP)
356     ngx_flag_t    gzip_vary;               /* gzip_vary */
357
358     ngx_uint_t    gzip_http_version;       /* gzip_http_version */
359     ngx_uint_t    gzip_proxied;            /* gzip_proxied */
360
361 #if (NGX_PCRE)
362     ngx_array_t  *gzip_disable;            /* gzip_disable */
363 #endif
364 #endif
365
366     ngx_array_t  *error_pages;             /* error_page */
367     ngx_http_try_file_t    *try_files;     /* try_files */
368
369     ngx_path_t   *client_body_temp_path;   /* client_body_temp_path */
370
371     ngx_open_file_cache_t  *open_file_cache;
372     time_t        open_file_cache_valid;
373     ngx_uint_t    open_file_cache_min_uses;
374     ngx_flag_t    open_file_cache_errors;
375     ngx_flag_t    open_file_cache_events;
376
377     ngx_log_t    *err_log;
378
379     ngx_uint_t    types_hash_max_size;
380     ngx_uint_t    types_hash_bucket_size;
381
382     ngx_queue_t  *locations;
383
384 #if 0
385     ngx_http_core_loc_conf_t  *prev_location;
386 #endif
387 };
388
389
390 typedef struct {
391     ngx_queue_t                      queue;
392     ngx_http_core_loc_conf_t        *exact;
393     ngx_http_core_loc_conf_t        *inclusive;
394     ngx_str_t                       *name;
395     u_char                          *file_name;
396     ngx_uint_t                       line;
397     ngx_queue_t                      list;
398 } ngx_http_location_queue_t;
399
400
401 struct ngx_http_location_tree_node_s {
402     ngx_http_location_tree_node_t   *left;
403     ngx_http_location_tree_node_t   *right;
404     ngx_http_location_tree_node_t   *tree;
405
406     ngx_http_core_loc_conf_t        *exact;
407     ngx_http_core_loc_conf_t        *inclusive;
408
409     u_char                           auto_redirect;
410     u_char                           len;
411     u_char                           name[1];
412 };
413
414
415 void ngx_http_core_run_phases(ngx_http_request_t *r);
416 ngx_int_t ngx_http_core_generic_phase(ngx_http_request_t *r,
417     ngx_http_phase_handler_t *ph);
418 ngx_int_t ngx_http_core_find_config_phase(ngx_http_request_t *r,
419     ngx_http_phase_handler_t *ph);
420 ngx_int_t ngx_http_core_post_rewrite_phase(ngx_http_request_t *r,
421     ngx_http_phase_handler_t *ph);
422 ngx_int_t ngx_http_core_access_phase(ngx_http_request_t *r,
423     ngx_http_phase_handler_t *ph);
424 ngx_int_t ngx_http_core_post_access_phase(ngx_http_request_t *r,
425     ngx_http_phase_handler_t *ph);
426 ngx_int_t ngx_http_core_try_files_phase(ngx_http_request_t *r,
427     ngx_http_phase_handler_t *ph);
428 ngx_int_t ngx_http_core_content_phase(ngx_http_request_t *r,
429     ngx_http_phase_handler_t *ph);
430
431
432 void *ngx_http_test_content_type(ngx_http_request_t *r, ngx_hash_t *types_hash);
433 ngx_int_t ngx_http_set_content_type(ngx_http_request_t *r);
434 ngx_int_t ngx_http_set_exten(ngx_http_request_t *r);
435 u_char *ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *name,
436     size_t *root_length, size_t reserved);
437 ngx_int_t ngx_http_auth_basic_user(ngx_http_request_t *r);
438 ngx_int_t ngx_http_server_addr(ngx_http_request_t *r, ngx_str_t *s);
439 #if (NGX_HTTP_GZIP)
440 ngx_int_t ngx_http_gzip_ok(ngx_http_request_t *r);
441 #endif
442
443
444 ngx_int_t ngx_http_subrequest(ngx_http_request_t *r,
445     ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **sr,
446     ngx_http_post_subrequest_t *psr, ngx_uint_t flags);
447 ngx_int_t ngx_http_internal_redirect(ngx_http_request_t *r,
448     ngx_str_t *uri, ngx_str_t *args);
449 ngx_int_t ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name);
450
451
452 ngx_http_cleanup_t *ngx_http_cleanup_add(ngx_http_request_t *r, size_t size);
453
454
455 typedef ngx_int_t (*ngx_http_output_header_filter_pt)(ngx_http_request_t *r);
456 typedef ngx_int_t (*ngx_http_output_body_filter_pt)
457     (ngx_http_request_t *r, ngx_chain_t *chain);
458
459
460 ngx_int_t ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *chain);
461 ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *chain);
462
463
464 extern ngx_module_t  ngx_http_core_module;
465
466 extern ngx_uint_t ngx_http_max_module;
467
468
469 #define ngx_http_clear_content_length(r)                                      \
470                                                                               \
471     r->headers_out.content_length_n = -1;                                     \
472     if (r->headers_out.content_length) {                                      \
473         r->headers_out.content_length->hash = 0;                              \
474         r->headers_out.content_length = NULL;                                 \
475     }
476                                                                               \
477 #define ngx_http_clear_accept_ranges(r)                                       \
478                                                                               \
479     r->allow_ranges = 0;                                                      \
480     if (r->headers_out.accept_ranges) {                                       \
481         r->headers_out.accept_ranges->hash = 0;                               \
482         r->headers_out.accept_ranges = NULL;                                  \
483     }
484
485 #define ngx_http_clear_last_modified(r)                                       \
486                                                                               \
487     r->headers_out.last_modified_time = -1;                                   \
488     if (r->headers_out.last_modified) {                                       \
489         r->headers_out.last_modified->hash = 0;                               \
490         r->headers_out.last_modified = NULL;                                  \
491     }
492
493
494 #endif /* _NGX_HTTP_CORE_H_INCLUDED_ */