0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #ifndef _NGX_HTTP_PROXY_HANDLER_H_INCLUDED_
|
|
8 #define _NGX_HTTP_PROXY_HANDLER_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 typedef enum {
|
|
20 NGX_HTTP_PROXY_CACHE_PASS = 1,
|
|
21 NGX_HTTP_PROXY_CACHE_BYPASS,
|
|
22 NGX_HTTP_PROXY_CACHE_AUTH,
|
|
23 NGX_HTTP_PROXY_CACHE_PGNC,
|
|
24 NGX_HTTP_PROXY_CACHE_MISS,
|
|
25 NGX_HTTP_PROXY_CACHE_EXPR,
|
|
26 NGX_HTTP_PROXY_CACHE_AGED,
|
|
27 NGX_HTTP_PROXY_CACHE_HIT
|
|
28 } ngx_http_proxy_state_e;
|
|
29
|
|
30
|
|
31 typedef enum {
|
|
32 NGX_HTTP_PROXY_CACHE_BPS = 1,
|
|
33 NGX_HTTP_PROXY_CACHE_XAE,
|
|
34 NGX_HTTP_PROXY_CACHE_CTL,
|
|
35 NGX_HTTP_PROXY_CACHE_EXP,
|
|
36 NGX_HTTP_PROXY_CACHE_MVD,
|
|
37 NGX_HTTP_PROXY_CACHE_LMF,
|
|
38 NGX_HTTP_PROXY_CACHE_PDE
|
|
39 } ngx_http_proxy_reason_e;
|
|
40
|
|
41
|
|
42 typedef struct {
|
|
43 ngx_str_t url;
|
|
44 ngx_str_t host;
|
|
45 ngx_str_t uri;
|
|
46 ngx_str_t host_header;
|
|
47 ngx_str_t port_text;
|
|
48 ngx_str_t *location;
|
|
49
|
|
50 in_port_t port;
|
|
51
|
|
52 unsigned default_port:1;
|
|
53 } ngx_http_proxy_upstream_conf_t;
|
|
54
|
|
55
|
|
56 typedef struct {
|
2
|
57 size_t send_lowat;
|
0
|
58 size_t header_buffer_size;
|
|
59 size_t busy_buffers_size;
|
|
60 size_t max_temp_file_size;
|
|
61 size_t temp_file_write_size;
|
|
62
|
|
63 ngx_msec_t connect_timeout;
|
|
64 ngx_msec_t send_timeout;
|
|
65 ngx_msec_t read_timeout;
|
|
66 time_t default_expires;
|
|
67
|
|
68 ngx_int_t lm_factor;
|
|
69
|
|
70 ngx_uint_t next_upstream;
|
|
71 ngx_uint_t use_stale;
|
|
72
|
|
73 ngx_bufs_t bufs;
|
|
74
|
|
75 ngx_flag_t cyclic_temp_file;
|
|
76 ngx_flag_t cache;
|
|
77 ngx_flag_t preserve_host;
|
|
78 ngx_flag_t set_x_real_ip;
|
|
79 ngx_flag_t add_x_forwarded_for;
|
|
80 ngx_flag_t pass_server;
|
|
81 ngx_flag_t pass_x_accel_expires;
|
|
82 ngx_flag_t ignore_expires;
|
|
83
|
|
84 ngx_path_t *cache_path;
|
|
85 ngx_path_t *temp_path;
|
|
86
|
|
87 ngx_http_busy_lock_t *busy_lock;
|
|
88
|
|
89 ngx_http_proxy_upstream_conf_t *upstream;
|
|
90 ngx_peers_t *peers;
|
|
91 } ngx_http_proxy_loc_conf_t;
|
|
92
|
|
93
|
|
94 /*
|
|
95 * "EXPR/10/5/- 200/EXP/60 4"
|
|
96 * "MISS/-/-/B 503/-/- -"
|
|
97 * "EXPR/10/20/SB HIT/-/- -"
|
|
98 * "EXPR/10/15/NB HIT/-/- -"
|
|
99 */
|
|
100
|
|
101 typedef struct {
|
|
102 ngx_http_proxy_state_e cache_state;
|
|
103 time_t expired;
|
|
104 time_t bl_time;
|
|
105 ngx_uint_t bl_state;
|
|
106
|
|
107 ngx_uint_t status;
|
|
108 ngx_http_proxy_reason_e reason;
|
|
109 time_t time;
|
|
110 time_t expires;
|
|
111
|
|
112 ngx_str_t *peer;
|
|
113 } ngx_http_proxy_state_t;
|
|
114
|
|
115
|
|
116 typedef struct {
|
|
117 ngx_list_t headers;
|
|
118 #if 0
|
|
119 ngx_table_t headers; /* it must be first field */
|
|
120 #endif
|
|
121
|
|
122 ngx_table_elt_t *date;
|
|
123 ngx_table_elt_t *server;
|
|
124
|
|
125 ngx_table_elt_t *expires;
|
|
126 ngx_table_elt_t *cache_control;
|
|
127 ngx_table_elt_t *etag;
|
|
128 ngx_table_elt_t *x_accel_expires;
|
|
129
|
|
130 ngx_table_elt_t *connection;
|
|
131 ngx_table_elt_t *content_type;
|
|
132 ngx_table_elt_t *content_length;
|
|
133 ngx_table_elt_t *last_modified;
|
|
134 ngx_table_elt_t *location;
|
|
135 ngx_table_elt_t *accept_ranges;
|
|
136 ngx_table_elt_t *x_pad;
|
|
137
|
|
138 off_t content_length_n;
|
|
139 } ngx_http_proxy_headers_in_t;
|
|
140
|
|
141
|
|
142 typedef struct {
|
|
143 ngx_http_cache_ctx_t ctx;
|
|
144 ngx_uint_t status;
|
|
145 ngx_str_t status_line;
|
|
146
|
|
147 ngx_http_proxy_headers_in_t headers_in;
|
|
148 } ngx_http_proxy_cache_t;
|
|
149
|
|
150
|
|
151 typedef struct {
|
|
152 ngx_peer_connection_t peer;
|
|
153 ngx_uint_t status;
|
|
154 ngx_str_t status_line;
|
|
155 ngx_uint_t method;
|
|
156
|
|
157 ngx_output_chain_ctx_t *output_chain_ctx;
|
|
158 ngx_event_pipe_t *event_pipe;
|
|
159
|
|
160 ngx_http_proxy_headers_in_t headers_in;
|
|
161 } ngx_http_proxy_upstream_t;
|
|
162
|
|
163
|
|
164 typedef struct ngx_http_proxy_ctx_s ngx_http_proxy_ctx_t;
|
|
165
|
|
166 struct ngx_http_proxy_ctx_s {
|
|
167 ngx_http_request_t *request;
|
|
168 ngx_http_proxy_loc_conf_t *lcf;
|
|
169 ngx_http_proxy_upstream_t *upstream;
|
|
170 ngx_http_proxy_cache_t *cache;
|
|
171
|
|
172 ngx_buf_t *header_in;
|
|
173
|
|
174 ngx_http_busy_lock_ctx_t busy_lock;
|
|
175
|
|
176 unsigned accel:1;
|
|
177
|
|
178 unsigned cachable:1;
|
|
179 unsigned stale:1;
|
|
180 unsigned try_busy_lock:1;
|
|
181 unsigned busy_locked:1;
|
|
182 unsigned valid_header_in:1;
|
|
183
|
|
184 unsigned request_sent:1;
|
|
185 unsigned header_sent:1;
|
|
186
|
|
187
|
|
188 /* used to parse an upstream HTTP header */
|
|
189 ngx_uint_t status;
|
|
190 u_char *status_start;
|
|
191 u_char *status_end;
|
|
192 ngx_uint_t status_count;
|
|
193 ngx_uint_t parse_state;
|
|
194
|
|
195 ngx_http_proxy_state_t *state;
|
|
196 ngx_array_t states; /* of ngx_http_proxy_state_t */
|
|
197
|
|
198 /*
|
|
199 * we declare "action" as "char *" because the actions are usually
|
|
200 * the static strings and in the "u_char *" case we have to override
|
|
201 * all the time their types
|
|
202 */
|
|
203
|
|
204 char *action;
|
|
205 ngx_http_log_ctx_t *saved_ctx;
|
|
206 ngx_log_handler_pt saved_handler;
|
|
207 };
|
|
208
|
|
209
|
|
210 typedef struct {
|
|
211 ngx_uint_t connection;
|
|
212 ngx_http_proxy_ctx_t *proxy;
|
|
213 } ngx_http_proxy_log_ctx_t;
|
|
214
|
|
215
|
|
216 #define NGX_HTTP_PROXY_PARSE_NO_HEADER 30
|
|
217
|
|
218
|
|
219 #define NGX_HTTP_PROXY_FT_ERROR 0x02
|
|
220 #define NGX_HTTP_PROXY_FT_TIMEOUT 0x04
|
|
221 #define NGX_HTTP_PROXY_FT_INVALID_HEADER 0x08
|
|
222 #define NGX_HTTP_PROXY_FT_HTTP_500 0x10
|
|
223 #define NGX_HTTP_PROXY_FT_HTTP_404 0x20
|
|
224 #define NGX_HTTP_PROXY_FT_BUSY_LOCK 0x40
|
|
225 #define NGX_HTTP_PROXY_FT_MAX_WAITING 0x80
|
|
226
|
|
227
|
|
228 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p);
|
|
229
|
|
230 #if (NGX_HTTP_FILE_CACHE)
|
|
231
|
|
232 int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p);
|
|
233 int ngx_http_proxy_send_cached_response(ngx_http_proxy_ctx_t *p);
|
|
234 int ngx_http_proxy_is_cachable(ngx_http_proxy_ctx_t *p);
|
|
235 int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p);
|
|
236
|
|
237 void ngx_http_proxy_cache_busy_lock(ngx_http_proxy_ctx_t *p);
|
|
238
|
|
239 #endif
|
|
240
|
|
241 void ngx_http_proxy_check_broken_connection(ngx_event_t *ev);
|
|
242
|
|
243 void ngx_http_proxy_busy_lock_handler(ngx_event_t *rev);
|
|
244 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p);
|
|
245
|
|
246 size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len);
|
|
247 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc);
|
|
248 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p);
|
|
249
|
|
250 int ngx_http_proxy_parse_status_line(ngx_http_proxy_ctx_t *p);
|
|
251 int ngx_http_proxy_copy_header(ngx_http_proxy_ctx_t *p,
|
|
252 ngx_http_proxy_headers_in_t *headers_in);
|
|
253
|
|
254
|
|
255
|
|
256 extern ngx_module_t ngx_http_proxy_module;
|
|
257 extern ngx_http_header_t ngx_http_proxy_headers_in[];
|
|
258
|
|
259
|
|
260
|
|
261 #endif /* _NGX_HTTP_PROXY_HANDLER_H_INCLUDED_ */
|