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