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
|
508
|
787 while (p > r->uri.data) {
|
|
788
|
|
789 if (*p == ' ') {
|
|
790 p--;
|
|
791 continue;
|
|
792 }
|
|
793
|
|
794 if (*p == '.') {
|
|
795 p--;
|
|
796 continue;
|
506
|
797 }
|
500
|
798
|
508
|
799 if (ngx_strncasecmp(p - 6, (u_char *) "::$data", 7) == 0) {
|
|
800 p -= 7;
|
|
801 continue;
|
|
802 }
|
|
803
|
|
804 break;
|
|
805 }
|
|
806
|
|
807 if (p != r->uri.data + r->uri.len - 1) {
|
500
|
808 r->uri.len = p + 1 - r->uri.data;
|
|
809 ngx_http_set_exten(r);
|
|
810 }
|
508
|
811
|
500
|
812 }
|
|
813 #endif
|
10
|
814
|
|
815 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
816 "http request line: \"%V\"", &r->request_line);
|
|
817
|
0
|
818 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
819 "http uri: \"%V\"", &r->uri);
|
0
|
820
|
|
821 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
822 "http args: \"%V\"", &r->args);
|
0
|
823
|
|
824 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
825 "http exten: \"%V\"", &r->exten);
|
|
826
|
376
|
827 if (r->host_start && r->host_end) {
|
506
|
828
|
|
829 host = r->host_start;
|
|
830 n = ngx_http_validate_host(r, &host,
|
|
831 r->host_end - r->host_start, 0);
|
|
832
|
|
833 if (n == 0) {
|
376
|
834 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
835 "client sent invalid host in request line");
|
|
836 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
837 return;
|
|
838 }
|
|
839
|
506
|
840 if (n < 0) {
|
|
841 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
842 return;
|
|
843 }
|
|
844
|
376
|
845 r->headers_in.server.len = n;
|
506
|
846 r->headers_in.server.data = host;
|
376
|
847 }
|
|
848
|
0
|
849 if (r->http_version < NGX_HTTP_VERSION_10) {
|
376
|
850
|
|
851 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
|
|
852 r->headers_in.server.len)
|
|
853 == NGX_ERROR)
|
|
854 {
|
|
855 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
856 return;
|
|
857 }
|
|
858
|
308
|
859 ngx_http_process_request(r);
|
0
|
860 return;
|
|
861 }
|
|
862
|
|
863
|
|
864 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
|
130
|
865 sizeof(ngx_table_elt_t))
|
454
|
866 != NGX_OK)
|
0
|
867 {
|
|
868 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
869 return;
|
|
870 }
|
|
871
|
|
872
|
48
|
873 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
|
130
|
874 sizeof(ngx_table_elt_t *))
|
454
|
875 != NGX_OK)
|
0
|
876 {
|
|
877 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
878 return;
|
|
879 }
|
|
880
|
32
|
881 c->log->action = "reading client request headers";
|
0
|
882
|
58
|
883 rev->handler = ngx_http_process_request_headers;
|
0
|
884 ngx_http_process_request_headers(rev);
|
|
885
|
|
886 return;
|
54
|
887 }
|
|
888
|
|
889 if (rc != NGX_AGAIN) {
|
0
|
890
|
|
891 /* there was error while a request line parsing */
|
|
892
|
54
|
893 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
894 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
|
|
895 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
896 return;
|
|
897 }
|
|
898
|
|
899 /* NGX_AGAIN: a request line parsing is still incomplete */
|
|
900
|
|
901 if (r->header_in->pos == r->header_in->end) {
|
|
902
|
|
903 rv = ngx_http_alloc_large_header_buffer(r, 1);
|
|
904
|
|
905 if (rv == NGX_ERROR) {
|
|
906 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
907 return;
|
|
908 }
|
|
909
|
|
910 if (rv == NGX_DECLINED) {
|
54
|
911 r->request_line.len = r->header_in->end - r->request_start;
|
|
912 r->request_line.data = r->request_start;
|
|
913
|
|
914 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
915 "client sent too long URI");
|
|
916 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
|
0
|
917 return;
|
|
918 }
|
|
919 }
|
|
920 }
|
|
921 }
|
|
922
|
|
923
|
50
|
924 static void
|
|
925 ngx_http_process_request_headers(ngx_event_t *rev)
|
0
|
926 {
|
500
|
927 u_char *p;
|
|
928 size_t len;
|
56
|
929 ssize_t n;
|
|
930 ngx_int_t rc, rv;
|
58
|
931 ngx_table_elt_t *h;
|
56
|
932 ngx_connection_t *c;
|
|
933 ngx_http_header_t *hh;
|
|
934 ngx_http_request_t *r;
|
60
|
935 ngx_http_core_srv_conf_t *cscf;
|
56
|
936 ngx_http_core_main_conf_t *cmcf;
|
0
|
937
|
|
938 c = rev->data;
|
|
939 r = c->data;
|
|
940
|
|
941 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
942 "http process request header line");
|
|
943
|
|
944 if (rev->timedout) {
|
54
|
945 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
946 c->timedout = 1;
|
|
947 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
948 return;
|
|
949 }
|
|
950
|
56
|
951 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
60
|
952 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
56
|
953
|
0
|
954 rc = NGX_AGAIN;
|
|
955
|
|
956 for ( ;; ) {
|
|
957
|
|
958 if (rc == NGX_AGAIN) {
|
|
959
|
|
960 if (r->header_in->pos == r->header_in->end) {
|
|
961
|
|
962 rv = ngx_http_alloc_large_header_buffer(r, 0);
|
|
963
|
|
964 if (rv == NGX_ERROR) {
|
|
965 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
966 return;
|
|
967 }
|
|
968
|
|
969 if (rv == NGX_DECLINED) {
|
500
|
970 p = r->header_name_start;
|
|
971
|
506
|
972 if (p == NULL) {
|
|
973 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
974 "client sent too large request");
|
|
975 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
976 return;
|
|
977 }
|
|
978
|
|
979 len = r->header_in->end - p;
|
|
980
|
500
|
981 if (len > NGX_MAX_ERROR_STR - 300) {
|
|
982 len = NGX_MAX_ERROR_STR - 300;
|
|
983 p[len++] = '.'; p[len++] = '.'; p[len++] = '.';
|
80
|
984 }
|
|
985
|
54
|
986 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
500
|
987 "client sent too long header line: \"%*s\"",
|
|
988 len, r->header_name_start);
|
330
|
989 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
990 return;
|
|
991 }
|
|
992 }
|
|
993
|
|
994 n = ngx_http_read_request_header(r);
|
|
995
|
|
996 if (n == NGX_AGAIN || n == NGX_ERROR) {
|
|
997 return;
|
|
998 }
|
|
999 }
|
|
1000
|
412
|
1001 rc = ngx_http_parse_header_line(r, r->header_in,
|
|
1002 cscf->underscores_in_headers);
|
0
|
1003
|
|
1004 if (rc == NGX_OK) {
|
|
1005
|
60
|
1006 if (r->invalid_header && cscf->ignore_invalid_headers) {
|
52
|
1007
|
|
1008 /* there was error while a header line parsing */
|
|
1009
|
54
|
1010 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
500
|
1011 "client sent invalid header line: \"%*s\"",
|
|
1012 r->header_end - r->header_name_start,
|
|
1013 r->header_name_start);
|
52
|
1014 continue;
|
|
1015 }
|
|
1016
|
0
|
1017 /* a header line has been parsed successfully */
|
|
1018
|
50
|
1019 h = ngx_list_push(&r->headers_in.headers);
|
|
1020 if (h == NULL) {
|
0
|
1021 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1022 return;
|
|
1023 }
|
|
1024
|
56
|
1025 h->hash = r->header_hash;
|
|
1026
|
0
|
1027 h->key.len = r->header_name_end - r->header_name_start;
|
|
1028 h->key.data = r->header_name_start;
|
|
1029 h->key.data[h->key.len] = '\0';
|
|
1030
|
|
1031 h->value.len = r->header_end - r->header_start;
|
|
1032 h->value.data = r->header_start;
|
|
1033 h->value.data[h->value.len] = '\0';
|
|
1034
|
382
|
1035 h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
|
198
|
1036 if (h->lowcase_key == NULL) {
|
|
1037 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1038 return;
|
|
1039 }
|
|
1040
|
|
1041 if (h->key.len == r->lowcase_index) {
|
|
1042 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
1043
|
|
1044 } else {
|
392
|
1045 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
|
0
|
1046 }
|
|
1047
|
198
|
1048 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
|
|
1049 h->lowcase_key, h->key.len);
|
|
1050
|
|
1051 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
|
1052 return;
|
|
1053 }
|
|
1054
|
0
|
1055 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1056 "http header: \"%V: %V\"",
|
|
1057 &h->key, &h->value);
|
0
|
1058
|
|
1059 continue;
|
54
|
1060 }
|
|
1061
|
|
1062 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
0
|
1063
|
|
1064 /* a whole header has been parsed successfully */
|
|
1065
|
|
1066 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1067 "http header done");
|
|
1068
|
24
|
1069 r->request_length += r->header_in->pos - r->header_in->start;
|
|
1070
|
0
|
1071 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
|
|
1072
|
|
1073 rc = ngx_http_process_request_header(r);
|
|
1074
|
|
1075 if (rc != NGX_OK) {
|
|
1076 return;
|
|
1077 }
|
|
1078
|
308
|
1079 ngx_http_process_request(r);
|
86
|
1080
|
0
|
1081 return;
|
54
|
1082 }
|
|
1083
|
|
1084 if (rc == NGX_AGAIN) {
|
|
1085
|
|
1086 /* a header line parsing is still not complete */
|
|
1087
|
|
1088 continue;
|
0
|
1089 }
|
|
1090
|
54
|
1091 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
|
|
1092
|
|
1093 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
500
|
1094 "client sent invalid header line: \"%*s\\r...\"",
|
|
1095 r->header_end - r->header_name_start,
|
|
1096 r->header_name_start);
|
330
|
1097 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
54
|
1098 return;
|
0
|
1099 }
|
|
1100 }
|
|
1101
|
|
1102
|
50
|
1103 static ssize_t
|
|
1104 ngx_http_read_request_header(ngx_http_request_t *r)
|
0
|
1105 {
|
|
1106 ssize_t n;
|
|
1107 ngx_event_t *rev;
|
230
|
1108 ngx_connection_t *c;
|
0
|
1109 ngx_http_core_srv_conf_t *cscf;
|
|
1110
|
230
|
1111 c = r->connection;
|
|
1112 rev = c->read;
|
0
|
1113
|
|
1114 n = r->header_in->last - r->header_in->pos;
|
|
1115
|
|
1116 if (n > 0) {
|
|
1117 return n;
|
|
1118 }
|
|
1119
|
72
|
1120 if (rev->ready) {
|
230
|
1121 n = c->recv(c, r->header_in->last,
|
|
1122 r->header_in->end - r->header_in->last);
|
72
|
1123 } else {
|
|
1124 n = NGX_AGAIN;
|
0
|
1125 }
|
|
1126
|
|
1127 if (n == NGX_AGAIN) {
|
364
|
1128 if (!rev->timer_set) {
|
0
|
1129 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1130 ngx_add_timer(rev, cscf->client_header_timeout);
|
|
1131 }
|
|
1132
|
430
|
1133 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
0
|
1134 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1135 return NGX_ERROR;
|
|
1136 }
|
|
1137
|
|
1138 return NGX_AGAIN;
|
|
1139 }
|
|
1140
|
|
1141 if (n == 0) {
|
230
|
1142 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
0
|
1143 "client closed prematurely connection");
|
|
1144 }
|
|
1145
|
|
1146 if (n == 0 || n == NGX_ERROR) {
|
272
|
1147 c->error = 1;
|
|
1148 c->log->action = "reading client request headers";
|
|
1149
|
|
1150 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
1151 return NGX_ERROR;
|
|
1152 }
|
|
1153
|
|
1154 r->header_in->last += n;
|
|
1155
|
|
1156 return n;
|
|
1157 }
|
|
1158
|
|
1159
|
50
|
1160 static ngx_int_t
|
|
1161 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
|
|
1162 ngx_uint_t request_line)
|
0
|
1163 {
|
|
1164 u_char *old, *new;
|
|
1165 ngx_buf_t *b;
|
|
1166 ngx_http_connection_t *hc;
|
|
1167 ngx_http_core_srv_conf_t *cscf;
|
|
1168
|
|
1169 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1170 "http alloc large header buffer");
|
|
1171
|
|
1172 if (request_line && r->state == 0) {
|
|
1173
|
|
1174 /* the client fills up the buffer with "\r\n" */
|
|
1175
|
24
|
1176 r->request_length += r->header_in->end - r->header_in->start;
|
|
1177
|
0
|
1178 r->header_in->pos = r->header_in->start;
|
|
1179 r->header_in->last = r->header_in->start;
|
|
1180
|
|
1181 return NGX_OK;
|
|
1182 }
|
|
1183
|
|
1184 old = request_line ? r->request_start : r->header_name_start;
|
|
1185
|
|
1186 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1187
|
|
1188 if (r->state != 0
|
|
1189 && (size_t) (r->header_in->pos - old)
|
|
1190 >= cscf->large_client_header_buffers.size)
|
|
1191 {
|
|
1192 return NGX_DECLINED;
|
|
1193 }
|
|
1194
|
|
1195 hc = r->http_connection;
|
|
1196
|
|
1197 if (hc->nfree) {
|
|
1198 b = hc->free[--hc->nfree];
|
|
1199
|
|
1200 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1201 "http large header free: %p %uz",
|
0
|
1202 b->pos, b->end - b->last);
|
|
1203
|
|
1204 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
|
|
1205
|
|
1206 if (hc->busy == NULL) {
|
|
1207 hc->busy = ngx_palloc(r->connection->pool,
|
|
1208 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
1209 if (hc->busy == NULL) {
|
|
1210 return NGX_ERROR;
|
|
1211 }
|
|
1212 }
|
|
1213
|
|
1214 b = ngx_create_temp_buf(r->connection->pool,
|
|
1215 cscf->large_client_header_buffers.size);
|
|
1216 if (b == NULL) {
|
|
1217 return NGX_ERROR;
|
|
1218 }
|
|
1219
|
|
1220 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1221 "http large header alloc: %p %uz",
|
0
|
1222 b->pos, b->end - b->last);
|
|
1223
|
|
1224 } else {
|
|
1225 return NGX_DECLINED;
|
|
1226 }
|
|
1227
|
|
1228 hc->busy[hc->nbusy++] = b;
|
|
1229
|
|
1230 if (r->state == 0) {
|
|
1231 /*
|
|
1232 * r->state == 0 means that a header line was parsed successfully
|
|
1233 * and we do not need to copy incomplete header line and
|
|
1234 * to relocate the parser header pointers
|
|
1235 */
|
|
1236
|
24
|
1237 r->request_length += r->header_in->end - r->header_in->start;
|
|
1238
|
0
|
1239 r->header_in = b;
|
|
1240
|
|
1241 return NGX_OK;
|
|
1242 }
|
|
1243
|
|
1244 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1245 "http large header copy: %d", r->header_in->pos - old);
|
|
1246
|
24
|
1247 r->request_length += old - r->header_in->start;
|
|
1248
|
0
|
1249 new = b->start;
|
|
1250
|
|
1251 ngx_memcpy(new, old, r->header_in->pos - old);
|
|
1252
|
|
1253 b->pos = new + (r->header_in->pos - old);
|
|
1254 b->last = new + (r->header_in->pos - old);
|
|
1255
|
|
1256 if (request_line) {
|
|
1257 r->request_start = new;
|
|
1258
|
|
1259 if (r->request_end) {
|
|
1260 r->request_end = new + (r->request_end - old);
|
|
1261 }
|
|
1262
|
|
1263 r->method_end = new + (r->method_end - old);
|
|
1264
|
|
1265 r->uri_start = new + (r->uri_start - old);
|
|
1266 r->uri_end = new + (r->uri_end - old);
|
|
1267
|
|
1268 if (r->schema_start) {
|
|
1269 r->schema_start = new + (r->schema_start - old);
|
|
1270 r->schema_end = new + (r->schema_end - old);
|
|
1271 }
|
|
1272
|
|
1273 if (r->host_start) {
|
|
1274 r->host_start = new + (r->host_start - old);
|
270
|
1275 if (r->host_end) {
|
|
1276 r->host_end = new + (r->host_end - old);
|
|
1277 }
|
0
|
1278 }
|
|
1279
|
|
1280 if (r->port_start) {
|
|
1281 r->port_start = new + (r->port_start - old);
|
|
1282 r->port_end = new + (r->port_end - old);
|
|
1283 }
|
|
1284
|
|
1285 if (r->uri_ext) {
|
|
1286 r->uri_ext = new + (r->uri_ext - old);
|
|
1287 }
|
|
1288
|
|
1289 if (r->args_start) {
|
|
1290 r->args_start = new + (r->args_start - old);
|
|
1291 }
|
|
1292
|
354
|
1293 if (r->http_protocol.data) {
|
|
1294 r->http_protocol.data = new + (r->http_protocol.data - old);
|
|
1295 }
|
|
1296
|
0
|
1297 } else {
|
|
1298 r->header_name_start = new;
|
|
1299 r->header_name_end = new + (r->header_name_end - old);
|
|
1300 r->header_start = new + (r->header_start - old);
|
|
1301 r->header_end = new + (r->header_end - old);
|
|
1302 }
|
|
1303
|
|
1304 r->header_in = b;
|
|
1305
|
|
1306 return NGX_OK;
|
|
1307 }
|
|
1308
|
|
1309
|
50
|
1310 static ngx_int_t
|
58
|
1311 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1312 ngx_uint_t offset)
|
|
1313 {
|
|
1314 ngx_table_elt_t **ph;
|
|
1315
|
|
1316 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
|
|
1317
|
|
1318 if (*ph == NULL) {
|
|
1319 *ph = h;
|
|
1320 }
|
|
1321
|
|
1322 return NGX_OK;
|
|
1323 }
|
|
1324
|
|
1325
|
|
1326 static ngx_int_t
|
72
|
1327 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1328 ngx_uint_t offset)
|
|
1329 {
|
|
1330 ngx_table_elt_t **ph;
|
|
1331
|
|
1332 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
|
|
1333
|
|
1334 if (*ph == NULL) {
|
|
1335 *ph = h;
|
|
1336 return NGX_OK;
|
|
1337 }
|
|
1338
|
|
1339 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
230
|
1340 "client sent duplicate header line: \"%V: %V\", "
|
|
1341 "previous value: \"%V: %V\"",
|
|
1342 &h->key, &h->value, &(*ph)->key, &(*ph)->value);
|
72
|
1343
|
|
1344 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1345
|
|
1346 return NGX_ERROR;
|
|
1347 }
|
|
1348
|
|
1349
|
|
1350 static ngx_int_t
|
376
|
1351 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1352 ngx_uint_t offset)
|
|
1353 {
|
506
|
1354 u_char *host;
|
|
1355 ssize_t len;
|
376
|
1356
|
|
1357 if (r->headers_in.host == NULL) {
|
|
1358 r->headers_in.host = h;
|
|
1359 }
|
|
1360
|
506
|
1361 host = h->value.data;
|
|
1362 len = ngx_http_validate_host(r, &host, h->value.len, 0);
|
|
1363
|
|
1364 if (len == 0) {
|
376
|
1365 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1366 "client sent invalid host header");
|
|
1367 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1368 return NGX_ERROR;
|
|
1369 }
|
|
1370
|
506
|
1371 if (len < 0) {
|
|
1372 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1373 return NGX_ERROR;
|
|
1374 }
|
|
1375
|
376
|
1376 if (r->headers_in.server.len) {
|
|
1377 return NGX_OK;
|
|
1378 }
|
|
1379
|
|
1380 r->headers_in.server.len = len;
|
506
|
1381 r->headers_in.server.data = host;
|
376
|
1382
|
|
1383 return NGX_OK;
|
|
1384 }
|
|
1385
|
|
1386
|
|
1387 static ngx_int_t
|
336
|
1388 ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1389 ngx_uint_t offset)
|
|
1390 {
|
340
|
1391 if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
|
336
|
1392 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
|
|
1393
|
340
|
1394 } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
|
336
|
1395 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
|
|
1396 }
|
|
1397
|
|
1398 return NGX_OK;
|
|
1399 }
|
|
1400
|
|
1401
|
|
1402 static ngx_int_t
|
376
|
1403 ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1404 ngx_uint_t offset)
|
|
1405 {
|
400
|
1406 u_char *user_agent, *msie;
|
376
|
1407
|
|
1408 if (r->headers_in.user_agent) {
|
|
1409 return NGX_OK;
|
|
1410 }
|
|
1411
|
|
1412 r->headers_in.user_agent = h;
|
|
1413
|
|
1414 /* check some widespread browsers while the header is in CPU cache */
|
|
1415
|
|
1416 user_agent = h->value.data;
|
|
1417
|
400
|
1418 msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
|
|
1419
|
|
1420 if (msie && msie + 7 < user_agent + h->value.len) {
|
376
|
1421
|
|
1422 r->headers_in.msie = 1;
|
|
1423
|
400
|
1424 if (msie[6] == '.') {
|
|
1425
|
|
1426 switch (msie[5]) {
|
|
1427 case '4':
|
|
1428 r->headers_in.msie4 = 1;
|
|
1429 /* fall through */
|
|
1430 case '5':
|
502
|
1431 r->headers_in.msie6 = 1;
|
|
1432 break;
|
400
|
1433 case '6':
|
502
|
1434 if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
|
|
1435 r->headers_in.msie6 = 1;
|
|
1436 }
|
|
1437 break;
|
400
|
1438 }
|
376
|
1439 }
|
|
1440
|
|
1441 #if 0
|
|
1442 /* MSIE ignores the SSL "close notify" alert */
|
|
1443 if (c->ssl) {
|
|
1444 c->ssl->no_send_shutdown = 1;
|
|
1445 }
|
|
1446 #endif
|
|
1447 }
|
|
1448
|
|
1449 if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
|
|
1450 r->headers_in.opera = 1;
|
|
1451 r->headers_in.msie = 0;
|
|
1452 r->headers_in.msie4 = 0;
|
400
|
1453 r->headers_in.msie6 = 0;
|
376
|
1454 }
|
|
1455
|
|
1456 if (!r->headers_in.msie && !r->headers_in.opera) {
|
|
1457
|
|
1458 if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
|
|
1459 r->headers_in.gecko = 1;
|
|
1460
|
508
|
1461 } else if (ngx_strstrn(user_agent, "Chrome/", 7 - 1)) {
|
|
1462 r->headers_in.chrome = 1;
|
|
1463
|
|
1464 } else if (ngx_strstrn(user_agent, "Safari/", 7 - 1)) {
|
|
1465 r->headers_in.safari = 1;
|
|
1466
|
376
|
1467 } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
|
|
1468 r->headers_in.konqueror = 1;
|
|
1469 }
|
|
1470 }
|
|
1471
|
|
1472 return NGX_OK;
|
|
1473 }
|
|
1474
|
|
1475
|
|
1476 static ngx_int_t
|
58
|
1477 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1478 ngx_uint_t offset)
|
|
1479 {
|
|
1480 ngx_table_elt_t **cookie;
|
|
1481
|
|
1482 cookie = ngx_array_push(&r->headers_in.cookies);
|
72
|
1483 if (cookie) {
|
|
1484 *cookie = h;
|
|
1485 return NGX_OK;
|
58
|
1486 }
|
|
1487
|
72
|
1488 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1489
|
|
1490 return NGX_ERROR;
|
58
|
1491 }
|
|
1492
|
|
1493
|
|
1494 static ngx_int_t
|
50
|
1495 ngx_http_process_request_header(ngx_http_request_t *r)
|
0
|
1496 {
|
376
|
1497 if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
|
|
1498 r->headers_in.server.len)
|
|
1499 == NGX_ERROR)
|
|
1500 {
|
|
1501 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1502 return NGX_ERROR;
|
|
1503 }
|
|
1504
|
|
1505 if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
|
|
1506 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1507 "client sent HTTP/1.1 request without \"Host\" header");
|
|
1508 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1509 return NGX_ERROR;
|
0
|
1510 }
|
|
1511
|
|
1512 if (r->headers_in.content_length) {
|
|
1513 r->headers_in.content_length_n =
|
212
|
1514 ngx_atoof(r->headers_in.content_length->value.data,
|
0
|
1515 r->headers_in.content_length->value.len);
|
|
1516
|
|
1517 if (r->headers_in.content_length_n == NGX_ERROR) {
|
54
|
1518 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1519 "client sent invalid \"Content-Length\" header");
|
130
|
1520 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
54
|
1521 return NGX_ERROR;
|
0
|
1522 }
|
|
1523 }
|
|
1524
|
426
|
1525 if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
|
54
|
1526 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
362
|
1527 "client sent %V method without \"Content-Length\" header",
|
|
1528 &r->method_name);
|
130
|
1529 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
|
1530 return NGX_ERROR;
|
|
1531 }
|
|
1532
|
336
|
1533 if (r->method & NGX_HTTP_TRACE) {
|
294
|
1534 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1535 "client sent TRACE method");
|
|
1536 ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
|
|
1537 return NGX_ERROR;
|
|
1538 }
|
|
1539
|
130
|
1540 if (r->headers_in.transfer_encoding
|
340
|
1541 && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
|
|
1542 "chunked", 7 - 1))
|
130
|
1543 {
|
|
1544 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1545 "client sent \"Transfer-Encoding: chunked\" header");
|
|
1546 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
54
|
1547 return NGX_ERROR;
|
0
|
1548 }
|
|
1549
|
336
|
1550 if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
|
|
1551 if (r->headers_in.keep_alive) {
|
|
1552 r->headers_in.keep_alive_n =
|
|
1553 ngx_atotm(r->headers_in.keep_alive->value.data,
|
|
1554 r->headers_in.keep_alive->value.len);
|
0
|
1555 }
|
|
1556 }
|
|
1557
|
308
|
1558 return NGX_OK;
|
|
1559 }
|
|
1560
|
|
1561
|
|
1562 static void
|
|
1563 ngx_http_process_request(ngx_http_request_t *r)
|
|
1564 {
|
356
|
1565 ngx_connection_t *c;
|
308
|
1566
|
|
1567 c = r->connection;
|
|
1568
|
354
|
1569 if (r->plain_http) {
|
|
1570 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1571 "client sent plain HTTP request to HTTPS port");
|
|
1572 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
|
|
1573 return;
|
|
1574 }
|
|
1575
|
308
|
1576 #if (NGX_HTTP_SSL)
|
|
1577
|
|
1578 if (c->ssl) {
|
356
|
1579 long rc;
|
372
|
1580 X509 *cert;
|
356
|
1581 ngx_http_ssl_srv_conf_t *sscf;
|
|
1582
|
218
|
1583 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
|
|
1584
|
502
|
1585 if (sscf->verify) {
|
308
|
1586 rc = SSL_get_verify_result(c->ssl->connection);
|
218
|
1587
|
|
1588 if (rc != X509_V_OK) {
|
308
|
1589 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
220
|
1590 "client SSL certificate verify error: (%l:%s)",
|
218
|
1591 rc, X509_verify_cert_error_string(rc));
|
366
|
1592
|
|
1593 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
|
|
1594 (SSL_get0_session(c->ssl->connection)));
|
|
1595
|
218
|
1596 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
|
308
|
1597 return;
|
218
|
1598 }
|
|
1599
|
502
|
1600 if (sscf->verify == 1) {
|
|
1601 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
1602
|
|
1603 if (cert == NULL) {
|
|
1604 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1605 "client sent no required SSL certificate");
|
|
1606
|
|
1607 ngx_ssl_remove_cached_session(sscf->ssl.ctx,
|
366
|
1608 (SSL_get0_session(c->ssl->connection)));
|
|
1609
|
502
|
1610 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
|
|
1611 return;
|
|
1612 }
|
|
1613
|
|
1614 X509_free(cert);
|
218
|
1615 }
|
|
1616 }
|
|
1617 }
|
|
1618
|
|
1619 #endif
|
|
1620
|
308
|
1621 if (c->read->timer_set) {
|
|
1622 ngx_del_timer(c->read);
|
|
1623 }
|
|
1624
|
|
1625 #if (NGX_STAT_STUB)
|
498
|
1626 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
308
|
1627 r->stat_reading = 0;
|
498
|
1628 (void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
|
308
|
1629 r->stat_writing = 1;
|
|
1630 #endif
|
|
1631
|
|
1632 c->read->handler = ngx_http_request_handler;
|
|
1633 c->write->handler = ngx_http_request_handler;
|
326
|
1634 r->read_event_handler = ngx_http_block_reading;
|
308
|
1635
|
|
1636 ngx_http_handler(r);
|
|
1637
|
426
|
1638 ngx_http_run_posted_requests(c);
|
0
|
1639 }
|
|
1640
|
|
1641
|
376
|
1642 static ssize_t
|
506
|
1643 ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len,
|
|
1644 ngx_uint_t alloc)
|
26
|
1645 {
|
506
|
1646 u_char *h, ch;
|
|
1647 size_t i, last;
|
|
1648 ngx_uint_t dot;
|
376
|
1649
|
|
1650 last = len;
|
506
|
1651 h = *host;
|
376
|
1652 dot = 0;
|
|
1653
|
|
1654 for (i = 0; i < len; i++) {
|
506
|
1655 ch = h[i];
|
376
|
1656
|
|
1657 if (ch == '.') {
|
|
1658 if (dot) {
|
506
|
1659 return 0;
|
376
|
1660 }
|
|
1661
|
|
1662 dot = 1;
|
|
1663 continue;
|
|
1664 }
|
|
1665
|
|
1666 dot = 0;
|
|
1667
|
|
1668 if (ch == ':') {
|
|
1669 last = i;
|
|
1670 continue;
|
|
1671 }
|
|
1672
|
482
|
1673 if (ngx_path_separator(ch) || ch == '\0') {
|
506
|
1674 return 0;
|
|
1675 }
|
|
1676
|
|
1677 if (ch >= 'A' || ch < 'Z') {
|
|
1678 alloc = 1;
|
376
|
1679 }
|
|
1680 }
|
|
1681
|
|
1682 if (dot) {
|
|
1683 last--;
|
|
1684 }
|
|
1685
|
506
|
1686 if (alloc) {
|
|
1687 *host = ngx_pnalloc(r->pool, last) ;
|
|
1688 if (*host == NULL) {
|
|
1689 return -1;
|
|
1690 }
|
|
1691
|
|
1692 ngx_strlow(*host, h, last);
|
|
1693 }
|
|
1694
|
376
|
1695 return last;
|
|
1696 }
|
|
1697
|
|
1698
|
|
1699 static ngx_int_t
|
|
1700 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
|
|
1701 {
|
142
|
1702 ngx_http_core_loc_conf_t *clcf;
|
|
1703 ngx_http_core_srv_conf_t *cscf;
|
376
|
1704
|
478
|
1705 if (r->virtual_names == NULL) {
|
376
|
1706 return NGX_DECLINED;
|
|
1707 }
|
|
1708
|
506
|
1709 cscf = ngx_hash_find_combined(&r->virtual_names->names,
|
|
1710 ngx_hash_key(host, len), host, len);
|
312
|
1711
|
|
1712 if (cscf) {
|
|
1713 goto found;
|
26
|
1714 }
|
|
1715
|
326
|
1716 #if (NGX_PCRE)
|
|
1717
|
504
|
1718 if (len && r->virtual_names->nregex) {
|
460
|
1719 size_t ncaptures;
|
356
|
1720 ngx_int_t n;
|
|
1721 ngx_uint_t i;
|
|
1722 ngx_str_t name;
|
|
1723 ngx_http_server_name_t *sn;
|
326
|
1724
|
|
1725 name.len = len;
|
506
|
1726 name.data = host;
|
326
|
1727
|
456
|
1728 ncaptures = 0;
|
|
1729
|
478
|
1730 sn = r->virtual_names->regex;
|
|
1731
|
|
1732 for (i = 0; i < r->virtual_names->nregex; i++) {
|
326
|
1733
|
456
|
1734 if (sn[i].captures && r->captures == NULL) {
|
|
1735
|
458
|
1736 ncaptures = (NGX_HTTP_MAX_CAPTURES + 1) * 3;
|
|
1737
|
|
1738 r->captures = ngx_palloc(r->pool, ncaptures * sizeof(int));
|
456
|
1739 if (r->captures == NULL) {
|
|
1740 return NGX_ERROR;
|
|
1741 }
|
|
1742 }
|
|
1743
|
|
1744 n = ngx_regex_exec(sn[i].regex, &name, r->captures, ncaptures);
|
326
|
1745
|
|
1746 if (n == NGX_REGEX_NO_MATCHED) {
|
|
1747 continue;
|
|
1748 }
|
|
1749
|
|
1750 if (n < 0) {
|
|
1751 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
1752 ngx_regex_exec_n
|
|
1753 " failed: %d on \"%V\" using \"%V\"",
|
|
1754 n, &name, &sn[i].name);
|
376
|
1755 return NGX_ERROR;
|
326
|
1756 }
|
|
1757
|
|
1758 /* match */
|
|
1759
|
|
1760 cscf = sn[i].core_srv_conf;
|
|
1761
|
456
|
1762 r->ncaptures = ncaptures;
|
506
|
1763 r->captures_data = host;
|
456
|
1764
|
326
|
1765 goto found;
|
|
1766 }
|
|
1767 }
|
|
1768
|
|
1769 #endif
|
|
1770
|
376
|
1771 return NGX_OK;
|
58
|
1772
|
|
1773 found:
|
|
1774
|
142
|
1775 r->srv_conf = cscf->ctx->srv_conf;
|
|
1776 r->loc_conf = cscf->ctx->loc_conf;
|
58
|
1777
|
|
1778 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
484
|
1779 r->connection->log->file = clcf->error_log->file;
|
58
|
1780
|
|
1781 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
484
|
1782 r->connection->log->log_level = clcf->error_log->log_level;
|
26
|
1783 }
|
|
1784
|
376
|
1785 return NGX_OK;
|
26
|
1786 }
|
|
1787
|
|
1788
|
58
|
1789 static void
|
|
1790 ngx_http_request_handler(ngx_event_t *ev)
|
|
1791 {
|
|
1792 ngx_connection_t *c;
|
|
1793 ngx_http_request_t *r;
|
214
|
1794 ngx_http_log_ctx_t *ctx;
|
58
|
1795
|
|
1796 c = ev->data;
|
|
1797 r = c->data;
|
|
1798
|
214
|
1799 ctx = c->log->data;
|
|
1800 ctx->current_request = r;
|
|
1801
|
426
|
1802 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1803 "http run request: \"%V?%V\"", &r->uri, &r->args);
|
|
1804
|
58
|
1805 if (ev->write) {
|
|
1806 r->write_event_handler(r);
|
|
1807
|
|
1808 } else {
|
|
1809 r->read_event_handler(r);
|
|
1810 }
|
426
|
1811
|
|
1812 ngx_http_run_posted_requests(c);
|
|
1813 }
|
|
1814
|
|
1815
|
|
1816 void
|
|
1817 ngx_http_run_posted_requests(ngx_connection_t *c)
|
|
1818 {
|
|
1819 ngx_http_request_t *r;
|
|
1820 ngx_http_log_ctx_t *ctx;
|
|
1821 ngx_http_posted_request_t *pr;
|
|
1822
|
|
1823 for ( ;; ) {
|
|
1824
|
|
1825 if (c->destroyed) {
|
|
1826 return;
|
|
1827 }
|
|
1828
|
|
1829 r = c->data;
|
|
1830 pr = r->main->posted_requests;
|
|
1831
|
|
1832 if (pr == NULL) {
|
|
1833 return;
|
|
1834 }
|
|
1835
|
|
1836 r->main->posted_requests = pr->next;
|
|
1837
|
|
1838 r = pr->request;
|
|
1839
|
|
1840 ctx = c->log->data;
|
|
1841 ctx->current_request = r;
|
|
1842
|
|
1843 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1844 "http posted request: \"%V?%V\"", &r->uri, &r->args);
|
|
1845
|
|
1846 r->write_event_handler(r);
|
|
1847 }
|
|
1848 }
|
|
1849
|
|
1850
|
|
1851 ngx_int_t
|
|
1852 ngx_http_post_request(ngx_http_request_t *r)
|
|
1853 {
|
|
1854 ngx_http_posted_request_t *pr, **p;
|
|
1855
|
|
1856 pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
|
|
1857 if (pr == NULL) {
|
|
1858 return NGX_ERROR;
|
|
1859 }
|
|
1860
|
|
1861 pr->request = r;
|
|
1862 pr->next = NULL;
|
|
1863
|
|
1864 for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
|
|
1865
|
|
1866 *p = pr;
|
|
1867
|
|
1868 return NGX_OK;
|
58
|
1869 }
|
|
1870
|
|
1871
|
50
|
1872 void
|
58
|
1873 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
0
|
1874 {
|
330
|
1875 ngx_connection_t *c;
|
58
|
1876 ngx_http_request_t *pr;
|
0
|
1877 ngx_http_core_loc_conf_t *clcf;
|
|
1878
|
58
|
1879 if (rc == NGX_DONE) {
|
118
|
1880 /* the request pool may be already destroyed */
|
58
|
1881 return;
|
|
1882 }
|
|
1883
|
330
|
1884 c = r->connection;
|
|
1885
|
426
|
1886 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1887 "http finalize request: %d, \"%V?%V\" %d",
|
|
1888 rc, &r->uri, &r->args, r == c->data);
|
132
|
1889
|
488
|
1890 if (rc == NGX_OK && r->filter_finalize) {
|
|
1891 c->error = 1;
|
|
1892 return;
|
|
1893 }
|
|
1894
|
230
|
1895 if (rc == NGX_DECLINED) {
|
|
1896 r->content_handler = NULL;
|
|
1897 r->write_event_handler = ngx_http_core_run_phases;
|
|
1898 ngx_http_core_run_phases(r);
|
|
1899 return;
|
|
1900 }
|
|
1901
|
248
|
1902 if (r != r->main && r->post_subrequest) {
|
|
1903 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
|
216
|
1904 }
|
|
1905
|
358
|
1906 if (rc == NGX_ERROR
|
|
1907 || rc == NGX_HTTP_REQUEST_TIME_OUT
|
|
1908 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
|
|
1909 || c->error)
|
|
1910 {
|
282
|
1911 if (rc > 0 && r->headers_out.status == 0) {
|
|
1912 r->headers_out.status = rc;
|
190
|
1913 }
|
|
1914
|
162
|
1915 if (ngx_http_post_action(r) == NGX_OK) {
|
|
1916 return;
|
|
1917 }
|
|
1918
|
118
|
1919 ngx_http_close_request(r, 0);
|
0
|
1920 return;
|
|
1921 }
|
|
1922
|
186
|
1923 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
|
|
1924 || rc == NGX_HTTP_CREATED
|
|
1925 || rc == NGX_HTTP_NO_CONTENT)
|
|
1926 {
|
136
|
1927 if (rc == NGX_HTTP_CLOSE) {
|
|
1928 ngx_http_close_request(r, rc);
|
|
1929 return;
|
|
1930 }
|
|
1931
|
146
|
1932 if (r == r->main) {
|
330
|
1933 if (c->read->timer_set) {
|
|
1934 ngx_del_timer(c->read);
|
118
|
1935 }
|
|
1936
|
330
|
1937 if (c->write->timer_set) {
|
|
1938 ngx_del_timer(c->write);
|
118
|
1939 }
|
|
1940 }
|
|
1941
|
410
|
1942 c->read->handler = ngx_http_request_handler;
|
|
1943 c->write->handler = ngx_http_request_handler;
|
|
1944
|
118
|
1945 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
|
|
1946 return;
|
|
1947 }
|
|
1948
|
426
|
1949 if (r != r->main) {
|
|
1950
|
|
1951 if (r->buffered || r->postponed) {
|
|
1952
|
|
1953 if (ngx_http_set_write_handler(r) != NGX_OK) {
|
|
1954 ngx_http_close_request(r->main, 0);
|
|
1955 }
|
|
1956
|
74
|
1957 return;
|
|
1958 }
|
426
|
1959
|
|
1960 #if (NGX_DEBUG)
|
|
1961 if (r != c->data) {
|
|
1962 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1963 "http finalize non-active request: \"%V?%V\"",
|
|
1964 &r->uri, &r->args);
|
|
1965 }
|
|
1966 #endif
|
58
|
1967
|
|
1968 pr = r->parent;
|
|
1969
|
426
|
1970 if (r == c->data) {
|
|
1971
|
|
1972 if (!r->logged) {
|
|
1973
|
|
1974 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1975
|
|
1976 if (clcf->log_subrequest) {
|
|
1977 ngx_http_log_request(r);
|
|
1978 }
|
|
1979
|
|
1980 r->logged = 1;
|
|
1981
|
|
1982 } else {
|
|
1983 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
1984 "subrequest: \"%V?%V\" logged again",
|
|
1985 &r->uri, &r->args);
|
|
1986 }
|
|
1987
|
|
1988 r->done = 1;
|
|
1989
|
|
1990 if (pr->postponed && pr->postponed->request == r) {
|
|
1991 pr->postponed = pr->postponed->next;
|
|
1992 }
|
|
1993
|
|
1994 c->data = pr;
|
|
1995
|
|
1996 } else {
|
|
1997
|
|
1998 r->write_event_handler = ngx_http_request_finalizer;
|
|
1999
|
|
2000 if (r->waited) {
|
|
2001 r->done = 1;
|
|
2002 }
|
|
2003 }
|
|
2004
|
|
2005 if (ngx_http_post_request(pr) != NGX_OK) {
|
|
2006 ngx_http_close_request(r->main, 0);
|
|
2007 return;
|
|
2008 }
|
|
2009
|
330
|
2010 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
426
|
2011 "http wake parent request: \"%V?%V\"",
|
|
2012 &pr->uri, &pr->args);
|
|
2013
|
|
2014 return;
|
|
2015 }
|
|
2016
|
|
2017 if (r->buffered || c->buffered || r->postponed) {
|
|
2018
|
|
2019 if (ngx_http_set_write_handler(r) != NGX_OK) {
|
|
2020 ngx_http_close_request(r, 0);
|
58
|
2021 }
|
|
2022
|
|
2023 return;
|
|
2024 }
|
0
|
2025
|
426
|
2026 if (r != c->data) {
|
|
2027 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
2028 "http finalize non-active request: \"%V?%V\"",
|
|
2029 &r->uri, &r->args);
|
0
|
2030 return;
|
76
|
2031 }
|
|
2032
|
426
|
2033 r->done = 1;
|
0
|
2034
|
178
|
2035 if (!r->post_action) {
|
|
2036 r->request_complete = 1;
|
|
2037 }
|
|
2038
|
162
|
2039 if (ngx_http_post_action(r) == NGX_OK) {
|
126
|
2040 return;
|
|
2041 }
|
|
2042
|
330
|
2043 if (c->read->timer_set) {
|
|
2044 ngx_del_timer(c->read);
|
|
2045 }
|
|
2046
|
|
2047 if (c->write->timer_set) {
|
|
2048 c->write->delayed = 0;
|
|
2049 ngx_del_timer(c->write);
|
0
|
2050 }
|
|
2051
|
330
|
2052 if (c->destroyed) {
|
|
2053 return;
|
0
|
2054 }
|
|
2055
|
330
|
2056 if (c->read->eof) {
|
|
2057 ngx_http_close_request(r, 0);
|
138
|
2058 return;
|
|
2059 }
|
|
2060
|
426
|
2061 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2062
|
0
|
2063 if (!ngx_terminate
|
|
2064 && !ngx_exiting
|
378
|
2065 && r->keepalive
|
0
|
2066 && clcf->keepalive_timeout > 0)
|
|
2067 {
|
|
2068 ngx_http_set_keepalive(r);
|
|
2069 return;
|
|
2070
|
|
2071 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
|
|
2072 ngx_http_set_lingering_close(r);
|
|
2073 return;
|
|
2074 }
|
|
2075
|
|
2076 ngx_http_close_request(r, 0);
|
|
2077 }
|
|
2078
|
|
2079
|
74
|
2080 static ngx_int_t
|
50
|
2081 ngx_http_set_write_handler(ngx_http_request_t *r)
|
0
|
2082 {
|
|
2083 ngx_event_t *wev;
|
|
2084 ngx_http_core_loc_conf_t *clcf;
|
|
2085
|
58
|
2086 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
|
|
2087
|
326
|
2088 r->read_event_handler = ngx_http_test_reading;
|
74
|
2089 r->write_event_handler = ngx_http_writer;
|
|
2090
|
0
|
2091 wev = r->connection->write;
|
|
2092
|
|
2093 if (wev->ready && wev->delayed) {
|
74
|
2094 return NGX_OK;
|
0
|
2095 }
|
|
2096
|
58
|
2097 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
0
|
2098 if (!wev->delayed) {
|
|
2099 ngx_add_timer(wev, clcf->send_timeout);
|
|
2100 }
|
|
2101
|
430
|
2102 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
0
|
2103 ngx_http_close_request(r, 0);
|
74
|
2104 return NGX_ERROR;
|
0
|
2105 }
|
74
|
2106
|
|
2107 return NGX_OK;
|
0
|
2108 }
|
|
2109
|
|
2110
|
58
|
2111 static void
|
|
2112 ngx_http_writer(ngx_http_request_t *r)
|
0
|
2113 {
|
|
2114 int rc;
|
58
|
2115 ngx_event_t *wev;
|
0
|
2116 ngx_connection_t *c;
|
|
2117 ngx_http_core_loc_conf_t *clcf;
|
|
2118
|
58
|
2119 c = r->connection;
|
|
2120 wev = c->write;
|
|
2121
|
132
|
2122 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2123 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
|
0
|
2124
|
426
|
2125 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
|
2126
|
0
|
2127 if (wev->timedout) {
|
|
2128 if (!wev->delayed) {
|
54
|
2129 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
2130 "client timed out");
|
|
2131 c->timedout = 1;
|
116
|
2132
|
162
|
2133 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
2134 return;
|
|
2135 }
|
|
2136
|
|
2137 wev->timedout = 0;
|
|
2138 wev->delayed = 0;
|
|
2139
|
|
2140 if (!wev->ready) {
|
|
2141 ngx_add_timer(wev, clcf->send_timeout);
|
|
2142
|
430
|
2143 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
118
|
2144 ngx_http_close_request(r, 0);
|
0
|
2145 }
|
|
2146
|
|
2147 return;
|
|
2148 }
|
|
2149
|
|
2150 } else {
|
|
2151 if (wev->delayed) {
|
|
2152 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2153 "http writer delayed");
|
|
2154
|
430
|
2155 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
118
|
2156 ngx_http_close_request(r, 0);
|
0
|
2157 }
|
|
2158
|
|
2159 return;
|
|
2160 }
|
|
2161 }
|
|
2162
|
132
|
2163 rc = ngx_http_output_filter(r, NULL);
|
|
2164
|
|
2165 if (c->destroyed) {
|
|
2166 return;
|
58
|
2167 }
|
|
2168
|
132
|
2169 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2170 "http writer output filter: %d, \"%V?%V\"",
|
|
2171 rc, &r->uri, &r->args);
|
0
|
2172
|
494
|
2173 if (rc == NGX_ERROR) {
|
|
2174 ngx_http_finalize_request(r, rc);
|
|
2175 return;
|
|
2176 }
|
|
2177
|
426
|
2178 if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
|
|
2179
|
0
|
2180 if (!wev->ready && !wev->delayed) {
|
|
2181 ngx_add_timer(wev, clcf->send_timeout);
|
|
2182 }
|
|
2183
|
430
|
2184 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
118
|
2185 ngx_http_close_request(r, 0);
|
0
|
2186 }
|
|
2187
|
426
|
2188 return;
|
0
|
2189 }
|
|
2190
|
132
|
2191 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2192 "http writer done: \"%V?%V\"", &r->uri, &r->args);
|
0
|
2193
|
|
2194 ngx_http_finalize_request(r, rc);
|
|
2195 }
|
|
2196
|
|
2197
|
426
|
2198 static void
|
|
2199 ngx_http_request_finalizer(ngx_http_request_t *r)
|
|
2200 {
|
|
2201 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2202 "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
|
|
2203
|
|
2204 ngx_http_finalize_request(r, 0);
|
|
2205 }
|
|
2206
|
|
2207
|
326
|
2208 void
|
|
2209 ngx_http_block_reading(ngx_http_request_t *r)
|
0
|
2210 {
|
318
|
2211 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
326
|
2212 "http reading blocked");
|
318
|
2213
|
|
2214 /* aio does not call this handler */
|
|
2215
|
|
2216 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
|
|
2217 && r->connection->read->active)
|
|
2218 {
|
430
|
2219 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
|
318
|
2220 ngx_http_close_request(r, 0);
|
|
2221 }
|
|
2222 }
|
|
2223 }
|
|
2224
|
|
2225
|
416
|
2226 void
|
326
|
2227 ngx_http_test_reading(ngx_http_request_t *r)
|
318
|
2228 {
|
276
|
2229 int n;
|
|
2230 char buf[1];
|
|
2231 ngx_err_t err;
|
|
2232 ngx_event_t *rev;
|
|
2233 ngx_connection_t *c;
|
|
2234
|
|
2235 c = r->connection;
|
|
2236 rev = c->read;
|
|
2237
|
326
|
2238 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
|
276
|
2239
|
|
2240 #if (NGX_HAVE_KQUEUE)
|
|
2241
|
|
2242 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
|
2243
|
|
2244 if (!rev->pending_eof) {
|
|
2245 return;
|
|
2246 }
|
|
2247
|
|
2248 rev->eof = 1;
|
|
2249 c->error = 1;
|
|
2250 err = rev->kq_errno;
|
|
2251
|
|
2252 goto closed;
|
|
2253 }
|
|
2254
|
|
2255 #endif
|
|
2256
|
|
2257 n = recv(c->fd, buf, 1, MSG_PEEK);
|
|
2258
|
|
2259 if (n == 0) {
|
|
2260 rev->eof = 1;
|
|
2261 c->error = 1;
|
|
2262 err = 0;
|
|
2263
|
|
2264 goto closed;
|
|
2265
|
|
2266 } else if (n == -1) {
|
|
2267 err = ngx_socket_errno;
|
|
2268
|
|
2269 if (err != NGX_EAGAIN) {
|
|
2270 rev->eof = 1;
|
|
2271 c->error = 1;
|
|
2272
|
|
2273 goto closed;
|
|
2274 }
|
|
2275 }
|
0
|
2276
|
|
2277 /* aio does not call this handler */
|
|
2278
|
276
|
2279 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
|
|
2280
|
430
|
2281 if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
|
0
|
2282 ngx_http_close_request(r, 0);
|
|
2283 }
|
|
2284 }
|
276
|
2285
|
|
2286 return;
|
|
2287
|
|
2288 closed:
|
|
2289
|
|
2290 if (err) {
|
|
2291 rev->error = 1;
|
|
2292 }
|
|
2293
|
|
2294 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
2295 "client closed prematurely connection");
|
|
2296
|
286
|
2297 ngx_http_finalize_request(r, 0);
|
0
|
2298 }
|
|
2299
|
|
2300
|
50
|
2301 static void
|
|
2302 ngx_http_set_keepalive(ngx_http_request_t *r)
|
0
|
2303 {
|
2
|
2304 int tcp_nodelay;
|
0
|
2305 ngx_int_t i;
|
|
2306 ngx_buf_t *b, *f;
|
|
2307 ngx_event_t *rev, *wev;
|
|
2308 ngx_connection_t *c;
|
|
2309 ngx_http_connection_t *hc;
|
|
2310 ngx_http_core_srv_conf_t *cscf;
|
|
2311 ngx_http_core_loc_conf_t *clcf;
|
|
2312
|
|
2313 c = r->connection;
|
|
2314 rev = c->read;
|
|
2315
|
326
|
2316 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2317
|
0
|
2318 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
|
|
2319
|
326
|
2320 if (r->discard_body) {
|
442
|
2321 r->write_event_handler = ngx_http_request_empty_handler;
|
326
|
2322 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
|
|
2323 ngx_add_timer(rev, clcf->lingering_timeout);
|
|
2324 return;
|
|
2325 }
|
|
2326
|
32
|
2327 c->log->action = "closing request";
|
0
|
2328
|
|
2329 hc = r->http_connection;
|
|
2330 b = r->header_in;
|
|
2331
|
|
2332 if (b->pos < b->last) {
|
|
2333
|
|
2334 /* the pipelined request */
|
|
2335
|
|
2336 if (b != c->buffer) {
|
|
2337
|
4
|
2338 /*
|
|
2339 * If the large header buffers were allocated while the previous
|
|
2340 * request processing then we do not use c->buffer for
|
|
2341 * the pipelined request (see ngx_http_init_request()).
|
126
|
2342 *
|
4
|
2343 * Now we would move the large header buffers to the free list.
|
|
2344 */
|
0
|
2345
|
|
2346 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
2347
|
|
2348 if (hc->free == NULL) {
|
|
2349 hc->free = ngx_palloc(c->pool,
|
|
2350 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
2351
|
|
2352 if (hc->free == NULL) {
|
26
|
2353 ngx_http_close_request(r, 0);
|
0
|
2354 return;
|
|
2355 }
|
|
2356 }
|
|
2357
|
|
2358 for (i = 0; i < hc->nbusy - 1; i++) {
|
|
2359 f = hc->busy[i];
|
|
2360 hc->free[hc->nfree++] = f;
|
|
2361 f->pos = f->start;
|
|
2362 f->last = f->start;
|
|
2363 }
|
|
2364
|
|
2365 hc->busy[0] = b;
|
|
2366 hc->nbusy = 1;
|
|
2367 }
|
|
2368 }
|
|
2369
|
118
|
2370 ngx_http_request_done(r, 0);
|
|
2371
|
0
|
2372 c->data = hc;
|
|
2373
|
|
2374 ngx_add_timer(rev, clcf->keepalive_timeout);
|
|
2375
|
430
|
2376 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
0
|
2377 ngx_http_close_connection(c);
|
|
2378 return;
|
|
2379 }
|
|
2380
|
|
2381 wev = c->write;
|
58
|
2382 wev->handler = ngx_http_empty_handler;
|
0
|
2383
|
|
2384 if (b->pos < b->last) {
|
|
2385
|
|
2386 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
|
|
2387
|
116
|
2388 #if (NGX_STAT_STUB)
|
498
|
2389 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
116
|
2390 #endif
|
|
2391
|
0
|
2392 hc->pipeline = 1;
|
32
|
2393 c->log->action = "reading client pipelined request line";
|
116
|
2394
|
376
|
2395 rev->handler = ngx_http_init_request;
|
|
2396 ngx_post_event(rev, &ngx_posted_events);
|
0
|
2397 return;
|
|
2398 }
|
|
2399
|
|
2400 hc->pipeline = 0;
|
|
2401
|
4
|
2402 /*
|
|
2403 * To keep a memory footprint as small as possible for an idle
|
|
2404 * keepalive connection we try to free the ngx_http_request_t and
|
|
2405 * c->buffer's memory if they were allocated outside the c->pool.
|
|
2406 * The large header buffers are always allocated outside the c->pool and
|
|
2407 * are freed too.
|
|
2408 */
|
|
2409
|
0
|
2410 if (ngx_pfree(c->pool, r) == NGX_OK) {
|
|
2411 hc->request = NULL;
|
|
2412 }
|
|
2413
|
|
2414 b = c->buffer;
|
|
2415
|
|
2416 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
|
4
|
2417
|
|
2418 /*
|
|
2419 * the special note for ngx_http_keepalive_handler() that
|
|
2420 * c->buffer's memory was freed
|
|
2421 */
|
|
2422
|
0
|
2423 b->pos = NULL;
|
|
2424
|
|
2425 } else {
|
|
2426 b->pos = b->start;
|
|
2427 b->last = b->start;
|
|
2428 }
|
|
2429
|
10
|
2430 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
|
0
|
2431 hc->free, hc->nfree);
|
|
2432
|
|
2433 if (hc->free) {
|
|
2434 for (i = 0; i < hc->nfree; i++) {
|
356
|
2435 ngx_pfree(c->pool, hc->free[i]->start);
|
0
|
2436 hc->free[i] = NULL;
|
|
2437 }
|
|
2438
|
|
2439 hc->nfree = 0;
|
|
2440 }
|
|
2441
|
10
|
2442 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
|
0
|
2443 hc->busy, hc->nbusy);
|
|
2444
|
|
2445 if (hc->busy) {
|
|
2446 for (i = 0; i < hc->nbusy; i++) {
|
356
|
2447 ngx_pfree(c->pool, hc->busy[i]->start);
|
0
|
2448 hc->busy[i] = NULL;
|
|
2449 }
|
|
2450
|
|
2451 hc->nbusy = 0;
|
|
2452 }
|
|
2453
|
358
|
2454 #if (NGX_HTTP_SSL)
|
|
2455 if (c->ssl) {
|
|
2456 ngx_ssl_free_buffer(c);
|
|
2457 }
|
|
2458 #endif
|
|
2459
|
58
|
2460 rev->handler = ngx_http_keepalive_handler;
|
0
|
2461
|
132
|
2462 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
430
|
2463 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
|
132
|
2464 ngx_http_close_connection(c);
|
|
2465 return;
|
0
|
2466 }
|
|
2467 }
|
|
2468
|
32
|
2469 c->log->action = "keepalive";
|
0
|
2470
|
|
2471 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
|
50
|
2472 if (ngx_tcp_push(c->fd) == -1) {
|
0
|
2473 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
|
|
2474 ngx_http_close_connection(c);
|
|
2475 return;
|
|
2476 }
|
2
|
2477
|
0
|
2478 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
|
22
|
2479 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
|
2
|
2480
|
|
2481 } else {
|
22
|
2482 tcp_nodelay = 1;
|
|
2483 }
|
|
2484
|
32
|
2485 if (tcp_nodelay
|
|
2486 && clcf->tcp_nodelay
|
|
2487 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
|
|
2488 {
|
22
|
2489 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
|
|
2490
|
|
2491 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
184
|
2492 (const void *) &tcp_nodelay, sizeof(int))
|
|
2493 == -1)
|
22
|
2494 {
|
460
|
2495 #if (NGX_SOLARIS)
|
|
2496 /* Solaris returns EINVAL if a socket has been shut down */
|
|
2497 c->log_error = NGX_ERROR_IGNORE_EINVAL;
|
|
2498 #endif
|
|
2499
|
22
|
2500 ngx_connection_error(c, ngx_socket_errno,
|
|
2501 "setsockopt(TCP_NODELAY) failed");
|
460
|
2502
|
|
2503 c->log_error = NGX_ERROR_INFO;
|
22
|
2504 ngx_http_close_connection(c);
|
|
2505 return;
|
2
|
2506 }
|
22
|
2507
|
32
|
2508 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
|
0
|
2509 }
|
|
2510
|
|
2511 #if 0
|
4
|
2512 /* if ngx_http_request_t was freed then we need some other place */
|
0
|
2513 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
|
|
2514 #endif
|
|
2515
|
290
|
2516 c->idle = 1;
|
|
2517
|
0
|
2518 if (rev->ready) {
|
376
|
2519 ngx_post_event(rev, &ngx_posted_events);
|
0
|
2520 }
|
|
2521 }
|
|
2522
|
|
2523
|
50
|
2524 static void
|
|
2525 ngx_http_keepalive_handler(ngx_event_t *rev)
|
0
|
2526 {
|
50
|
2527 size_t size;
|
|
2528 ssize_t n;
|
|
2529 ngx_buf_t *b;
|
|
2530 ngx_connection_t *c;
|
0
|
2531
|
|
2532 c = rev->data;
|
|
2533
|
|
2534 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
|
|
2535
|
290
|
2536 if (rev->timedout || c->close) {
|
0
|
2537 ngx_http_close_connection(c);
|
|
2538 return;
|
|
2539 }
|
|
2540
|
18
|
2541 #if (NGX_HAVE_KQUEUE)
|
0
|
2542
|
4
|
2543 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
136
|
2544 if (rev->pending_eof) {
|
32
|
2545 c->log->handler = NULL;
|
0
|
2546 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
|
10
|
2547 "kevent() reported that client %V closed "
|
32
|
2548 "keepalive connection", &c->addr_text);
|
22
|
2549 #if (NGX_HTTP_SSL)
|
|
2550 if (c->ssl) {
|
96
|
2551 c->ssl->no_send_shutdown = 1;
|
22
|
2552 }
|
|
2553 #endif
|
0
|
2554 ngx_http_close_connection(c);
|
|
2555 return;
|
|
2556 }
|
|
2557 }
|
|
2558
|
|
2559 #endif
|
|
2560
|
|
2561 b = c->buffer;
|
|
2562 size = b->end - b->start;
|
|
2563
|
|
2564 if (b->pos == NULL) {
|
4
|
2565
|
|
2566 /*
|
|
2567 * The c->buffer's memory was freed by ngx_http_set_keepalive().
|
|
2568 * However, the c->buffer->start and c->buffer->end were not changed
|
|
2569 * to keep the buffer size.
|
|
2570 */
|
|
2571
|
50
|
2572 b->pos = ngx_palloc(c->pool, size);
|
|
2573 if (b->pos == NULL) {
|
0
|
2574 ngx_http_close_connection(c);
|
|
2575 return;
|
|
2576 }
|
|
2577
|
|
2578 b->start = b->pos;
|
|
2579 b->last = b->pos;
|
|
2580 b->end = b->pos + size;
|
|
2581 }
|
|
2582
|
|
2583 /*
|
|
2584 * MSIE closes a keepalive connection with RST flag
|
|
2585 * so we ignore ECONNRESET here.
|
|
2586 */
|
|
2587
|
|
2588 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
|
|
2589 ngx_set_socket_errno(0);
|
|
2590
|
|
2591 n = c->recv(c, b->last, size);
|
|
2592 c->log_error = NGX_ERROR_INFO;
|
|
2593
|
|
2594 if (n == NGX_AGAIN) {
|
430
|
2595 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
24
|
2596 ngx_http_close_connection(c);
|
|
2597 }
|
|
2598
|
0
|
2599 return;
|
|
2600 }
|
|
2601
|
|
2602 if (n == NGX_ERROR) {
|
|
2603 ngx_http_close_connection(c);
|
|
2604 return;
|
|
2605 }
|
|
2606
|
32
|
2607 c->log->handler = NULL;
|
0
|
2608
|
|
2609 if (n == 0) {
|
|
2610 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
|
32
|
2611 "client %V closed keepalive connection", &c->addr_text);
|
0
|
2612 ngx_http_close_connection(c);
|
|
2613 return;
|
|
2614 }
|
|
2615
|
|
2616 b->last += n;
|
32
|
2617
|
116
|
2618 #if (NGX_STAT_STUB)
|
498
|
2619 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
116
|
2620 #endif
|
|
2621
|
32
|
2622 c->log->handler = ngx_http_log_error;
|
|
2623 c->log->action = "reading client request line";
|
0
|
2624
|
290
|
2625 c->idle = 0;
|
|
2626
|
0
|
2627 ngx_http_init_request(rev);
|
|
2628 }
|
|
2629
|
|
2630
|
50
|
2631 static void
|
|
2632 ngx_http_set_lingering_close(ngx_http_request_t *r)
|
126
|
2633 {
|
0
|
2634 ngx_event_t *rev, *wev;
|
|
2635 ngx_connection_t *c;
|
|
2636 ngx_http_core_loc_conf_t *clcf;
|
|
2637
|
|
2638 c = r->connection;
|
|
2639
|
|
2640 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2641
|
|
2642 rev = c->read;
|
58
|
2643 rev->handler = ngx_http_lingering_close_handler;
|
0
|
2644
|
106
|
2645 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
|
0
|
2646 ngx_add_timer(rev, clcf->lingering_timeout);
|
|
2647
|
430
|
2648 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
0
|
2649 ngx_http_close_request(r, 0);
|
|
2650 return;
|
|
2651 }
|
|
2652
|
|
2653 wev = c->write;
|
58
|
2654 wev->handler = ngx_http_empty_handler;
|
0
|
2655
|
132
|
2656 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
430
|
2657 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
|
132
|
2658 ngx_http_close_request(r, 0);
|
|
2659 return;
|
0
|
2660 }
|
|
2661 }
|
|
2662
|
|
2663 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
|
|
2664 ngx_connection_error(c, ngx_socket_errno,
|
|
2665 ngx_shutdown_socket_n " failed");
|
|
2666 ngx_http_close_request(r, 0);
|
|
2667 return;
|
|
2668 }
|
|
2669
|
|
2670 if (rev->ready) {
|
|
2671 ngx_http_lingering_close_handler(rev);
|
|
2672 }
|
|
2673 }
|
|
2674
|
|
2675
|
50
|
2676 static void
|
|
2677 ngx_http_lingering_close_handler(ngx_event_t *rev)
|
0
|
2678 {
|
|
2679 ssize_t n;
|
|
2680 ngx_msec_t timer;
|
|
2681 ngx_connection_t *c;
|
|
2682 ngx_http_request_t *r;
|
|
2683 ngx_http_core_loc_conf_t *clcf;
|
|
2684 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
|
|
2685
|
|
2686 c = rev->data;
|
|
2687 r = c->data;
|
|
2688
|
|
2689 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2690 "http lingering close handler");
|
|
2691
|
|
2692 if (rev->timedout) {
|
126
|
2693 c->timedout = 1;
|
0
|
2694 ngx_http_close_request(r, 0);
|
|
2695 return;
|
|
2696 }
|
|
2697
|
346
|
2698 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
|
0
|
2699 if (timer <= 0) {
|
|
2700 ngx_http_close_request(r, 0);
|
|
2701 return;
|
|
2702 }
|
|
2703
|
|
2704 do {
|
|
2705 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
|
|
2706
|
|
2707 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
|
|
2708
|
|
2709 if (n == NGX_ERROR || n == 0) {
|
|
2710 ngx_http_close_request(r, 0);
|
|
2711 return;
|
|
2712 }
|
|
2713
|
|
2714 } while (rev->ready);
|
|
2715
|
430
|
2716 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
26
|
2717 ngx_http_close_request(r, 0);
|
24
|
2718 return;
|
|
2719 }
|
|
2720
|
0
|
2721 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2722
|
|
2723 timer *= 1000;
|
|
2724
|
|
2725 if (timer > clcf->lingering_timeout) {
|
|
2726 timer = clcf->lingering_timeout;
|
|
2727 }
|
|
2728
|
|
2729 ngx_add_timer(rev, timer);
|
|
2730 }
|
|
2731
|
|
2732
|
50
|
2733 void
|
|
2734 ngx_http_empty_handler(ngx_event_t *wev)
|
0
|
2735 {
|
|
2736 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
|
|
2737
|
|
2738 return;
|
|
2739 }
|
|
2740
|
|
2741
|
58
|
2742 void
|
|
2743 ngx_http_request_empty_handler(ngx_http_request_t *r)
|
|
2744 {
|
|
2745 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
132
|
2746 "http request empty handler");
|
58
|
2747
|
|
2748 return;
|
|
2749 }
|
|
2750
|
|
2751
|
50
|
2752 ngx_int_t
|
130
|
2753 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
|
0
|
2754 {
|
|
2755 ngx_buf_t *b;
|
|
2756 ngx_chain_t out;
|
|
2757
|
50
|
2758 b = ngx_calloc_buf(r->pool);
|
|
2759 if (b == NULL) {
|
0
|
2760 return NGX_ERROR;
|
|
2761 }
|
|
2762
|
130
|
2763 if (flags & NGX_HTTP_LAST) {
|
502
|
2764
|
|
2765 if (r == r->main && !r->post_action) {
|
|
2766 b->last_buf = 1;
|
|
2767
|
|
2768 } else {
|
|
2769 b->sync = 1;
|
|
2770 b->last_in_chain = 1;
|
|
2771 }
|
130
|
2772 }
|
|
2773
|
|
2774 if (flags & NGX_HTTP_FLUSH) {
|
|
2775 b->flush = 1;
|
|
2776 }
|
|
2777
|
0
|
2778 out.buf = b;
|
|
2779 out.next = NULL;
|
|
2780
|
|
2781 return ngx_http_output_filter(r, &out);
|
|
2782 }
|
|
2783
|
|
2784
|
162
|
2785 static ngx_int_t
|
|
2786 ngx_http_post_action(ngx_http_request_t *r)
|
|
2787 {
|
|
2788 ngx_http_core_loc_conf_t *clcf;
|
|
2789
|
|
2790 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2791
|
|
2792 if (clcf->post_action.data == NULL) {
|
|
2793 return NGX_DECLINED;
|
|
2794 }
|
|
2795
|
172
|
2796 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2797 "post action: \"%V\"", &clcf->post_action);
|
|
2798
|
162
|
2799 r->http_version = NGX_HTTP_VERSION_9;
|
|
2800 r->header_only = 1;
|
178
|
2801 r->post_action = 1;
|
162
|
2802
|
326
|
2803 r->read_event_handler = ngx_http_block_reading;
|
262
|
2804
|
366
|
2805 if (clcf->post_action.data[0] == '/') {
|
|
2806 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
|
|
2807
|
|
2808 } else {
|
|
2809 ngx_http_named_location(r, &clcf->post_action);
|
|
2810 }
|
162
|
2811
|
|
2812 return NGX_OK;
|
|
2813 }
|
|
2814
|
|
2815
|
|
2816 static void
|
58
|
2817 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
|
0
|
2818 {
|
188
|
2819 ngx_connection_t *c;
|
118
|
2820
|
|
2821 c = r->connection;
|
188
|
2822
|
|
2823 ngx_http_request_done(r->main, error);
|
118
|
2824 ngx_http_close_connection(c);
|
|
2825 }
|
|
2826
|
|
2827
|
162
|
2828 static void
|
118
|
2829 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
|
|
2830 {
|
412
|
2831 ngx_log_t *log;
|
|
2832 struct linger linger;
|
|
2833 ngx_http_cleanup_t *cln;
|
|
2834 ngx_http_log_ctx_t *ctx;
|
|
2835 ngx_http_core_loc_conf_t *clcf;
|
0
|
2836
|
|
2837 log = r->connection->log;
|
|
2838
|
|
2839 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
|
|
2840
|
|
2841 if (r->pool == NULL) {
|
80
|
2842 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
|
0
|
2843 return;
|
|
2844 }
|
|
2845
|
188
|
2846 for (cln = r->cleanup; cln; cln = cln->next) {
|
|
2847 if (cln->handler) {
|
|
2848 cln->handler(cln->data);
|
|
2849 }
|
|
2850 }
|
|
2851
|
0
|
2852 #if (NGX_STAT_STUB)
|
116
|
2853
|
0
|
2854 if (r->stat_reading) {
|
498
|
2855 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
0
|
2856 }
|
|
2857
|
|
2858 if (r->stat_writing) {
|
498
|
2859 (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
|
0
|
2860 }
|
116
|
2861
|
0
|
2862 #endif
|
|
2863
|
|
2864 if (error && r->headers_out.status == 0) {
|
|
2865 r->headers_out.status = error;
|
|
2866 }
|
|
2867
|
386
|
2868 log->action = "logging request";
|
|
2869
|
412
|
2870 ngx_http_log_request(r);
|
0
|
2871
|
386
|
2872 log->action = "closing request";
|
|
2873
|
0
|
2874 if (r->connection->timedout) {
|
|
2875 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2876
|
|
2877 if (clcf->reset_timedout_connection) {
|
58
|
2878 linger.l_onoff = 1;
|
|
2879 linger.l_linger = 0;
|
0
|
2880
|
|
2881 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
|
58
|
2882 (const void *) &linger, sizeof(struct linger)) == -1)
|
0
|
2883 {
|
|
2884 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
|
|
2885 "setsockopt(SO_LINGER) failed");
|
|
2886 }
|
|
2887 }
|
|
2888 }
|
|
2889
|
58
|
2890 /* the various request strings were allocated from r->pool */
|
0
|
2891 ctx = log->data;
|
10
|
2892 ctx->request = NULL;
|
0
|
2893
|
|
2894 r->request_line.len = 0;
|
|
2895
|
132
|
2896 r->connection->destroyed = 1;
|
|
2897
|
0
|
2898 ngx_destroy_pool(r->pool);
|
|
2899 }
|
|
2900
|
|
2901
|
80
|
2902 static void
|
412
|
2903 ngx_http_log_request(ngx_http_request_t *r)
|
|
2904 {
|
|
2905 ngx_uint_t i, n;
|
|
2906 ngx_http_handler_pt *log_handler;
|
|
2907 ngx_http_core_main_conf_t *cmcf;
|
|
2908
|
|
2909 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
2910
|
|
2911 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
|
|
2912 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
|
|
2913
|
|
2914 for (i = 0; i < n; i++) {
|
|
2915 log_handler[i](r);
|
|
2916 }
|
|
2917 }
|
|
2918
|
|
2919
|
|
2920 static void
|
50
|
2921 ngx_http_close_connection(ngx_connection_t *c)
|
0
|
2922 {
|
28
|
2923 ngx_pool_t *pool;
|
|
2924
|
0
|
2925 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2926 "close http connection: %d", c->fd);
|
|
2927
|
88
|
2928 #if (NGX_HTTP_SSL)
|
2
|
2929
|
|
2930 if (c->ssl) {
|
|
2931 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
2932 c->ssl->handler = ngx_http_close_connection;
|
2
|
2933 return;
|
|
2934 }
|
|
2935 }
|
|
2936
|
|
2937 #endif
|
|
2938
|
0
|
2939 #if (NGX_STAT_STUB)
|
498
|
2940 (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
|
0
|
2941 #endif
|
|
2942
|
132
|
2943 c->destroyed = 1;
|
|
2944
|
28
|
2945 pool = c->pool;
|
|
2946
|
0
|
2947 ngx_close_connection(c);
|
28
|
2948
|
50
|
2949 ngx_destroy_pool(pool);
|
0
|
2950 }
|
|
2951
|
|
2952
|
50
|
2953 static u_char *
|
|
2954 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
0
|
2955 {
|
32
|
2956 u_char *p;
|
54
|
2957 ngx_http_request_t *r;
|
32
|
2958 ngx_http_log_ctx_t *ctx;
|
10
|
2959
|
32
|
2960 if (log->action) {
|
58
|
2961 p = ngx_snprintf(buf, len, " while %s", log->action);
|
10
|
2962 len -= p - buf;
|
54
|
2963 buf = p;
|
10
|
2964 }
|
|
2965
|
58
|
2966 ctx = log->data;
|
|
2967
|
362
|
2968 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
|
58
|
2969 len -= p - buf;
|
54
|
2970
|
|
2971 r = ctx->request;
|
|
2972
|
58
|
2973 if (r) {
|
214
|
2974 return r->log_handler(r, ctx->current_request, p, len);
|
362
|
2975
|
|
2976 } else {
|
|
2977 p = ngx_snprintf(p, len, ", server: %V",
|
|
2978 &ctx->connection->listening->addr_text);
|
0
|
2979 }
|
10
|
2980
|
58
|
2981 return p;
|
|
2982 }
|
|
2983
|
|
2984
|
|
2985 static u_char *
|
214
|
2986 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
|
|
2987 u_char *buf, size_t len)
|
58
|
2988 {
|
362
|
2989 char *uri_separator;
|
|
2990 u_char *p;
|
|
2991 ngx_http_upstream_t *u;
|
|
2992 ngx_http_core_srv_conf_t *cscf;
|
|
2993
|
|
2994 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
2995
|
|
2996 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
|
|
2997 len -= p - buf;
|
|
2998 buf = p;
|
52
|
2999
|
348
|
3000 if (r->request_line.data == NULL && r->request_start) {
|
|
3001 for (p = r->request_start; p < r->header_in->last; p++) {
|
|
3002 if (*p == CR || *p == LF) {
|
|
3003 break;
|
|
3004 }
|
|
3005 }
|
|
3006
|
|
3007 r->request_line.len = p - r->request_start;
|
|
3008 r->request_line.data = r->request_start;
|
|
3009 }
|
|
3010
|
|
3011 if (r->request_line.len) {
|
|
3012 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
|
54
|
3013 len -= p - buf;
|
|
3014 buf = p;
|
10
|
3015 }
|
|
3016
|
214
|
3017 if (r != sr) {
|
|
3018 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
|
|
3019 len -= p - buf;
|
|
3020 buf = p;
|
|
3021 }
|
|
3022
|
|
3023 u = sr->upstream;
|
|
3024
|
260
|
3025 if (u && u->peer.name) {
|
|
3026
|
|
3027 uri_separator = "";
|
|
3028
|
|
3029 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
3030 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
|
|
3031 uri_separator = ":";
|
|
3032 }
|
|
3033 #endif
|
214
|
3034
|
|
3035 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
|
348
|
3036 &u->schema, u->peer.name,
|
260
|
3037 uri_separator, &u->uri);
|
214
|
3038 len -= p - buf;
|
|
3039 buf = p;
|
|
3040 }
|
|
3041
|
54
|
3042 if (r->headers_in.host) {
|
|
3043 p = ngx_snprintf(buf, len, ", host: \"%V\"",
|
|
3044 &r->headers_in.host->value);
|
|
3045 len -= p - buf;
|
|
3046 buf = p;
|
|
3047 }
|
|
3048
|
|
3049 if (r->headers_in.referer) {
|
|
3050 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
|
|
3051 &r->headers_in.referer->value);
|
|
3052 buf = p;
|
|
3053 }
|
|
3054
|
|
3055 return buf;
|
|
3056 }
|