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