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