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