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