0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.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);
|
58
|
24 static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
|
|
25 ngx_table_elt_t *h, ngx_uint_t offset);
|
|
26
|
0
|
27 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
|
198
|
28 static void ngx_http_find_virtual_server(ngx_http_request_t *r,
|
|
29 ngx_http_virtual_names_t *vn, ngx_uint_t hash);
|
0
|
30
|
58
|
31 static void ngx_http_request_handler(ngx_event_t *ev);
|
74
|
32 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
|
58
|
33 static void ngx_http_writer(ngx_http_request_t *r);
|
|
34
|
|
35 static void ngx_http_block_read(ngx_http_request_t *r);
|
0
|
36 static void ngx_http_set_keepalive(ngx_http_request_t *r);
|
|
37 static void ngx_http_keepalive_handler(ngx_event_t *ev);
|
|
38 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
|
|
39 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
|
162
|
40 static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
|
80
|
41 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
|
118
|
42 static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
|
80
|
43 static void ngx_http_close_connection(ngx_connection_t *c);
|
0
|
44
|
32
|
45 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
58
|
46 static u_char *ngx_http_log_error_handler(ngx_http_request_t *r, u_char *buf,
|
|
47 size_t len);
|
0
|
48
|
88
|
49 #if (NGX_HTTP_SSL)
|
|
50 static void ngx_http_ssl_handshake(ngx_event_t *rev);
|
96
|
51 static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
|
88
|
52 #endif
|
|
53
|
0
|
54
|
54
|
55 static char *ngx_http_client_errors[] = {
|
|
56
|
|
57 /* NGX_HTTP_PARSE_INVALID_METHOD */
|
|
58 "client sent invalid method",
|
|
59
|
|
60 /* NGX_HTTP_PARSE_INVALID_REQUEST */
|
|
61 "client sent invalid request",
|
|
62
|
|
63 /* NGX_HTTP_PARSE_INVALID_09_METHOD */
|
|
64 "client sent invalid method in HTTP/0.9 request"
|
0
|
65 };
|
|
66
|
|
67
|
|
68 ngx_http_header_t ngx_http_headers_in[] = {
|
58
|
69 { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
|
72
|
70 ngx_http_process_unique_header_line },
|
58
|
71
|
|
72 { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
|
72
|
73 ngx_http_process_unique_header_line },
|
58
|
74
|
0
|
75 { ngx_string("If-Modified-Since"),
|
58
|
76 offsetof(ngx_http_headers_in_t, if_modified_since),
|
|
77 ngx_http_process_header_line },
|
|
78
|
|
79 { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
|
|
80 ngx_http_process_header_line },
|
|
81
|
|
82 { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
|
|
83 ngx_http_process_header_line },
|
|
84
|
0
|
85 { ngx_string("Content-Length"),
|
58
|
86 offsetof(ngx_http_headers_in_t, content_length),
|
72
|
87 ngx_http_process_unique_header_line },
|
58
|
88
|
28
|
89 { ngx_string("Content-Type"),
|
58
|
90 offsetof(ngx_http_headers_in_t, content_type),
|
|
91 ngx_http_process_header_line },
|
|
92
|
|
93 { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
|
|
94 ngx_http_process_header_line },
|
|
95
|
130
|
96 { ngx_string("Transfer-Encoding"),
|
|
97 offsetof(ngx_http_headers_in_t, transfer_encoding),
|
|
98 ngx_http_process_header_line },
|
|
99
|
0
|
100 #if (NGX_HTTP_GZIP)
|
|
101 { ngx_string("Accept-Encoding"),
|
58
|
102 offsetof(ngx_http_headers_in_t, accept_encoding),
|
|
103 ngx_http_process_header_line },
|
|
104
|
|
105 { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
|
|
106 ngx_http_process_header_line },
|
0
|
107 #endif
|
|
108
|
|
109 { ngx_string("Authorization"),
|
58
|
110 offsetof(ngx_http_headers_in_t, authorization),
|
72
|
111 ngx_http_process_unique_header_line },
|
58
|
112
|
|
113 { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
|
|
114 ngx_http_process_header_line },
|
0
|
115
|
122
|
116 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
|
0
|
117 { ngx_string("X-Forwarded-For"),
|
58
|
118 offsetof(ngx_http_headers_in_t, x_forwarded_for),
|
|
119 ngx_http_process_header_line },
|
0
|
120 #endif
|
28
|
121
|
122
|
122 #if (NGX_HTTP_REALIP)
|
|
123 { ngx_string("X-Real-IP"),
|
|
124 offsetof(ngx_http_headers_in_t, x_real_ip),
|
|
125 ngx_http_process_header_line },
|
|
126 #endif
|
|
127
|
38
|
128 #if (NGX_HTTP_HEADERS)
|
58
|
129 { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
|
|
130 ngx_http_process_header_line },
|
|
131
|
38
|
132 { ngx_string("Accept-Language"),
|
58
|
133 offsetof(ngx_http_headers_in_t, accept_language),
|
|
134 ngx_http_process_header_line },
|
38
|
135 #endif
|
|
136
|
186
|
137 #if (NGX_HTTP_DAV)
|
|
138 { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
|
|
139 ngx_http_process_header_line },
|
|
140
|
|
141 { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
|
|
142 ngx_http_process_header_line },
|
|
143 #endif
|
|
144
|
58
|
145 { ngx_string("Cookie"), 0, ngx_http_process_cookie },
|
|
146
|
|
147 { ngx_null_string, 0, NULL }
|
0
|
148 };
|
|
149
|
|
150
|
50
|
151 void
|
|
152 ngx_http_init_connection(ngx_connection_t *c)
|
0
|
153 {
|
|
154 ngx_event_t *rev;
|
|
155 ngx_http_log_ctx_t *ctx;
|
|
156
|
50
|
157 ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
|
|
158 if (ctx == NULL) {
|
0
|
159 ngx_http_close_connection(c);
|
|
160 return;
|
|
161 }
|
|
162
|
10
|
163 ctx->client = &c->addr_text;
|
|
164 ctx->request = NULL;
|
32
|
165
|
|
166 c->log->connection = c->number;
|
|
167 c->log->handler = ngx_http_log_error;
|
0
|
168 c->log->data = ctx;
|
32
|
169 c->log->action = "reading client request line";
|
|
170
|
0
|
171 c->log_error = NGX_ERROR_INFO;
|
|
172
|
|
173 rev = c->read;
|
58
|
174 rev->handler = ngx_http_init_request;
|
118
|
175 c->write->handler = ngx_http_empty_handler;
|
0
|
176
|
116
|
177 #if (NGX_STAT_STUB)
|
|
178 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
|
179 #endif
|
|
180
|
0
|
181 if (rev->ready) {
|
|
182 /* the deferred accept(), rtsig, aio, iocp */
|
|
183
|
160
|
184 if (ngx_use_accept_mutex) {
|
112
|
185 ngx_post_event(rev, &ngx_posted_events);
|
0
|
186 return;
|
|
187 }
|
|
188
|
|
189 ngx_http_init_request(rev);
|
|
190 return;
|
|
191 }
|
|
192
|
|
193 ngx_add_timer(rev, c->listening->post_accept_timeout);
|
|
194
|
|
195 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
116
|
196 #if (NGX_STAT_STUB)
|
|
197 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
|
198 #endif
|
0
|
199 ngx_http_close_connection(c);
|
|
200 return;
|
|
201 }
|
|
202 }
|
|
203
|
|
204
|
190
|
205 static void
|
|
206 ngx_http_init_request(ngx_event_t *rev)
|
0
|
207 {
|
190
|
208 socklen_t len;
|
122
|
209 ngx_uint_t i;
|
|
210 struct sockaddr_in sin;
|
|
211 ngx_connection_t *c;
|
|
212 ngx_http_request_t *r;
|
142
|
213 ngx_http_in_port_t *hip;
|
|
214 ngx_http_in_addr_t *hia;
|
122
|
215 ngx_http_log_ctx_t *ctx;
|
|
216 ngx_http_connection_t *hc;
|
|
217 ngx_http_core_srv_conf_t *cscf;
|
|
218 ngx_http_core_loc_conf_t *clcf;
|
|
219 ngx_http_core_main_conf_t *cmcf;
|
0
|
220 #if (NGX_HTTP_SSL)
|
122
|
221 ngx_http_ssl_srv_conf_t *sscf;
|
0
|
222 #endif
|
|
223
|
116
|
224 #if (NGX_STAT_STUB)
|
|
225 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
|
226 #endif
|
|
227
|
0
|
228 c = rev->data;
|
|
229
|
|
230 if (rev->timedout) {
|
|
231 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
232
|
|
233 ngx_http_close_connection(c);
|
|
234 return;
|
|
235 }
|
|
236
|
|
237 hc = c->data;
|
|
238
|
116
|
239 if (hc == NULL) {
|
50
|
240 hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
|
|
241 if (hc == NULL) {
|
0
|
242 ngx_http_close_connection(c);
|
|
243 return;
|
|
244 }
|
|
245 }
|
|
246
|
|
247 r = hc->request;
|
|
248
|
|
249 if (r) {
|
|
250 ngx_memzero(r, sizeof(ngx_http_request_t));
|
|
251
|
|
252 r->pipeline = hc->pipeline;
|
|
253
|
|
254 if (hc->nbusy) {
|
|
255 r->header_in = hc->busy[0];
|
|
256 }
|
|
257
|
|
258 } else {
|
50
|
259 r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
|
|
260 if (r == NULL) {
|
0
|
261 ngx_http_close_connection(c);
|
|
262 return;
|
|
263 }
|
|
264
|
|
265 hc->request = r;
|
|
266 }
|
|
267
|
|
268 c->data = r;
|
|
269 r->http_connection = hc;
|
|
270
|
|
271 c->sent = 0;
|
|
272 r->signature = NGX_HTTP_MODULE;
|
|
273
|
|
274 /* find the server configuration for the address:port */
|
|
275
|
|
276 /* AF_INET only */
|
|
277
|
190
|
278 hip = c->listening->servers;
|
142
|
279 hia = hip->addrs;
|
|
280
|
|
281 r->port = hip->port;
|
|
282 r->port_text = &hip->port_text;
|
0
|
283
|
|
284 i = 0;
|
|
285
|
142
|
286 if (hip->naddrs > 1) {
|
0
|
287
|
|
288 /*
|
|
289 * There are several addresses on this port and one of them
|
|
290 * is the "*:port" wildcard so getsockname() is needed to determine
|
|
291 * the server address.
|
|
292 *
|
|
293 * AcceptEx() already gave this address.
|
|
294 */
|
|
295
|
10
|
296 #if (NGX_WIN32)
|
0
|
297 if (c->local_sockaddr) {
|
|
298 r->in_addr =
|
|
299 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
|
|
300
|
28
|
301 } else
|
0
|
302 #endif
|
28
|
303 {
|
0
|
304 len = sizeof(struct sockaddr_in);
|
28
|
305 if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
|
0
|
306 ngx_connection_error(c, ngx_socket_errno,
|
|
307 "getsockname() failed");
|
|
308 ngx_http_close_connection(c);
|
|
309 return;
|
|
310 }
|
|
311
|
28
|
312 r->in_addr = sin.sin_addr.s_addr;
|
0
|
313 }
|
|
314
|
190
|
315 /* the last address is "*" */
|
0
|
316
|
142
|
317 for ( /* void */ ; i < hip->naddrs - 1; i++) {
|
|
318 if (hia[i].addr == r->in_addr) {
|
0
|
319 break;
|
|
320 }
|
|
321 }
|
|
322
|
|
323 } else {
|
142
|
324 r->in_addr = hia[0].addr;
|
0
|
325 }
|
|
326
|
142
|
327 r->virtual_names = hia[i].virtual_names;
|
0
|
328
|
|
329 /* the default server configuration for the address:port */
|
142
|
330 cscf = hia[i].core_srv_conf;
|
0
|
331
|
|
332 r->main_conf = cscf->ctx->main_conf;
|
|
333 r->srv_conf = cscf->ctx->srv_conf;
|
|
334 r->loc_conf = cscf->ctx->loc_conf;
|
|
335
|
142
|
336 r->server_name = cscf->server_name;
|
|
337
|
58
|
338 rev->handler = ngx_http_process_request_line;
|
0
|
339
|
|
340 #if (NGX_HTTP_SSL)
|
|
341
|
|
342 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
|
|
343 if (sscf->enable) {
|
|
344
|
|
345 if (c->ssl == NULL) {
|
96
|
346 if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
|
56
|
347 == NGX_ERROR)
|
0
|
348 {
|
|
349 ngx_http_close_connection(c);
|
|
350 return;
|
|
351 }
|
|
352
|
58
|
353 rev->handler = ngx_http_ssl_handshake;
|
0
|
354 }
|
|
355
|
62
|
356 r->main_filter_need_in_memory = 1;
|
0
|
357 }
|
|
358
|
|
359 #endif
|
|
360
|
|
361 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
362 c->log->file = clcf->err_log->file;
|
|
363 if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
|
364 c->log->log_level = clcf->err_log->log_level;
|
|
365 }
|
|
366
|
|
367 if (c->buffer == NULL) {
|
|
368 c->buffer = ngx_create_temp_buf(c->pool,
|
|
369 cscf->client_header_buffer_size);
|
|
370 if (c->buffer == NULL) {
|
|
371 ngx_http_close_connection(c);
|
|
372 return;
|
|
373 }
|
|
374 }
|
|
375
|
|
376 if (r->header_in == NULL) {
|
|
377 r->header_in = c->buffer;
|
|
378 }
|
|
379
|
50
|
380 r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
|
|
381 if (r->pool == NULL) {
|
0
|
382 ngx_http_close_connection(c);
|
|
383 return;
|
|
384 }
|
|
385
|
|
386
|
|
387 if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
|
130
|
388 sizeof(ngx_table_elt_t))
|
|
389 == NGX_ERROR)
|
0
|
390 {
|
|
391 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
392 return;
|
|
393 }
|
|
394
|
|
395 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
|
|
396 if (r->ctx == NULL) {
|
|
397 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
398 return;
|
|
399 }
|
|
400
|
122
|
401 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
402
|
|
403 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
|
|
404 * sizeof(ngx_http_variable_value_t));
|
|
405 if (r->variables == NULL) {
|
|
406 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
407 return;
|
|
408 }
|
|
409
|
0
|
410 c->single_connection = 1;
|
132
|
411 c->destroyed = 0;
|
|
412
|
0
|
413 r->connection = c;
|
|
414
|
102
|
415 r->main = r;
|
|
416
|
46
|
417 r->start_time = ngx_time();
|
|
418
|
180
|
419 r->method = NGX_HTTP_UNKNOWN;
|
|
420
|
0
|
421 r->headers_in.content_length_n = -1;
|
|
422 r->headers_in.keep_alive_n = -1;
|
|
423 r->headers_out.content_length_n = -1;
|
|
424 r->headers_out.last_modified_time = -1;
|
|
425
|
|
426 r->http_state = NGX_HTTP_READING_REQUEST_STATE;
|
|
427
|
54
|
428 ctx = c->log->data;
|
|
429 ctx->request = r;
|
58
|
430 r->log_handler = ngx_http_log_error_handler;
|
54
|
431
|
0
|
432 #if (NGX_STAT_STUB)
|
110
|
433 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
26
|
434 r->stat_reading = 1;
|
110
|
435 ngx_atomic_fetch_add(ngx_stat_requests, 1);
|
0
|
436 #endif
|
|
437
|
58
|
438 rev->handler(rev);
|
0
|
439 }
|
|
440
|
|
441
|
|
442 #if (NGX_HTTP_SSL)
|
|
443
|
50
|
444 static void
|
|
445 ngx_http_ssl_handshake(ngx_event_t *rev)
|
0
|
446 {
|
10
|
447 u_char buf[1];
|
|
448 ssize_t n;
|
0
|
449 ngx_int_t rc;
|
|
450 ngx_connection_t *c;
|
|
451 ngx_http_request_t *r;
|
|
452
|
|
453 c = rev->data;
|
|
454 r = c->data;
|
|
455
|
|
456 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
457 "http check ssl handshake");
|
|
458
|
|
459 if (rev->timedout) {
|
54
|
460 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
461 c->timedout = 1;
|
|
462 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
463 return;
|
|
464 }
|
|
465
|
132
|
466 n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
|
0
|
467
|
|
468 if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
|
|
469 return;
|
|
470 }
|
|
471
|
|
472 if (n == 1) {
|
|
473 if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
|
|
474 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
10
|
475 "https ssl handshake: 0x%02Xd", buf[0]);
|
0
|
476
|
|
477 rc = ngx_ssl_handshake(c);
|
|
478
|
96
|
479 if (rc == NGX_AGAIN) {
|
|
480 c->ssl->handler = ngx_http_ssl_handshake_handler;
|
0
|
481 return;
|
|
482 }
|
|
483
|
96
|
484 ngx_http_ssl_handshake_handler(c);
|
|
485
|
|
486 return;
|
0
|
487
|
|
488 } else {
|
|
489 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
490 "plain http");
|
|
491
|
|
492 r->plain_http = 1;
|
|
493 }
|
|
494 }
|
|
495
|
58
|
496 rev->handler = ngx_http_process_request_line;
|
0
|
497 ngx_http_process_request_line(rev);
|
|
498 }
|
|
499
|
96
|
500
|
|
501 static void
|
|
502 ngx_http_ssl_handshake_handler(ngx_connection_t *c)
|
|
503 {
|
|
504 ngx_http_request_t *r;
|
|
505
|
|
506 if (c->ssl->handshaked) {
|
|
507
|
|
508 /*
|
|
509 * The majority of browsers do not send the "close notify" alert.
|
|
510 * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
|
|
511 * and Links. And what is more, MSIE ignores the server's alert.
|
|
512 *
|
|
513 * Opera and recent Mozilla send the alert.
|
|
514 */
|
|
515
|
|
516 c->ssl->no_wait_shutdown = 1;
|
|
517
|
|
518 c->read->handler = ngx_http_process_request_line;
|
|
519 /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
|
|
520
|
|
521 ngx_http_process_request_line(c->read);
|
|
522
|
|
523 return;
|
|
524 }
|
|
525
|
|
526 r = c->data;
|
|
527
|
|
528 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
|
|
529
|
|
530 return;
|
|
531 }
|
|
532
|
|
533
|
0
|
534 #endif
|
|
535
|
|
536
|
50
|
537 static void
|
|
538 ngx_http_process_request_line(ngx_event_t *rev)
|
0
|
539 {
|
|
540 ssize_t n;
|
|
541 ngx_int_t rc, rv;
|
|
542 ngx_connection_t *c;
|
|
543 ngx_http_request_t *r;
|
|
544 ngx_http_log_ctx_t *ctx;
|
|
545
|
|
546 c = rev->data;
|
|
547 r = c->data;
|
|
548
|
|
549 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
550 "http process request line");
|
|
551
|
|
552 if (rev->timedout) {
|
54
|
553 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
554 c->timedout = 1;
|
|
555 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
556 return;
|
|
557 }
|
|
558
|
|
559 rc = NGX_AGAIN;
|
|
560
|
|
561 for ( ;; ) {
|
|
562
|
|
563 if (rc == NGX_AGAIN) {
|
|
564 n = ngx_http_read_request_header(r);
|
|
565
|
|
566 if (n == NGX_AGAIN || n == NGX_ERROR) {
|
|
567 return;
|
|
568 }
|
|
569 }
|
|
570
|
|
571 rc = ngx_http_parse_request_line(r, r->header_in);
|
|
572
|
|
573 if (rc == NGX_OK) {
|
|
574
|
|
575 /* the request line has been parsed successfully */
|
|
576
|
10
|
577 r->request_line.len = r->request_end - r->request_start;
|
|
578 r->request_line.data = r->request_start;
|
|
579
|
0
|
580
|
|
581 if (r->args_start) {
|
|
582 r->uri.len = r->args_start - 1 - r->uri_start;
|
|
583 } else {
|
|
584 r->uri.len = r->uri_end - r->uri_start;
|
|
585 }
|
|
586
|
18
|
587
|
2
|
588 if (r->complex_uri || r->quoted_uri) {
|
10
|
589
|
50
|
590 r->uri.data = ngx_palloc(r->pool, r->uri.len + 1);
|
|
591 if (r->uri.data == NULL) {
|
10
|
592 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
593 return;
|
|
594 }
|
|
595
|
0
|
596 rc = ngx_http_parse_complex_uri(r);
|
|
597
|
54
|
598 if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
|
|
599 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
600 "client sent invalid request");
|
|
601 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
602 return;
|
|
603 }
|
|
604
|
|
605 } else {
|
10
|
606 r->uri.data = r->uri_start;
|
0
|
607 }
|
|
608
|
18
|
609
|
10
|
610 r->unparsed_uri.len = r->uri_end - r->uri_start;
|
|
611 r->unparsed_uri.data = r->uri_start;
|
|
612
|
0
|
613
|
58
|
614 r->method_name.len = r->method_end - r->request_start + 1;
|
|
615 r->method_name.data = r->request_line.data;
|
0
|
616
|
10
|
617
|
28
|
618 if (r->http_protocol.data) {
|
|
619 r->http_protocol.len = r->request_end - r->http_protocol.data;
|
|
620 }
|
|
621
|
|
622
|
0
|
623 if (r->uri_ext) {
|
|
624 if (r->args_start) {
|
|
625 r->exten.len = r->args_start - 1 - r->uri_ext;
|
|
626 } else {
|
|
627 r->exten.len = r->uri_end - r->uri_ext;
|
|
628 }
|
|
629
|
10
|
630 r->exten.data = r->uri_ext;
|
0
|
631 }
|
|
632
|
10
|
633
|
0
|
634 if (r->args_start && r->uri_end > r->args_start) {
|
|
635 r->args.len = r->uri_end - r->args_start;
|
10
|
636 r->args.data = r->args_start;
|
0
|
637 }
|
|
638
|
10
|
639
|
|
640 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
641 "http request line: \"%V\"", &r->request_line);
|
|
642
|
0
|
643 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
644 "http uri: \"%V\"", &r->uri);
|
0
|
645
|
|
646 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
647 "http args: \"%V\"", &r->args);
|
0
|
648
|
|
649 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
650 "http exten: \"%V\"", &r->exten);
|
|
651
|
0
|
652 if (r->http_version < NGX_HTTP_VERSION_10) {
|
86
|
653
|
|
654 if (rev->timer_set) {
|
|
655 ngx_del_timer(rev);
|
|
656 }
|
|
657
|
|
658 #if (NGX_STAT_STUB)
|
110
|
659 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
86
|
660 r->stat_reading = 0;
|
110
|
661 ngx_atomic_fetch_add(ngx_stat_writing, 1);
|
86
|
662 r->stat_writing = 1;
|
|
663 #endif
|
|
664
|
|
665 rev->handler = ngx_http_request_handler;
|
|
666 c->write->handler = ngx_http_request_handler;
|
58
|
667 r->read_event_handler = ngx_http_block_read;
|
86
|
668
|
200
|
669 r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
|
|
670
|
0
|
671 ngx_http_handler(r);
|
86
|
672
|
0
|
673 return;
|
|
674 }
|
|
675
|
|
676
|
|
677 if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
|
130
|
678 sizeof(ngx_table_elt_t))
|
|
679 == NGX_ERROR)
|
0
|
680 {
|
|
681 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
682 return;
|
|
683 }
|
|
684
|
|
685
|
48
|
686 if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
|
130
|
687 sizeof(ngx_table_elt_t *))
|
|
688 == NGX_ERROR)
|
0
|
689 {
|
|
690 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
691 return;
|
|
692 }
|
|
693
|
32
|
694 c->log->action = "reading client request headers";
|
0
|
695
|
58
|
696 rev->handler = ngx_http_process_request_headers;
|
0
|
697 ngx_http_process_request_headers(rev);
|
|
698
|
|
699 return;
|
54
|
700 }
|
|
701
|
|
702 if (rc != NGX_AGAIN) {
|
0
|
703
|
|
704 /* there was error while a request line parsing */
|
|
705
|
54
|
706 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
707 ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
|
|
708 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
709 return;
|
|
710 }
|
|
711
|
|
712 /* NGX_AGAIN: a request line parsing is still incomplete */
|
|
713
|
|
714 if (r->header_in->pos == r->header_in->end) {
|
|
715
|
|
716 rv = ngx_http_alloc_large_header_buffer(r, 1);
|
|
717
|
|
718 if (rv == NGX_ERROR) {
|
|
719 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
720 return;
|
|
721 }
|
|
722
|
|
723 if (rv == NGX_DECLINED) {
|
54
|
724 ctx = c->log->data;
|
|
725 ctx->request = r;
|
|
726
|
|
727 r->request_line.len = r->header_in->end - r->request_start;
|
|
728 r->request_line.data = r->request_start;
|
|
729
|
|
730 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
731 "client sent too long URI");
|
|
732 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
|
0
|
733 return;
|
|
734 }
|
|
735 }
|
|
736 }
|
|
737 }
|
|
738
|
|
739
|
50
|
740 static void
|
|
741 ngx_http_process_request_headers(ngx_event_t *rev)
|
0
|
742 {
|
56
|
743 ssize_t n;
|
|
744 ngx_int_t rc, rv;
|
|
745 ngx_str_t header;
|
198
|
746 ngx_uint_t i;
|
58
|
747 ngx_table_elt_t *h;
|
56
|
748 ngx_connection_t *c;
|
|
749 ngx_http_header_t *hh;
|
|
750 ngx_http_request_t *r;
|
60
|
751 ngx_http_core_srv_conf_t *cscf;
|
56
|
752 ngx_http_core_main_conf_t *cmcf;
|
0
|
753
|
|
754 c = rev->data;
|
|
755 r = c->data;
|
|
756
|
|
757 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
758 "http process request header line");
|
|
759
|
|
760 if (rev->timedout) {
|
54
|
761 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
762 c->timedout = 1;
|
|
763 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
764 return;
|
|
765 }
|
|
766
|
56
|
767 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
60
|
768 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
56
|
769
|
0
|
770 rc = NGX_AGAIN;
|
|
771
|
|
772 for ( ;; ) {
|
|
773
|
|
774 if (rc == NGX_AGAIN) {
|
|
775
|
|
776 if (r->header_in->pos == r->header_in->end) {
|
|
777
|
|
778 rv = ngx_http_alloc_large_header_buffer(r, 0);
|
|
779
|
|
780 if (rv == NGX_ERROR) {
|
|
781 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
782 return;
|
|
783 }
|
|
784
|
|
785 if (rv == NGX_DECLINED) {
|
54
|
786 header.len = r->header_in->end - r->header_name_start;
|
|
787 header.data = r->header_name_start;
|
80
|
788
|
|
789 if (header.len > NGX_MAX_ERROR_STR - 300) {
|
|
790 header.len = NGX_MAX_ERROR_STR - 300;
|
|
791 header.data[header.len++] = '.';
|
|
792 header.data[header.len++] = '.';
|
|
793 header.data[header.len++] = '.';
|
|
794 }
|
|
795
|
54
|
796 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
797 "client sent too long header line: \"%V\"",
|
|
798 &header);
|
|
799 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
800 return;
|
|
801 }
|
|
802 }
|
|
803
|
|
804 n = ngx_http_read_request_header(r);
|
|
805
|
|
806 if (n == NGX_AGAIN || n == NGX_ERROR) {
|
|
807 return;
|
|
808 }
|
|
809 }
|
|
810
|
|
811 rc = ngx_http_parse_header_line(r, r->header_in);
|
|
812
|
|
813 if (rc == NGX_OK) {
|
|
814
|
60
|
815 if (r->invalid_header && cscf->ignore_invalid_headers) {
|
52
|
816
|
|
817 /* there was error while a header line parsing */
|
|
818
|
|
819 header.len = r->header_end - r->header_name_start;
|
|
820 header.data = r->header_name_start;
|
|
821
|
54
|
822 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
823 "client sent invalid header line: \"%V\"",
|
52
|
824 &header);
|
|
825 continue;
|
|
826 }
|
|
827
|
0
|
828 /* a header line has been parsed successfully */
|
|
829
|
50
|
830 h = ngx_list_push(&r->headers_in.headers);
|
|
831 if (h == NULL) {
|
0
|
832 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
833 return;
|
|
834 }
|
|
835
|
56
|
836 h->hash = r->header_hash;
|
|
837
|
0
|
838 h->key.len = r->header_name_end - r->header_name_start;
|
|
839 h->key.data = r->header_name_start;
|
|
840 h->key.data[h->key.len] = '\0';
|
|
841
|
|
842 h->value.len = r->header_end - r->header_start;
|
|
843 h->value.data = r->header_start;
|
|
844 h->value.data[h->value.len] = '\0';
|
|
845
|
198
|
846 h->lowcase_key = ngx_palloc(r->pool, h->key.len);
|
|
847 if (h->lowcase_key == NULL) {
|
|
848 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
849 return;
|
|
850 }
|
|
851
|
|
852 if (h->key.len == r->lowcase_index) {
|
|
853 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
854
|
|
855 } else {
|
|
856 for (i = 0; i < h->key.len; i++) {
|
|
857 h->lowcase_key[i] = ngx_tolower(h->lowcase_key[i]);
|
0
|
858 }
|
|
859 }
|
|
860
|
198
|
861 hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
|
|
862 h->lowcase_key, h->key.len);
|
|
863
|
|
864 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
|
865 return;
|
|
866 }
|
|
867
|
0
|
868 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
869 "http header: \"%V: %V\"",
|
|
870 &h->key, &h->value);
|
0
|
871
|
|
872 continue;
|
54
|
873 }
|
|
874
|
|
875 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
0
|
876
|
|
877 /* a whole header has been parsed successfully */
|
|
878
|
|
879 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
880 "http header done");
|
|
881
|
24
|
882 r->request_length += r->header_in->pos - r->header_in->start;
|
|
883
|
0
|
884 r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
|
|
885
|
|
886 rc = ngx_http_process_request_header(r);
|
|
887
|
|
888 if (rc != NGX_OK) {
|
|
889 return;
|
|
890 }
|
|
891
|
|
892 if (rev->timer_set) {
|
|
893 ngx_del_timer(rev);
|
|
894 }
|
|
895
|
|
896 #if (NGX_STAT_STUB)
|
110
|
897 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
0
|
898 r->stat_reading = 0;
|
110
|
899 ngx_atomic_fetch_add(ngx_stat_writing, 1);
|
0
|
900 r->stat_writing = 1;
|
|
901 #endif
|
|
902
|
58
|
903 rev->handler = ngx_http_request_handler;
|
|
904 c->write->handler = ngx_http_request_handler;
|
|
905 r->read_event_handler = ngx_http_block_read;
|
|
906
|
0
|
907 ngx_http_handler(r);
|
86
|
908
|
0
|
909 return;
|
54
|
910 }
|
|
911
|
|
912 if (rc == NGX_AGAIN) {
|
|
913
|
|
914 /* a header line parsing is still not complete */
|
|
915
|
|
916 continue;
|
0
|
917 }
|
|
918
|
54
|
919 /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
|
|
920
|
|
921 header.len = r->header_end - r->header_name_start;
|
|
922 header.data = r->header_name_start;
|
|
923 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
924 "client sent invalid header line: \"%V\\r...\"",
|
|
925 &header);
|
|
926 ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
|
|
927 return;
|
0
|
928 }
|
|
929 }
|
|
930
|
|
931
|
50
|
932 static ssize_t
|
|
933 ngx_http_read_request_header(ngx_http_request_t *r)
|
0
|
934 {
|
|
935 ssize_t n;
|
|
936 ngx_event_t *rev;
|
|
937 ngx_http_core_srv_conf_t *cscf;
|
|
938
|
|
939 rev = r->connection->read;
|
|
940
|
|
941 n = r->header_in->last - r->header_in->pos;
|
|
942
|
|
943 if (n > 0) {
|
|
944 return n;
|
|
945 }
|
|
946
|
72
|
947 if (rev->ready) {
|
|
948 n = r->connection->recv(r->connection, r->header_in->last,
|
|
949 r->header_in->end - r->header_in->last);
|
|
950 } else {
|
|
951 n = NGX_AGAIN;
|
0
|
952 }
|
|
953
|
|
954 if (n == NGX_AGAIN) {
|
|
955 if (!r->header_timeout_set) {
|
|
956 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
957 ngx_add_timer(rev, cscf->client_header_timeout);
|
|
958 r->header_timeout_set = 1;
|
|
959 }
|
|
960
|
|
961 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
962 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
963 return NGX_ERROR;
|
|
964 }
|
|
965
|
|
966 return NGX_AGAIN;
|
|
967 }
|
|
968
|
|
969 if (n == 0) {
|
|
970 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
971 "client closed prematurely connection");
|
|
972 }
|
|
973
|
|
974 if (n == 0 || n == NGX_ERROR) {
|
194
|
975 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
0
|
976 return NGX_ERROR;
|
|
977 }
|
|
978
|
|
979 r->header_in->last += n;
|
|
980
|
|
981 return n;
|
|
982 }
|
|
983
|
|
984
|
50
|
985 static ngx_int_t
|
|
986 ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
|
|
987 ngx_uint_t request_line)
|
0
|
988 {
|
|
989 u_char *old, *new;
|
|
990 ngx_buf_t *b;
|
|
991 ngx_http_connection_t *hc;
|
|
992 ngx_http_core_srv_conf_t *cscf;
|
|
993
|
|
994 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
995 "http alloc large header buffer");
|
|
996
|
|
997 if (request_line && r->state == 0) {
|
|
998
|
|
999 /* the client fills up the buffer with "\r\n" */
|
|
1000
|
24
|
1001 r->request_length += r->header_in->end - r->header_in->start;
|
|
1002
|
0
|
1003 r->header_in->pos = r->header_in->start;
|
|
1004 r->header_in->last = r->header_in->start;
|
|
1005
|
|
1006 return NGX_OK;
|
|
1007 }
|
|
1008
|
|
1009 old = request_line ? r->request_start : r->header_name_start;
|
|
1010
|
|
1011 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1012
|
|
1013 if (r->state != 0
|
|
1014 && (size_t) (r->header_in->pos - old)
|
|
1015 >= cscf->large_client_header_buffers.size)
|
|
1016 {
|
|
1017 return NGX_DECLINED;
|
|
1018 }
|
|
1019
|
|
1020 hc = r->http_connection;
|
|
1021
|
|
1022 if (hc->nfree) {
|
|
1023 b = hc->free[--hc->nfree];
|
|
1024
|
|
1025 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1026 "http large header free: %p %uz",
|
0
|
1027 b->pos, b->end - b->last);
|
|
1028
|
|
1029 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
|
|
1030
|
|
1031 if (hc->busy == NULL) {
|
|
1032 hc->busy = ngx_palloc(r->connection->pool,
|
|
1033 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
1034 if (hc->busy == NULL) {
|
|
1035 return NGX_ERROR;
|
|
1036 }
|
|
1037 }
|
|
1038
|
|
1039 b = ngx_create_temp_buf(r->connection->pool,
|
|
1040 cscf->large_client_header_buffers.size);
|
|
1041 if (b == NULL) {
|
|
1042 return NGX_ERROR;
|
|
1043 }
|
|
1044
|
|
1045 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
1046 "http large header alloc: %p %uz",
|
0
|
1047 b->pos, b->end - b->last);
|
|
1048
|
|
1049 } else {
|
|
1050 return NGX_DECLINED;
|
|
1051 }
|
|
1052
|
|
1053 hc->busy[hc->nbusy++] = b;
|
|
1054
|
|
1055 if (r->state == 0) {
|
|
1056 /*
|
|
1057 * r->state == 0 means that a header line was parsed successfully
|
|
1058 * and we do not need to copy incomplete header line and
|
|
1059 * to relocate the parser header pointers
|
|
1060 */
|
|
1061
|
24
|
1062 r->request_length += r->header_in->end - r->header_in->start;
|
|
1063
|
0
|
1064 r->header_in = b;
|
|
1065
|
|
1066 return NGX_OK;
|
|
1067 }
|
|
1068
|
|
1069 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1070 "http large header copy: %d", r->header_in->pos - old);
|
|
1071
|
24
|
1072 r->request_length += old - r->header_in->start;
|
|
1073
|
0
|
1074 new = b->start;
|
|
1075
|
|
1076 ngx_memcpy(new, old, r->header_in->pos - old);
|
|
1077
|
|
1078 b->pos = new + (r->header_in->pos - old);
|
|
1079 b->last = new + (r->header_in->pos - old);
|
|
1080
|
|
1081 if (request_line) {
|
|
1082 r->request_start = new;
|
|
1083
|
|
1084 if (r->request_end) {
|
|
1085 r->request_end = new + (r->request_end - old);
|
|
1086 }
|
|
1087
|
|
1088 r->method_end = new + (r->method_end - old);
|
|
1089
|
|
1090 r->uri_start = new + (r->uri_start - old);
|
|
1091 r->uri_end = new + (r->uri_end - old);
|
|
1092
|
|
1093 if (r->schema_start) {
|
|
1094 r->schema_start = new + (r->schema_start - old);
|
|
1095 r->schema_end = new + (r->schema_end - old);
|
|
1096 }
|
|
1097
|
|
1098 if (r->host_start) {
|
|
1099 r->host_start = new + (r->host_start - old);
|
|
1100 r->host_end = new + (r->host_end - old);
|
|
1101 }
|
|
1102
|
|
1103 if (r->port_start) {
|
|
1104 r->port_start = new + (r->port_start - old);
|
|
1105 r->port_end = new + (r->port_end - old);
|
|
1106 }
|
|
1107
|
|
1108 if (r->uri_ext) {
|
|
1109 r->uri_ext = new + (r->uri_ext - old);
|
|
1110 }
|
|
1111
|
|
1112 if (r->args_start) {
|
|
1113 r->args_start = new + (r->args_start - old);
|
|
1114 }
|
|
1115
|
|
1116 } else {
|
|
1117 r->header_name_start = new;
|
|
1118 r->header_name_end = new + (r->header_name_end - old);
|
|
1119 r->header_start = new + (r->header_start - old);
|
|
1120 r->header_end = new + (r->header_end - old);
|
|
1121 }
|
|
1122
|
|
1123 r->header_in = b;
|
|
1124
|
|
1125 return NGX_OK;
|
|
1126 }
|
|
1127
|
|
1128
|
50
|
1129 static ngx_int_t
|
58
|
1130 ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1131 ngx_uint_t offset)
|
|
1132 {
|
|
1133 ngx_table_elt_t **ph;
|
|
1134
|
|
1135 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
|
|
1136
|
|
1137 if (*ph == NULL) {
|
|
1138 *ph = h;
|
|
1139 }
|
|
1140
|
|
1141 return NGX_OK;
|
|
1142 }
|
|
1143
|
|
1144
|
|
1145 static ngx_int_t
|
72
|
1146 ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1147 ngx_uint_t offset)
|
|
1148 {
|
|
1149 ngx_table_elt_t **ph;
|
|
1150
|
|
1151 ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
|
|
1152
|
|
1153 if (*ph == NULL) {
|
|
1154 *ph = h;
|
|
1155 return NGX_OK;
|
|
1156 }
|
|
1157
|
|
1158 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1159 "client sent duplicate header line: \"%V: %V\"",
|
|
1160 &h->key, &h->value);
|
|
1161
|
|
1162 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1163
|
|
1164 return NGX_ERROR;
|
|
1165 }
|
|
1166
|
|
1167
|
|
1168 static ngx_int_t
|
58
|
1169 ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1170 ngx_uint_t offset)
|
|
1171 {
|
|
1172 ngx_table_elt_t **cookie;
|
|
1173
|
|
1174 cookie = ngx_array_push(&r->headers_in.cookies);
|
72
|
1175 if (cookie) {
|
|
1176 *cookie = h;
|
|
1177 return NGX_OK;
|
58
|
1178 }
|
|
1179
|
72
|
1180 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
1181
|
|
1182 return NGX_ERROR;
|
58
|
1183 }
|
|
1184
|
|
1185
|
|
1186 static ngx_int_t
|
50
|
1187 ngx_http_process_request_header(ngx_http_request_t *r)
|
0
|
1188 {
|
196
|
1189 size_t len;
|
|
1190 u_char *ua, *user_agent, ch;
|
198
|
1191 ngx_uint_t hash;
|
196
|
1192 #if (NGX_HTTP_SSL)
|
|
1193 long rc;
|
|
1194 ngx_http_ssl_srv_conf_t *sscf;
|
|
1195 #endif
|
0
|
1196
|
|
1197 if (r->headers_in.host) {
|
198
|
1198
|
|
1199 hash = 0;
|
|
1200
|
0
|
1201 for (len = 0; len < r->headers_in.host->value.len; len++) {
|
26
|
1202 ch = r->headers_in.host->value.data[len];
|
|
1203
|
|
1204 if (ch == ':') {
|
0
|
1205 break;
|
|
1206 }
|
26
|
1207
|
198
|
1208 ch = ngx_tolower(ch);
|
|
1209 r->headers_in.host->value.data[len] = ch;
|
|
1210 hash = ngx_hash(hash, ch);
|
0
|
1211 }
|
54
|
1212
|
198
|
1213 if (len && r->headers_in.host->value.data[len - 1] == '.') {
|
58
|
1214 len--;
|
198
|
1215 hash = ngx_hash_key(r->headers_in.host->value.data, len);
|
58
|
1216 }
|
|
1217
|
0
|
1218 r->headers_in.host_name_len = len;
|
|
1219
|
198
|
1220 if (r->virtual_names) {
|
|
1221 ngx_http_find_virtual_server(r, r->virtual_names, hash);
|
|
1222 }
|
0
|
1223
|
|
1224 } else {
|
|
1225 if (r->http_version > NGX_HTTP_VERSION_10) {
|
54
|
1226 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1227 "client sent HTTP/1.1 request without \"Host\" header");
|
|
1228 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
|
|
1229 return NGX_ERROR;
|
0
|
1230 }
|
54
|
1231
|
0
|
1232 r->headers_in.host_name_len = 0;
|
|
1233 }
|
|
1234
|
|
1235 if (r->headers_in.content_length) {
|
|
1236 r->headers_in.content_length_n =
|
50
|
1237 ngx_atosz(r->headers_in.content_length->value.data,
|
0
|
1238 r->headers_in.content_length->value.len);
|
|
1239
|
|
1240 if (r->headers_in.content_length_n == NGX_ERROR) {
|
54
|
1241 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1242 "client sent invalid \"Content-Length\" header");
|
130
|
1243 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
54
|
1244 return NGX_ERROR;
|
0
|
1245 }
|
|
1246 }
|
|
1247
|
188
|
1248 if (r->method & (NGX_HTTP_POST|NGX_HTTP_PUT)
|
|
1249 && r->headers_in.content_length_n == -1)
|
|
1250 {
|
54
|
1251 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1252 "client sent POST method without \"Content-Length\" header");
|
130
|
1253 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
|
1254 return NGX_ERROR;
|
|
1255 }
|
|
1256
|
|
1257 if (r->headers_in.transfer_encoding
|
|
1258 && ngx_strstr(r->headers_in.transfer_encoding->value.data, "chunked"))
|
|
1259 {
|
|
1260 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1261 "client sent \"Transfer-Encoding: chunked\" header");
|
|
1262 ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
|
54
|
1263 return NGX_ERROR;
|
0
|
1264 }
|
|
1265
|
|
1266 if (r->plain_http) {
|
54
|
1267 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1268 "client sent plain HTTP request to HTTPS port");
|
|
1269 ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
|
|
1270 return NGX_ERROR;
|
0
|
1271 }
|
|
1272
|
196
|
1273 #if (NGX_HTTP_SSL)
|
|
1274
|
|
1275 if (r->connection->ssl) {
|
|
1276 sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
|
|
1277
|
|
1278 if (sscf->verify) {
|
|
1279 rc = SSL_get_verify_result(r->connection->ssl->connection);
|
|
1280
|
|
1281 if (rc != X509_V_OK) {
|
|
1282 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1283 "client SSL certificate verify error: %l ", rc);
|
|
1284 ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
|
|
1285 return NGX_ERROR;
|
|
1286 }
|
|
1287
|
|
1288 if (SSL_get_peer_certificate(r->connection->ssl->connection)
|
|
1289 == NULL)
|
|
1290 {
|
|
1291 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
|
|
1292 "client sent no required SSL certificate");
|
|
1293 ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
|
|
1294 return NGX_ERROR;
|
|
1295 }
|
|
1296 }
|
|
1297 }
|
|
1298
|
|
1299 #endif
|
|
1300
|
0
|
1301 if (r->headers_in.connection) {
|
|
1302 if (r->headers_in.connection->value.len == 5
|
|
1303 && ngx_strcasecmp(r->headers_in.connection->value.data, "close")
|
58
|
1304 == 0)
|
0
|
1305 {
|
|
1306 r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
|
|
1307
|
|
1308 } else if (r->headers_in.connection->value.len == 10
|
|
1309 && ngx_strcasecmp(r->headers_in.connection->value.data,
|
58
|
1310 "keep-alive") == 0)
|
0
|
1311 {
|
|
1312 r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
|
|
1313
|
|
1314 if (r->headers_in.keep_alive) {
|
|
1315 r->headers_in.keep_alive_n =
|
50
|
1316 ngx_atotm(r->headers_in.keep_alive->value.data,
|
0
|
1317 r->headers_in.keep_alive->value.len);
|
|
1318 }
|
|
1319 }
|
|
1320 }
|
|
1321
|
|
1322 if (r->headers_in.user_agent) {
|
|
1323
|
|
1324 /*
|
|
1325 * check some widespread browsers while the headers are still
|
|
1326 * in CPU cache
|
|
1327 */
|
|
1328
|
|
1329 user_agent = r->headers_in.user_agent->value.data;
|
|
1330
|
|
1331 ua = (u_char *) ngx_strstr(user_agent, "MSIE");
|
|
1332
|
|
1333 if (ua && ua + 8 < user_agent + r->headers_in.user_agent->value.len) {
|
|
1334
|
|
1335 r->headers_in.msie = 1;
|
|
1336
|
|
1337 if (ua[4] == ' ' && ua[5] == '4' && ua[6] == '.') {
|
|
1338 r->headers_in.msie4 = 1;
|
|
1339 }
|
|
1340
|
|
1341 #if 0
|
|
1342 /* MSIE ignores the SSL "close notify" alert */
|
22
|
1343 if (c->ssl) {
|
96
|
1344 c->ssl->no_send_shutdown = 1;
|
22
|
1345 }
|
0
|
1346 #endif
|
|
1347 }
|
|
1348
|
|
1349 if (ngx_strstr(user_agent, "Opera")) {
|
|
1350 r->headers_in.opera = 1;
|
|
1351 r->headers_in.msie = 0;
|
|
1352 r->headers_in.msie4 = 0;
|
|
1353 }
|
|
1354
|
|
1355 if (!r->headers_in.msie && !r->headers_in.opera) {
|
|
1356
|
|
1357 if (ngx_strstr(user_agent, "Gecko/")) {
|
|
1358 r->headers_in.gecko = 1;
|
|
1359
|
|
1360 } else if (ngx_strstr(user_agent, "Konqueror")) {
|
|
1361 r->headers_in.konqueror = 1;
|
|
1362 }
|
|
1363 }
|
|
1364 }
|
|
1365
|
|
1366 return NGX_OK;
|
|
1367 }
|
|
1368
|
|
1369
|
162
|
1370 static void
|
198
|
1371 ngx_http_find_virtual_server(ngx_http_request_t *r,
|
|
1372 ngx_http_virtual_names_t *vn, ngx_uint_t hash)
|
26
|
1373 {
|
142
|
1374 size_t len;
|
|
1375 u_char *host;
|
|
1376 ngx_http_core_loc_conf_t *clcf;
|
|
1377 ngx_http_core_srv_conf_t *cscf;
|
|
1378
|
|
1379 host = r->headers_in.host->value.data;
|
|
1380 len = r->headers_in.host_name_len;
|
|
1381
|
|
1382 if (vn->hash.buckets) {
|
198
|
1383 cscf = ngx_hash_find(&vn->hash, hash, host, len);
|
142
|
1384 if (cscf) {
|
58
|
1385 goto found;
|
26
|
1386 }
|
|
1387 }
|
|
1388
|
142
|
1389 if (vn->dns_wildcards && vn->dns_wildcards->hash.buckets) {
|
|
1390 cscf = ngx_hash_find_wildcard(vn->dns_wildcards, host, len);
|
|
1391
|
|
1392 if (cscf) {
|
|
1393 goto found;
|
26
|
1394 }
|
|
1395 }
|
|
1396
|
162
|
1397 return;
|
58
|
1398
|
|
1399 found:
|
|
1400
|
142
|
1401 r->server_name.len = len;
|
|
1402 r->server_name.data = host;
|
|
1403
|
|
1404 r->srv_conf = cscf->ctx->srv_conf;
|
|
1405 r->loc_conf = cscf->ctx->loc_conf;
|
58
|
1406
|
|
1407 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1408 r->connection->log->file = clcf->err_log->file;
|
|
1409
|
|
1410 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
|
1411 r->connection->log->log_level = clcf->err_log->log_level;
|
26
|
1412 }
|
|
1413
|
162
|
1414 return;
|
26
|
1415 }
|
|
1416
|
|
1417
|
58
|
1418 static void
|
|
1419 ngx_http_request_handler(ngx_event_t *ev)
|
|
1420 {
|
|
1421 ngx_connection_t *c;
|
|
1422 ngx_http_request_t *r;
|
|
1423
|
|
1424 c = ev->data;
|
|
1425 r = c->data;
|
|
1426
|
|
1427 if (ev->write) {
|
|
1428 r->write_event_handler(r);
|
|
1429
|
|
1430 } else {
|
|
1431 r->read_event_handler(r);
|
|
1432 }
|
|
1433 }
|
|
1434
|
|
1435
|
50
|
1436 void
|
58
|
1437 ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
0
|
1438 {
|
58
|
1439 ngx_http_request_t *pr;
|
0
|
1440 ngx_http_core_loc_conf_t *clcf;
|
|
1441
|
58
|
1442 if (rc == NGX_DONE) {
|
118
|
1443 /* the request pool may be already destroyed */
|
58
|
1444 return;
|
|
1445 }
|
|
1446
|
132
|
1447 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1448 "http finalize request: %d, \"%V?%V\"",
|
|
1449 rc, &r->uri, &r->args);
|
|
1450
|
162
|
1451 if (rc == NGX_ERROR
|
|
1452 || rc == NGX_HTTP_REQUEST_TIME_OUT
|
|
1453 || r->connection->error)
|
|
1454 {
|
190
|
1455 if (rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
|
|
1456 && r->headers_out.status == 0)
|
|
1457 {
|
|
1458 r->headers_out.status = NGX_HTTP_CLIENT_CLOSED_REQUEST;
|
|
1459 }
|
|
1460
|
162
|
1461 if (ngx_http_post_action(r) == NGX_OK) {
|
|
1462 return;
|
|
1463 }
|
|
1464
|
118
|
1465 ngx_http_close_request(r, 0);
|
0
|
1466 return;
|
|
1467 }
|
|
1468
|
186
|
1469 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
|
|
1470 || rc == NGX_HTTP_CREATED
|
|
1471 || rc == NGX_HTTP_NO_CONTENT)
|
|
1472 {
|
136
|
1473 if (rc == NGX_HTTP_CLOSE) {
|
|
1474 ngx_http_close_request(r, rc);
|
|
1475 return;
|
|
1476 }
|
|
1477
|
146
|
1478 if (r == r->main) {
|
118
|
1479 if (r->connection->read->timer_set) {
|
|
1480 ngx_del_timer(r->connection->read);
|
|
1481 }
|
|
1482
|
|
1483 if (r->connection->write->timer_set) {
|
|
1484 ngx_del_timer(r->connection->write);
|
|
1485 }
|
|
1486 }
|
|
1487
|
|
1488 ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
|
|
1489 return;
|
|
1490 }
|
|
1491
|
146
|
1492 if (r != r->main || rc == NGX_AGAIN) {
|
74
|
1493 if (ngx_http_set_write_handler(r) != NGX_OK) {
|
|
1494 return;
|
|
1495 }
|
58
|
1496 }
|
|
1497
|
|
1498 r->done = 1;
|
|
1499
|
|
1500 if (r != r->connection->data) {
|
132
|
1501 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1502 "http finalize non-active request: \"%V?%V\"",
|
|
1503 &r->uri, &r->args);
|
58
|
1504 return;
|
|
1505 }
|
|
1506
|
146
|
1507 if (r != r->main) {
|
58
|
1508
|
|
1509 pr = r->parent;
|
|
1510
|
132
|
1511 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1512 "http parent request: \"%V?%V\"", &pr->uri, &pr->args);
|
76
|
1513
|
58
|
1514 if (rc != NGX_AGAIN) {
|
146
|
1515 r->connection->data = pr;
|
58
|
1516 }
|
|
1517
|
|
1518 if (pr->postponed) {
|
|
1519
|
132
|
1520 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1521 "http request: \"%V?%V\" has postponed",
|
|
1522 &pr->uri, &pr->args);
|
76
|
1523
|
58
|
1524 if (rc != NGX_AGAIN && pr->postponed->request == r) {
|
|
1525 pr->postponed = pr->postponed->next;
|
146
|
1526 }
|
|
1527
|
|
1528 if (r->fast_subrequest) {
|
194
|
1529
|
|
1530 if (rc == NGX_AGAIN) {
|
|
1531 r->fast_subrequest = 0;
|
|
1532 }
|
|
1533
|
146
|
1534 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1535 "http fast subrequest: \"%V?%V\" done",
|
|
1536 &r->uri, &r->args);
|
|
1537 return;
|
58
|
1538 }
|
|
1539
|
194
|
1540 if (rc != NGX_AGAIN) {
|
|
1541 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1542 "http wake parent request: \"%V?%V\"",
|
|
1543 &pr->uri, &pr->args);
|
|
1544
|
|
1545 pr->write_event_handler(pr);
|
|
1546 }
|
58
|
1547 }
|
|
1548
|
|
1549 return;
|
|
1550 }
|
0
|
1551
|
118
|
1552 if (rc == NGX_AGAIN) {
|
0
|
1553 return;
|
76
|
1554 }
|
|
1555
|
146
|
1556 if (r->connection->buffered) {
|
74
|
1557 (void) ngx_http_set_write_handler(r);
|
0
|
1558 return;
|
|
1559 }
|
|
1560
|
178
|
1561 if (!r->post_action) {
|
|
1562 r->request_complete = 1;
|
|
1563 }
|
|
1564
|
162
|
1565 if (ngx_http_post_action(r) == NGX_OK) {
|
126
|
1566 return;
|
|
1567 }
|
|
1568
|
0
|
1569 if (r->connection->read->timer_set) {
|
|
1570 ngx_del_timer(r->connection->read);
|
|
1571 }
|
|
1572
|
|
1573 if (r->connection->write->timer_set) {
|
|
1574 r->connection->write->delayed = 0;
|
|
1575 ngx_del_timer(r->connection->write);
|
|
1576 }
|
|
1577
|
138
|
1578 if (r->connection->destroyed) {
|
|
1579 return;
|
|
1580 }
|
|
1581
|
162
|
1582 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
0
|
1583
|
|
1584 if (!ngx_terminate
|
|
1585 && !ngx_exiting
|
|
1586 && r->keepalive != 0
|
|
1587 && clcf->keepalive_timeout > 0)
|
|
1588 {
|
|
1589 ngx_http_set_keepalive(r);
|
|
1590 return;
|
|
1591
|
|
1592 } else if (r->lingering_close && clcf->lingering_timeout > 0) {
|
|
1593 ngx_http_set_lingering_close(r);
|
|
1594 return;
|
|
1595 }
|
|
1596
|
|
1597 ngx_http_close_request(r, 0);
|
|
1598 }
|
|
1599
|
|
1600
|
74
|
1601 static ngx_int_t
|
50
|
1602 ngx_http_set_write_handler(ngx_http_request_t *r)
|
0
|
1603 {
|
|
1604 ngx_event_t *wev;
|
|
1605 ngx_http_core_loc_conf_t *clcf;
|
|
1606
|
58
|
1607 r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
|
|
1608
|
74
|
1609 r->write_event_handler = ngx_http_writer;
|
|
1610
|
0
|
1611 wev = r->connection->write;
|
|
1612
|
|
1613 if (wev->ready && wev->delayed) {
|
74
|
1614 return NGX_OK;
|
0
|
1615 }
|
|
1616
|
58
|
1617 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
0
|
1618 if (!wev->delayed) {
|
|
1619 ngx_add_timer(wev, clcf->send_timeout);
|
|
1620 }
|
|
1621
|
2
|
1622 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
0
|
1623 ngx_http_close_request(r, 0);
|
74
|
1624 return NGX_ERROR;
|
0
|
1625 }
|
74
|
1626
|
|
1627 return NGX_OK;
|
0
|
1628 }
|
|
1629
|
|
1630
|
58
|
1631 static void
|
|
1632 ngx_http_writer(ngx_http_request_t *r)
|
0
|
1633 {
|
|
1634 int rc;
|
58
|
1635 ngx_event_t *wev;
|
0
|
1636 ngx_connection_t *c;
|
|
1637 ngx_http_core_loc_conf_t *clcf;
|
|
1638
|
58
|
1639 c = r->connection;
|
|
1640 wev = c->write;
|
|
1641
|
132
|
1642 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
1643 "http writer handler: \"%V?%V\"", &r->uri, &r->args);
|
0
|
1644
|
|
1645 if (wev->timedout) {
|
|
1646 if (!wev->delayed) {
|
54
|
1647 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
1648 "client timed out");
|
|
1649 c->timedout = 1;
|
116
|
1650
|
162
|
1651 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
|
0
|
1652 return;
|
|
1653 }
|
|
1654
|
|
1655 wev->timedout = 0;
|
|
1656 wev->delayed = 0;
|
|
1657
|
|
1658 if (!wev->ready) {
|
102
|
1659 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
0
|
1660 ngx_add_timer(wev, clcf->send_timeout);
|
|
1661
|
2
|
1662 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
118
|
1663 ngx_http_close_request(r, 0);
|
0
|
1664 }
|
|
1665
|
|
1666 return;
|
|
1667 }
|
|
1668
|
|
1669 } else {
|
|
1670 if (wev->delayed) {
|
|
1671 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
1672 "http writer delayed");
|
|
1673
|
102
|
1674 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
2
|
1675
|
|
1676 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
118
|
1677 ngx_http_close_request(r, 0);
|
0
|
1678 }
|
|
1679
|
|
1680 return;
|
|
1681 }
|
|
1682 }
|
|
1683
|
132
|
1684 rc = ngx_http_output_filter(r, NULL);
|
|
1685
|
|
1686 if (c->destroyed) {
|
|
1687 return;
|
58
|
1688 }
|
|
1689
|
132
|
1690 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
1691 "http writer output filter: %d, \"%V?%V\"",
|
|
1692 rc, &r->uri, &r->args);
|
0
|
1693
|
|
1694 if (rc == NGX_AGAIN) {
|
102
|
1695 clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
|
0
|
1696 if (!wev->ready && !wev->delayed) {
|
|
1697 ngx_add_timer(wev, clcf->send_timeout);
|
|
1698 }
|
|
1699
|
2
|
1700 if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
|
118
|
1701 ngx_http_close_request(r, 0);
|
0
|
1702 }
|
|
1703
|
146
|
1704 if (r == r->main) {
|
|
1705 return;
|
|
1706 }
|
|
1707
|
|
1708 rc = NGX_OK;
|
0
|
1709 }
|
|
1710
|
132
|
1711 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
|
|
1712 "http writer done: \"%V?%V\"", &r->uri, &r->args);
|
0
|
1713
|
|
1714 ngx_http_finalize_request(r, rc);
|
|
1715 }
|
|
1716
|
|
1717
|
50
|
1718 static void
|
58
|
1719 ngx_http_block_read(ngx_http_request_t *r)
|
0
|
1720 {
|
58
|
1721 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1722 "http read blocked");
|
0
|
1723
|
|
1724 /* aio does not call this handler */
|
|
1725
|
58
|
1726 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
|
|
1727 && r->connection->read->active)
|
|
1728 {
|
|
1729 if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0)
|
|
1730 == NGX_ERROR)
|
|
1731 {
|
0
|
1732 ngx_http_close_request(r, 0);
|
|
1733 }
|
|
1734 }
|
|
1735 }
|
|
1736
|
|
1737
|
50
|
1738 static void
|
|
1739 ngx_http_set_keepalive(ngx_http_request_t *r)
|
0
|
1740 {
|
2
|
1741 int tcp_nodelay;
|
0
|
1742 ngx_int_t i;
|
|
1743 ngx_buf_t *b, *f;
|
|
1744 ngx_event_t *rev, *wev;
|
|
1745 ngx_connection_t *c;
|
|
1746 ngx_http_connection_t *hc;
|
|
1747 ngx_http_core_srv_conf_t *cscf;
|
|
1748 ngx_http_core_loc_conf_t *clcf;
|
|
1749
|
|
1750 c = r->connection;
|
|
1751 rev = c->read;
|
|
1752
|
|
1753 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
|
|
1754
|
32
|
1755 c->log->action = "closing request";
|
0
|
1756
|
|
1757 hc = r->http_connection;
|
|
1758 b = r->header_in;
|
|
1759
|
|
1760 if (b->pos < b->last) {
|
|
1761
|
|
1762 /* the pipelined request */
|
|
1763
|
|
1764 if (b != c->buffer) {
|
|
1765
|
4
|
1766 /*
|
|
1767 * If the large header buffers were allocated while the previous
|
|
1768 * request processing then we do not use c->buffer for
|
|
1769 * the pipelined request (see ngx_http_init_request()).
|
126
|
1770 *
|
4
|
1771 * Now we would move the large header buffers to the free list.
|
|
1772 */
|
0
|
1773
|
|
1774 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
1775
|
|
1776 if (hc->free == NULL) {
|
|
1777 hc->free = ngx_palloc(c->pool,
|
|
1778 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
|
|
1779
|
|
1780 if (hc->free == NULL) {
|
26
|
1781 ngx_http_close_request(r, 0);
|
0
|
1782 return;
|
|
1783 }
|
|
1784 }
|
|
1785
|
|
1786 for (i = 0; i < hc->nbusy - 1; i++) {
|
|
1787 f = hc->busy[i];
|
|
1788 hc->free[hc->nfree++] = f;
|
|
1789 f->pos = f->start;
|
|
1790 f->last = f->start;
|
|
1791 }
|
|
1792
|
|
1793 hc->busy[0] = b;
|
|
1794 hc->nbusy = 1;
|
|
1795 }
|
|
1796 }
|
|
1797
|
26
|
1798 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
1799
|
118
|
1800 ngx_http_request_done(r, 0);
|
|
1801
|
0
|
1802 c->data = hc;
|
|
1803
|
|
1804 ngx_add_timer(rev, clcf->keepalive_timeout);
|
|
1805
|
58
|
1806 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
0
|
1807 ngx_http_close_connection(c);
|
|
1808 return;
|
|
1809 }
|
|
1810
|
|
1811 wev = c->write;
|
58
|
1812 wev->handler = ngx_http_empty_handler;
|
0
|
1813
|
|
1814 if (b->pos < b->last) {
|
|
1815
|
|
1816 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
|
|
1817
|
116
|
1818 #if (NGX_STAT_STUB)
|
|
1819 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
|
1820 #endif
|
|
1821
|
0
|
1822 hc->pipeline = 1;
|
32
|
1823 c->log->action = "reading client pipelined request line";
|
116
|
1824
|
0
|
1825 ngx_http_init_request(rev);
|
|
1826 return;
|
|
1827 }
|
|
1828
|
|
1829 hc->pipeline = 0;
|
|
1830
|
4
|
1831 /*
|
|
1832 * To keep a memory footprint as small as possible for an idle
|
|
1833 * keepalive connection we try to free the ngx_http_request_t and
|
|
1834 * c->buffer's memory if they were allocated outside the c->pool.
|
|
1835 * The large header buffers are always allocated outside the c->pool and
|
|
1836 * are freed too.
|
|
1837 */
|
|
1838
|
0
|
1839 if (ngx_pfree(c->pool, r) == NGX_OK) {
|
|
1840 hc->request = NULL;
|
|
1841 }
|
|
1842
|
|
1843 b = c->buffer;
|
|
1844
|
|
1845 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
|
4
|
1846
|
|
1847 /*
|
|
1848 * the special note for ngx_http_keepalive_handler() that
|
|
1849 * c->buffer's memory was freed
|
|
1850 */
|
|
1851
|
0
|
1852 b->pos = NULL;
|
|
1853
|
|
1854 } else {
|
|
1855 b->pos = b->start;
|
|
1856 b->last = b->start;
|
|
1857 }
|
|
1858
|
10
|
1859 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
|
0
|
1860 hc->free, hc->nfree);
|
|
1861
|
|
1862 if (hc->free) {
|
|
1863 for (i = 0; i < hc->nfree; i++) {
|
|
1864 ngx_pfree(c->pool, hc->free[i]);
|
|
1865 hc->free[i] = NULL;
|
|
1866 }
|
|
1867
|
|
1868 hc->nfree = 0;
|
|
1869 }
|
|
1870
|
10
|
1871 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
|
0
|
1872 hc->busy, hc->nbusy);
|
|
1873
|
|
1874 if (hc->busy) {
|
|
1875 for (i = 0; i < hc->nbusy; i++) {
|
|
1876 ngx_pfree(c->pool, hc->busy[i]);
|
|
1877 hc->busy[i] = NULL;
|
|
1878 }
|
|
1879
|
|
1880 hc->nbusy = 0;
|
|
1881 }
|
|
1882
|
58
|
1883 rev->handler = ngx_http_keepalive_handler;
|
0
|
1884
|
132
|
1885 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
|
1886 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
|
|
1887 ngx_http_close_connection(c);
|
|
1888 return;
|
0
|
1889 }
|
|
1890 }
|
|
1891
|
32
|
1892 c->log->action = "keepalive";
|
0
|
1893
|
|
1894 if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
|
50
|
1895 if (ngx_tcp_push(c->fd) == -1) {
|
0
|
1896 ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
|
|
1897 ngx_http_close_connection(c);
|
|
1898 return;
|
|
1899 }
|
2
|
1900
|
0
|
1901 c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
|
22
|
1902 tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
|
2
|
1903
|
|
1904 } else {
|
22
|
1905 tcp_nodelay = 1;
|
|
1906 }
|
|
1907
|
32
|
1908 if (tcp_nodelay
|
|
1909 && clcf->tcp_nodelay
|
|
1910 && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
|
|
1911 {
|
22
|
1912 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
|
|
1913
|
|
1914 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
184
|
1915 (const void *) &tcp_nodelay, sizeof(int))
|
|
1916 == -1)
|
22
|
1917 {
|
|
1918 ngx_connection_error(c, ngx_socket_errno,
|
|
1919 "setsockopt(TCP_NODELAY) failed");
|
|
1920 ngx_http_close_connection(c);
|
|
1921 return;
|
2
|
1922 }
|
22
|
1923
|
32
|
1924 c->tcp_nodelay = NGX_TCP_NODELAY_SET;
|
0
|
1925 }
|
|
1926
|
|
1927 #if 0
|
4
|
1928 /* if ngx_http_request_t was freed then we need some other place */
|
0
|
1929 r->http_state = NGX_HTTP_KEEPALIVE_STATE;
|
|
1930 #endif
|
|
1931
|
|
1932 if (rev->ready) {
|
|
1933 ngx_http_keepalive_handler(rev);
|
|
1934 }
|
|
1935 }
|
|
1936
|
|
1937
|
50
|
1938 static void
|
|
1939 ngx_http_keepalive_handler(ngx_event_t *rev)
|
0
|
1940 {
|
50
|
1941 size_t size;
|
|
1942 ssize_t n;
|
|
1943 ngx_buf_t *b;
|
|
1944 ngx_connection_t *c;
|
0
|
1945
|
|
1946 c = rev->data;
|
|
1947
|
|
1948 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
|
|
1949
|
|
1950 if (rev->timedout) {
|
|
1951 ngx_http_close_connection(c);
|
|
1952 return;
|
|
1953 }
|
|
1954
|
18
|
1955 #if (NGX_HAVE_KQUEUE)
|
0
|
1956
|
4
|
1957 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
136
|
1958 if (rev->pending_eof) {
|
32
|
1959 c->log->handler = NULL;
|
0
|
1960 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
|
10
|
1961 "kevent() reported that client %V closed "
|
32
|
1962 "keepalive connection", &c->addr_text);
|
22
|
1963 #if (NGX_HTTP_SSL)
|
|
1964 if (c->ssl) {
|
96
|
1965 c->ssl->no_send_shutdown = 1;
|
22
|
1966 }
|
|
1967 #endif
|
0
|
1968 ngx_http_close_connection(c);
|
|
1969 return;
|
|
1970 }
|
|
1971 }
|
|
1972
|
|
1973 #endif
|
|
1974
|
|
1975 b = c->buffer;
|
|
1976 size = b->end - b->start;
|
|
1977
|
|
1978 if (b->pos == NULL) {
|
4
|
1979
|
|
1980 /*
|
|
1981 * The c->buffer's memory was freed by ngx_http_set_keepalive().
|
|
1982 * However, the c->buffer->start and c->buffer->end were not changed
|
|
1983 * to keep the buffer size.
|
|
1984 */
|
|
1985
|
50
|
1986 b->pos = ngx_palloc(c->pool, size);
|
|
1987 if (b->pos == NULL) {
|
0
|
1988 ngx_http_close_connection(c);
|
|
1989 return;
|
|
1990 }
|
|
1991
|
|
1992 b->start = b->pos;
|
|
1993 b->last = b->pos;
|
|
1994 b->end = b->pos + size;
|
|
1995 }
|
|
1996
|
|
1997 /*
|
|
1998 * MSIE closes a keepalive connection with RST flag
|
|
1999 * so we ignore ECONNRESET here.
|
|
2000 */
|
|
2001
|
|
2002 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
|
|
2003 ngx_set_socket_errno(0);
|
|
2004
|
|
2005 n = c->recv(c, b->last, size);
|
|
2006 c->log_error = NGX_ERROR_INFO;
|
|
2007
|
|
2008 if (n == NGX_AGAIN) {
|
58
|
2009 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
24
|
2010 ngx_http_close_connection(c);
|
|
2011 }
|
|
2012
|
0
|
2013 return;
|
|
2014 }
|
|
2015
|
|
2016 if (n == NGX_ERROR) {
|
|
2017 ngx_http_close_connection(c);
|
|
2018 return;
|
|
2019 }
|
|
2020
|
32
|
2021 c->log->handler = NULL;
|
0
|
2022
|
|
2023 if (n == 0) {
|
|
2024 ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
|
32
|
2025 "client %V closed keepalive connection", &c->addr_text);
|
0
|
2026 ngx_http_close_connection(c);
|
|
2027 return;
|
|
2028 }
|
|
2029
|
|
2030 b->last += n;
|
32
|
2031
|
116
|
2032 #if (NGX_STAT_STUB)
|
|
2033 ngx_atomic_fetch_add(ngx_stat_reading, 1);
|
|
2034 #endif
|
|
2035
|
32
|
2036 c->log->handler = ngx_http_log_error;
|
|
2037 c->log->action = "reading client request line";
|
0
|
2038
|
|
2039 ngx_http_init_request(rev);
|
|
2040 }
|
|
2041
|
|
2042
|
50
|
2043 static void
|
|
2044 ngx_http_set_lingering_close(ngx_http_request_t *r)
|
126
|
2045 {
|
0
|
2046 ngx_event_t *rev, *wev;
|
|
2047 ngx_connection_t *c;
|
|
2048 ngx_http_core_loc_conf_t *clcf;
|
|
2049
|
|
2050 c = r->connection;
|
|
2051
|
|
2052 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2053
|
|
2054 rev = c->read;
|
58
|
2055 rev->handler = ngx_http_lingering_close_handler;
|
0
|
2056
|
106
|
2057 r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
|
0
|
2058 ngx_add_timer(rev, clcf->lingering_timeout);
|
|
2059
|
58
|
2060 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
0
|
2061 ngx_http_close_request(r, 0);
|
|
2062 return;
|
|
2063 }
|
|
2064
|
|
2065 wev = c->write;
|
58
|
2066 wev->handler = ngx_http_empty_handler;
|
0
|
2067
|
132
|
2068 if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
|
|
2069 if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
|
|
2070 ngx_http_close_request(r, 0);
|
|
2071 return;
|
0
|
2072 }
|
|
2073 }
|
|
2074
|
|
2075 if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
|
|
2076 ngx_connection_error(c, ngx_socket_errno,
|
|
2077 ngx_shutdown_socket_n " failed");
|
|
2078 ngx_http_close_request(r, 0);
|
|
2079 return;
|
|
2080 }
|
|
2081
|
|
2082 if (rev->ready) {
|
|
2083 ngx_http_lingering_close_handler(rev);
|
|
2084 }
|
|
2085 }
|
|
2086
|
|
2087
|
50
|
2088 static void
|
|
2089 ngx_http_lingering_close_handler(ngx_event_t *rev)
|
0
|
2090 {
|
|
2091 ssize_t n;
|
|
2092 ngx_msec_t timer;
|
|
2093 ngx_connection_t *c;
|
|
2094 ngx_http_request_t *r;
|
|
2095 ngx_http_core_loc_conf_t *clcf;
|
|
2096 u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
|
|
2097
|
|
2098 c = rev->data;
|
|
2099 r = c->data;
|
|
2100
|
|
2101 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2102 "http lingering close handler");
|
|
2103
|
|
2104 if (rev->timedout) {
|
126
|
2105 c->timedout = 1;
|
0
|
2106 ngx_http_close_request(r, 0);
|
|
2107 return;
|
|
2108 }
|
|
2109
|
|
2110 timer = r->lingering_time - ngx_time();
|
|
2111 if (timer <= 0) {
|
|
2112 ngx_http_close_request(r, 0);
|
|
2113 return;
|
|
2114 }
|
|
2115
|
|
2116 do {
|
|
2117 n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
|
|
2118
|
|
2119 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
|
|
2120
|
|
2121 if (n == NGX_ERROR || n == 0) {
|
|
2122 ngx_http_close_request(r, 0);
|
|
2123 return;
|
|
2124 }
|
|
2125
|
|
2126 } while (rev->ready);
|
|
2127
|
58
|
2128 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
26
|
2129 ngx_http_close_request(r, 0);
|
24
|
2130 return;
|
|
2131 }
|
|
2132
|
0
|
2133 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2134
|
|
2135 timer *= 1000;
|
|
2136
|
|
2137 if (timer > clcf->lingering_timeout) {
|
|
2138 timer = clcf->lingering_timeout;
|
|
2139 }
|
|
2140
|
|
2141 ngx_add_timer(rev, timer);
|
|
2142 }
|
|
2143
|
|
2144
|
50
|
2145 void
|
|
2146 ngx_http_empty_handler(ngx_event_t *wev)
|
0
|
2147 {
|
|
2148 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
|
|
2149
|
|
2150 return;
|
|
2151 }
|
|
2152
|
|
2153
|
58
|
2154 void
|
|
2155 ngx_http_request_empty_handler(ngx_http_request_t *r)
|
|
2156 {
|
|
2157 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
132
|
2158 "http request empty handler");
|
58
|
2159
|
|
2160 return;
|
|
2161 }
|
|
2162
|
|
2163
|
50
|
2164 ngx_int_t
|
130
|
2165 ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
|
0
|
2166 {
|
|
2167 ngx_buf_t *b;
|
|
2168 ngx_chain_t out;
|
|
2169
|
50
|
2170 b = ngx_calloc_buf(r->pool);
|
|
2171 if (b == NULL) {
|
0
|
2172 return NGX_ERROR;
|
|
2173 }
|
|
2174
|
130
|
2175 if (flags & NGX_HTTP_LAST) {
|
|
2176 b->last_buf = 1;
|
|
2177 }
|
|
2178
|
|
2179 if (flags & NGX_HTTP_FLUSH) {
|
|
2180 b->flush = 1;
|
|
2181 }
|
|
2182
|
0
|
2183 out.buf = b;
|
|
2184 out.next = NULL;
|
|
2185
|
|
2186 return ngx_http_output_filter(r, &out);
|
|
2187 }
|
|
2188
|
|
2189
|
162
|
2190 static ngx_int_t
|
|
2191 ngx_http_post_action(ngx_http_request_t *r)
|
|
2192 {
|
|
2193 ngx_http_core_loc_conf_t *clcf;
|
|
2194
|
|
2195 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2196
|
|
2197 if (clcf->post_action.data == NULL) {
|
|
2198 return NGX_DECLINED;
|
|
2199 }
|
|
2200
|
172
|
2201 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
2202 "post action: \"%V\"", &clcf->post_action);
|
|
2203
|
162
|
2204 r->http_version = NGX_HTTP_VERSION_9;
|
|
2205 r->header_only = 1;
|
178
|
2206 r->post_action = 1;
|
162
|
2207
|
|
2208 ngx_http_internal_redirect(r, &clcf->post_action, NULL);
|
|
2209
|
|
2210 return NGX_OK;
|
|
2211 }
|
|
2212
|
|
2213
|
|
2214 static void
|
58
|
2215 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
|
0
|
2216 {
|
188
|
2217 ngx_connection_t *c;
|
118
|
2218
|
|
2219 c = r->connection;
|
188
|
2220
|
|
2221 ngx_http_request_done(r->main, error);
|
118
|
2222 ngx_http_close_connection(c);
|
|
2223 }
|
|
2224
|
|
2225
|
162
|
2226 static void
|
118
|
2227 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
|
|
2228 {
|
|
2229 ngx_log_t *log;
|
130
|
2230 ngx_uint_t i, n;
|
118
|
2231 struct linger linger;
|
188
|
2232 ngx_http_cleanup_t *cln;
|
118
|
2233 ngx_http_log_ctx_t *ctx;
|
130
|
2234 ngx_http_handler_pt *log_handler;
|
118
|
2235 ngx_http_core_loc_conf_t *clcf;
|
|
2236 ngx_http_core_main_conf_t *cmcf;
|
0
|
2237
|
|
2238 log = r->connection->log;
|
|
2239
|
|
2240 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
|
|
2241
|
|
2242 if (r->pool == NULL) {
|
80
|
2243 ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
|
0
|
2244 return;
|
|
2245 }
|
|
2246
|
188
|
2247 for (cln = r->cleanup; cln; cln = cln->next) {
|
|
2248 if (cln->handler) {
|
|
2249 cln->handler(cln->data);
|
|
2250 }
|
|
2251 }
|
|
2252
|
0
|
2253 #if (NGX_STAT_STUB)
|
116
|
2254
|
0
|
2255 if (r->stat_reading) {
|
110
|
2256 ngx_atomic_fetch_add(ngx_stat_reading, -1);
|
0
|
2257 }
|
|
2258
|
|
2259 if (r->stat_writing) {
|
110
|
2260 ngx_atomic_fetch_add(ngx_stat_writing, -1);
|
0
|
2261 }
|
116
|
2262
|
0
|
2263 #endif
|
|
2264
|
|
2265 if (error && r->headers_out.status == 0) {
|
|
2266 r->headers_out.status = error;
|
|
2267 }
|
|
2268
|
118
|
2269 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
2270
|
130
|
2271 log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
|
|
2272 n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
|
|
2273 for (i = 0; i < n; i++) {
|
|
2274 log_handler[i](r);
|
118
|
2275 }
|
0
|
2276
|
|
2277 if (r->connection->timedout) {
|
|
2278 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
2279
|
|
2280 if (clcf->reset_timedout_connection) {
|
58
|
2281 linger.l_onoff = 1;
|
|
2282 linger.l_linger = 0;
|
0
|
2283
|
|
2284 if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
|
58
|
2285 (const void *) &linger, sizeof(struct linger)) == -1)
|
0
|
2286 {
|
|
2287 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
|
|
2288 "setsockopt(SO_LINGER) failed");
|
|
2289 }
|
|
2290 }
|
|
2291 }
|
|
2292
|
58
|
2293 /* the various request strings were allocated from r->pool */
|
0
|
2294 ctx = log->data;
|
10
|
2295 ctx->request = NULL;
|
0
|
2296
|
|
2297 r->request_line.len = 0;
|
|
2298
|
132
|
2299 r->connection->destroyed = 1;
|
|
2300
|
0
|
2301 ngx_destroy_pool(r->pool);
|
|
2302 }
|
|
2303
|
|
2304
|
80
|
2305 static void
|
50
|
2306 ngx_http_close_connection(ngx_connection_t *c)
|
0
|
2307 {
|
28
|
2308 ngx_pool_t *pool;
|
|
2309
|
0
|
2310 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
2311 "close http connection: %d", c->fd);
|
|
2312
|
88
|
2313 #if (NGX_HTTP_SSL)
|
2
|
2314
|
|
2315 if (c->ssl) {
|
|
2316 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
2317 c->ssl->handler = ngx_http_close_connection;
|
2
|
2318 return;
|
|
2319 }
|
|
2320 }
|
|
2321
|
|
2322 #endif
|
|
2323
|
0
|
2324 #if (NGX_STAT_STUB)
|
110
|
2325 ngx_atomic_fetch_add(ngx_stat_active, -1);
|
0
|
2326 #endif
|
|
2327
|
132
|
2328 c->destroyed = 1;
|
|
2329
|
28
|
2330 pool = c->pool;
|
|
2331
|
0
|
2332 ngx_close_connection(c);
|
28
|
2333
|
50
|
2334 ngx_destroy_pool(pool);
|
0
|
2335 }
|
|
2336
|
|
2337
|
50
|
2338 static u_char *
|
|
2339 ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
0
|
2340 {
|
32
|
2341 u_char *p;
|
54
|
2342 ngx_http_request_t *r;
|
32
|
2343 ngx_http_log_ctx_t *ctx;
|
10
|
2344
|
32
|
2345 if (log->action) {
|
58
|
2346 p = ngx_snprintf(buf, len, " while %s", log->action);
|
10
|
2347 len -= p - buf;
|
54
|
2348 buf = p;
|
10
|
2349 }
|
|
2350
|
58
|
2351 ctx = log->data;
|
|
2352
|
54
|
2353 p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
58
|
2354 len -= p - buf;
|
54
|
2355
|
|
2356 r = ctx->request;
|
|
2357
|
58
|
2358 if (r) {
|
|
2359 return r->log_handler(r, p, len);
|
0
|
2360 }
|
10
|
2361
|
58
|
2362 return p;
|
|
2363 }
|
|
2364
|
|
2365
|
|
2366 static u_char *
|
|
2367 ngx_http_log_error_handler(ngx_http_request_t *r, u_char *buf, size_t len)
|
|
2368 {
|
|
2369 u_char *p;
|
54
|
2370
|
|
2371 if (r->server_name.data) {
|
|
2372 p = ngx_snprintf(buf, len, ", server: %V", &r->server_name);
|
52
|
2373 len -= p - buf;
|
54
|
2374 buf = p;
|
52
|
2375 }
|
|
2376
|
54
|
2377 if (r->unparsed_uri.data) {
|
|
2378 p = ngx_snprintf(buf, len, ", URL: \"%V\"", &r->unparsed_uri);
|
|
2379 len -= p - buf;
|
|
2380 buf = p;
|
|
2381
|
|
2382 } else {
|
|
2383 if (r->request_line.data == NULL && r->request_start) {
|
|
2384 for (p = r->request_start; p < r->header_in->last; p++) {
|
|
2385 if (*p == CR || *p == LF) {
|
|
2386 break;
|
|
2387 }
|
|
2388 }
|
|
2389
|
|
2390 r->request_line.len = p - r->request_start;
|
|
2391 r->request_line.data = r->request_start;
|
|
2392 }
|
|
2393
|
|
2394 if (r->request_line.len) {
|
|
2395 p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
|
|
2396 len -= p - buf;
|
|
2397 buf = p;
|
|
2398 }
|
10
|
2399 }
|
|
2400
|
54
|
2401 return ngx_http_log_error_info(r, buf, len);
|
0
|
2402 }
|
54
|
2403
|
|
2404
|
|
2405 u_char *
|
|
2406 ngx_http_log_error_info(ngx_http_request_t *r, u_char *buf, size_t len)
|
|
2407 {
|
|
2408 u_char *p;
|
|
2409
|
|
2410 if (r->headers_in.host) {
|
|
2411 p = ngx_snprintf(buf, len, ", host: \"%V\"",
|
|
2412 &r->headers_in.host->value);
|
|
2413 len -= p - buf;
|
|
2414 buf = p;
|
|
2415 }
|
|
2416
|
|
2417 if (r->headers_in.referer) {
|
|
2418 p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
|
|
2419 &r->headers_in.referer->value);
|
|
2420 buf = p;
|
|
2421 }
|
|
2422
|
|
2423 return buf;
|
|
2424 }
|