0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 static void ngx_http_init_request(ngx_event_t *ev);
|
|
14 static void ngx_http_process_request_line(ngx_event_t *rev);
|
|
15 static void ngx_http_process_request_headers(ngx_event_t *rev);
|
|
16 static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
|
|
17 static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
|
50
|
18 ngx_uint_t request_line);
|
58
|
19
|
|
20 static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
|
|
21 ngx_table_elt_t *h, ngx_uint_t offset);
|
72
|
22 static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
|
|
23 ngx_table_elt_t *h, ngx_uint_t offset);
|
324
|
24 static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
|
|
25 ngx_table_elt_t *h, ngx_uint_t offset);
|
58
|
26 static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
|
|
27 ngx_table_elt_t *h, ngx_uint_t offset);
|
|
28
|
0
|
29 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
|
308
|
30 static void ngx_http_process_request(ngx_http_request_t *r);
|
306
|
31 static void ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host,
|
|
32 size_t len, ngx_uint_t hash);
|
0
|
33
|
58
|
34 static void ngx_http_request_handler(ngx_event_t *ev);
|
74
|
35 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
|
58
|
36 static void ngx_http_writer(ngx_http_request_t *r);
|
|
37
|
|
38 static void ngx_http_block_read(ngx_http_request_t *r);
|
324
|
39 static void ngx_http_test_read(ngx_http_request_t *r);
|
0
|
40 static void ngx_http_set_keepalive(ngx_http_request_t *r);
|
|
41 static void ngx_http_keepalive_handler(ngx_event_t *ev);
|
|
42 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
|
|
43 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
|
162
|
44 static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
|
80
|
45 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
|
118
|
46 static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
|
80
|
47 static void ngx_http_close_connection(ngx_connection_t *c);
|
0
|
48
|
32
|
49 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
214
|
50 static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
|
|
51 ngx_http_request_t *sr, u_char *buf, size_t len);
|
0
|
52
|
88
|
53 #if (NGX_HTTP_SSL)
|
|
54 static void ngx_http_ssl_handshake(ngx_event_t *rev);
|
96
|
55 static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
|
88
|
56 #endif
|
|
57
|
0
|
58
|
54
|
59 static char *ngx_http_client_errors[] = {
|
|
60
|
|
61 /* NGX_HTTP_PARSE_INVALID_METHOD */
|
|
62 "client sent invalid method",
|
|
63
|
|
64 /* NGX_HTTP_PARSE_INVALID_REQUEST */
|
|
65 "client sent invalid request",
|
|
66
|
|
67 /* NGX_HTTP_PARSE_INVALID_09_METHOD */
|
|
68 "client sent invalid method in HTTP/0.9 request"
|
0
|
69 };
|
|
70
|
|
71
|
|
72 ngx_http_header_t ngx_http_headers_in[] = {
|
58
|
73 { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
|
72
|
74 ngx_http_process_unique_header_line },
|
58
|
75
|
|
76 { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
|
324
|
77 ngx_http_process_connection },
|
58
|
78
|
0
|
79 { ngx_string("If-Modified-Since"),
|
58
|
80 offsetof(ngx_http_headers_in_t, if_modified_since),
|
324
|
81 ngx_http_process_unique_header_line },
|
58
|
82
|
|
83 { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
|
|
84 ngx_http_process_header_line },
|
|
85
|
|
86 { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
|
|
87 ngx_http_process_header_line },
|
|
88
|
0
|
89 { ngx_string("Content-Length"),
|
58
|
90 offsetof(ngx_http_headers_in_t, content_length),
|
72
|
91 ngx_http_process_unique_header_line },
|
58
|
92
|
28
|
93 { ngx_string("Content-Type"),
|
58
|
94 offsetof(ngx_http_headers_in_t, content_type),
|
|
95 ngx_http_process_header_line },
|
|
96
|
|
97 { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
|
|
98 ngx_http_process_header_line },
|
|
99
|
130
|
100 { ngx_string("Transfer-Encoding"),
|
|
101 offsetof(ngx_http_headers_in_t, transfer_encoding),
|
|
102 ngx_http_process_header_line },
|
|
103
|
0
|
104 #if (NGX_HTTP_GZIP)
|
|
105 { ngx_string("Accept-Encoding"),
|
58
|
106 offsetof(ngx_http_headers_in_t, accept_encoding),
|
|
107 ngx_http_process_header_line },
|
|
108
|
|
109 { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
|
|
110 ngx_http_process_header_line },
|
0
|
111 #endif
|
|
112
|
|
113 { ngx_string("Authorization"),
|
58
|
114 offsetof(ngx_http_headers_in_t, authorization),
|
72
|
115 ngx_http_process_unique_header_line },
|
58
|
116
|
|
117 { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
|
|
118 ngx_http_process_header_line },
|
0
|
119
|
122
|
120 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
|
0
|
121 { ngx_string("X-Forwarded-For"),
|
58
|
122 offsetof(ngx_http_headers_in_t, x_forwarded_for),
|
|
123 ngx_http_process_header_line },
|
0
|
124 #endif
|
28
|
125
|
122
|
126 #if (NGX_HTTP_REALIP)
|
|
127 { ngx_string("X-Real-IP"),
|
|
128 offsetof(ngx_http_headers_in_t, x_real_ip),
|
|
129 ngx_http_process_header_line },
|
|
130 #endif
|
|
131
|
38
|
132 #if (NGX_HTTP_HEADERS)
|
58
|
133 { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
|
|
134 ngx_http_process_header_line },
|
|
135
|
38
|
136 { ngx_string("Accept-Language"),
|
58
|
137 offsetof(ngx_http_headers_in_t, accept_language),
|
|
138 ngx_http_process_header_line },
|
38
|
139 #endif
|
|
140
|
186
|
141 #if (NGX_HTTP_DAV)
|
|
142 { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
|
|
143 ngx_http_process_header_line },
|
|
144
|
|
145 { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
|
|
146 ngx_http_process_header_line },
|
230
|
147
|
276
|
148 { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
|
|
149 ngx_http_process_header_line },
|
|
150
|
230
|
151 { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
|
|
152 ngx_http_process_header_line },
|
186
|
153 #endif
|
|
154
|
58
|
155 { ngx_string("Cookie"), 0, ngx_http_process_cookie },
|
|
156
|
|
157 { ngx_null_string, 0, NULL }
|
0
|
158 };
|
|
159
|
|
160
|
50
|
161 void
|
|
162 ngx_http_init_connection(ngx_connection_t *c)
|
0
|
163 {
|
|
164 ngx_event_t *rev;
|
|
165 ngx_http_log_ctx_t *ctx;
|
|
166
|
50
|
167 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
|
|
168 if (ctx == NULL) {
|
0
|
169 ngx_http_close_connection(c);
|
|
170 return;
|
|
171 }
|
|
172
|
10
|
173 ctx->client = &c->addr_text;
|
|
174 ctx->request = NULL;
|
214
|
175 ctx->current_request = NULL;
|
32
|
176
|
|
177 c->log->connection = c->number;
|
|
178 c->log->handler = ngx_http_log_error;
|
0
|
179 c->log->data = ctx;
|
32
|
180 c->log->action = "reading client request line";
|
|
181
|
0
|
182 c->log_error = NGX_ERROR_INFO;
|
|
183
|
|
184 rev = c->read;
|
58
|
185 rev->handler = ngx_http_init_request;
|
118
|
186 c->write->handler = ngx_http_empty_handler;
|
0
|
187
|
116
|
188 #if (NGX_STAT_STUB)
|
|
189 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
|
190 #endif
|
|
191
|
0
|
192 if (rev->ready) {
|
|
193 /* the deferred accept(), rtsig, aio, iocp */
|
|
194
|
160
|
195 if (ngx_use_accept_mutex) {
|
112
|
196 ngx_post_event(rev, &ngx_posted_events);
|
0
|
197 return;
|
|
198 }
|
|
199
|
|
200 ngx_http_init_request(rev);
|
|
201 return;
|
|
202 }
|
|
203
|
|
204 ngx_add_timer(rev, c->listening->post_accept_timeout);
|
|
205
|
|
206 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
116
|
207 #if (NGX_STAT_STUB)
|
|
208 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
|
209 #endif
|
0
|
210 ngx_http_close_connection(c);
|
|
211 return;
|
|
212 }
|
|
213 }
|
|
214
|
|
215
|
190
|
216 static void
|
|
217 ngx_http_init_request(ngx_event_t *rev)
|
0
|
218 {
|
298
|
219 ngx_time_t *tp;
|
190
|
220 socklen_t len;
|
122
|
221 ngx_uint_t i;
|
|
222 struct sockaddr_in sin;
|
|
223 ngx_connection_t *c;
|
|
224 ngx_http_request_t *r;
|
142
|
225 ngx_http_in_port_t *hip;
|
|
226 ngx_http_in_addr_t *hia;
|
122
|
227 ngx_http_log_ctx_t *ctx;
|
|
228 ngx_http_connection_t *hc;
|
|
229 ngx_http_core_srv_conf_t *cscf;
|
|
230 ngx_http_core_loc_conf_t *clcf;
|
|
231 ngx_http_core_main_conf_t *cmcf;
|
0
|
232 #if (NGX_HTTP_SSL)
|
122
|
233 ngx_http_ssl_srv_conf_t *sscf;
|
0
|
234 #endif
|
|
235
|
116
|
236 #if (NGX_STAT_STUB)
|
|
237 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
|
238 #endif
|
|
239
|
0
|
240 c = rev->data;
|
|
241
|
|
242 if (rev->timedout) {
|
|
243 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
244
|
|
245 ngx_http_close_connection(c);
|
|
246 return;
|
|
247 }
|
|
248
|
|
249 hc = c->data;
|
|
250
|
116
|
251 if (hc == NULL) {
|
50
|
252 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
|
|
253 if (hc == NULL) {
|
0
|
254 ngx_http_close_connection(c);
|
|
255 return;
|
|
256 }
|
|
257 }
|
|
258
|
|
259 r = hc->request;
|
|
260
|
|
261 if (r) {
|
|
262 ngx_memzero(r, sizeof(ngx_http_request_t));
|
|
263
|
|
264 r->pipeline = hc->pipeline;
|
|
265
|
|
266 if (hc->nbusy) {
|
|
267 r->header_in = hc->busy[0];
|
|
268 }
|
|
269
|
|
270 } else {
|
50
|
271 r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
|
|
272 if (r == NULL) {
|
0
|
273 ngx_http_close_connection(c);
|
|
274 return;
|
|
275 }
|
|
276
|
|
277 hc->request = r;
|
|
278 }
|
|
279
|
|
280 c->data = r;
|
|
281 r->http_connection = hc;
|
|
282
|
|
283 c->sent = 0;
|
|
284 r->signature = NGX_HTTP_MODULE;
|
|
285
|
|
286 /* find the server configuration for the address:port */
|
|
287
|
|
288 /* AF_INET only */
|
|
289
|
190
|
290 hip = c->listening->servers;
|
142
|
291 hia = hip->addrs;
|
|
292
|
|
293 r->port = hip->port;
|
|
294 r->port_text = &hip->port_text;
|
0
|
295
|
|
296 i = 0;
|
|
297
|
142
|
298 if (hip->naddrs > 1) {
|
0
|
299
|
|
300 /*
|
|
301 * There are several addresses on this port and one of them
|
|
302 * is the "*:port" wildcard so getsockname() is needed to determine
|
|
303 * the server address.
|
|
304 *
|
|
305 * AcceptEx() already gave this address.
|
|
306 */
|
|
307
|
10
|
308 #if (NGX_WIN32)
|
0
|
309 if (c->local_sockaddr) {
|
|
310 r->in_addr =
|
|
311 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
|
|
312
|
28
|
313 } else
|
0
|
314 #endif
|
28
|
315 {
|
0
|
316 len = sizeof(struct sockaddr_in);
|
28
|
317 if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
|
0
|
318 ngx_connection_error(c, ngx_socket_errno,
|
|
319 "getsockname() failed");
|
|
320 ngx_http_close_connection(c);
|
|
321 return;
|
|
322 }
|
|
323
|
28
|
324 r->in_addr = sin.sin_addr.s_addr;
|
0
|
325 }
|
|
326
|
190
|
327 /* the last address is "*" */
|
0
|
328
|
142
|
329 for ( /* void */ ; i < hip->naddrs - 1; i++) {
|
|
330 if (hia[i].addr == r->in_addr) {
|
0
|
331 break;
|
|
332 }
|
|
333 }
|
|
334
|
|
335 } else {
|
142
|
336 r->in_addr = hia[0].addr;
|
0
|
337 }
|
|
338
|
142
|
339 r->virtual_names = hia[i].virtual_names;
|
0
|
340
|
|
341 /* the default server configuration for the address:port */
|
142
|
342 cscf = hia[i].core_srv_conf;
|
0
|
343
|
|
344 r->main_conf = cscf->ctx->main_conf;
|
|
345 r->srv_conf = cscf->ctx->srv_conf;
|
|
346 r->loc_conf = cscf->ctx->loc_conf;
|
|
347
|
142
|
348 r->server_name = cscf->server_name;
|
|
349
|
58
|
350 rev->handler = ngx_http_process_request_line;
|
0
|
351
|
|
352 #if (NGX_HTTP_SSL)
|
|
353
|
|
354 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
|
|
355 if (sscf->enable) {
|
|
356
|
|
357 if (c->ssl == NULL) {
|
96
|
358 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
|
56
|
359 == NGX_ERROR)
|
0
|
360 {
|
|
361 ngx_http_close_connection(c);
|
|
362 return;
|
|
363 }
|
|
364
|
58
|
365 rev->handler = ngx_http_ssl_handshake;
|
0
|
366 }
|
|
367
|
62
|
368 r->main_filter_need_in_memory = 1;
|
0
|
369 }
|
|
370
|
|
371 #endif
|
|
372
|
|
373 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
374 c->log->file = clcf->err_log->file;
|
|
375 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
|
376 c->log->log_level = clcf->err_log->log_level;
|
|
377 }
|
|
378
|
|
379 if (c->buffer == NULL) {
|
|
380 c->buffer = ngx_create_temp_buf(c->pool,
|
|
381 cscf->client_header_buffer_size);
|
|
382 if (c->buffer == NULL) {
|
|
383 ngx_http_close_connection(c);
|
|
384 return;
|
|
385 }
|
|
386 }
|
|
387
|
|
388 if (r->header_in == NULL) {
|
|
389 r->header_in = c->buffer;
|
|
390 }
|
|
391
|
50
|
392 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
|
|
393 if (r->pool == NULL) {
|
0
|
394 ngx_http_close_connection(c);
|
|
395 return;
|
|
396 }
|
|
397
|
|
398
|
|
399 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
|
130
|
400 sizeof(ngx_table_elt_t))
|
|
401 == NGX_ERROR)
|
0
|
402 {
|
|
403 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
404 return;
|
|
405 }
|
|
406
|
|
407 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
|
|
408 if (r->ctx == NULL) {
|
|
409 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
410 return;
|
|
411 }
|
|
412
|
122
|
413 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
414
|
|
415 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
|
|
416 * sizeof(ngx_http_variable_value_t));
|
|
417 if (r->variables == NULL) {
|
|
418 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
419 return;
|
|
420 }
|
|
421
|
0
|
422 c->single_connection = 1;
|
132
|
423 c->destroyed = 0;
|
|
424
|
0
|
425 r->connection = c;
|
|
426
|
102
|
427 r->main = r;
|
|
428
|
298
|
429 tp = ngx_timeofday();
|
|
430 r->start_sec = tp->sec;
|
|
431 r->start_msec = tp->msec;
|
46
|
432
|
180
|
433 r->method = NGX_HTTP_UNKNOWN;
|
|
434
|
0
|
435 r->headers_in.content_length_n = -1;
|
|
436 r->headers_in.keep_alive_n = -1;
|
|
437 r->headers_out.content_length_n = -1;
|
|
438 r->headers_out.last_modified_time = -1;
|
|
439
|
226
|
440 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
|
|
441 r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
|
|
442
|
0
|
443 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
|
|
444
|
54
|
445 ctx = c->log->data;
|
|
446 ctx->request = r;
|
214
|
447 ctx->current_request = r;
|
58
|
448 r->log_handler = ngx_http_log_error_handler;
|
54
|
449
|
0
|
450 #if (NGX_STAT_STUB)
|
110
|
451 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
26
|
452 r->stat_reading = 1;
|
110
|
453 ngx_atomic_fetch_add(ngx_stat_requests, 1);
|
0
|
454 #endif
|
|
455
|
58
|
456 rev->handler(rev);
|
0
|
457 }
|
|
458
|
|
459
|
|
460 #if (NGX_HTTP_SSL)
|
|
461
|
50
|
462 static void
|
|
463 ngx_http_ssl_handshake(ngx_event_t *rev)
|
0
|
464 {
|
10
|
465 u_char buf[1];
|
|
466 ssize_t n;
|
0
|
467 ngx_int_t rc;
|
|
468 ngx_connection_t *c;
|
|
469 ngx_http_request_t *r;
|
|
470
|
|
471 c = rev->data;
|
|
472 r = c->data;
|
|
473
|
|
474 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
475 "http check ssl handshake");
|
|
476
|
|
477 if (rev->timedout) {
|
54
|
478 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
479 c->timedout = 1;
|
|
480 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
481 return;
|
|
482 }
|
|
483
|
132
|
484 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
|
0
|
485
|
|
486 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
|
|
487 return;
|
|
488 }
|
|
489
|
|
490 if (n == 1) {
|
|
491 if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
|
|
492 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
10
|
493 "https ssl handshake: 0x%02Xd", buf[0]);
|
0
|
494
|
|
495 rc = ngx_ssl_handshake(c);
|
|
496
|
96
|
497 if (rc == NGX_AGAIN) {
|
|
498 c->ssl->handler = ngx_http_ssl_handshake_handler;
|
0
|
499 return;
|
|
500 }
|
|
501
|
96
|
502 ngx_http_ssl_handshake_handler(c);
|
|
503
|
|
504 return;
|
0
|
505
|
|
506 } else {
|
|
507 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
508 "plain http");
|
|
509
|
|
510 r->plain_http = 1;
|
|
511 }
|
|
512 }
|
|
513
|
58
|
514 rev->handler = ngx_http_process_request_line;
|
0
|
515 ngx_http_process_request_line(rev);
|
|
516 }
|
|
517
|
96
|
518
|
|
519 static void
|
|
520 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
|
|
521 {
|
|
522 ngx_http_request_t *r;
|
|
523
|
|
524 if (c->ssl->handshaked) {
|
|
525
|
|
526 /*
|
|
527 * The majority of browsers do not send the "close notify" alert.
|
|
528 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
|
|
529 * and Links. And what is more, MSIE ignores the server's alert.
|
|
530 *
|
|
531 * Opera and recent Mozilla send the alert.
|
|
532 */
|
|
533
|
|
534 c->ssl->no_wait_shutdown = 1;
|
|
535
|
|
536 c->read->handler = ngx_http_process_request_line;
|
|
537 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
|
|
538
|
|
539 ngx_http_process_request_line(c->read);
|
|
540
|
|
541 return;
|
|
542 }
|
|
543
|
|
544 r = c->data;
|
|
545
|
|
546 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
|
|
547
|
|
548 return;
|
|
549 }
|
|
550
|
306
|
551 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
|
|
552
|
|
553 int
|
|
554 ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
|
|
555 {
|
|
556 u_char *p;
|
|
557 ngx_uint_t hash;
|
|
558 const char *servername;
|
|
559 ngx_connection_t *c;
|
|
560 ngx_http_request_t *r;
|
|
561 ngx_http_ssl_srv_conf_t *sscf;
|
|
562
|
|
563 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
|
|
564
|
|
565 if (servername == NULL) {
|
|
566 return SSL_TLSEXT_ERR_NOACK;
|
|
567 }
|
|
568
|
|
569 c = ngx_ssl_get_connection(ssl_conn);
|
|
570
|
|
571 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
572 "SSL server name: \"%s\"", servername);
|
|
573
|
|
574 r = c->data;
|
|
575
|
|
576 if (r->virtual_names == NULL) {
|
|
577 return SSL_TLSEXT_ERR_NOACK;
|
|
578 }
|
|
579
|
|
580 /* it seems browsers send low case server name */
|
|
581
|
|
582 hash = 0;
|
|
583
|
|
584 for (p = (u_char *) servername; *p; p++) {
|
|
585 hash = ngx_hash(hash, *p);
|
|
586 }
|
|
587
|
|
588 ngx_http_find_virtual_server(r, (u_char *) servername,
|
|
589 p - (u_char *) servername, hash);
|
|
590
|
|
591 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
|
|
592
|
|
593 SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
|
|
594
|
|
595 return SSL_TLSEXT_ERR_OK;
|
|
596 }
|
|
597
|
|
598 #endif
|
96
|
599
|
0
|
600 #endif
|
|
601
|
|
602
|
50
|
603 static void
|
|
604 ngx_http_process_request_line(ngx_event_t *rev)
|
0
|
605 {
|
|
606 ssize_t n;
|
|
607 ngx_int_t rc, rv;
|
|
608 ngx_connection_t *c;
|
|
609 ngx_http_request_t *r;
|
|
610
|
|
611 c = rev->data;
|
|
612 r = c->data;
|
|
613
|
|
614 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
615 "http process request line");
|
|
616
|
|
617 if (rev->timedout) {
|
54
|
618 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
619 c->timedout = 1;
|
|
620 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
621 return;
|
|
622 }
|
|
623
|
|
624 rc = NGX_AGAIN;
|
|
625
|
|
626 for ( ;; ) {
|
|
627
|
|
628 if (rc == NGX_AGAIN) {
|
|
629 n = ngx_http_read_request_header(r);
|
|
630
|
|
631 if (n == NGX_AGAIN || n == NGX_ERROR) {
|
|
632 return;
|
|
633 }
|
|
634 }
|
|
635
|
|
636 rc = ngx_http_parse_request_line(r, r->header_in);
|
|
637
|
|
638 if (rc == NGX_OK) {
|
|
639
|
|
640 /* the request line has been parsed successfully */
|
|
641
|
10
|
642 r->request_line.len = r->request_end - r->request_start;
|
|
643 r->request_line.data = r->request_start;
|
|
644
|
0
|
645
|
|
646 if (r->args_start) {
|
|
647 r->uri.len = r->args_start - 1 - r->uri_start;
|
|
648 } else {
|
|
649 r->uri.len = r->uri_end - r->uri_start;
|
|
650 }
|
|
651
|
18
|
652
|
2
|
653 if (r->complex_uri || r->quoted_uri) {
|
10
|
654
|
50
|
655 r->uri.data = ngx_palloc(r->pool, r->uri.len + 1);
|
|
656 if (r->uri.data == NULL) {
|
10
|
657 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
658 return;
|
|
659 }
|
|
660
|
0
|
661 rc = ngx_http_parse_complex_uri(r);
|
|
662
|
54
|
663 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
|
|
664 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
665 "client sent invalid request");
|
|
666 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
667 return;
|
|
668 }
|
|
669
|
|
670 } else {
|
10
|
671 r->uri.data = r->uri_start;
|
0
|
672 }
|
|
673
|
18
|
674
|
10
|
675 r->unparsed_uri.len = r->uri_end - r->uri_start;
|
|
676 r->unparsed_uri.data = r->uri_start;
|
|
677
|
0
|
678
|
58
|
679 r->method_name.len = r->method_end - r->request_start + 1;
|
|
680 r->method_name.data = r->request_line.data;
|
0
|
681
|
10
|
682
|
28
|
683 if (r->http_protocol.data) {
|
|
684 r->http_protocol.len = r->request_end - r->http_protocol.data;
|
|
685 }
|
|
686
|
|
687
|
0
|
688 if (r->uri_ext) {
|
|
689 if (r->args_start) {
|
|
690 r->exten.len = r->args_start - 1 - r->uri_ext;
|
|
691 } else {
|
|
692 r->exten.len = r->uri_end - r->uri_ext;
|
|
693 }
|
|
694
|
10
|
695 r->exten.data = r->uri_ext;
|
0
|
696 }
|
|
697
|
10
|
698
|
0
|
699 if (r->args_start && r->uri_end > r->args_start) {
|
|
700 r->args.len = r->uri_end - r->args_start;
|
10
|
701 r->args.data = r->args_start;
|
0
|
702 }
|
|
703
|
10
|
704
|
|
705 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
706 "http request line: \"%V\"", &r->request_line);
|
|
707
|
0
|
708 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
709 "http uri: \"%V\"", &r->uri);
|
0
|
710
|
|
711 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
712 "http args: \"%V\"", &r->args);
|
0
|
713
|
|
714 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
715 "http exten: \"%V\"", &r->exten);
|
|
716
|
0
|
717 if (r->http_version < NGX_HTTP_VERSION_10) {
|
308
|
718 ngx_http_process_request(r);
|
0
|
719 return;
|
|
720 }
|
|
721
|
|
722
|
|
723 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
|
130
|
724 sizeof(ngx_table_elt_t))
|
|
725 == NGX_ERROR)
|
0
|
726 {
|
|
727 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
728 return;
|
|
729 }
|
|
730
|
|
731
|
48
|
732 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
|
130
|
733 sizeof(ngx_table_elt_t *))
|
|
734 == NGX_ERROR)
|
0
|
735 {
|
|
736 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
737 return;
|
|
738 }
|
|
739
|
32
|
740 c->log->action = "reading client request headers";
|
0
|
741
|
58
|
742 rev->handler = ngx_http_process_request_headers;
|
0
|
743 ngx_http_process_request_headers(rev);
|
|
744
|
|
745 return;
|
54
|
746 }
|
|
747
|
|
748 if (rc != NGX_AGAIN) {
|
0
|
749
|
|
750 /* there was error while a request line parsing */
|
|
751
|
54
|
752 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
753 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
|
|
754 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
755 return;
|
|
756 }
|
|
757
|
|
758 /* NGX_AGAIN: a request line parsing is still incomplete */
|
|
759
|
|
760 if (r->header_in->pos == r->header_in->end) {
|
|
761
|
|
762 rv = ngx_http_alloc_large_header_buffer(r, 1);
|
|
763
|
|
764 if (rv == NGX_ERROR) {
|
|
765 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
766 return;
|
|
767 }
|
|
768
|
|
769 if (rv == NGX_DECLINED) {
|
54
|
770 r->request_line.len = r->header_in->end - r->request_start;
|
|
771 r->request_line.data = r->request_start;
|
|
772
|
|
773 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
774 "client sent too long URI");
|
|
775 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
|
0
|
776 return;
|
|
777 }
|
|
778 }
|
|
779 }
|
|
780 }
|
|
781
|
|
782
|
50
|
783 static void
|
|
784 ngx_http_process_request_headers(ngx_event_t *rev)
|
0
|
785 {
|
56
|
786 ssize_t n;
|
|
787 ngx_int_t rc, rv;
|
|
788 ngx_str_t header;
|
198
|
789 ngx_uint_t i;
|
58
|
790 ngx_table_elt_t *h;
|
56
|
791 ngx_connection_t *c;
|
|
792 ngx_http_header_t *hh;
|
|
793 ngx_http_request_t *r;
|
60
|
794 ngx_http_core_srv_conf_t *cscf;
|
56
|
795 ngx_http_core_main_conf_t *cmcf;
|
0
|
796
|
|
797 c = rev->data;
|
|
798 r = c->data;
|
|
799
|
|
800 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
801 "http process request header line");
|
|
802
|
|
803 if (rev->timedout) {
|
54
|
804 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
805 c->timedout = 1;
|
|
806 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
807 return;
|
|
808 }
|
|
809
|
56
|
810 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
60
|
811 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
56
|
812
|
0
|
813 rc = NGX_AGAIN;
|
|
814
|
|
815 for ( ;; ) {
|
|
816
|
|
817 if (rc == NGX_AGAIN) {
|
|
818
|
|
819 if (r->header_in->pos == r->header_in->end) {
|
|
820
|
|
821 rv = ngx_http_alloc_large_header_buffer(r, 0);
|
|
822
|
|
823 if (rv == NGX_ERROR) {
|
|
824 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
825 return;
|
|
826 }
|
|
827
|
|
828 if (rv == NGX_DECLINED) {
|
54
|
829 header.len = r->header_in->end - r->header_name_start;
|
|
830 header.data = r->header_name_start;
|
80
|
831
|
|
832 if (header.len > NGX_MAX_ERROR_STR - 300) {
|
|
833 header.len = NGX_MAX_ERROR_STR - 300;
|
|
834 header.data[header.len++] = '.';
|
|
835 header.data[header.len++] = '.';
|
|
836 header.data[header.len++] = '.';
|
|
837 }
|
|
838
|
54
|
839 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
840 "client sent too long header line: \"%V\"",
|
|
841 &header);
|
324
|
842 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
843 return;
|
|
844 }
|
|
845 }
|
|
846
|
|
847 n = ngx_http_read_request_header(r);
|
|
848
|
|
849 if (n == NGX_AGAIN || n == NGX_ERROR) {
|
|
850 return;
|
|
851 }
|
|
852 }
|
|
853
|
|
854 rc = ngx_http_parse_header_line(r, r->header_in);
|
|
855
|
|
856 if (rc == NGX_OK) {
|
|
857
|
60
|
858 if (r->invalid_header && cscf->ignore_invalid_headers) {
|
52
|
859
|
|
860 /* there was error while a header line parsing */
|
|
861
|
|
862 header.len = r->header_end - r->header_name_start;
|
|
863 header.data = r->header_name_start;
|
|
864
|
54
|
865 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
866 "client sent invalid header line: \"%V\"",
|
52
|
867 &header);
|
|
868 continue;
|
|
869 }
|
|
870
|
0
|
871 /* a header line has been parsed successfully */
|
|
872
|
50
|
873 h = ngx_list_push(&r->headers_in.headers);
|
|
874 if (h == NULL) {
|
0
|
875 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
876 return;
|
|
877 }
|
|
878
|
56
|
879 h->hash = r->header_hash;
|
|
880
|
0
|
881 h->key.len = r->header_name_end - r->header_name_start;
|
|
882 h->key.data = r->header_name_start;
|
|
883 h->key.data[h->key.len] = '\0';
|
|
884
|
|
885 h->value.len = r->header_end - r->header_start;
|
|
886 h->value.data = r->header_start;
|
|
887 h->value.data[h->value.len] = '\0';
|
|
888
|
198
|
889 h->lowcase_key = ngx_palloc(r->pool, h->key.len);
|
|
890 if (h->lowcase_key == NULL) {
|
|
891 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
892 return;
|
|
893 }
|
|
894
|
|
895 if (h->key.len == r->lowcase_index) {
|
|
896 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
897
|
|
898 } else {
|
|
899 for (i = 0; i < h->key.len; i++) {
|
216
|
900 h->lowcase_key[i] = ngx_tolower(h->key.data[i]);
|
0
|
901 }
|
|
902 }
|
|
903
|
198
|
904 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
|
|
905 h->lowcase_key, h->key.len);
|
|
906
|
|
907 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
|
908 return;
|
|
909 }
|
|
910
|
0
|
911 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
912 "http header: \"%V: %V\"",
|
|
913 &h->key, &h->value);
|
0
|
914
|
|
915 continue;
|
54
|
916 }
|
|
917
|
|
918 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
0
|
919
|
|
920 /* a whole header has been parsed successfully */
|
|
921
|
|
922 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
923 "http header done");
|
|
924
|
24
|
925 r->request_length += r->header_in->pos - r->header_in->start;
|
|
926
|
0
|
927 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
|
|
928
|
|
929 rc = ngx_http_process_request_header(r);
|
|
930
|
|
931 if (rc != NGX_OK) {
|
|
932 return;
|
|
933 }
|
|
934
|
308
|
935 ngx_http_process_request(r);
|
86
|
936
|
0
|
937 return;
|
54
|
938 }
|
|
939
|
|
940 if (rc == NGX_AGAIN) {
|
|
941
|
|
942 /* a header line parsing is still not complete */
|
|
943
|
|
944 continue;
|
0
|
945 }
|
|
946
|
54
|
947 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
|
|
948
|
|
949 header.len = r->header_end - r->header_name_start;
|
|
950 header.data = r->header_name_start;
|
|
951 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
952 "client sent invalid header line: \"%V\\r...\"",
|
|
953 &header);
|
324
|
954 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
54
|
955 return;
|
0
|
956 }
|
|
957 }
|
|
958
|
|
959
|
50
|
960 static ssize_t
|
|
961 ngx_http_read_request_header(ngx_http_request_t *r)
|
0
|
962 {
|
|
963 ssize_t n;
|
|
964 ngx_event_t *rev;
|
230
|
965 ngx_connection_t *c;
|
0
|
966 ngx_http_core_srv_conf_t *cscf;
|
|
967
|
230
|
968 c = r->connection;
|
|
969 rev = c->read;
|
0
|
970
|
|
971 n = r->header_in->last - r->header_in->pos;
|
|
972
|
|
973 if (n > 0) {
|
|
974 return n;
|
|
975 }
|
|
976
|
72
|
977 if (rev->ready) {
|
230
|
978 n = c->recv(c, r->header_in->last,
|
|
979 r->header_in->end - r->header_in->last);
|
72
|
980 } else {
|
|
981 n = NGX_AGAIN;
|
0
|
982 }
|
|
983
|
|
984 if (n == NGX_AGAIN) {
|
|
985 if (!r->header_timeout_set) {
|
|
986 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
987 ngx_add_timer(rev, cscf->client_header_timeout);
|
|
988 r->header_timeout_set = 1;
|
|
989 }
|
|
990
|
|
991 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
992 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
993 return NGX_ERROR;
|
|
994 }
|
|
995
|
|
996 return NGX_AGAIN;
|
|
997 }
|
|
998
|
|
999 if (n == 0) {
|
230
|
1000 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
0
|
1001 "client closed prematurely connection");
|
|
1002 }
|
|
1003
|
|
1004 if (n == 0 || n == NGX_ERROR) {
|
272
|
1005 c->error = 1;
|
|
1006 c->log->action = "reading client request headers";
|
|
1007
|
|
1008 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
1009 return NGX_ERROR;
|
|
1010 }
|
|
1011
|
|
1012 r->header_in->last += n;
|
|
1013
|
|
1014 return n;
|
|
1015 }
|
|
1016
|
|
1017
|
50
|
1018 static ngx_int_t
|
|
1019 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
|
|
1020 ngx_uint_t request_line)
|
0
|
1021 {
|
|
1022 u_char *old, *new;
|
|
1023 ngx_buf_t *b;
|
|
1024 ngx_http_connection_t *hc;
|
|
1025 ngx_http_core_srv_conf_t *cscf;
|
|
1026
|
|
1027 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1028 "http alloc large header buffer");
|
|
1029
|
|
1030 if (request_line && r->state == 0) {
|
|
1031
|
|
1032 /* the client fills up the buffer with "\r\n" */
|
|
1033
|
24
|
1034 r->request_length += r->header_in->end - r->header_in->start;
|
|
1035
|
0
|
1036 r->header_in->pos = r->header_in->start;
|
|
1037 r->header_in->last = r->header_in->start;
|
|
1038
|
|
1039 return NGX_OK;
|
|
1040 }
|
|
1041
|
|
1042 old = request_line ? r->request_start : r->header_name_start;
|
|
1043
|
|
1044 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1045
|
|
1046 if (r->state != 0
|
|
1047 && (size_t) (r->header_in->pos - old)
|
|
1048 >= cscf->large_client_header_buffers.size)
|
|
1049 {
|
|
1050 return NGX_DECLINED;
|
|
1051 }
|
|
1052
|
|
1053 hc = r->http_connection;
|
|
1054
|
|
1055 if (hc->nfree) {
|
|
1056 b = hc->free[--hc->nfree];
|
|
1057
|
|
1058 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1059 "http large header free: %p %uz",
|
0
|
1060 b->pos, b->end - b->last);
|
|
1061
|
|
1062 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
|
|
1063
|
|
1064 if (hc->busy == NULL) {
|
|
1065 hc->busy = ngx_palloc(r->connection->pool,
|
|
1066 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
1067 if (hc->busy == NULL) {
|
|
1068 return NGX_ERROR;
|
|
1069 }
|
|
1070 }
|
|
1071
|
|
1072 b = ngx_create_temp_buf(r->connection->pool,
|
|
1073 cscf->large_client_header_buffers.size);
|
|
1074 if (b == NULL) {
|
|
1075 return NGX_ERROR;
|
|
1076 }
|
|
1077
|
|
1078 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1079 "http large header alloc: %p %uz",
|
0
|
1080 b->pos, b->end - b->last);
|
|
1081
|
|
1082 } else {
|
|
1083 return NGX_DECLINED;
|
|
1084 }
|
|
1085
|
|
1086 hc->busy[hc->nbusy++] = b;
|
|
1087
|
|
1088 if (r->state == 0) {
|
|
1089 /*
|
|
1090 * r->state == 0 means that a header line was parsed successfully
|
|
1091 * and we do not need to copy incomplete header line and
|
|
1092 * to relocate the parser header pointers
|
|
1093 */
|
|
1094
|
24
|
1095 r->request_length += r->header_in->end - r->header_in->start;
|
|
1096
|
0
|
1097 r->header_in = b;
|
|
1098
|
|
1099 return NGX_OK;
|
|
1100 }
|
|
1101
|
|
1102 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1103 "http large header copy: %d", r->header_in->pos - old);
|
|
1104
|
24
|
1105 r->request_length += old - r->header_in->start;
|
|
1106
|
0
|
1107 new = b->start;
|
|
1108
|
|
1109 ngx_memcpy(new, old, r->header_in->pos - old);
|
|
1110
|
|
1111 b->pos = new + (r->header_in->pos - old);
|
|
1112 b->last = new + (r->header_in->pos - old);
|
|
1113
|
|
1114 if (request_line) {
|
|
1115 r->request_start = new;
|
|
1116
|
|
1117 if (r->request_end) {
|
|
1118 r->request_end = new + (r->request_end - old);
|
|
1119 }
|
|
1120
|
|
1121 r->method_end = new + (r->method_end - old);
|
|
1122
|
|
1123 r->uri_start = new + (r->uri_start - old);
|
|
1124 r->uri_end = new + (r->uri_end - old);
|
|
1125
|
|
1126 if (r->schema_start) {
|
|
1127 r->schema_start = new + (r->schema_start - old);
|
|
1128 r->schema_end = new + (r->schema_end - old);
|
|
1129 }
|
|
1130
|
|
1131 if (r->host_start) {
|
|
1132 r->host_start = new + (r->host_start - old);
|
270
|
1133 if (r->host_end) {
|
|
1134 r->host_end = new + (r->host_end - old);
|
|
1135 }
|
0
|
1136 }
|
|
1137
|
|
1138 if (r->port_start) {
|
|
1139 r->port_start = new + (r->port_start - old);
|
|
1140 r->port_end = new + (r->port_end - old);
|
|
1141 }
|
|
1142
|
|
1143 if (r->uri_ext) {
|
|
1144 r->uri_ext = new + (r->uri_ext - old);
|
|
1145 }
|
|
1146
|
|
1147 if (r->args_start) {
|
|
1148 r->args_start = new + (r->args_start - old);
|
|
1149 }
|
|
1150
|
|
1151 } else {
|
|
1152 r->header_name_start = new;
|
|
1153 r->header_name_end = new + (r->header_name_end - old);
|
|
1154 r->header_start = new + (r->header_start - old);
|
|
1155 r->header_end = new + (r->header_end - old);
|
|
1156 }
|
|
1157
|
|
1158 r->header_in = b;
|
|
1159
|
|
1160 return NGX_OK;
|
|
1161 }
|
|
1162
|
|
1163
|
50
|
1164 static ngx_int_t
|
58
|
1165 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1166 ngx_uint_t offset)
|
|
1167 {
|
|
1168 ngx_table_elt_t **ph;
|
|
1169
|
|
1170 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
|
|
1171
|
|
1172 if (*ph == NULL) {
|
|
1173 *ph = h;
|
|
1174 }
|
|
1175
|
|
1176 return NGX_OK;
|
|
1177 }
|
|
1178
|
|
1179
|
|
1180 static ngx_int_t
|
72
|
1181 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1182 ngx_uint_t offset)
|
|
1183 {
|
|
1184 ngx_table_elt_t **ph;
|
|
1185
|
|
1186 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
|
|
1187
|
|
1188 if (*ph == NULL) {
|
|
1189 *ph = h;
|
|
1190 return NGX_OK;
|
|
1191 }
|
|
1192
|
|
1193 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
230
|
1194 "client sent duplicate header line: \"%V: %V\", "
|
|
1195 "previous value: \"%V: %V\"",
|
|
1196 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
|
72
|
1197
|
|
1198 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1199
|
|
1200 return NGX_ERROR;
|
|
1201 }
|
|
1202
|
|
1203
|
|
1204 static ngx_int_t
|
324
|
1205 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1206 ngx_uint_t offset)
|
|
1207 {
|
326
|
1208 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
|
324
|
1209 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
|
|
1210
|
326
|
1211 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
|
324
|
1212 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
|
|
1213 }
|
|
1214
|
|
1215 return NGX_OK;
|
|
1216 }
|
|
1217
|
|
1218
|
|
1219 static ngx_int_t
|
58
|
1220 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1221 ngx_uint_t offset)
|
|
1222 {
|
|
1223 ngx_table_elt_t **cookie;
|
|
1224
|
|
1225 cookie = ngx_array_push(&r->headers_in.cookies);
|
72
|
1226 if (cookie) {
|
|
1227 *cookie = h;
|
|
1228 return NGX_OK;
|
58
|
1229 }
|
|
1230
|
72
|
1231 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1232
|
|
1233 return NGX_ERROR;
|
58
|
1234 }
|
|
1235
|
|
1236
|
|
1237 static ngx_int_t
|
50
|
1238 ngx_http_process_request_header(ngx_http_request_t *r)
|
0
|
1239 {
|
308
|
1240 size_t len;
|
|
1241 u_char *host, *ua, *user_agent, ch;
|
|
1242 ngx_uint_t hash;
|
0
|
1243
|
|
1244 if (r->headers_in.host) {
|
198
|
1245
|
|
1246 hash = 0;
|
|
1247
|
0
|
1248 for (len = 0; len < r->headers_in.host->value.len; len++) {
|
26
|
1249 ch = r->headers_in.host->value.data[len];
|
|
1250
|
|
1251 if (ch == ':') {
|
0
|
1252 break;
|
|
1253 }
|
26
|
1254
|
198
|
1255 ch = ngx_tolower(ch);
|
|
1256 r->headers_in.host->value.data[len] = ch;
|
|
1257 hash = ngx_hash(hash, ch);
|
0
|
1258 }
|
54
|
1259
|
198
|
1260 if (len && r->headers_in.host->value.data[len - 1] == '.') {
|
58
|
1261 len--;
|
198
|
1262 hash = ngx_hash_key(r->headers_in.host->value.data, len);
|
58
|
1263 }
|
|
1264
|
0
|
1265 r->headers_in.host_name_len = len;
|
|
1266
|
198
|
1267 if (r->virtual_names) {
|
306
|
1268
|
|
1269 host = r->host_start;
|
|
1270
|
|
1271 if (host == NULL) {
|
|
1272 host = r->headers_in.host->value.data;
|
|
1273 len = r->headers_in.host_name_len;
|
|
1274
|
|
1275 } else {
|
|
1276 len = r->host_end - host;
|
|
1277 }
|
|
1278
|
|
1279 ngx_http_find_virtual_server(r, host, len, hash);
|
198
|
1280 }
|
0
|
1281
|
|
1282 } else {
|
|
1283 if (r->http_version > NGX_HTTP_VERSION_10) {
|
54
|
1284 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1285 "client sent HTTP/1.1 request without \"Host\" header");
|
|
1286 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1287 return NGX_ERROR;
|
0
|
1288 }
|
54
|
1289
|
0
|
1290 r->headers_in.host_name_len = 0;
|
|
1291 }
|
|
1292
|
|
1293 if (r->headers_in.content_length) {
|
|
1294 r->headers_in.content_length_n =
|
212
|
1295 ngx_atoof(r->headers_in.content_length->value.data,
|
0
|
1296 r->headers_in.content_length->value.len);
|
|
1297
|
|
1298 if (r->headers_in.content_length_n == NGX_ERROR) {
|
54
|
1299 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1300 "client sent invalid \"Content-Length\" header");
|
130
|
1301 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
54
|
1302 return NGX_ERROR;
|
0
|
1303 }
|
|
1304 }
|
|
1305
|
188
|
1306 if (r->method & (NGX_HTTP_POST|NGX_HTTP_PUT)
|
|
1307 && r->headers_in.content_length_n == -1)
|
|
1308 {
|
54
|
1309 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1310 "client sent POST method without \"Content-Length\" header");
|
130
|
1311 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
|
1312 return NGX_ERROR;
|
|
1313 }
|
|
1314
|
324
|
1315 if (r->method & NGX_HTTP_TRACE) {
|
294
|
1316 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1317 "client sent TRACE method");
|
|
1318 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
|
|
1319 return NGX_ERROR;
|
|
1320 }
|
|
1321
|
130
|
1322 if (r->headers_in.transfer_encoding
|
326
|
1323 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
|
|
1324 "chunked", 7 - 1))
|
130
|
1325 {
|
|
1326 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1327 "client sent \"Transfer-Encoding: chunked\" header");
|
|
1328 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
54
|
1329 return NGX_ERROR;
|
0
|
1330 }
|
|
1331
|
|
1332 if (r->plain_http) {
|
54
|
1333 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1334 "client sent plain HTTP request to HTTPS port");
|
|
1335 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
|
|
1336 return NGX_ERROR;
|
0
|
1337 }
|
|
1338
|
324
|
1339 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
|
|
1340 if (r->headers_in.keep_alive) {
|
|
1341 r->headers_in.keep_alive_n =
|
|
1342 ngx_atotm(r->headers_in.keep_alive->value.data,
|
|
1343 r->headers_in.keep_alive->value.len);
|
0
|
1344 }
|
|
1345 }
|
|
1346
|
|
1347 if (r->headers_in.user_agent) {
|
|
1348
|
|
1349 /*
|
|
1350 * check some widespread browsers while the headers are still
|
|
1351 * in CPU cache
|
|
1352 */
|
|
1353
|
|
1354 user_agent = r->headers_in.user_agent->value.data;
|
|
1355
|
326
|
1356 ua = ngx_strstrn(user_agent, "MSIE", 4 - 1);
|
0
|
1357
|
|
1358 if (ua && ua + 8 < user_agent + r->headers_in.user_agent->value.len) {
|
|
1359
|
|
1360 r->headers_in.msie = 1;
|
|
1361
|
|
1362 if (ua[4] == ' ' && ua[5] == '4' && ua[6] == '.') {
|
|
1363 r->headers_in.msie4 = 1;
|
|
1364 }
|
|
1365
|
|
1366 #if 0
|
|
1367 /* MSIE ignores the SSL "close notify" alert */
|
22
|
1368 if (c->ssl) {
|
96
|
1369 c->ssl->no_send_shutdown = 1;
|
22
|
1370 }
|
0
|
1371 #endif
|
|
1372 }
|
|
1373
|
326
|
1374 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
|
0
|
1375 r->headers_in.opera = 1;
|
|
1376 r->headers_in.msie = 0;
|
|
1377 r->headers_in.msie4 = 0;
|
|
1378 }
|
|
1379
|
|
1380 if (!r->headers_in.msie && !r->headers_in.opera) {
|
|
1381
|
326
|
1382 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
|
0
|
1383 r->headers_in.gecko = 1;
|
|
1384
|
326
|
1385 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
|
0
|
1386 r->headers_in.konqueror = 1;
|
|
1387 }
|
|
1388 }
|
|
1389 }
|
|
1390
|
308
|
1391 return NGX_OK;
|
|
1392 }
|
|
1393
|
|
1394
|
|
1395 static void
|
|
1396 ngx_http_process_request(ngx_http_request_t *r)
|
|
1397 {
|
|
1398 ngx_connection_t *c;
|
218
|
1399 #if (NGX_HTTP_SSL)
|
308
|
1400 long rc;
|
|
1401 ngx_http_ssl_srv_conf_t *sscf;
|
|
1402 #endif
|
|
1403
|
|
1404 c = r->connection;
|
|
1405
|
|
1406 #if (NGX_HTTP_SSL)
|
|
1407
|
|
1408 if (c->ssl) {
|
218
|
1409 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
|
|
1410
|
|
1411 if (sscf->verify) {
|
308
|
1412 rc = SSL_get_verify_result(c->ssl->connection);
|
218
|
1413
|
|
1414 if (rc != X509_V_OK) {
|
308
|
1415 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
220
|
1416 "client SSL certificate verify error: (%l:%s)",
|
218
|
1417 rc, X509_verify_cert_error_string(rc));
|
|
1418 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
|
308
|
1419 return;
|
218
|
1420 }
|
|
1421
|
308
|
1422 if (SSL_get_peer_certificate(c->ssl->connection)
|
218
|
1423 == NULL)
|
|
1424 {
|
308
|
1425 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
218
|
1426 "client sent no required SSL certificate");
|
|
1427 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
|
308
|
1428 return;
|
218
|
1429 }
|
|
1430 }
|
|
1431 }
|
|
1432
|
|
1433 #endif
|
|
1434
|
308
|
1435 if (c->read->timer_set) {
|
|
1436 ngx_del_timer(c->read);
|
|
1437 }
|
|
1438
|
|
1439 #if (NGX_STAT_STUB)
|
|
1440 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
|
1441 r->stat_reading = 0;
|
|
1442 ngx_atomic_fetch_add(ngx_stat_writing, 1);
|
|
1443 r->stat_writing = 1;
|
|
1444 #endif
|
|
1445
|
|
1446 c->read->handler = ngx_http_request_handler;
|
|
1447 c->write->handler = ngx_http_request_handler;
|
|
1448 r->read_event_handler = ngx_http_block_read;
|
|
1449
|
|
1450 ngx_http_handler(r);
|
|
1451
|
|
1452 return;
|
0
|
1453 }
|
|
1454
|
|
1455
|
162
|
1456 static void
|
306
|
1457 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len,
|
|
1458 ngx_uint_t hash)
|
26
|
1459 {
|
142
|
1460 ngx_http_core_loc_conf_t *clcf;
|
|
1461 ngx_http_core_srv_conf_t *cscf;
|
326
|
1462 #if (NGX_PCRE)
|
|
1463 ngx_int_t n;
|
|
1464 ngx_uint_t i;
|
|
1465 ngx_str_t name;
|
|
1466 ngx_http_server_name_t *sn;
|
|
1467 #endif
|
|
1468
|
|
1469 cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, host, len);
|
|
1470
|
|
1471 if (cscf) {
|
|
1472 goto found;
|
|
1473 }
|
|
1474
|
|
1475 #if (NGX_PCRE)
|
|
1476
|
|
1477 if (r->virtual_names->nregex) {
|
|
1478
|
|
1479 name.len = len;
|
|
1480 name.data = host;
|
|
1481
|
|
1482 sn = r->virtual_names->regex;
|
|
1483
|
|
1484 for (i = 0; i < r->virtual_names->nregex; i++) {
|
|
1485
|
|
1486 n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
|
|
1487
|
|
1488 if (n == NGX_REGEX_NO_MATCHED) {
|
|
1489 continue;
|
|
1490 }
|
|
1491
|
|
1492 if (n < 0) {
|
|
1493 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1494 ngx_regex_exec_n
|
|
1495 " failed: %d on \"%V\" using \"%V\"",
|
|
1496 n, &name, &sn[i].name);
|
|
1497 return;
|
|
1498 }
|
|
1499
|
|
1500 /* match */
|
|
1501
|
|
1502 cscf = sn[i].core_srv_conf;
|
|
1503
|
58
|
1504 goto found;
|
26
|
1505 }
|
|
1506 }
|
|
1507
|
326
|
1508 #endif
|
26
|
1509
|
212
|
1510 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1511
|
|
1512 if (cscf->wildcard) {
|
|
1513 r->server_name.len = len;
|
|
1514 r->server_name.data = host;
|
|
1515 }
|
|
1516
|
162
|
1517 return;
|
58
|
1518
|
|
1519 found:
|
|
1520
|
142
|
1521 r->server_name.len = len;
|
|
1522 r->server_name.data = host;
|
|
1523
|
|
1524 r->srv_conf = cscf->ctx->srv_conf;
|
|
1525 r->loc_conf = cscf->ctx->loc_conf;
|
58
|
1526
|
|
1527 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1528 r->connection->log->file = clcf->err_log->file;
|
|
1529
|
|
1530 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
|
1531 r->connection->log->log_level = clcf->err_log->log_level;
|
26
|
1532 }
|
|
1533
|
162
|
1534 return;
|
26
|
1535 }
|
|
1536
|
|
1537
|
58
|
1538 static void
|
|
1539 ngx_http_request_handler(ngx_event_t *ev)
|
|
1540 {
|
|
1541 ngx_connection_t *c;
|
|
1542 ngx_http_request_t *r;
|
214
|
1543 ngx_http_log_ctx_t *ctx;
|
58
|
1544
|
|
1545 c = ev->data;
|
|
1546 r = c->data;
|
|
1547
|
214
|
1548 ctx = c->log->data;
|
|
1549 ctx->current_request = r;
|
|
1550
|
58
|
1551 if (ev->write) {
|
|
1552 r->write_event_handler(r);
|
|
1553
|
|
1554 } else {
|
|
1555 r->read_event_handler(r);
|
|
1556 }
|
|
1557 }
|
|
1558
|
|
1559
|
50
|
1560 void
|
58
|
1561 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
0
|
1562 {
|
58
|
1563 ngx_http_request_t *pr;
|
214
|
1564 ngx_http_log_ctx_t *ctx;
|
0
|
1565 ngx_http_core_loc_conf_t *clcf;
|
|
1566
|
58
|
1567 if (rc == NGX_DONE) {
|
118
|
1568 /* the request pool may be already destroyed */
|
58
|
1569 return;
|
|
1570 }
|
|
1571
|
132
|
1572 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1573 "http finalize request: %d, \"%V?%V\"",
|
|
1574 rc, &r->uri, &r->args);
|
|
1575
|
230
|
1576 if (rc == NGX_DECLINED) {
|
|
1577 r->content_handler = NULL;
|
|
1578 r->write_event_handler = ngx_http_core_run_phases;
|
|
1579 ngx_http_core_run_phases(r);
|
|
1580 return;
|
|
1581 }
|
|
1582
|
248
|
1583 if (r != r->main && r->post_subrequest) {
|
|
1584 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
|
216
|
1585 }
|
|
1586
|
162
|
1587 if (rc == NGX_ERROR
|
|
1588 || rc == NGX_HTTP_REQUEST_TIME_OUT
|
|
1589 || r->connection->error)
|
|
1590 {
|
282
|
1591 if (rc > 0 && r->headers_out.status == 0) {
|
|
1592 r->headers_out.status = rc;
|
190
|
1593 }
|
|
1594
|
162
|
1595 if (ngx_http_post_action(r) == NGX_OK) {
|
|
1596 return;
|
|
1597 }
|
|
1598
|
118
|
1599 ngx_http_close_request(r, 0);
|
0
|
1600 return;
|
|
1601 }
|
|
1602
|
186
|
1603 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
|
|
1604 || rc == NGX_HTTP_CREATED
|
|
1605 || rc == NGX_HTTP_NO_CONTENT)
|
|
1606 {
|
136
|
1607 if (rc == NGX_HTTP_CLOSE) {
|
|
1608 ngx_http_close_request(r, rc);
|
|
1609 return;
|
|
1610 }
|
|
1611
|
146
|
1612 if (r == r->main) {
|
118
|
1613 if (r->connection->read->timer_set) {
|
|
1614 ngx_del_timer(r->connection->read);
|
|
1615 }
|
|
1616
|
|
1617 if (r->connection->write->timer_set) {
|
|
1618 ngx_del_timer(r->connection->write);
|
|
1619 }
|
|
1620 }
|
|
1621
|
|
1622 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
|
|
1623 return;
|
|
1624 }
|
|
1625
|
146
|
1626 if (r != r->main || rc == NGX_AGAIN) {
|
74
|
1627 if (ngx_http_set_write_handler(r) != NGX_OK) {
|
|
1628 return;
|
|
1629 }
|
58
|
1630 }
|
|
1631
|
|
1632 r->done = 1;
|
|
1633
|
|
1634 if (r != r->connection->data) {
|
132
|
1635 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1636 "http finalize non-active request: \"%V?%V\"",
|
|
1637 &r->uri, &r->args);
|
58
|
1638 return;
|
|
1639 }
|
|
1640
|
146
|
1641 if (r != r->main) {
|
58
|
1642
|
|
1643 pr = r->parent;
|
|
1644
|
132
|
1645 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1646 "http parent request: \"%V?%V\"", &pr->uri, &pr->args);
|
76
|
1647
|
58
|
1648 if (rc != NGX_AGAIN) {
|
146
|
1649 r->connection->data = pr;
|
58
|
1650 }
|
|
1651
|
214
|
1652 ctx = r->connection->log->data;
|
|
1653 ctx->current_request = pr;
|
|
1654
|
58
|
1655 if (pr->postponed) {
|
|
1656
|
132
|
1657 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1658 "http request: \"%V?%V\" has postponed",
|
|
1659 &pr->uri, &pr->args);
|
76
|
1660
|
58
|
1661 if (rc != NGX_AGAIN && pr->postponed->request == r) {
|
|
1662 pr->postponed = pr->postponed->next;
|
146
|
1663 }
|
|
1664
|
|
1665 if (r->fast_subrequest) {
|
194
|
1666
|
|
1667 if (rc == NGX_AGAIN) {
|
|
1668 r->fast_subrequest = 0;
|
|
1669 }
|
|
1670
|
146
|
1671 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1672 "http fast subrequest: \"%V?%V\" done",
|
|
1673 &r->uri, &r->args);
|
|
1674 return;
|
58
|
1675 }
|
|
1676
|
194
|
1677 if (rc != NGX_AGAIN) {
|
|
1678 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1679 "http wake parent request: \"%V?%V\"",
|
|
1680 &pr->uri, &pr->args);
|
|
1681
|
|
1682 pr->write_event_handler(pr);
|
|
1683 }
|
58
|
1684 }
|
|
1685
|
|
1686 return;
|
|
1687 }
|
0
|
1688
|
118
|
1689 if (rc == NGX_AGAIN) {
|
0
|
1690 return;
|
76
|
1691 }
|
|
1692
|
146
|
1693 if (r->connection->buffered) {
|
74
|
1694 (void) ngx_http_set_write_handler(r);
|
0
|
1695 return;
|
|
1696 }
|
|
1697
|
178
|
1698 if (!r->post_action) {
|
|
1699 r->request_complete = 1;
|
|
1700 }
|
|
1701
|
162
|
1702 if (ngx_http_post_action(r) == NGX_OK) {
|
126
|
1703 return;
|
|
1704 }
|
|
1705
|
0
|
1706 if (r->connection->read->timer_set) {
|
|
1707 ngx_del_timer(r->connection->read);
|
|
1708 }
|
|
1709
|
|
1710 if (r->connection->write->timer_set) {
|
|
1711 r->connection->write->delayed = 0;
|
|
1712 ngx_del_timer(r->connection->write);
|
|
1713 }
|
|
1714
|
138
|
1715 if (r->connection->destroyed) {
|
|
1716 return;
|
|
1717 }
|
|
1718
|
162
|
1719 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
0
|
1720
|
|
1721 if (!ngx_terminate
|
|
1722 && !ngx_exiting
|
|
1723 && r->keepalive != 0
|
|
1724 && clcf->keepalive_timeout > 0)
|
|
1725 {
|
|
1726 ngx_http_set_keepalive(r);
|
|
1727 return;
|
|
1728
|
|
1729 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
|
|
1730 ngx_http_set_lingering_close(r);
|
|
1731 return;
|
|
1732 }
|
|
1733
|
|
1734 ngx_http_close_request(r, 0);
|
|
1735 }
|
|
1736
|
|
1737
|
74
|
1738 static ngx_int_t
|
50
|
1739 ngx_http_set_write_handler(ngx_http_request_t *r)
|
0
|
1740 {
|
|
1741 ngx_event_t *wev;
|
|
1742 ngx_http_core_loc_conf_t *clcf;
|
|
1743
|
58
|
1744 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
|
|
1745
|
324
|
1746 r->read_event_handler = ngx_http_test_read;
|
74
|
1747 r->write_event_handler = ngx_http_writer;
|
|
1748
|
0
|
1749 wev = r->connection->write;
|
|
1750
|
|
1751 if (wev->ready && wev->delayed) {
|
74
|
1752 return NGX_OK;
|
0
|
1753 }
|
|
1754
|
58
|
1755 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
0
|
1756 if (!wev->delayed) {
|
|
1757 ngx_add_timer(wev, clcf->send_timeout);
|
|
1758 }
|
|
1759
|
2
|
1760 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
0
|
1761 ngx_http_close_request(r, 0);
|
74
|
1762 return NGX_ERROR;
|
0
|
1763 }
|
74
|
1764
|
|
1765 return NGX_OK;
|
0
|
1766 }
|
|
1767
|
|
1768
|
58
|
1769 static void
|
|
1770 ngx_http_writer(ngx_http_request_t *r)
|
0
|
1771 {
|
|
1772 int rc;
|
58
|
1773 ngx_event_t *wev;
|
0
|
1774 ngx_connection_t *c;
|
|
1775 ngx_http_core_loc_conf_t *clcf;
|
|
1776
|
58
|
1777 c = r->connection;
|
|
1778 wev = c->write;
|
|
1779
|
132
|
1780 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
1781 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
|
0
|
1782
|
|
1783 if (wev->timedout) {
|
|
1784 if (!wev->delayed) {
|
54
|
1785 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
1786 "client timed out");
|
|
1787 c->timedout = 1;
|
116
|
1788
|
162
|
1789 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
1790 return;
|
|
1791 }
|
|
1792
|
|
1793 wev->timedout = 0;
|
|
1794 wev->delayed = 0;
|
|
1795
|
|
1796 if (!wev->ready) {
|
102
|
1797 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
0
|
1798 ngx_add_timer(wev, clcf->send_timeout);
|
|
1799
|
2
|
1800 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
118
|
1801 ngx_http_close_request(r, 0);
|
0
|
1802 }
|
|
1803
|
|
1804 return;
|
|
1805 }
|
|
1806
|
|
1807 } else {
|
|
1808 if (wev->delayed) {
|
|
1809 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
1810 "http writer delayed");
|
|
1811
|
102
|
1812 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
2
|
1813
|
|
1814 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
118
|
1815 ngx_http_close_request(r, 0);
|
0
|
1816 }
|
|
1817
|
|
1818 return;
|
|
1819 }
|
|
1820 }
|
|
1821
|
132
|
1822 rc = ngx_http_output_filter(r, NULL);
|
|
1823
|
|
1824 if (c->destroyed) {
|
|
1825 return;
|
58
|
1826 }
|
|
1827
|
132
|
1828 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1829 "http writer output filter: %d, \"%V?%V\"",
|
|
1830 rc, &r->uri, &r->args);
|
0
|
1831
|
|
1832 if (rc == NGX_AGAIN) {
|
102
|
1833 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
0
|
1834 if (!wev->ready && !wev->delayed) {
|
|
1835 ngx_add_timer(wev, clcf->send_timeout);
|
|
1836 }
|
|
1837
|
2
|
1838 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
118
|
1839 ngx_http_close_request(r, 0);
|
0
|
1840 }
|
|
1841
|
254
|
1842 if (r == r->main || r->buffered) {
|
146
|
1843 return;
|
|
1844 }
|
|
1845
|
|
1846 rc = NGX_OK;
|
0
|
1847 }
|
|
1848
|
132
|
1849 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
1850 "http writer done: \"%V?%V\"", &r->uri, &r->args);
|
0
|
1851
|
|
1852 ngx_http_finalize_request(r, rc);
|
|
1853 }
|
|
1854
|
|
1855
|
50
|
1856 static void
|
58
|
1857 ngx_http_block_read(ngx_http_request_t *r)
|
0
|
1858 {
|
324
|
1859 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1860 "http read blocked");
|
|
1861
|
|
1862 /* aio does not call this handler */
|
|
1863
|
|
1864 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
|
|
1865 && r->connection->read->active)
|
|
1866 {
|
|
1867 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0)
|
|
1868 == NGX_ERROR)
|
|
1869 {
|
|
1870 ngx_http_close_request(r, 0);
|
|
1871 }
|
|
1872 }
|
|
1873 }
|
|
1874
|
|
1875
|
|
1876 static void
|
|
1877 ngx_http_test_read(ngx_http_request_t *r)
|
|
1878 {
|
276
|
1879 int n;
|
|
1880 char buf[1];
|
|
1881 ngx_err_t err;
|
|
1882 ngx_event_t *rev;
|
|
1883 ngx_connection_t *c;
|
|
1884
|
|
1885 c = r->connection;
|
|
1886 rev = c->read;
|
|
1887
|
324
|
1888 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test read");
|
276
|
1889
|
|
1890 #if (NGX_HAVE_KQUEUE)
|
|
1891
|
|
1892 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
|
1893
|
|
1894 if (!rev->pending_eof) {
|
|
1895 return;
|
|
1896 }
|
|
1897
|
|
1898 rev->eof = 1;
|
|
1899 c->error = 1;
|
|
1900 err = rev->kq_errno;
|
|
1901
|
|
1902 goto closed;
|
|
1903 }
|
|
1904
|
|
1905 #endif
|
|
1906
|
|
1907 n = recv(c->fd, buf, 1, MSG_PEEK);
|
|
1908
|
|
1909 if (n == 0) {
|
|
1910 rev->eof = 1;
|
|
1911 c->error = 1;
|
|
1912 err = 0;
|
|
1913
|
|
1914 goto closed;
|
|
1915
|
|
1916 } else if (n == -1) {
|
|
1917 err = ngx_socket_errno;
|
|
1918
|
|
1919 if (err != NGX_EAGAIN) {
|
|
1920 rev->eof = 1;
|
|
1921 c->error = 1;
|
|
1922
|
|
1923 goto closed;
|
|
1924 }
|
|
1925 }
|
0
|
1926
|
|
1927 /* aio does not call this handler */
|
|
1928
|
276
|
1929 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
|
|
1930
|
|
1931 if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
0
|
1932 ngx_http_close_request(r, 0);
|
|
1933 }
|
|
1934 }
|
276
|
1935
|
|
1936 return;
|
|
1937
|
|
1938 closed:
|
|
1939
|
|
1940 if (err) {
|
|
1941 rev->error = 1;
|
|
1942 }
|
|
1943
|
|
1944 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
1945 "client closed prematurely connection");
|
|
1946
|
286
|
1947 ngx_http_finalize_request(r, 0);
|
0
|
1948 }
|
|
1949
|
|
1950
|
50
|
1951 static void
|
|
1952 ngx_http_set_keepalive(ngx_http_request_t *r)
|
0
|
1953 {
|
2
|
1954 int tcp_nodelay;
|
0
|
1955 ngx_int_t i;
|
|
1956 ngx_buf_t *b, *f;
|
|
1957 ngx_event_t *rev, *wev;
|
|
1958 ngx_connection_t *c;
|
|
1959 ngx_http_connection_t *hc;
|
|
1960 ngx_http_core_srv_conf_t *cscf;
|
|
1961 ngx_http_core_loc_conf_t *clcf;
|
|
1962
|
|
1963 c = r->connection;
|
|
1964 rev = c->read;
|
|
1965
|
|
1966 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
|
|
1967
|
32
|
1968 c->log->action = "closing request";
|
0
|
1969
|
|
1970 hc = r->http_connection;
|
|
1971 b = r->header_in;
|
|
1972
|
|
1973 if (b->pos < b->last) {
|
|
1974
|
|
1975 /* the pipelined request */
|
|
1976
|
|
1977 if (b != c->buffer) {
|
|
1978
|
4
|
1979 /*
|
|
1980 * If the large header buffers were allocated while the previous
|
|
1981 * request processing then we do not use c->buffer for
|
|
1982 * the pipelined request (see ngx_http_init_request()).
|
126
|
1983 *
|
4
|
1984 * Now we would move the large header buffers to the free list.
|
|
1985 */
|
0
|
1986
|
|
1987 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1988
|
|
1989 if (hc->free == NULL) {
|
|
1990 hc->free = ngx_palloc(c->pool,
|
|
1991 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
1992
|
|
1993 if (hc->free == NULL) {
|
26
|
1994 ngx_http_close_request(r, 0);
|
0
|
1995 return;
|
|
1996 }
|
|
1997 }
|
|
1998
|
|
1999 for (i = 0; i < hc->nbusy - 1; i++) {
|
|
2000 f = hc->busy[i];
|
|
2001 hc->free[hc->nfree++] = f;
|
|
2002 f->pos = f->start;
|
|
2003 f->last = f->start;
|
|
2004 }
|
|
2005
|
|
2006 hc->busy[0] = b;
|
|
2007 hc->nbusy = 1;
|
|
2008 }
|
|
2009 }
|
|
2010
|
26
|
2011 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2012
|
118
|
2013 ngx_http_request_done(r, 0);
|
|
2014
|
0
|
2015 c->data = hc;
|
|
2016
|
|
2017 ngx_add_timer(rev, clcf->keepalive_timeout);
|
|
2018
|
58
|
2019 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
0
|
2020 ngx_http_close_connection(c);
|
|
2021 return;
|
|
2022 }
|
|
2023
|
|
2024 wev = c->write;
|
58
|
2025 wev->handler = ngx_http_empty_handler;
|
0
|
2026
|
|
2027 if (b->pos < b->last) {
|
|
2028
|
|
2029 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
|
|
2030
|
116
|
2031 #if (NGX_STAT_STUB)
|
|
2032 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
|
2033 #endif
|
|
2034
|
0
|
2035 hc->pipeline = 1;
|
32
|
2036 c->log->action = "reading client pipelined request line";
|
116
|
2037
|
0
|
2038 ngx_http_init_request(rev);
|
|
2039 return;
|
|
2040 }
|
|
2041
|
|
2042 hc->pipeline = 0;
|
|
2043
|
4
|
2044 /*
|
|
2045 * To keep a memory footprint as small as possible for an idle
|
|
2046 * keepalive connection we try to free the ngx_http_request_t and
|
|
2047 * c->buffer's memory if they were allocated outside the c->pool.
|
|
2048 * The large header buffers are always allocated outside the c->pool and
|
|
2049 * are freed too.
|
|
2050 */
|
|
2051
|
0
|
2052 if (ngx_pfree(c->pool, r) == NGX_OK) {
|
|
2053 hc->request = NULL;
|
|
2054 }
|
|
2055
|
|
2056 b = c->buffer;
|
|
2057
|
|
2058 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
|
4
|
2059
|
|
2060 /*
|
|
2061 * the special note for ngx_http_keepalive_handler() that
|
|
2062 * c->buffer's memory was freed
|
|
2063 */
|
|
2064
|
0
|
2065 b->pos = NULL;
|
|
2066
|
|
2067 } else {
|
|
2068 b->pos = b->start;
|
|
2069 b->last = b->start;
|
|
2070 }
|
|
2071
|
10
|
2072 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
|
0
|
2073 hc->free, hc->nfree);
|
|
2074
|
|
2075 if (hc->free) {
|
|
2076 for (i = 0; i < hc->nfree; i++) {
|
|
2077 ngx_pfree(c->pool, hc->free[i]);
|
|
2078 hc->free[i] = NULL;
|
|
2079 }
|
|
2080
|
|
2081 hc->nfree = 0;
|
|
2082 }
|
|
2083
|
10
|
2084 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
|
0
|
2085 hc->busy, hc->nbusy);
|
|
2086
|
|
2087 if (hc->busy) {
|
|
2088 for (i = 0; i < hc->nbusy; i++) {
|
|
2089 ngx_pfree(c->pool, hc->busy[i]);
|
|
2090 hc->busy[i] = NULL;
|
|
2091 }
|
|
2092
|
|
2093 hc->nbusy = 0;
|
|
2094 }
|
|
2095
|
58
|
2096 rev->handler = ngx_http_keepalive_handler;
|
0
|
2097
|
132
|
2098 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
|
2099 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
|
|
2100 ngx_http_close_connection(c);
|
|
2101 return;
|
0
|
2102 }
|
|
2103 }
|
|
2104
|
32
|
2105 c->log->action = "keepalive";
|
0
|
2106
|
|
2107 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
|
50
|
2108 if (ngx_tcp_push(c->fd) == -1) {
|
0
|
2109 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
|
|
2110 ngx_http_close_connection(c);
|
|
2111 return;
|
|
2112 }
|
2
|
2113
|
0
|
2114 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
|
22
|
2115 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
|
2
|
2116
|
|
2117 } else {
|
22
|
2118 tcp_nodelay = 1;
|
|
2119 }
|
|
2120
|
32
|
2121 if (tcp_nodelay
|
|
2122 && clcf->tcp_nodelay
|
|
2123 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
|
|
2124 {
|
22
|
2125 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
|
|
2126
|
|
2127 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
184
|
2128 (const void *) &tcp_nodelay, sizeof(int))
|
|
2129 == -1)
|
22
|
2130 {
|
|
2131 ngx_connection_error(c, ngx_socket_errno,
|
|
2132 "setsockopt(TCP_NODELAY) failed");
|
|
2133 ngx_http_close_connection(c);
|
|
2134 return;
|
2
|
2135 }
|
22
|
2136
|
32
|
2137 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
|
0
|
2138 }
|
|
2139
|
|
2140 #if 0
|
4
|
2141 /* if ngx_http_request_t was freed then we need some other place */
|
0
|
2142 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
|
|
2143 #endif
|
|
2144
|
290
|
2145 c->idle = 1;
|
|
2146
|
0
|
2147 if (rev->ready) {
|
|
2148 ngx_http_keepalive_handler(rev);
|
|
2149 }
|
|
2150 }
|
|
2151
|
|
2152
|
50
|
2153 static void
|
|
2154 ngx_http_keepalive_handler(ngx_event_t *rev)
|
0
|
2155 {
|
50
|
2156 size_t size;
|
|
2157 ssize_t n;
|
|
2158 ngx_buf_t *b;
|
|
2159 ngx_connection_t *c;
|
0
|
2160
|
|
2161 c = rev->data;
|
|
2162
|
|
2163 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
|
|
2164
|
290
|
2165 if (rev->timedout || c->close) {
|
0
|
2166 ngx_http_close_connection(c);
|
|
2167 return;
|
|
2168 }
|
|
2169
|
18
|
2170 #if (NGX_HAVE_KQUEUE)
|
0
|
2171
|
4
|
2172 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
136
|
2173 if (rev->pending_eof) {
|
32
|
2174 c->log->handler = NULL;
|
0
|
2175 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
|
10
|
2176 "kevent() reported that client %V closed "
|
32
|
2177 "keepalive connection", &c->addr_text);
|
22
|
2178 #if (NGX_HTTP_SSL)
|
|
2179 if (c->ssl) {
|
96
|
2180 c->ssl->no_send_shutdown = 1;
|
22
|
2181 }
|
|
2182 #endif
|
0
|
2183 ngx_http_close_connection(c);
|
|
2184 return;
|
|
2185 }
|
|
2186 }
|
|
2187
|
|
2188 #endif
|
|
2189
|
|
2190 b = c->buffer;
|
|
2191 size = b->end - b->start;
|
|
2192
|
|
2193 if (b->pos == NULL) {
|
4
|
2194
|
|
2195 /*
|
|
2196 * The c->buffer's memory was freed by ngx_http_set_keepalive().
|
|
2197 * However, the c->buffer->start and c->buffer->end were not changed
|
|
2198 * to keep the buffer size.
|
|
2199 */
|
|
2200
|
50
|
2201 b->pos = ngx_palloc(c->pool, size);
|
|
2202 if (b->pos == NULL) {
|
0
|
2203 ngx_http_close_connection(c);
|
|
2204 return;
|
|
2205 }
|
|
2206
|
|
2207 b->start = b->pos;
|
|
2208 b->last = b->pos;
|
|
2209 b->end = b->pos + size;
|
|
2210 }
|
|
2211
|
|
2212 /*
|
|
2213 * MSIE closes a keepalive connection with RST flag
|
|
2214 * so we ignore ECONNRESET here.
|
|
2215 */
|
|
2216
|
|
2217 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
|
|
2218 ngx_set_socket_errno(0);
|
|
2219
|
|
2220 n = c->recv(c, b->last, size);
|
|
2221 c->log_error = NGX_ERROR_INFO;
|
|
2222
|
|
2223 if (n == NGX_AGAIN) {
|
58
|
2224 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
24
|
2225 ngx_http_close_connection(c);
|
|
2226 }
|
|
2227
|
0
|
2228 return;
|
|
2229 }
|
|
2230
|
|
2231 if (n == NGX_ERROR) {
|
|
2232 ngx_http_close_connection(c);
|
|
2233 return;
|
|
2234 }
|
|
2235
|
32
|
2236 c->log->handler = NULL;
|
0
|
2237
|
|
2238 if (n == 0) {
|
|
2239 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
|
32
|
2240 "client %V closed keepalive connection", &c->addr_text);
|
0
|
2241 ngx_http_close_connection(c);
|
|
2242 return;
|
|
2243 }
|
|
2244
|
|
2245 b->last += n;
|
32
|
2246
|
116
|
2247 #if (NGX_STAT_STUB)
|
|
2248 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
|
2249 #endif
|
|
2250
|
32
|
2251 c->log->handler = ngx_http_log_error;
|
|
2252 c->log->action = "reading client request line";
|
0
|
2253
|
290
|
2254 c->idle = 0;
|
|
2255
|
0
|
2256 ngx_http_init_request(rev);
|
|
2257 }
|
|
2258
|
|
2259
|
50
|
2260 static void
|
|
2261 ngx_http_set_lingering_close(ngx_http_request_t *r)
|
126
|
2262 {
|
0
|
2263 ngx_event_t *rev, *wev;
|
|
2264 ngx_connection_t *c;
|
|
2265 ngx_http_core_loc_conf_t *clcf;
|
|
2266
|
|
2267 c = r->connection;
|
|
2268
|
|
2269 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2270
|
|
2271 rev = c->read;
|
58
|
2272 rev->handler = ngx_http_lingering_close_handler;
|
0
|
2273
|
106
|
2274 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
|
0
|
2275 ngx_add_timer(rev, clcf->lingering_timeout);
|
|
2276
|
58
|
2277 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
0
|
2278 ngx_http_close_request(r, 0);
|
|
2279 return;
|
|
2280 }
|
|
2281
|
|
2282 wev = c->write;
|
58
|
2283 wev->handler = ngx_http_empty_handler;
|
0
|
2284
|
132
|
2285 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
|
2286 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
|
|
2287 ngx_http_close_request(r, 0);
|
|
2288 return;
|
0
|
2289 }
|
|
2290 }
|
|
2291
|
|
2292 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
|
|
2293 ngx_connection_error(c, ngx_socket_errno,
|
|
2294 ngx_shutdown_socket_n " failed");
|
|
2295 ngx_http_close_request(r, 0);
|
|
2296 return;
|
|
2297 }
|
|
2298
|
|
2299 if (rev->ready) {
|
|
2300 ngx_http_lingering_close_handler(rev);
|
|
2301 }
|
|
2302 }
|
|
2303
|
|
2304
|
50
|
2305 static void
|
|
2306 ngx_http_lingering_close_handler(ngx_event_t *rev)
|
0
|
2307 {
|
|
2308 ssize_t n;
|
|
2309 ngx_msec_t timer;
|
|
2310 ngx_connection_t *c;
|
|
2311 ngx_http_request_t *r;
|
|
2312 ngx_http_core_loc_conf_t *clcf;
|
|
2313 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
|
|
2314
|
|
2315 c = rev->data;
|
|
2316 r = c->data;
|
|
2317
|
|
2318 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2319 "http lingering close handler");
|
|
2320
|
|
2321 if (rev->timedout) {
|
126
|
2322 c->timedout = 1;
|
0
|
2323 ngx_http_close_request(r, 0);
|
|
2324 return;
|
|
2325 }
|
|
2326
|
|
2327 timer = r->lingering_time - ngx_time();
|
|
2328 if (timer <= 0) {
|
|
2329 ngx_http_close_request(r, 0);
|
|
2330 return;
|
|
2331 }
|
|
2332
|
|
2333 do {
|
|
2334 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
|
|
2335
|
|
2336 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
|
|
2337
|
|
2338 if (n == NGX_ERROR || n == 0) {
|
|
2339 ngx_http_close_request(r, 0);
|
|
2340 return;
|
|
2341 }
|
|
2342
|
|
2343 } while (rev->ready);
|
|
2344
|
58
|
2345 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
26
|
2346 ngx_http_close_request(r, 0);
|
24
|
2347 return;
|
|
2348 }
|
|
2349
|
0
|
2350 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2351
|
|
2352 timer *= 1000;
|
|
2353
|
|
2354 if (timer > clcf->lingering_timeout) {
|
|
2355 timer = clcf->lingering_timeout;
|
|
2356 }
|
|
2357
|
|
2358 ngx_add_timer(rev, timer);
|
|
2359 }
|
|
2360
|
|
2361
|
50
|
2362 void
|
|
2363 ngx_http_empty_handler(ngx_event_t *wev)
|
0
|
2364 {
|
|
2365 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
|
|
2366
|
|
2367 return;
|
|
2368 }
|
|
2369
|
|
2370
|
58
|
2371 void
|
|
2372 ngx_http_request_empty_handler(ngx_http_request_t *r)
|
|
2373 {
|
|
2374 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
132
|
2375 "http request empty handler");
|
58
|
2376
|
|
2377 return;
|
|
2378 }
|
|
2379
|
|
2380
|
50
|
2381 ngx_int_t
|
130
|
2382 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
|
0
|
2383 {
|
|
2384 ngx_buf_t *b;
|
|
2385 ngx_chain_t out;
|
|
2386
|
50
|
2387 b = ngx_calloc_buf(r->pool);
|
|
2388 if (b == NULL) {
|
0
|
2389 return NGX_ERROR;
|
|
2390 }
|
|
2391
|
130
|
2392 if (flags & NGX_HTTP_LAST) {
|
|
2393 b->last_buf = 1;
|
|
2394 }
|
|
2395
|
|
2396 if (flags & NGX_HTTP_FLUSH) {
|
|
2397 b->flush = 1;
|
|
2398 }
|
|
2399
|
0
|
2400 out.buf = b;
|
|
2401 out.next = NULL;
|
|
2402
|
|
2403 return ngx_http_output_filter(r, &out);
|
|
2404 }
|
|
2405
|
|
2406
|
162
|
2407 static ngx_int_t
|
|
2408 ngx_http_post_action(ngx_http_request_t *r)
|
|
2409 {
|
|
2410 ngx_http_core_loc_conf_t *clcf;
|
|
2411
|
|
2412 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2413
|
|
2414 if (clcf->post_action.data == NULL) {
|
|
2415 return NGX_DECLINED;
|
|
2416 }
|
|
2417
|
172
|
2418 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2419 "post action: \"%V\"", &clcf->post_action);
|
|
2420
|
162
|
2421 r->http_version = NGX_HTTP_VERSION_9;
|
|
2422 r->header_only = 1;
|
178
|
2423 r->post_action = 1;
|
162
|
2424
|
262
|
2425 r->read_event_handler = ngx_http_block_read;
|
|
2426
|
162
|
2427 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
|
|
2428
|
|
2429 return NGX_OK;
|
|
2430 }
|
|
2431
|
|
2432
|
|
2433 static void
|
58
|
2434 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
|
0
|
2435 {
|
188
|
2436 ngx_connection_t *c;
|
118
|
2437
|
|
2438 c = r->connection;
|
188
|
2439
|
|
2440 ngx_http_request_done(r->main, error);
|
118
|
2441 ngx_http_close_connection(c);
|
|
2442 }
|
|
2443
|
|
2444
|
162
|
2445 static void
|
118
|
2446 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
|
|
2447 {
|
|
2448 ngx_log_t *log;
|
130
|
2449 ngx_uint_t i, n;
|
118
|
2450 struct linger linger;
|
188
|
2451 ngx_http_cleanup_t *cln;
|
118
|
2452 ngx_http_log_ctx_t *ctx;
|
130
|
2453 ngx_http_handler_pt *log_handler;
|
118
|
2454 ngx_http_core_loc_conf_t *clcf;
|
|
2455 ngx_http_core_main_conf_t *cmcf;
|
0
|
2456
|
|
2457 log = r->connection->log;
|
|
2458
|
|
2459 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
|
|
2460
|
|
2461 if (r->pool == NULL) {
|
80
|
2462 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
|
0
|
2463 return;
|
|
2464 }
|
|
2465
|
188
|
2466 for (cln = r->cleanup; cln; cln = cln->next) {
|
|
2467 if (cln->handler) {
|
|
2468 cln->handler(cln->data);
|
|
2469 }
|
|
2470 }
|
|
2471
|
0
|
2472 #if (NGX_STAT_STUB)
|
116
|
2473
|
0
|
2474 if (r->stat_reading) {
|
110
|
2475 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
0
|
2476 }
|
|
2477
|
|
2478 if (r->stat_writing) {
|
110
|
2479 ngx_atomic_fetch_add(ngx_stat_writing, -1);
|
0
|
2480 }
|
116
|
2481
|
0
|
2482 #endif
|
|
2483
|
|
2484 if (error && r->headers_out.status == 0) {
|
|
2485 r->headers_out.status = error;
|
|
2486 }
|
|
2487
|
118
|
2488 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
2489
|
130
|
2490 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
|
|
2491 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
|
|
2492 for (i = 0; i < n; i++) {
|
|
2493 log_handler[i](r);
|
118
|
2494 }
|
0
|
2495
|
|
2496 if (r->connection->timedout) {
|
|
2497 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2498
|
|
2499 if (clcf->reset_timedout_connection) {
|
58
|
2500 linger.l_onoff = 1;
|
|
2501 linger.l_linger = 0;
|
0
|
2502
|
|
2503 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
|
58
|
2504 (const void *) &linger, sizeof(struct linger)) == -1)
|
0
|
2505 {
|
|
2506 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
|
|
2507 "setsockopt(SO_LINGER) failed");
|
|
2508 }
|
|
2509 }
|
|
2510 }
|
|
2511
|
58
|
2512 /* the various request strings were allocated from r->pool */
|
0
|
2513 ctx = log->data;
|
10
|
2514 ctx->request = NULL;
|
0
|
2515
|
|
2516 r->request_line.len = 0;
|
|
2517
|
132
|
2518 r->connection->destroyed = 1;
|
|
2519
|
0
|
2520 ngx_destroy_pool(r->pool);
|
|
2521 }
|
|
2522
|
|
2523
|
80
|
2524 static void
|
50
|
2525 ngx_http_close_connection(ngx_connection_t *c)
|
0
|
2526 {
|
28
|
2527 ngx_pool_t *pool;
|
|
2528
|
0
|
2529 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2530 "close http connection: %d", c->fd);
|
|
2531
|
88
|
2532 #if (NGX_HTTP_SSL)
|
2
|
2533
|
|
2534 if (c->ssl) {
|
|
2535 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
2536 c->ssl->handler = ngx_http_close_connection;
|
2
|
2537 return;
|
|
2538 }
|
|
2539 }
|
|
2540
|
|
2541 #endif
|
|
2542
|
0
|
2543 #if (NGX_STAT_STUB)
|
110
|
2544 ngx_atomic_fetch_add(ngx_stat_active, -1);
|
0
|
2545 #endif
|
|
2546
|
132
|
2547 c->destroyed = 1;
|
|
2548
|
28
|
2549 pool = c->pool;
|
|
2550
|
0
|
2551 ngx_close_connection(c);
|
28
|
2552
|
50
|
2553 ngx_destroy_pool(pool);
|
0
|
2554 }
|
|
2555
|
|
2556
|
50
|
2557 static u_char *
|
|
2558 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
0
|
2559 {
|
32
|
2560 u_char *p;
|
54
|
2561 ngx_http_request_t *r;
|
32
|
2562 ngx_http_log_ctx_t *ctx;
|
10
|
2563
|
32
|
2564 if (log->action) {
|
58
|
2565 p = ngx_snprintf(buf, len, " while %s", log->action);
|
10
|
2566 len -= p - buf;
|
54
|
2567 buf = p;
|
10
|
2568 }
|
|
2569
|
58
|
2570 ctx = log->data;
|
|
2571
|
54
|
2572 p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
58
|
2573 len -= p - buf;
|
54
|
2574
|
|
2575 r = ctx->request;
|
|
2576
|
58
|
2577 if (r) {
|
214
|
2578 return r->log_handler(r, ctx->current_request, p, len);
|
0
|
2579 }
|
10
|
2580
|
58
|
2581 return p;
|
|
2582 }
|
|
2583
|
|
2584
|
|
2585 static u_char *
|
214
|
2586 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
|
|
2587 u_char *buf, size_t len)
|
58
|
2588 {
|
260
|
2589 char *uri_separator;
|
|
2590 u_char *p;
|
|
2591 ngx_http_upstream_t *u;
|
54
|
2592
|
|
2593 if (r->server_name.data) {
|
|
2594 p = ngx_snprintf(buf, len, ", server: %V", &r->server_name);
|
52
|
2595 len -= p - buf;
|
54
|
2596 buf = p;
|
52
|
2597 }
|
|
2598
|
54
|
2599 if (r->unparsed_uri.data) {
|
|
2600 p = ngx_snprintf(buf, len, ", URL: \"%V\"", &r->unparsed_uri);
|
|
2601 len -= p - buf;
|
|
2602 buf = p;
|
|
2603
|
|
2604 } else {
|
|
2605 if (r->request_line.data == NULL && r->request_start) {
|
|
2606 for (p = r->request_start; p < r->header_in->last; p++) {
|
|
2607 if (*p == CR || *p == LF) {
|
|
2608 break;
|
|
2609 }
|
|
2610 }
|
|
2611
|
|
2612 r->request_line.len = p - r->request_start;
|
|
2613 r->request_line.data = r->request_start;
|
|
2614 }
|
|
2615
|
|
2616 if (r->request_line.len) {
|
|
2617 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
|
|
2618 len -= p - buf;
|
|
2619 buf = p;
|
|
2620 }
|
10
|
2621 }
|
|
2622
|
214
|
2623 if (r != sr) {
|
|
2624 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
|
|
2625 len -= p - buf;
|
|
2626 buf = p;
|
|
2627 }
|
|
2628
|
|
2629 u = sr->upstream;
|
|
2630
|
260
|
2631 if (u && u->peer.name) {
|
|
2632
|
|
2633 uri_separator = "";
|
|
2634
|
|
2635 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
2636 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
|
|
2637 uri_separator = ":";
|
|
2638 }
|
|
2639 #endif
|
214
|
2640
|
|
2641 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
|
260
|
2642 &u->conf->schema, u->peer.name,
|
|
2643 uri_separator, &u->uri);
|
214
|
2644 len -= p - buf;
|
|
2645 buf = p;
|
|
2646 }
|
|
2647
|
54
|
2648 if (r->headers_in.host) {
|
|
2649 p = ngx_snprintf(buf, len, ", host: \"%V\"",
|
|
2650 &r->headers_in.host->value);
|
|
2651 len -= p - buf;
|
|
2652 buf = p;
|
|
2653 }
|
|
2654
|
|
2655 if (r->headers_in.referer) {
|
|
2656 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
|
|
2657 &r->headers_in.referer->value);
|
|
2658 buf = p;
|
|
2659 }
|
|
2660
|
|
2661 return buf;
|
|
2662 }
|