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