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