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;
|
6
|
78 ngx_flag_t set_x_url;
|
0
|
79 ngx_flag_t set_x_real_ip;
|
|
80 ngx_flag_t add_x_forwarded_for;
|
|
81 ngx_flag_t pass_server;
|
|
82 ngx_flag_t pass_x_accel_expires;
|
|
83 ngx_flag_t ignore_expires;
|
|
84
|
|
85 ngx_path_t *cache_path;
|
|
86 ngx_path_t *temp_path;
|
|
87
|
|
88 ngx_http_busy_lock_t *busy_lock;
|
|
89
|
|
90 ngx_http_proxy_upstream_conf_t *upstream;
|
|
91 ngx_peers_t *peers;
|
|
92 } ngx_http_proxy_loc_conf_t;
|
|
93
|
|
94
|
|
95 /*
|
|
96 * "EXPR/10/5/- 200/EXP/60 4"
|
|
97 * "MISS/-/-/B 503/-/- -"
|
|
98 * "EXPR/10/20/SB HIT/-/- -"
|
|
99 * "EXPR/10/15/NB HIT/-/- -"
|
|
100 */
|
|
101
|
|
102 typedef struct {
|
|
103 ngx_http_proxy_state_e cache_state;
|
|
104 time_t expired;
|
|
105 time_t bl_time;
|
|
106 ngx_uint_t bl_state;
|
|
107
|
|
108 ngx_uint_t status;
|
|
109 ngx_http_proxy_reason_e reason;
|
|
110 time_t time;
|
|
111 time_t expires;
|
|
112
|
|
113 ngx_str_t *peer;
|
|
114 } ngx_http_proxy_state_t;
|
|
115
|
|
116
|
|
117 typedef struct {
|
|
118 ngx_list_t headers;
|
|
119 #if 0
|
|
120 ngx_table_t headers; /* it must be first field */
|
|
121 #endif
|
|
122
|
|
123 ngx_table_elt_t *date;
|
|
124 ngx_table_elt_t *server;
|
|
125
|
|
126 ngx_table_elt_t *expires;
|
|
127 ngx_table_elt_t *cache_control;
|
|
128 ngx_table_elt_t *etag;
|
|
129 ngx_table_elt_t *x_accel_expires;
|
|
130
|
|
131 ngx_table_elt_t *connection;
|
|
132 ngx_table_elt_t *content_type;
|
|
133 ngx_table_elt_t *content_length;
|
4
|
134 ngx_table_elt_t *content_encoding;
|
0
|
135 ngx_table_elt_t *last_modified;
|
|
136 ngx_table_elt_t *location;
|
|
137 ngx_table_elt_t *accept_ranges;
|
|
138 ngx_table_elt_t *x_pad;
|
|
139
|
|
140 off_t content_length_n;
|
|
141 } ngx_http_proxy_headers_in_t;
|
|
142
|
|
143
|
|
144 typedef struct {
|
|
145 ngx_http_cache_ctx_t ctx;
|
|
146 ngx_uint_t status;
|
|
147 ngx_str_t status_line;
|
|
148
|
|
149 ngx_http_proxy_headers_in_t headers_in;
|
|
150 } ngx_http_proxy_cache_t;
|
|
151
|
|
152
|
|
153 typedef struct {
|
|
154 ngx_peer_connection_t peer;
|
|
155 ngx_uint_t status;
|
|
156 ngx_str_t status_line;
|
|
157 ngx_uint_t method;
|
|
158
|
|
159 ngx_output_chain_ctx_t *output_chain_ctx;
|
|
160 ngx_event_pipe_t *event_pipe;
|
|
161
|
|
162 ngx_http_proxy_headers_in_t headers_in;
|
|
163 } ngx_http_proxy_upstream_t;
|
|
164
|
|
165
|
|
166 typedef struct ngx_http_proxy_ctx_s ngx_http_proxy_ctx_t;
|
|
167
|
|
168 struct ngx_http_proxy_ctx_s {
|
|
169 ngx_http_request_t *request;
|
|
170 ngx_http_proxy_loc_conf_t *lcf;
|
|
171 ngx_http_proxy_upstream_t *upstream;
|
|
172 ngx_http_proxy_cache_t *cache;
|
|
173
|
|
174 ngx_buf_t *header_in;
|
|
175
|
|
176 ngx_http_busy_lock_ctx_t busy_lock;
|
|
177
|
|
178 unsigned accel:1;
|
|
179
|
|
180 unsigned cachable:1;
|
|
181 unsigned stale:1;
|
|
182 unsigned try_busy_lock:1;
|
|
183 unsigned busy_locked:1;
|
|
184 unsigned valid_header_in:1;
|
|
185
|
|
186 unsigned request_sent:1;
|
|
187 unsigned header_sent:1;
|
|
188
|
|
189
|
|
190 /* used to parse an upstream HTTP header */
|
|
191 ngx_uint_t status;
|
|
192 u_char *status_start;
|
|
193 u_char *status_end;
|
|
194 ngx_uint_t status_count;
|
|
195 ngx_uint_t parse_state;
|
|
196
|
|
197 ngx_http_proxy_state_t *state;
|
|
198 ngx_array_t states; /* of ngx_http_proxy_state_t */
|
|
199
|
|
200 /*
|
|
201 * we declare "action" as "char *" because the actions are usually
|
|
202 * the static strings and in the "u_char *" case we have to override
|
|
203 * all the time their types
|
|
204 */
|
|
205
|
|
206 char *action;
|
|
207 ngx_http_log_ctx_t *saved_ctx;
|
|
208 ngx_log_handler_pt saved_handler;
|
|
209 };
|
|
210
|
|
211
|
|
212 typedef struct {
|
|
213 ngx_uint_t connection;
|
|
214 ngx_http_proxy_ctx_t *proxy;
|
|
215 } ngx_http_proxy_log_ctx_t;
|
|
216
|
|
217
|
|
218 #define NGX_HTTP_PROXY_PARSE_NO_HEADER 30
|
|
219
|
|
220
|
|
221 #define NGX_HTTP_PROXY_FT_ERROR 0x02
|
|
222 #define NGX_HTTP_PROXY_FT_TIMEOUT 0x04
|
|
223 #define NGX_HTTP_PROXY_FT_INVALID_HEADER 0x08
|
|
224 #define NGX_HTTP_PROXY_FT_HTTP_500 0x10
|
|
225 #define NGX_HTTP_PROXY_FT_HTTP_404 0x20
|
|
226 #define NGX_HTTP_PROXY_FT_BUSY_LOCK 0x40
|
|
227 #define NGX_HTTP_PROXY_FT_MAX_WAITING 0x80
|
|
228
|
|
229
|
|
230 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p);
|
|
231
|
|
232 #if (NGX_HTTP_FILE_CACHE)
|
|
233
|
|
234 int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p);
|
|
235 int ngx_http_proxy_send_cached_response(ngx_http_proxy_ctx_t *p);
|
|
236 int ngx_http_proxy_is_cachable(ngx_http_proxy_ctx_t *p);
|
|
237 int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p);
|
|
238
|
|
239 void ngx_http_proxy_cache_busy_lock(ngx_http_proxy_ctx_t *p);
|
|
240
|
|
241 #endif
|
|
242
|
|
243 void ngx_http_proxy_check_broken_connection(ngx_event_t *ev);
|
|
244
|
|
245 void ngx_http_proxy_busy_lock_handler(ngx_event_t *rev);
|
|
246 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p);
|
|
247
|
|
248 size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len);
|
|
249 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc);
|
|
250 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p);
|
|
251
|
|
252 int ngx_http_proxy_parse_status_line(ngx_http_proxy_ctx_t *p);
|
|
253 int ngx_http_proxy_copy_header(ngx_http_proxy_ctx_t *p,
|
|
254 ngx_http_proxy_headers_in_t *headers_in);
|
|
255
|
|
256
|
|
257
|
|
258 extern ngx_module_t ngx_http_proxy_module;
|
|
259 extern ngx_http_header_t ngx_http_proxy_headers_in[];
|
|
260
|
|
261
|
|
262
|
|
263 #endif /* _NGX_HTTP_PROXY_HANDLER_H_INCLUDED_ */
|