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