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
|
650
|
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));
|
636
|
686
|
|
687 #ifdef SSL_CTRL_CLEAR_OPTIONS
|
650
|
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));
|
636
|
691 #endif
|
|
692
|
650
|
693 SSL_set_options(ssl_conn, SSL_CTX_get_options(sscf->ssl.ctx));
|
|
694 }
|
636
|
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 {
|
650
|
1677 u_char *h, ch;
|
|
1678 size_t i, dot_pos, host_len;
|
|
1679
|
|
1680 enum {
|
|
1681 sw_usual = 0,
|
|
1682 sw_literal,
|
|
1683 sw_rest
|
|
1684 } state;
|
|
1685
|
|
1686 dot_pos = len;
|
|
1687 host_len = len;
|
|
1688
|
530
|
1689 h = *host;
|
650
|
1690
|
|
1691 state = sw_usual;
|
376
|
1692
|
|
1693 for (i = 0; i < len; i++) {
|
530
|
1694 ch = h[i];
|
376
|
1695
|
650
|
1696 switch (ch) {
|
|
1697
|
|
1698 case '.':
|
|
1699 if (dot_pos == i - 1) {
|
530
|
1700 return 0;
|
376
|
1701 }
|
650
|
1702 dot_pos = i;
|
|
1703 break;
|
|
1704
|
|
1705 case ':':
|
|
1706 if (state == sw_usual) {
|
|
1707 host_len = i;
|
|
1708 state = sw_rest;
|
|
1709 }
|
|
1710 break;
|
|
1711
|
|
1712 case '[':
|
|
1713 if (i == 0) {
|
|
1714 state = sw_literal;
|
|
1715 }
|
|
1716 break;
|
|
1717
|
|
1718 case ']':
|
|
1719 if (state == sw_literal) {
|
|
1720 host_len = i + 1;
|
|
1721 state = sw_rest;
|
|
1722 }
|
|
1723 break;
|
|
1724
|
|
1725 case '\0':
|
530
|
1726 return 0;
|
650
|
1727
|
|
1728 default:
|
|
1729
|
|
1730 if (ngx_path_separator(ch)) {
|
|
1731 return 0;
|
|
1732 }
|
|
1733
|
|
1734 if (ch >= 'A' && ch <= 'Z') {
|
|
1735 alloc = 1;
|
|
1736 }
|
|
1737
|
|
1738 break;
|
376
|
1739 }
|
|
1740 }
|
|
1741
|
650
|
1742 if (dot_pos == host_len - 1) {
|
|
1743 host_len--;
|
376
|
1744 }
|
|
1745
|
530
|
1746 if (alloc) {
|
650
|
1747 *host = ngx_pnalloc(r->pool, host_len);
|
530
|
1748 if (*host == NULL) {
|
|
1749 return -1;
|
|
1750 }
|
|
1751
|
650
|
1752 ngx_strlow(*host, h, host_len);
|
530
|
1753 }
|
|
1754
|
650
|
1755 return host_len;
|
376
|
1756 }
|
|
1757
|
|
1758
|
|
1759 static ngx_int_t
|
|
1760 ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
|
|
1761 {
|
142
|
1762 ngx_http_core_loc_conf_t *clcf;
|
|
1763 ngx_http_core_srv_conf_t *cscf;
|
376
|
1764
|
478
|
1765 if (r->virtual_names == NULL) {
|
376
|
1766 return NGX_DECLINED;
|
|
1767 }
|
|
1768
|
530
|
1769 cscf = ngx_hash_find_combined(&r->virtual_names->names,
|
|
1770 ngx_hash_key(host, len), host, len);
|
312
|
1771
|
|
1772 if (cscf) {
|
|
1773 goto found;
|
26
|
1774 }
|
|
1775
|
326
|
1776 #if (NGX_PCRE)
|
|
1777
|
538
|
1778 if (len && r->virtual_names->nregex) {
|
356
|
1779 ngx_int_t n;
|
|
1780 ngx_uint_t i;
|
|
1781 ngx_str_t name;
|
|
1782 ngx_http_server_name_t *sn;
|
326
|
1783
|
|
1784 name.len = len;
|
530
|
1785 name.data = host;
|
326
|
1786
|
478
|
1787 sn = r->virtual_names->regex;
|
|
1788
|
|
1789 for (i = 0; i < r->virtual_names->nregex; i++) {
|
326
|
1790
|
546
|
1791 n = ngx_http_regex_exec(r, sn[i].regex, &name);
|
|
1792
|
|
1793 if (n == NGX_OK) {
|
|
1794 cscf = sn[i].server;
|
|
1795 goto found;
|
456
|
1796 }
|
|
1797
|
546
|
1798 if (n == NGX_DECLINED) {
|
326
|
1799 continue;
|
|
1800 }
|
|
1801
|
546
|
1802 return NGX_ERROR;
|
326
|
1803 }
|
|
1804 }
|
|
1805
|
|
1806 #endif
|
|
1807
|
376
|
1808 return NGX_OK;
|
58
|
1809
|
|
1810 found:
|
|
1811
|
142
|
1812 r->srv_conf = cscf->ctx->srv_conf;
|
|
1813 r->loc_conf = cscf->ctx->loc_conf;
|
58
|
1814
|
|
1815 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
484
|
1816 r->connection->log->file = clcf->error_log->file;
|
58
|
1817
|
|
1818 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
484
|
1819 r->connection->log->log_level = clcf->error_log->log_level;
|
26
|
1820 }
|
|
1821
|
376
|
1822 return NGX_OK;
|
26
|
1823 }
|
|
1824
|
|
1825
|
58
|
1826 static void
|
|
1827 ngx_http_request_handler(ngx_event_t *ev)
|
|
1828 {
|
|
1829 ngx_connection_t *c;
|
|
1830 ngx_http_request_t *r;
|
214
|
1831 ngx_http_log_ctx_t *ctx;
|
58
|
1832
|
|
1833 c = ev->data;
|
|
1834 r = c->data;
|
|
1835
|
214
|
1836 ctx = c->log->data;
|
|
1837 ctx->current_request = r;
|
|
1838
|
426
|
1839 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1840 "http run request: \"%V?%V\"", &r->uri, &r->args);
|
|
1841
|
58
|
1842 if (ev->write) {
|
|
1843 r->write_event_handler(r);
|
|
1844
|
|
1845 } else {
|
|
1846 r->read_event_handler(r);
|
|
1847 }
|
426
|
1848
|
|
1849 ngx_http_run_posted_requests(c);
|
|
1850 }
|
|
1851
|
|
1852
|
|
1853 void
|
|
1854 ngx_http_run_posted_requests(ngx_connection_t *c)
|
|
1855 {
|
|
1856 ngx_http_request_t *r;
|
|
1857 ngx_http_log_ctx_t *ctx;
|
|
1858 ngx_http_posted_request_t *pr;
|
|
1859
|
|
1860 for ( ;; ) {
|
|
1861
|
|
1862 if (c->destroyed) {
|
|
1863 return;
|
|
1864 }
|
|
1865
|
|
1866 r = c->data;
|
|
1867 pr = r->main->posted_requests;
|
|
1868
|
|
1869 if (pr == NULL) {
|
|
1870 return;
|
|
1871 }
|
|
1872
|
|
1873 r->main->posted_requests = pr->next;
|
|
1874
|
|
1875 r = pr->request;
|
|
1876
|
|
1877 ctx = c->log->data;
|
|
1878 ctx->current_request = r;
|
|
1879
|
|
1880 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1881 "http posted request: \"%V?%V\"", &r->uri, &r->args);
|
|
1882
|
|
1883 r->write_event_handler(r);
|
|
1884 }
|
|
1885 }
|
|
1886
|
|
1887
|
|
1888 ngx_int_t
|
520
|
1889 ngx_http_post_request(ngx_http_request_t *r, ngx_http_posted_request_t *pr)
|
426
|
1890 {
|
520
|
1891 ngx_http_posted_request_t **p;
|
|
1892
|
426
|
1893 if (pr == NULL) {
|
520
|
1894 pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
|
|
1895 if (pr == NULL) {
|
|
1896 return NGX_ERROR;
|
|
1897 }
|
426
|
1898 }
|
|
1899
|
|
1900 pr->request = r;
|
|
1901 pr->next = NULL;
|
|
1902
|
|
1903 for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
|
|
1904
|
|
1905 *p = pr;
|
|
1906
|
|
1907 return NGX_OK;
|
58
|
1908 }
|
|
1909
|
|
1910
|
50
|
1911 void
|
58
|
1912 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
0
|
1913 {
|
330
|
1914 ngx_connection_t *c;
|
58
|
1915 ngx_http_request_t *pr;
|
0
|
1916 ngx_http_core_loc_conf_t *clcf;
|
|
1917
|
518
|
1918 c = r->connection;
|
|
1919
|
|
1920 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1921 "http finalize request: %d, \"%V?%V\" a:%d, c:%d",
|
|
1922 rc, &r->uri, &r->args, r == c->data, r->main->count);
|
|
1923
|
58
|
1924 if (rc == NGX_DONE) {
|
518
|
1925 ngx_http_finalize_connection(r);
|
58
|
1926 return;
|
|
1927 }
|
|
1928
|
488
|
1929 if (rc == NGX_OK && r->filter_finalize) {
|
|
1930 c->error = 1;
|
|
1931 return;
|
|
1932 }
|
|
1933
|
230
|
1934 if (rc == NGX_DECLINED) {
|
|
1935 r->content_handler = NULL;
|
|
1936 r->write_event_handler = ngx_http_core_run_phases;
|
|
1937 ngx_http_core_run_phases(r);
|
|
1938 return;
|
|
1939 }
|
|
1940
|
248
|
1941 if (r != r->main && r->post_subrequest) {
|
|
1942 rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
|
216
|
1943 }
|
|
1944
|
358
|
1945 if (rc == NGX_ERROR
|
|
1946 || rc == NGX_HTTP_REQUEST_TIME_OUT
|
|
1947 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
|
|
1948 || c->error)
|
|
1949 {
|
162
|
1950 if (ngx_http_post_action(r) == NGX_OK) {
|
|
1951 return;
|
|
1952 }
|
|
1953
|
518
|
1954 if (r->main->blocked) {
|
|
1955 r->write_event_handler = ngx_http_request_finalizer;
|
|
1956 }
|
|
1957
|
|
1958 ngx_http_terminate_request(r, rc);
|
0
|
1959 return;
|
|
1960 }
|
|
1961
|
186
|
1962 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
|
|
1963 || rc == NGX_HTTP_CREATED
|
|
1964 || rc == NGX_HTTP_NO_CONTENT)
|
|
1965 {
|
136
|
1966 if (rc == NGX_HTTP_CLOSE) {
|
518
|
1967 ngx_http_terminate_request(r, rc);
|
136
|
1968 return;
|
|
1969 }
|
|
1970
|
146
|
1971 if (r == r->main) {
|
330
|
1972 if (c->read->timer_set) {
|
|
1973 ngx_del_timer(c->read);
|
118
|
1974 }
|
|
1975
|
330
|
1976 if (c->write->timer_set) {
|
|
1977 ngx_del_timer(c->write);
|
118
|
1978 }
|
|
1979 }
|
|
1980
|
410
|
1981 c->read->handler = ngx_http_request_handler;
|
|
1982 c->write->handler = ngx_http_request_handler;
|
|
1983
|
118
|
1984 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
|
|
1985 return;
|
|
1986 }
|
|
1987
|
426
|
1988 if (r != r->main) {
|
|
1989
|
|
1990 if (r->buffered || r->postponed) {
|
|
1991
|
|
1992 if (ngx_http_set_write_handler(r) != NGX_OK) {
|
518
|
1993 ngx_http_terminate_request(r, 0);
|
426
|
1994 }
|
|
1995
|
74
|
1996 return;
|
|
1997 }
|
426
|
1998
|
|
1999 #if (NGX_DEBUG)
|
|
2000 if (r != c->data) {
|
|
2001 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2002 "http finalize non-active request: \"%V?%V\"",
|
|
2003 &r->uri, &r->args);
|
|
2004 }
|
|
2005 #endif
|
58
|
2006
|
|
2007 pr = r->parent;
|
|
2008
|
426
|
2009 if (r == c->data) {
|
|
2010
|
518
|
2011 r->main->count--;
|
|
2012
|
426
|
2013 if (!r->logged) {
|
|
2014
|
|
2015 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2016
|
|
2017 if (clcf->log_subrequest) {
|
|
2018 ngx_http_log_request(r);
|
|
2019 }
|
|
2020
|
|
2021 r->logged = 1;
|
|
2022
|
|
2023 } else {
|
|
2024 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
2025 "subrequest: \"%V?%V\" logged again",
|
|
2026 &r->uri, &r->args);
|
|
2027 }
|
|
2028
|
|
2029 r->done = 1;
|
|
2030
|
|
2031 if (pr->postponed && pr->postponed->request == r) {
|
|
2032 pr->postponed = pr->postponed->next;
|
|
2033 }
|
|
2034
|
|
2035 c->data = pr;
|
|
2036
|
|
2037 } else {
|
|
2038
|
|
2039 r->write_event_handler = ngx_http_request_finalizer;
|
|
2040
|
|
2041 if (r->waited) {
|
|
2042 r->done = 1;
|
|
2043 }
|
|
2044 }
|
|
2045
|
520
|
2046 if (ngx_http_post_request(pr, NULL) != NGX_OK) {
|
518
|
2047 r->main->count++;
|
|
2048 ngx_http_terminate_request(r, 0);
|
426
|
2049 return;
|
|
2050 }
|
|
2051
|
330
|
2052 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
426
|
2053 "http wake parent request: \"%V?%V\"",
|
|
2054 &pr->uri, &pr->args);
|
|
2055
|
|
2056 return;
|
|
2057 }
|
|
2058
|
518
|
2059 if (r->buffered || c->buffered || r->postponed || r->blocked) {
|
426
|
2060
|
|
2061 if (ngx_http_set_write_handler(r) != NGX_OK) {
|
518
|
2062 ngx_http_terminate_request(r, 0);
|
58
|
2063 }
|
|
2064
|
|
2065 return;
|
|
2066 }
|
0
|
2067
|
426
|
2068 if (r != c->data) {
|
|
2069 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
2070 "http finalize non-active request: \"%V?%V\"",
|
|
2071 &r->uri, &r->args);
|
0
|
2072 return;
|
76
|
2073 }
|
|
2074
|
426
|
2075 r->done = 1;
|
562
|
2076 r->write_event_handler = ngx_http_request_empty_handler;
|
0
|
2077
|
178
|
2078 if (!r->post_action) {
|
|
2079 r->request_complete = 1;
|
|
2080 }
|
|
2081
|
162
|
2082 if (ngx_http_post_action(r) == NGX_OK) {
|
126
|
2083 return;
|
|
2084 }
|
|
2085
|
330
|
2086 if (c->read->timer_set) {
|
|
2087 ngx_del_timer(c->read);
|
|
2088 }
|
|
2089
|
|
2090 if (c->write->timer_set) {
|
|
2091 c->write->delayed = 0;
|
|
2092 ngx_del_timer(c->write);
|
0
|
2093 }
|
|
2094
|
518
|
2095 if (c->read->eof) {
|
|
2096 ngx_http_close_request(r, 0);
|
330
|
2097 return;
|
0
|
2098 }
|
|
2099
|
518
|
2100 ngx_http_finalize_connection(r);
|
|
2101 }
|
|
2102
|
|
2103
|
|
2104 static void
|
|
2105 ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc)
|
|
2106 {
|
520
|
2107 ngx_http_cleanup_t *cln;
|
|
2108 ngx_http_request_t *mr;
|
|
2109 ngx_http_ephemeral_t *e;
|
518
|
2110
|
|
2111 mr = r->main;
|
|
2112
|
|
2113 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2114 "http terminate request count:%d", mr->count);
|
|
2115
|
532
|
2116 if (rc > 0 && (mr->headers_out.status == 0 || mr->connection->sent == 0)) {
|
|
2117 mr->headers_out.status = rc;
|
|
2118 }
|
|
2119
|
518
|
2120 cln = mr->cleanup;
|
|
2121 mr->cleanup = NULL;
|
|
2122
|
|
2123 while (cln) {
|
|
2124 if (cln->handler) {
|
|
2125 cln->handler(cln->data);
|
|
2126 }
|
|
2127
|
|
2128 cln = cln->next;
|
|
2129 }
|
|
2130
|
|
2131 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2132 "http terminate cleanup count:%d blk:%d",
|
|
2133 mr->count, mr->blocked);
|
|
2134
|
|
2135 if (mr->write_event_handler) {
|
|
2136
|
|
2137 if (mr->blocked) {
|
|
2138 return;
|
|
2139 }
|
|
2140
|
520
|
2141 e = ngx_http_ephemeral(mr);
|
518
|
2142 mr->posted_requests = NULL;
|
|
2143 mr->write_event_handler = ngx_http_terminate_handler;
|
520
|
2144 (void) ngx_http_post_request(mr, &e->terminal_posted_request);
|
518
|
2145 return;
|
|
2146 }
|
|
2147
|
|
2148 ngx_http_close_request(mr, rc);
|
|
2149 }
|
|
2150
|
|
2151
|
|
2152 static void
|
|
2153 ngx_http_terminate_handler(ngx_http_request_t *r)
|
|
2154 {
|
|
2155 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2156 "http terminate handler count:%d", r->count);
|
|
2157
|
|
2158 r->count = 1;
|
|
2159
|
|
2160 ngx_http_close_request(r, 0);
|
|
2161 }
|
|
2162
|
|
2163
|
|
2164 static void
|
|
2165 ngx_http_finalize_connection(ngx_http_request_t *r)
|
|
2166 {
|
|
2167 ngx_http_core_loc_conf_t *clcf;
|
|
2168
|
538
|
2169 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2170
|
518
|
2171 if (r->main->count != 1) {
|
538
|
2172
|
|
2173 if (r->discard_body) {
|
|
2174 r->read_event_handler = ngx_http_discarded_request_body_handler;
|
632
|
2175 ngx_add_timer(r->connection->read, clcf->lingering_timeout);
|
538
|
2176
|
|
2177 if (r->lingering_time == 0) {
|
|
2178 r->lingering_time = ngx_time()
|
|
2179 + (time_t) (clcf->lingering_time / 1000);
|
|
2180 }
|
|
2181 }
|
|
2182
|
330
|
2183 ngx_http_close_request(r, 0);
|
138
|
2184 return;
|
|
2185 }
|
|
2186
|
0
|
2187 if (!ngx_terminate
|
|
2188 && !ngx_exiting
|
378
|
2189 && r->keepalive
|
0
|
2190 && clcf->keepalive_timeout > 0)
|
|
2191 {
|
|
2192 ngx_http_set_keepalive(r);
|
|
2193 return;
|
632
|
2194 }
|
|
2195
|
|
2196 if (clcf->lingering_close == NGX_HTTP_LINGERING_ALWAYS
|
|
2197 || (clcf->lingering_close == NGX_HTTP_LINGERING_ON
|
|
2198 && (r->lingering_close
|
|
2199 || r->header_in->pos < r->header_in->last
|
|
2200 || r->connection->read->ready)))
|
|
2201 {
|
0
|
2202 ngx_http_set_lingering_close(r);
|
|
2203 return;
|
|
2204 }
|
|
2205
|
|
2206 ngx_http_close_request(r, 0);
|
|
2207 }
|
|
2208
|
|
2209
|
74
|
2210 static ngx_int_t
|
50
|
2211 ngx_http_set_write_handler(ngx_http_request_t *r)
|
0
|
2212 {
|
|
2213 ngx_event_t *wev;
|
|
2214 ngx_http_core_loc_conf_t *clcf;
|
|
2215
|
58
|
2216 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
|
|
2217
|
538
|
2218 r->read_event_handler = r->discard_body ?
|
|
2219 ngx_http_discarded_request_body_handler:
|
|
2220 ngx_http_test_reading;
|
74
|
2221 r->write_event_handler = ngx_http_writer;
|
|
2222
|
0
|
2223 wev = r->connection->write;
|
|
2224
|
|
2225 if (wev->ready && wev->delayed) {
|
74
|
2226 return NGX_OK;
|
0
|
2227 }
|
|
2228
|
58
|
2229 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
0
|
2230 if (!wev->delayed) {
|
|
2231 ngx_add_timer(wev, clcf->send_timeout);
|
|
2232 }
|
|
2233
|
430
|
2234 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
0
|
2235 ngx_http_close_request(r, 0);
|
74
|
2236 return NGX_ERROR;
|
0
|
2237 }
|
74
|
2238
|
|
2239 return NGX_OK;
|
0
|
2240 }
|
|
2241
|
|
2242
|
58
|
2243 static void
|
|
2244 ngx_http_writer(ngx_http_request_t *r)
|
0
|
2245 {
|
|
2246 int rc;
|
58
|
2247 ngx_event_t *wev;
|
0
|
2248 ngx_connection_t *c;
|
|
2249 ngx_http_core_loc_conf_t *clcf;
|
|
2250
|
58
|
2251 c = r->connection;
|
|
2252 wev = c->write;
|
|
2253
|
132
|
2254 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2255 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
|
0
|
2256
|
426
|
2257 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
|
2258
|
0
|
2259 if (wev->timedout) {
|
|
2260 if (!wev->delayed) {
|
54
|
2261 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
2262 "client timed out");
|
|
2263 c->timedout = 1;
|
116
|
2264
|
162
|
2265 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
2266 return;
|
|
2267 }
|
|
2268
|
|
2269 wev->timedout = 0;
|
|
2270 wev->delayed = 0;
|
|
2271
|
|
2272 if (!wev->ready) {
|
|
2273 ngx_add_timer(wev, clcf->send_timeout);
|
|
2274
|
430
|
2275 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
118
|
2276 ngx_http_close_request(r, 0);
|
0
|
2277 }
|
|
2278
|
|
2279 return;
|
|
2280 }
|
|
2281
|
644
|
2282 }
|
|
2283
|
|
2284 if (wev->delayed || r->aio) {
|
|
2285 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2286 "http writer delayed");
|
|
2287
|
|
2288 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
|
2289 ngx_http_close_request(r, 0);
|
0
|
2290 }
|
644
|
2291
|
|
2292 return;
|
0
|
2293 }
|
|
2294
|
132
|
2295 rc = ngx_http_output_filter(r, NULL);
|
|
2296
|
|
2297 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2298 "http writer output filter: %d, \"%V?%V\"",
|
|
2299 rc, &r->uri, &r->args);
|
0
|
2300
|
494
|
2301 if (rc == NGX_ERROR) {
|
|
2302 ngx_http_finalize_request(r, rc);
|
|
2303 return;
|
|
2304 }
|
|
2305
|
426
|
2306 if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
|
|
2307
|
644
|
2308 if (!wev->delayed) {
|
0
|
2309 ngx_add_timer(wev, clcf->send_timeout);
|
|
2310 }
|
|
2311
|
430
|
2312 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
|
118
|
2313 ngx_http_close_request(r, 0);
|
0
|
2314 }
|
|
2315
|
426
|
2316 return;
|
0
|
2317 }
|
|
2318
|
132
|
2319 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
2320 "http writer done: \"%V?%V\"", &r->uri, &r->args);
|
0
|
2321
|
538
|
2322 r->write_event_handler = ngx_http_request_empty_handler;
|
|
2323
|
0
|
2324 ngx_http_finalize_request(r, rc);
|
|
2325 }
|
|
2326
|
|
2327
|
426
|
2328 static void
|
|
2329 ngx_http_request_finalizer(ngx_http_request_t *r)
|
|
2330 {
|
|
2331 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2332 "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
|
|
2333
|
|
2334 ngx_http_finalize_request(r, 0);
|
|
2335 }
|
|
2336
|
|
2337
|
326
|
2338 void
|
|
2339 ngx_http_block_reading(ngx_http_request_t *r)
|
0
|
2340 {
|
318
|
2341 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
326
|
2342 "http reading blocked");
|
318
|
2343
|
|
2344 /* aio does not call this handler */
|
|
2345
|
|
2346 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
|
|
2347 && r->connection->read->active)
|
|
2348 {
|
430
|
2349 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
|
318
|
2350 ngx_http_close_request(r, 0);
|
|
2351 }
|
|
2352 }
|
|
2353 }
|
|
2354
|
|
2355
|
416
|
2356 void
|
326
|
2357 ngx_http_test_reading(ngx_http_request_t *r)
|
318
|
2358 {
|
276
|
2359 int n;
|
|
2360 char buf[1];
|
|
2361 ngx_err_t err;
|
|
2362 ngx_event_t *rev;
|
|
2363 ngx_connection_t *c;
|
|
2364
|
|
2365 c = r->connection;
|
|
2366 rev = c->read;
|
|
2367
|
326
|
2368 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
|
276
|
2369
|
|
2370 #if (NGX_HAVE_KQUEUE)
|
|
2371
|
|
2372 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
|
2373
|
|
2374 if (!rev->pending_eof) {
|
|
2375 return;
|
|
2376 }
|
|
2377
|
|
2378 rev->eof = 1;
|
|
2379 c->error = 1;
|
|
2380 err = rev->kq_errno;
|
|
2381
|
|
2382 goto closed;
|
|
2383 }
|
|
2384
|
|
2385 #endif
|
|
2386
|
|
2387 n = recv(c->fd, buf, 1, MSG_PEEK);
|
|
2388
|
|
2389 if (n == 0) {
|
|
2390 rev->eof = 1;
|
|
2391 c->error = 1;
|
|
2392 err = 0;
|
|
2393
|
|
2394 goto closed;
|
|
2395
|
|
2396 } else if (n == -1) {
|
|
2397 err = ngx_socket_errno;
|
|
2398
|
|
2399 if (err != NGX_EAGAIN) {
|
|
2400 rev->eof = 1;
|
|
2401 c->error = 1;
|
|
2402
|
|
2403 goto closed;
|
|
2404 }
|
|
2405 }
|
0
|
2406
|
|
2407 /* aio does not call this handler */
|
|
2408
|
276
|
2409 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
|
|
2410
|
430
|
2411 if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
|
0
|
2412 ngx_http_close_request(r, 0);
|
|
2413 }
|
|
2414 }
|
276
|
2415
|
|
2416 return;
|
|
2417
|
|
2418 closed:
|
|
2419
|
|
2420 if (err) {
|
|
2421 rev->error = 1;
|
|
2422 }
|
|
2423
|
|
2424 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
2425 "client closed prematurely connection");
|
|
2426
|
286
|
2427 ngx_http_finalize_request(r, 0);
|
0
|
2428 }
|
|
2429
|
|
2430
|
50
|
2431 static void
|
|
2432 ngx_http_set_keepalive(ngx_http_request_t *r)
|
0
|
2433 {
|
2
|
2434 int tcp_nodelay;
|
0
|
2435 ngx_int_t i;
|
|
2436 ngx_buf_t *b, *f;
|
|
2437 ngx_event_t *rev, *wev;
|
|
2438 ngx_connection_t *c;
|
|
2439 ngx_http_connection_t *hc;
|
|
2440 ngx_http_core_srv_conf_t *cscf;
|
|
2441 ngx_http_core_loc_conf_t *clcf;
|
|
2442
|
|
2443 c = r->connection;
|
|
2444 rev = c->read;
|
|
2445
|
326
|
2446 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2447
|
0
|
2448 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
|
|
2449
|
326
|
2450 if (r->discard_body) {
|
442
|
2451 r->write_event_handler = ngx_http_request_empty_handler;
|
326
|
2452 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
|
|
2453 ngx_add_timer(rev, clcf->lingering_timeout);
|
|
2454 return;
|
|
2455 }
|
|
2456
|
32
|
2457 c->log->action = "closing request";
|
0
|
2458
|
|
2459 hc = r->http_connection;
|
|
2460 b = r->header_in;
|
|
2461
|
|
2462 if (b->pos < b->last) {
|
|
2463
|
|
2464 /* the pipelined request */
|
|
2465
|
|
2466 if (b != c->buffer) {
|
|
2467
|
4
|
2468 /*
|
|
2469 * If the large header buffers were allocated while the previous
|
|
2470 * request processing then we do not use c->buffer for
|
|
2471 * the pipelined request (see ngx_http_init_request()).
|
126
|
2472 *
|
4
|
2473 * Now we would move the large header buffers to the free list.
|
|
2474 */
|
0
|
2475
|
|
2476 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
2477
|
|
2478 if (hc->free == NULL) {
|
|
2479 hc->free = ngx_palloc(c->pool,
|
|
2480 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
2481
|
|
2482 if (hc->free == NULL) {
|
26
|
2483 ngx_http_close_request(r, 0);
|
0
|
2484 return;
|
|
2485 }
|
|
2486 }
|
|
2487
|
|
2488 for (i = 0; i < hc->nbusy - 1; i++) {
|
|
2489 f = hc->busy[i];
|
|
2490 hc->free[hc->nfree++] = f;
|
|
2491 f->pos = f->start;
|
|
2492 f->last = f->start;
|
|
2493 }
|
|
2494
|
|
2495 hc->busy[0] = b;
|
|
2496 hc->nbusy = 1;
|
|
2497 }
|
|
2498 }
|
|
2499
|
532
|
2500 r->keepalive = 0;
|
|
2501
|
518
|
2502 ngx_http_free_request(r, 0);
|
118
|
2503
|
0
|
2504 c->data = hc;
|
|
2505
|
|
2506 ngx_add_timer(rev, clcf->keepalive_timeout);
|
|
2507
|
430
|
2508 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
0
|
2509 ngx_http_close_connection(c);
|
|
2510 return;
|
|
2511 }
|
|
2512
|
|
2513 wev = c->write;
|
58
|
2514 wev->handler = ngx_http_empty_handler;
|
0
|
2515
|
|
2516 if (b->pos < b->last) {
|
|
2517
|
|
2518 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
|
|
2519
|
116
|
2520 #if (NGX_STAT_STUB)
|
502
|
2521 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
116
|
2522 #endif
|
|
2523
|
0
|
2524 hc->pipeline = 1;
|
32
|
2525 c->log->action = "reading client pipelined request line";
|
116
|
2526
|
376
|
2527 rev->handler = ngx_http_init_request;
|
|
2528 ngx_post_event(rev, &ngx_posted_events);
|
0
|
2529 return;
|
|
2530 }
|
|
2531
|
|
2532 hc->pipeline = 0;
|
|
2533
|
4
|
2534 /*
|
|
2535 * To keep a memory footprint as small as possible for an idle
|
|
2536 * keepalive connection we try to free the ngx_http_request_t and
|
|
2537 * c->buffer's memory if they were allocated outside the c->pool.
|
|
2538 * The large header buffers are always allocated outside the c->pool and
|
|
2539 * are freed too.
|
|
2540 */
|
|
2541
|
0
|
2542 if (ngx_pfree(c->pool, r) == NGX_OK) {
|
|
2543 hc->request = NULL;
|
|
2544 }
|
|
2545
|
|
2546 b = c->buffer;
|
|
2547
|
|
2548 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
|
4
|
2549
|
|
2550 /*
|
|
2551 * the special note for ngx_http_keepalive_handler() that
|
|
2552 * c->buffer's memory was freed
|
|
2553 */
|
|
2554
|
0
|
2555 b->pos = NULL;
|
|
2556
|
|
2557 } else {
|
|
2558 b->pos = b->start;
|
|
2559 b->last = b->start;
|
|
2560 }
|
|
2561
|
10
|
2562 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
|
0
|
2563 hc->free, hc->nfree);
|
|
2564
|
|
2565 if (hc->free) {
|
|
2566 for (i = 0; i < hc->nfree; i++) {
|
356
|
2567 ngx_pfree(c->pool, hc->free[i]->start);
|
0
|
2568 hc->free[i] = NULL;
|
|
2569 }
|
|
2570
|
|
2571 hc->nfree = 0;
|
|
2572 }
|
|
2573
|
10
|
2574 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
|
0
|
2575 hc->busy, hc->nbusy);
|
|
2576
|
|
2577 if (hc->busy) {
|
|
2578 for (i = 0; i < hc->nbusy; i++) {
|
356
|
2579 ngx_pfree(c->pool, hc->busy[i]->start);
|
0
|
2580 hc->busy[i] = NULL;
|
|
2581 }
|
|
2582
|
|
2583 hc->nbusy = 0;
|
|
2584 }
|
|
2585
|
358
|
2586 #if (NGX_HTTP_SSL)
|
|
2587 if (c->ssl) {
|
|
2588 ngx_ssl_free_buffer(c);
|
|
2589 }
|
|
2590 #endif
|
|
2591
|
58
|
2592 rev->handler = ngx_http_keepalive_handler;
|
0
|
2593
|
132
|
2594 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
430
|
2595 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
|
132
|
2596 ngx_http_close_connection(c);
|
|
2597 return;
|
0
|
2598 }
|
|
2599 }
|
|
2600
|
32
|
2601 c->log->action = "keepalive";
|
0
|
2602
|
|
2603 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
|
50
|
2604 if (ngx_tcp_push(c->fd) == -1) {
|
0
|
2605 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
|
|
2606 ngx_http_close_connection(c);
|
|
2607 return;
|
|
2608 }
|
2
|
2609
|
0
|
2610 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
|
22
|
2611 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
|
2
|
2612
|
|
2613 } else {
|
22
|
2614 tcp_nodelay = 1;
|
|
2615 }
|
|
2616
|
32
|
2617 if (tcp_nodelay
|
|
2618 && clcf->tcp_nodelay
|
|
2619 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
|
|
2620 {
|
22
|
2621 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
|
|
2622
|
|
2623 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
184
|
2624 (const void *) &tcp_nodelay, sizeof(int))
|
|
2625 == -1)
|
22
|
2626 {
|
460
|
2627 #if (NGX_SOLARIS)
|
|
2628 /* Solaris returns EINVAL if a socket has been shut down */
|
|
2629 c->log_error = NGX_ERROR_IGNORE_EINVAL;
|
|
2630 #endif
|
|
2631
|
22
|
2632 ngx_connection_error(c, ngx_socket_errno,
|
|
2633 "setsockopt(TCP_NODELAY) failed");
|
460
|
2634
|
|
2635 c->log_error = NGX_ERROR_INFO;
|
22
|
2636 ngx_http_close_connection(c);
|
|
2637 return;
|
2
|
2638 }
|
22
|
2639
|
32
|
2640 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
|
0
|
2641 }
|
|
2642
|
|
2643 #if 0
|
4
|
2644 /* if ngx_http_request_t was freed then we need some other place */
|
0
|
2645 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
|
|
2646 #endif
|
|
2647
|
290
|
2648 c->idle = 1;
|
618
|
2649 ngx_reusable_connection(c, 1);
|
290
|
2650
|
0
|
2651 if (rev->ready) {
|
376
|
2652 ngx_post_event(rev, &ngx_posted_events);
|
0
|
2653 }
|
|
2654 }
|
|
2655
|
|
2656
|
50
|
2657 static void
|
|
2658 ngx_http_keepalive_handler(ngx_event_t *rev)
|
0
|
2659 {
|
50
|
2660 size_t size;
|
|
2661 ssize_t n;
|
|
2662 ngx_buf_t *b;
|
|
2663 ngx_connection_t *c;
|
0
|
2664
|
|
2665 c = rev->data;
|
|
2666
|
|
2667 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
|
|
2668
|
290
|
2669 if (rev->timedout || c->close) {
|
0
|
2670 ngx_http_close_connection(c);
|
|
2671 return;
|
|
2672 }
|
|
2673
|
18
|
2674 #if (NGX_HAVE_KQUEUE)
|
0
|
2675
|
4
|
2676 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
136
|
2677 if (rev->pending_eof) {
|
32
|
2678 c->log->handler = NULL;
|
0
|
2679 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
|
10
|
2680 "kevent() reported that client %V closed "
|
32
|
2681 "keepalive connection", &c->addr_text);
|
22
|
2682 #if (NGX_HTTP_SSL)
|
|
2683 if (c->ssl) {
|
96
|
2684 c->ssl->no_send_shutdown = 1;
|
22
|
2685 }
|
|
2686 #endif
|
0
|
2687 ngx_http_close_connection(c);
|
|
2688 return;
|
|
2689 }
|
|
2690 }
|
|
2691
|
|
2692 #endif
|
|
2693
|
|
2694 b = c->buffer;
|
|
2695 size = b->end - b->start;
|
|
2696
|
|
2697 if (b->pos == NULL) {
|
4
|
2698
|
|
2699 /*
|
|
2700 * The c->buffer's memory was freed by ngx_http_set_keepalive().
|
|
2701 * However, the c->buffer->start and c->buffer->end were not changed
|
|
2702 * to keep the buffer size.
|
|
2703 */
|
|
2704
|
50
|
2705 b->pos = ngx_palloc(c->pool, size);
|
|
2706 if (b->pos == NULL) {
|
0
|
2707 ngx_http_close_connection(c);
|
|
2708 return;
|
|
2709 }
|
|
2710
|
|
2711 b->start = b->pos;
|
|
2712 b->last = b->pos;
|
|
2713 b->end = b->pos + size;
|
|
2714 }
|
|
2715
|
|
2716 /*
|
|
2717 * MSIE closes a keepalive connection with RST flag
|
|
2718 * so we ignore ECONNRESET here.
|
|
2719 */
|
|
2720
|
|
2721 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
|
|
2722 ngx_set_socket_errno(0);
|
|
2723
|
|
2724 n = c->recv(c, b->last, size);
|
|
2725 c->log_error = NGX_ERROR_INFO;
|
|
2726
|
|
2727 if (n == NGX_AGAIN) {
|
430
|
2728 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
24
|
2729 ngx_http_close_connection(c);
|
|
2730 }
|
|
2731
|
0
|
2732 return;
|
|
2733 }
|
|
2734
|
|
2735 if (n == NGX_ERROR) {
|
|
2736 ngx_http_close_connection(c);
|
|
2737 return;
|
|
2738 }
|
|
2739
|
32
|
2740 c->log->handler = NULL;
|
0
|
2741
|
|
2742 if (n == 0) {
|
|
2743 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
|
32
|
2744 "client %V closed keepalive connection", &c->addr_text);
|
0
|
2745 ngx_http_close_connection(c);
|
|
2746 return;
|
|
2747 }
|
|
2748
|
|
2749 b->last += n;
|
32
|
2750
|
116
|
2751 #if (NGX_STAT_STUB)
|
502
|
2752 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
116
|
2753 #endif
|
|
2754
|
32
|
2755 c->log->handler = ngx_http_log_error;
|
|
2756 c->log->action = "reading client request line";
|
0
|
2757
|
290
|
2758 c->idle = 0;
|
618
|
2759 ngx_reusable_connection(c, 0);
|
290
|
2760
|
0
|
2761 ngx_http_init_request(rev);
|
|
2762 }
|
|
2763
|
|
2764
|
50
|
2765 static void
|
|
2766 ngx_http_set_lingering_close(ngx_http_request_t *r)
|
126
|
2767 {
|
0
|
2768 ngx_event_t *rev, *wev;
|
|
2769 ngx_connection_t *c;
|
|
2770 ngx_http_core_loc_conf_t *clcf;
|
|
2771
|
|
2772 c = r->connection;
|
|
2773
|
|
2774 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2775
|
|
2776 rev = c->read;
|
58
|
2777 rev->handler = ngx_http_lingering_close_handler;
|
0
|
2778
|
106
|
2779 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
|
0
|
2780 ngx_add_timer(rev, clcf->lingering_timeout);
|
|
2781
|
430
|
2782 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
0
|
2783 ngx_http_close_request(r, 0);
|
|
2784 return;
|
|
2785 }
|
|
2786
|
|
2787 wev = c->write;
|
58
|
2788 wev->handler = ngx_http_empty_handler;
|
0
|
2789
|
132
|
2790 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
430
|
2791 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
|
132
|
2792 ngx_http_close_request(r, 0);
|
|
2793 return;
|
0
|
2794 }
|
|
2795 }
|
|
2796
|
|
2797 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
|
|
2798 ngx_connection_error(c, ngx_socket_errno,
|
|
2799 ngx_shutdown_socket_n " failed");
|
|
2800 ngx_http_close_request(r, 0);
|
|
2801 return;
|
|
2802 }
|
|
2803
|
|
2804 if (rev->ready) {
|
|
2805 ngx_http_lingering_close_handler(rev);
|
|
2806 }
|
|
2807 }
|
|
2808
|
|
2809
|
50
|
2810 static void
|
|
2811 ngx_http_lingering_close_handler(ngx_event_t *rev)
|
0
|
2812 {
|
|
2813 ssize_t n;
|
|
2814 ngx_msec_t timer;
|
|
2815 ngx_connection_t *c;
|
|
2816 ngx_http_request_t *r;
|
|
2817 ngx_http_core_loc_conf_t *clcf;
|
|
2818 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
|
|
2819
|
|
2820 c = rev->data;
|
|
2821 r = c->data;
|
|
2822
|
|
2823 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2824 "http lingering close handler");
|
|
2825
|
|
2826 if (rev->timedout) {
|
|
2827 ngx_http_close_request(r, 0);
|
|
2828 return;
|
|
2829 }
|
|
2830
|
346
|
2831 timer = (ngx_msec_t) (r->lingering_time - ngx_time());
|
0
|
2832 if (timer <= 0) {
|
|
2833 ngx_http_close_request(r, 0);
|
|
2834 return;
|
|
2835 }
|
|
2836
|
|
2837 do {
|
|
2838 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
|
|
2839
|
|
2840 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
|
|
2841
|
|
2842 if (n == NGX_ERROR || n == 0) {
|
|
2843 ngx_http_close_request(r, 0);
|
|
2844 return;
|
|
2845 }
|
|
2846
|
|
2847 } while (rev->ready);
|
|
2848
|
430
|
2849 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
26
|
2850 ngx_http_close_request(r, 0);
|
24
|
2851 return;
|
|
2852 }
|
|
2853
|
0
|
2854 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2855
|
|
2856 timer *= 1000;
|
|
2857
|
|
2858 if (timer > clcf->lingering_timeout) {
|
|
2859 timer = clcf->lingering_timeout;
|
|
2860 }
|
|
2861
|
|
2862 ngx_add_timer(rev, timer);
|
|
2863 }
|
|
2864
|
|
2865
|
50
|
2866 void
|
|
2867 ngx_http_empty_handler(ngx_event_t *wev)
|
0
|
2868 {
|
|
2869 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
|
|
2870
|
|
2871 return;
|
|
2872 }
|
|
2873
|
|
2874
|
58
|
2875 void
|
|
2876 ngx_http_request_empty_handler(ngx_http_request_t *r)
|
|
2877 {
|
|
2878 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
132
|
2879 "http request empty handler");
|
58
|
2880
|
|
2881 return;
|
|
2882 }
|
|
2883
|
|
2884
|
50
|
2885 ngx_int_t
|
130
|
2886 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
|
0
|
2887 {
|
|
2888 ngx_buf_t *b;
|
|
2889 ngx_chain_t out;
|
|
2890
|
50
|
2891 b = ngx_calloc_buf(r->pool);
|
|
2892 if (b == NULL) {
|
0
|
2893 return NGX_ERROR;
|
|
2894 }
|
|
2895
|
130
|
2896 if (flags & NGX_HTTP_LAST) {
|
506
|
2897
|
|
2898 if (r == r->main && !r->post_action) {
|
|
2899 b->last_buf = 1;
|
|
2900
|
|
2901 } else {
|
510
|
2902 b->sync = 1;
|
506
|
2903 b->last_in_chain = 1;
|
|
2904 }
|
130
|
2905 }
|
|
2906
|
|
2907 if (flags & NGX_HTTP_FLUSH) {
|
|
2908 b->flush = 1;
|
|
2909 }
|
|
2910
|
0
|
2911 out.buf = b;
|
|
2912 out.next = NULL;
|
|
2913
|
|
2914 return ngx_http_output_filter(r, &out);
|
|
2915 }
|
|
2916
|
|
2917
|
162
|
2918 static ngx_int_t
|
|
2919 ngx_http_post_action(ngx_http_request_t *r)
|
|
2920 {
|
|
2921 ngx_http_core_loc_conf_t *clcf;
|
|
2922
|
|
2923 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2924
|
|
2925 if (clcf->post_action.data == NULL) {
|
|
2926 return NGX_DECLINED;
|
|
2927 }
|
|
2928
|
172
|
2929 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2930 "post action: \"%V\"", &clcf->post_action);
|
|
2931
|
528
|
2932 r->main->count--;
|
|
2933
|
162
|
2934 r->http_version = NGX_HTTP_VERSION_9;
|
|
2935 r->header_only = 1;
|
178
|
2936 r->post_action = 1;
|
162
|
2937
|
326
|
2938 r->read_event_handler = ngx_http_block_reading;
|
262
|
2939
|
366
|
2940 if (clcf->post_action.data[0] == '/') {
|
|
2941 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
|
|
2942
|
|
2943 } else {
|
|
2944 ngx_http_named_location(r, &clcf->post_action);
|
|
2945 }
|
162
|
2946
|
|
2947 return NGX_OK;
|
|
2948 }
|
|
2949
|
|
2950
|
|
2951 static void
|
518
|
2952 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc)
|
0
|
2953 {
|
188
|
2954 ngx_connection_t *c;
|
118
|
2955
|
518
|
2956 r = r->main;
|
118
|
2957 c = r->connection;
|
188
|
2958
|
518
|
2959 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2960 "http request count:%d blk:%d", r->count, r->blocked);
|
|
2961
|
|
2962 if (r->count == 0) {
|
|
2963 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero");
|
|
2964 }
|
|
2965
|
|
2966 r->count--;
|
|
2967
|
|
2968 if (r->count || r->blocked) {
|
|
2969 return;
|
|
2970 }
|
|
2971
|
|
2972 ngx_http_free_request(r, rc);
|
118
|
2973 ngx_http_close_connection(c);
|
|
2974 }
|
|
2975
|
|
2976
|
162
|
2977 static void
|
518
|
2978 ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc)
|
118
|
2979 {
|
412
|
2980 ngx_log_t *log;
|
|
2981 struct linger linger;
|
|
2982 ngx_http_cleanup_t *cln;
|
|
2983 ngx_http_log_ctx_t *ctx;
|
|
2984 ngx_http_core_loc_conf_t *clcf;
|
0
|
2985
|
|
2986 log = r->connection->log;
|
|
2987
|
|
2988 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
|
|
2989
|
|
2990 if (r->pool == NULL) {
|
80
|
2991 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
|
0
|
2992 return;
|
|
2993 }
|
|
2994
|
188
|
2995 for (cln = r->cleanup; cln; cln = cln->next) {
|
|
2996 if (cln->handler) {
|
|
2997 cln->handler(cln->data);
|
|
2998 }
|
|
2999 }
|
|
3000
|
0
|
3001 #if (NGX_STAT_STUB)
|
116
|
3002
|
0
|
3003 if (r->stat_reading) {
|
502
|
3004 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
0
|
3005 }
|
|
3006
|
|
3007 if (r->stat_writing) {
|
502
|
3008 (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
|
0
|
3009 }
|
116
|
3010
|
0
|
3011 #endif
|
|
3012
|
518
|
3013 if (rc > 0 && (r->headers_out.status == 0 || r->connection->sent == 0)) {
|
|
3014 r->headers_out.status = rc;
|
0
|
3015 }
|
|
3016
|
386
|
3017 log->action = "logging request";
|
|
3018
|
412
|
3019 ngx_http_log_request(r);
|
0
|
3020
|
386
|
3021 log->action = "closing request";
|
|
3022
|
0
|
3023 if (r->connection->timedout) {
|
|
3024 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
3025
|
|
3026 if (clcf->reset_timedout_connection) {
|
58
|
3027 linger.l_onoff = 1;
|
|
3028 linger.l_linger = 0;
|
0
|
3029
|
|
3030 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
|
58
|
3031 (const void *) &linger, sizeof(struct linger)) == -1)
|
0
|
3032 {
|
|
3033 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
|
|
3034 "setsockopt(SO_LINGER) failed");
|
|
3035 }
|
|
3036 }
|
|
3037 }
|
|
3038
|
58
|
3039 /* the various request strings were allocated from r->pool */
|
0
|
3040 ctx = log->data;
|
10
|
3041 ctx->request = NULL;
|
0
|
3042
|
|
3043 r->request_line.len = 0;
|
|
3044
|
132
|
3045 r->connection->destroyed = 1;
|
|
3046
|
0
|
3047 ngx_destroy_pool(r->pool);
|
|
3048 }
|
|
3049
|
|
3050
|
80
|
3051 static void
|
412
|
3052 ngx_http_log_request(ngx_http_request_t *r)
|
|
3053 {
|
|
3054 ngx_uint_t i, n;
|
|
3055 ngx_http_handler_pt *log_handler;
|
|
3056 ngx_http_core_main_conf_t *cmcf;
|
|
3057
|
|
3058 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
3059
|
|
3060 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
|
|
3061 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
|
|
3062
|
|
3063 for (i = 0; i < n; i++) {
|
|
3064 log_handler[i](r);
|
|
3065 }
|
|
3066 }
|
|
3067
|
|
3068
|
|
3069 static void
|
50
|
3070 ngx_http_close_connection(ngx_connection_t *c)
|
0
|
3071 {
|
28
|
3072 ngx_pool_t *pool;
|
|
3073
|
0
|
3074 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
3075 "close http connection: %d", c->fd);
|
|
3076
|
88
|
3077 #if (NGX_HTTP_SSL)
|
2
|
3078
|
|
3079 if (c->ssl) {
|
|
3080 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
3081 c->ssl->handler = ngx_http_close_connection;
|
2
|
3082 return;
|
|
3083 }
|
|
3084 }
|
|
3085
|
|
3086 #endif
|
|
3087
|
0
|
3088 #if (NGX_STAT_STUB)
|
502
|
3089 (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
|
0
|
3090 #endif
|
|
3091
|
132
|
3092 c->destroyed = 1;
|
|
3093
|
28
|
3094 pool = c->pool;
|
|
3095
|
0
|
3096 ngx_close_connection(c);
|
28
|
3097
|
50
|
3098 ngx_destroy_pool(pool);
|
0
|
3099 }
|
|
3100
|
|
3101
|
50
|
3102 static u_char *
|
|
3103 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
0
|
3104 {
|
32
|
3105 u_char *p;
|
54
|
3106 ngx_http_request_t *r;
|
32
|
3107 ngx_http_log_ctx_t *ctx;
|
10
|
3108
|
32
|
3109 if (log->action) {
|
58
|
3110 p = ngx_snprintf(buf, len, " while %s", log->action);
|
10
|
3111 len -= p - buf;
|
54
|
3112 buf = p;
|
10
|
3113 }
|
|
3114
|
58
|
3115 ctx = log->data;
|
|
3116
|
362
|
3117 p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
|
58
|
3118 len -= p - buf;
|
54
|
3119
|
|
3120 r = ctx->request;
|
|
3121
|
58
|
3122 if (r) {
|
214
|
3123 return r->log_handler(r, ctx->current_request, p, len);
|
362
|
3124
|
|
3125 } else {
|
|
3126 p = ngx_snprintf(p, len, ", server: %V",
|
|
3127 &ctx->connection->listening->addr_text);
|
0
|
3128 }
|
10
|
3129
|
58
|
3130 return p;
|
|
3131 }
|
|
3132
|
|
3133
|
|
3134 static u_char *
|
214
|
3135 ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
|
|
3136 u_char *buf, size_t len)
|
58
|
3137 {
|
362
|
3138 char *uri_separator;
|
|
3139 u_char *p;
|
|
3140 ngx_http_upstream_t *u;
|
|
3141 ngx_http_core_srv_conf_t *cscf;
|
|
3142
|
|
3143 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
3144
|
|
3145 p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
|
|
3146 len -= p - buf;
|
|
3147 buf = p;
|
52
|
3148
|
348
|
3149 if (r->request_line.data == NULL && r->request_start) {
|
|
3150 for (p = r->request_start; p < r->header_in->last; p++) {
|
|
3151 if (*p == CR || *p == LF) {
|
|
3152 break;
|
|
3153 }
|
|
3154 }
|
|
3155
|
|
3156 r->request_line.len = p - r->request_start;
|
|
3157 r->request_line.data = r->request_start;
|
|
3158 }
|
|
3159
|
|
3160 if (r->request_line.len) {
|
|
3161 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
|
54
|
3162 len -= p - buf;
|
|
3163 buf = p;
|
10
|
3164 }
|
|
3165
|
214
|
3166 if (r != sr) {
|
|
3167 p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
|
|
3168 len -= p - buf;
|
|
3169 buf = p;
|
|
3170 }
|
|
3171
|
|
3172 u = sr->upstream;
|
|
3173
|
260
|
3174 if (u && u->peer.name) {
|
|
3175
|
|
3176 uri_separator = "";
|
|
3177
|
|
3178 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
3179 if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
|
|
3180 uri_separator = ":";
|
|
3181 }
|
|
3182 #endif
|
214
|
3183
|
|
3184 p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
|
348
|
3185 &u->schema, u->peer.name,
|
260
|
3186 uri_separator, &u->uri);
|
214
|
3187 len -= p - buf;
|
|
3188 buf = p;
|
|
3189 }
|
|
3190
|
54
|
3191 if (r->headers_in.host) {
|
|
3192 p = ngx_snprintf(buf, len, ", host: \"%V\"",
|
|
3193 &r->headers_in.host->value);
|
|
3194 len -= p - buf;
|
|
3195 buf = p;
|
|
3196 }
|
|
3197
|
|
3198 if (r->headers_in.referer) {
|
|
3199 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
|
|
3200 &r->headers_in.referer->value);
|
|
3201 buf = p;
|
|
3202 }
|
|
3203
|
|
3204 return buf;
|
|
3205 }
|