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