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