patch -p0 < ../nginx_upstream_hash/nginx.patch
[nginx.git] / nginx / src / http / ngx_http_upstream.h
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #ifndef _NGX_HTTP_UPSTREAM_H_INCLUDED_
8 #define _NGX_HTTP_UPSTREAM_H_INCLUDED_
9
10
11 #include <ngx_config.h>
12 #include <ngx_core.h>
13 #include <ngx_event.h>
14 #include <ngx_event_connect.h>
15 #include <ngx_event_pipe.h>
16 #include <ngx_http.h>
17
18
19 #define NGX_HTTP_UPSTREAM_FT_ERROR           0x00000002
20 #define NGX_HTTP_UPSTREAM_FT_TIMEOUT         0x00000004
21 #define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER  0x00000008
22 #define NGX_HTTP_UPSTREAM_FT_HTTP_500        0x00000010
23 #define NGX_HTTP_UPSTREAM_FT_HTTP_502        0x00000020
24 #define NGX_HTTP_UPSTREAM_FT_HTTP_503        0x00000040
25 #define NGX_HTTP_UPSTREAM_FT_HTTP_504        0x00000080
26 #define NGX_HTTP_UPSTREAM_FT_HTTP_404        0x00000100
27 #define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x00000200
28 #define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x00000400
29 #define NGX_HTTP_UPSTREAM_FT_NOLIVE          0x40000000
30 #define NGX_HTTP_UPSTREAM_FT_OFF             0x80000000
31
32 #define NGX_HTTP_UPSTREAM_FT_STATUS          (NGX_HTTP_UPSTREAM_FT_HTTP_500  \
33                                              |NGX_HTTP_UPSTREAM_FT_HTTP_502  \
34                                              |NGX_HTTP_UPSTREAM_FT_HTTP_503  \
35                                              |NGX_HTTP_UPSTREAM_FT_HTTP_504  \
36                                              |NGX_HTTP_UPSTREAM_FT_HTTP_404)
37
38 #define NGX_HTTP_UPSTREAM_INVALID_HEADER     40
39
40
41 typedef struct {
42     ngx_msec_t                      bl_time;
43     ngx_uint_t                      bl_state;
44
45     ngx_uint_t                      status;
46     time_t                          response_sec;
47     ngx_uint_t                      response_msec;
48     off_t                           response_length;
49
50     ngx_str_t                      *peer;
51 } ngx_http_upstream_state_t;
52
53
54 typedef struct {
55     ngx_hash_t                      headers_in_hash;
56     ngx_array_t                     upstreams;
57                                              /* ngx_http_upstream_srv_conf_t */
58 } ngx_http_upstream_main_conf_t;
59
60 typedef struct ngx_http_upstream_srv_conf_s  ngx_http_upstream_srv_conf_t;
61
62 typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
63     ngx_http_upstream_srv_conf_t *us);
64 typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
65     ngx_http_upstream_srv_conf_t *us);
66
67
68 typedef struct {
69     ngx_http_upstream_init_pt       init_upstream;
70     ngx_http_upstream_init_peer_pt  init;
71     void                           *data;
72 } ngx_http_upstream_peer_t;
73
74
75 typedef struct {
76     ngx_peer_addr_t                *addrs;
77     ngx_uint_t                      naddrs;
78     ngx_uint_t                      weight;
79     ngx_uint_t                      max_fails;
80     time_t                          fail_timeout;
81
82     unsigned                        down:1;
83     unsigned                        backup:1;
84 } ngx_http_upstream_server_t;
85
86
87 #define NGX_HTTP_UPSTREAM_CREATE        0x0001
88 #define NGX_HTTP_UPSTREAM_WEIGHT        0x0002
89 #define NGX_HTTP_UPSTREAM_MAX_FAILS     0x0004
90 #define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT  0x0008
91 #define NGX_HTTP_UPSTREAM_DOWN          0x0010
92 #define NGX_HTTP_UPSTREAM_BACKUP        0x0020
93
94
95 struct ngx_http_upstream_srv_conf_s {
96     ngx_http_upstream_peer_t        peer;
97     void                          **srv_conf;
98
99     ngx_array_t                    *servers;   /* ngx_http_upstream_server_t */
100
101     ngx_array_t                    *values;
102     ngx_array_t                    *lengths;
103     ngx_uint_t                      retries;
104
105     ngx_uint_t                      flags;
106     ngx_str_t                       host;
107     u_char                         *file_name;
108     ngx_uint_t                      line;
109     in_port_t                       port;
110     in_port_t                       default_port;
111 };
112
113
114 typedef struct {
115     ngx_http_upstream_srv_conf_t   *upstream;
116
117     ngx_msec_t                      connect_timeout;
118     ngx_msec_t                      send_timeout;
119     ngx_msec_t                      read_timeout;
120     ngx_msec_t                      timeout;
121
122     size_t                          send_lowat;
123     size_t                          buffer_size;
124
125     size_t                          busy_buffers_size;
126     size_t                          max_temp_file_size;
127     size_t                          temp_file_write_size;
128
129     size_t                          busy_buffers_size_conf;
130     size_t                          max_temp_file_size_conf;
131     size_t                          temp_file_write_size_conf;
132
133     ngx_uint_t                      next_upstream;
134     ngx_uint_t                      store_access;
135
136     ngx_bufs_t                      bufs;
137
138     ngx_flag_t                      buffering;
139     ngx_flag_t                      pass_request_headers;
140     ngx_flag_t                      pass_request_body;
141
142     ngx_flag_t                      ignore_client_abort;
143     ngx_flag_t                      intercept_errors;
144     ngx_flag_t                      cyclic_temp_file;
145
146     ngx_path_t                     *temp_path;
147
148     ngx_hash_t                      hide_headers_hash;
149     ngx_array_t                    *hide_headers;
150     ngx_array_t                    *pass_headers;
151
152     ngx_array_t                    *store_lengths;
153     ngx_array_t                    *store_values;
154
155     signed                          store:2;
156     unsigned                        intercept_404:1;
157     unsigned                        change_buffering:1;
158
159 #if (NGX_HTTP_SSL)
160     ngx_ssl_t                      *ssl;
161     ngx_flag_t                      ssl_session_reuse;
162 #endif
163
164 } ngx_http_upstream_conf_t;
165
166
167 typedef struct {
168     ngx_str_t                       name;
169     ngx_http_header_handler_pt      handler;
170     ngx_uint_t                      offset;
171     ngx_http_header_handler_pt      copy_handler;
172     ngx_uint_t                      conf;
173     ngx_uint_t                      redirect;  /* unsigned   redirect:1; */
174 } ngx_http_upstream_header_t;
175
176
177 typedef struct {
178     ngx_list_t                      headers;
179
180     ngx_uint_t                      status_n;
181     ngx_str_t                       status_line;
182
183     ngx_table_elt_t                *status;
184     ngx_table_elt_t                *date;
185     ngx_table_elt_t                *server;
186     ngx_table_elt_t                *connection;
187
188     ngx_table_elt_t                *expires;
189     ngx_table_elt_t                *etag;
190     ngx_table_elt_t                *x_accel_expires;
191     ngx_table_elt_t                *x_accel_redirect;
192     ngx_table_elt_t                *x_accel_limit_rate;
193
194     ngx_table_elt_t                *content_type;
195     ngx_table_elt_t                *content_length;
196
197     ngx_table_elt_t                *last_modified;
198     ngx_table_elt_t                *location;
199     ngx_table_elt_t                *accept_ranges;
200     ngx_table_elt_t                *www_authenticate;
201
202 #if (NGX_HTTP_GZIP)
203     ngx_table_elt_t                *content_encoding;
204 #endif
205
206     off_t                           content_length_n;
207
208     ngx_array_t                     cache_control;
209 } ngx_http_upstream_headers_in_t;
210
211
212 typedef struct {
213     ngx_str_t                       host;
214     in_port_t                       port;
215     ngx_uint_t                      no_port; /* unsigned  no_port:1 */
216
217     ngx_uint_t                      naddrs;
218     in_addr_t                      *addrs;
219
220     struct sockaddr                *sockaddr;
221     socklen_t                       socklen;
222
223     ngx_resolver_ctx_t             *ctx;
224 } ngx_http_upstream_resolved_t;
225
226
227 typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
228     ngx_http_upstream_t *u);
229
230
231 struct ngx_http_upstream_s {
232     ngx_http_upstream_handler_pt    read_event_handler;
233     ngx_http_upstream_handler_pt    write_event_handler;
234
235     ngx_peer_connection_t           peer;
236
237     ngx_event_pipe_t               *pipe;
238
239     ngx_chain_t                    *request_bufs;
240
241     ngx_output_chain_ctx_t          output;
242     ngx_chain_writer_ctx_t          writer;
243
244     ngx_http_upstream_conf_t       *conf;
245
246     ngx_http_upstream_headers_in_t  headers_in;
247
248     ngx_http_upstream_resolved_t   *resolved;
249
250     ngx_buf_t                       buffer;
251     size_t                          length;
252
253     ngx_chain_t                    *out_bufs;
254     ngx_chain_t                    *busy_bufs;
255     ngx_chain_t                    *free_bufs;
256
257     ngx_int_t                     (*input_filter_init)(void *data);
258     ngx_int_t                     (*input_filter)(void *data, ssize_t bytes);
259     void                           *input_filter_ctx;
260
261     ngx_int_t                     (*create_request)(ngx_http_request_t *r);
262     ngx_int_t                     (*reinit_request)(ngx_http_request_t *r);
263     ngx_int_t                     (*process_header)(ngx_http_request_t *r);
264     void                          (*abort_request)(ngx_http_request_t *r);
265     void                          (*finalize_request)(ngx_http_request_t *r,
266                                         ngx_int_t rc);
267     ngx_int_t                     (*rewrite_redirect)(ngx_http_request_t *r,
268                                         ngx_table_elt_t *h, size_t prefix);
269
270     ngx_msec_t                      timeout;
271
272     ngx_http_upstream_state_t      *state;
273
274     ngx_str_t                       method;
275     ngx_str_t                       schema;
276     ngx_str_t                       uri;
277
278     ngx_http_cleanup_pt            *cleanup;
279
280     unsigned                        store:1;
281     unsigned                        cacheable:1;
282     unsigned                        accel:1;
283     unsigned                        ssl:1;
284
285     unsigned                        buffering:1;
286
287     unsigned                        request_sent:1;
288     unsigned                        header_sent:1;
289 };
290
291
292 typedef struct {
293     ngx_uint_t                      status;
294     ngx_uint_t                      mask;
295 } ngx_http_upstream_next_t;
296
297
298 ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
299     ngx_http_variable_value_t *v, uintptr_t data);
300
301 void ngx_http_upstream_init(ngx_http_request_t *r);
302 ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
303     ngx_url_t *u, ngx_uint_t flags);
304 ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
305     ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
306     ngx_str_t *default_hide_headers, ngx_hash_init_t *hash);
307
308
309 #define ngx_http_conf_upstream_srv_conf(uscf, module)                         \
310     uscf->srv_conf[module.ctx_index]
311
312
313 extern ngx_module_t  ngx_http_upstream_module;
314
315
316 #endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */