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