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