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