1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10 #include <nginx.h>
11
12
13 static ngx_int_t ngx_http_send_error_page(ngx_http_request_t *r,
14 ngx_http_err_page_t *err_page);
15 static ngx_int_t ngx_http_send_special_response(ngx_http_request_t *r,
16 ngx_http_core_loc_conf_t *clcf, ngx_uint_t err);
17 static ngx_int_t ngx_http_send_refresh(ngx_http_request_t *r);
18
19
20 static u_char ngx_http_error_full_tail[] =
21 "<hr><center>" NGINX_VER "</center>" CRLF
22 "</body>" CRLF
23 "</html>" CRLF
24 ;
25
26
27 static u_char ngx_http_error_tail[] =
28 "<hr><center>nginx</center>" CRLF
29 "</body>" CRLF
30 "</html>" CRLF
31 ;
32
33
34 static u_char ngx_http_msie_stub[] =
35 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
36 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
37 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
38 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
39 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
40 "<!-- The padding to disable MSIE's friendly error page -->" CRLF
41 ;
42
43
44 static u_char ngx_http_msie_refresh_head[] =
45 "<html><head><meta http-equiv=\"Refresh\" content=\"0; URL=";
46
47
48 static u_char ngx_http_msie_refresh_tail[] =
49 "\"></head><body></body></html>" CRLF;
50
51
52 static char ngx_http_error_301_page[] =
53 "<html>" CRLF
54 "<head><title>301 Moved Permanently</title></head>" CRLF
55 "<body bgcolor=\"white\">" CRLF
56 "<center><h1>301 Moved Permanently</h1></center>" CRLF
57 ;
58
59
60 static char ngx_http_error_302_page[] =
61 "<html>" CRLF
62 "<head><title>302 Found</title></head>" CRLF
63 "<body bgcolor=\"white\">" CRLF
64 "<center><h1>302 Found</h1></center>" CRLF
65 ;
66
67
68 static char ngx_http_error_400_page[] =
69 "<html>" CRLF
70 "<head><title>400 Bad Request</title></head>" CRLF
71 "<body bgcolor=\"white\">" CRLF
72 "<center><h1>400 Bad Request</h1></center>" CRLF
73 ;
74
75
76 static char ngx_http_error_401_page[] =
77 "<html>" CRLF
78 "<head><title>401 Authorization Required</title></head>" CRLF
79 "<body bgcolor=\"white\">" CRLF
80 "<center><h1>401 Authorization Required</h1></center>" CRLF
81 ;
82
83
84 static char ngx_http_error_402_page[] =
85 "<html>" CRLF
86 "<head><title>402 Payment Required</title></head>" CRLF
87 "<body bgcolor=\"white\">" CRLF
88 "<center><h1>402 Payment Required</h1></center>" CRLF
89 ;
90
91
92 static char ngx_http_error_403_page[] =
93 "<html>" CRLF
94 "<head><title>403 Forbidden</title></head>" CRLF
95 "<body bgcolor=\"white\">" CRLF
96 "<center><h1>403 Forbidden</h1></center>" CRLF
97 ;
98
99
100 static char ngx_http_error_404_page[] =
101 "<html>" CRLF
102 "<head><title>404 Not Found</title></head>" CRLF
103 "<body bgcolor=\"white\">" CRLF
104 "<center><h1>404 Not Found</h1></center>" CRLF
105 ;
106
107
108 static char ngx_http_error_405_page[] =
109 "<html>" CRLF
110 "<head><title>405 Not Allowed</title></head>" CRLF
111 "<body bgcolor=\"white\">" CRLF
112 "<center><h1>405 Not Allowed</h1></center>" CRLF
113 ;
114
115
116 static char ngx_http_error_406_page[] =
117 "<html>" CRLF
118 "<head><title>406 Not Acceptable</title></head>" CRLF
119 "<body bgcolor=\"white\">" CRLF
120 "<center><h1>406 Not Acceptable</h1></center>" CRLF
121 ;
122
123
124 static char ngx_http_error_408_page[] =
125 "<html>" CRLF
126 "<head><title>408 Request Time-out</title></head>" CRLF
127 "<body bgcolor=\"white\">" CRLF
128 "<center><h1>408 Request Time-out</h1></center>" CRLF
129 ;
130
131
132 static char ngx_http_error_409_page[] =
133 "<html>" CRLF
134 "<head><title>409 Conflict</title></head>" CRLF
135 "<body bgcolor=\"white\">" CRLF
136 "<center><h1>409 Conflict</h1></center>" CRLF
137 ;
138
139
140 static char ngx_http_error_410_page[] =
141 "<html>" CRLF
142 "<head><title>410 Gone</title></head>" CRLF
143 "<body bgcolor=\"white\">" CRLF
144 "<center><h1>410 Gone</h1></center>" CRLF
145 ;
146
147
148 static char ngx_http_error_411_page[] =
149 "<html>" CRLF
150 "<head><title>411 Length Required</title></head>" CRLF
151 "<body bgcolor=\"white\">" CRLF
152 "<center><h1>411 Length Required</h1></center>" CRLF
153 ;
154
155
156 static char ngx_http_error_412_page[] =
157 "<html>" CRLF
158 "<head><title>412 Precondition Failed</title></head>" CRLF
159 "<body bgcolor=\"white\">" CRLF
160 "<center><h1>412 Precondition Failed</h1></center>" CRLF
161 ;
162
163
164 static char ngx_http_error_413_page[] =
165 "<html>" CRLF
166 "<head><title>413 Request Entity Too Large</title></head>" CRLF
167 "<body bgcolor=\"white\">" CRLF
168 "<center><h1>413 Request Entity Too Large</h1></center>" CRLF
169 ;
170
171
172 static char ngx_http_error_414_page[] =
173 "<html>" CRLF
174 "<head><title>414 Request-URI Too Large</title></head>" CRLF
175 "<body bgcolor=\"white\">" CRLF
176 "<center><h1>414 Request-URI Too Large</h1></center>" CRLF
177 ;
178
179
180 static char ngx_http_error_415_page[] =
181 "<html>" CRLF
182 "<head><title>415 Unsupported Media Type</title></head>" CRLF
183 "<body bgcolor=\"white\">" CRLF
184 "<center><h1>415 Unsupported Media Type</h1></center>" CRLF
185 ;
186
187
188 static char ngx_http_error_416_page[] =
189 "<html>" CRLF
190 "<head><title>416 Requested Range Not Satisfiable</title></head>" CRLF
191 "<body bgcolor=\"white\">" CRLF
192 "<center><h1>416 Requested Range Not Satisfiable</h1></center>" CRLF
193 ;
194
195
196 static char ngx_http_error_495_page[] =
197 "<html>" CRLF
198 "<head><title>400 The SSL certificate error</title></head>"
199 CRLF
200 "<body bgcolor=\"white\">" CRLF
201 "<center><h1>400 Bad Request</h1></center>" CRLF
202 "<center>The SSL certificate error</center>" CRLF
203 ;
204
205
206 static char ngx_http_error_496_page[] =
207 "<html>" CRLF
208 "<head><title>400 No required SSL certificate was sent</title></head>"
209 CRLF
210 "<body bgcolor=\"white\">" CRLF
211 "<center><h1>400 Bad Request</h1></center>" CRLF
212 "<center>No required SSL certificate was sent</center>" CRLF
213 ;
214
215
216 static char ngx_http_error_497_page[] =
217 "<html>" CRLF
218 "<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>"
219 CRLF
220 "<body bgcolor=\"white\">" CRLF
221 "<center><h1>400 Bad Request</h1></center>" CRLF
222 "<center>The plain HTTP request was sent to HTTPS port</center>" CRLF
223 ;
224
225
226 static char ngx_http_error_500_page[] =
227 "<html>" CRLF
228 "<head><title>500 Internal Server Error</title></head>" CRLF
229 "<body bgcolor=\"white\">" CRLF
230 "<center><h1>500 Internal Server Error</h1></center>" CRLF
231 ;
232
233
234 static char ngx_http_error_501_page[] =
235 "<html>" CRLF
236 "<head><title>501 Method Not Implemented</title></head>" CRLF
237 "<body bgcolor=\"white\">" CRLF
238 "<center><h1>501 Method Not Implemented</h1></center>" CRLF
239 ;
240
241
242 static char ngx_http_error_502_page[] =
243 "<html>" CRLF
244 "<head><title>502 Bad Gateway</title></head>" CRLF
245 "<body bgcolor=\"white\">" CRLF
246 "<center><h1>502 Bad Gateway</h1></center>" CRLF
247 ;
248
249
250 static char ngx_http_error_503_page[] =
251 "<html>" CRLF
252 "<head><title>503 Service Temporarily Unavailable</title></head>" CRLF
253 "<body bgcolor=\"white\">" CRLF
254 "<center><h1>503 Service Temporarily Unavailable</h1></center>" CRLF
255 ;
256
257
258 static char ngx_http_error_504_page[] =
259 "<html>" CRLF
260 "<head><title>504 Gateway Time-out</title></head>" CRLF
261 "<body bgcolor=\"white\">" CRLF
262 "<center><h1>504 Gateway Time-out</h1></center>" CRLF
263 ;
264
265
266 static char ngx_http_error_507_page[] =
267 "<html>" CRLF
268 "<head><title>507 Insufficient Storage</title></head>" CRLF
269 "<body bgcolor=\"white\">" CRLF
270 "<center><h1>507 Insufficient Storage</h1></center>" CRLF
271 ;
272
273
274 static ngx_str_t ngx_http_error_pages[] = {
275
276 ngx_null_string, /* 201, 204 */
277
278 #define NGX_HTTP_LEVEL_200 1
279
280 /* ngx_null_string, */ /* 300 */
281 ngx_string(ngx_http_error_301_page),
282 ngx_string(ngx_http_error_302_page),
283 ngx_null_string, /* 303 */
284
285 #define NGX_HTTP_LEVEL_300 3
286
287 ngx_string(ngx_http_error_400_page),
288 ngx_string(ngx_http_error_401_page),
289 ngx_string(ngx_http_error_402_page),
290 ngx_string(ngx_http_error_403_page),
291 ngx_string(ngx_http_error_404_page),
292 ngx_string(ngx_http_error_405_page),
293 ngx_string(ngx_http_error_406_page),
294 ngx_null_string, /* 407 */
295 ngx_string(ngx_http_error_408_page),
296 ngx_string(ngx_http_error_409_page),
297 ngx_string(ngx_http_error_410_page),
298 ngx_string(ngx_http_error_411_page),
299 ngx_string(ngx_http_error_412_page),
300 ngx_string(ngx_http_error_413_page),
301 ngx_string(ngx_http_error_414_page),
302 ngx_string(ngx_http_error_415_page),
303 ngx_string(ngx_http_error_416_page),
304
305 #define NGX_HTTP_LEVEL_400 17
306
307 ngx_string(ngx_http_error_495_page), /* 495, https certificate error */
308 ngx_string(ngx_http_error_496_page), /* 496, https no certificate */
309 ngx_string(ngx_http_error_497_page), /* 497, http to https */
310 ngx_string(ngx_http_error_404_page), /* 498, canceled */
311 ngx_null_string, /* 499, client has closed connection */
312
313 ngx_string(ngx_http_error_500_page),
314 ngx_string(ngx_http_error_501_page),
315 ngx_string(ngx_http_error_502_page),
316 ngx_string(ngx_http_error_503_page),
317 ngx_string(ngx_http_error_504_page),
318 ngx_null_string, /* 505 */
319 ngx_null_string, /* 506 */
320 ngx_string(ngx_http_error_507_page)
321 };
322
323
324 static ngx_str_t ngx_http_get_name = { 3, (u_char *) "GET " };
325
326
327 ngx_int_t
328 ngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error)
329 {
330 ngx_int_t rc;
331 ngx_uint_t i, err;
332 ngx_http_err_page_t *err_page;
333 ngx_http_core_loc_conf_t *clcf;
334
335 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
336 "http special response: %d, \"%V\"", error, &r->uri);
337
338 rc = ngx_http_discard_request_body(r);
339
340 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
341 error = NGX_HTTP_INTERNAL_SERVER_ERROR;
342 }
343
344 r->err_status = error;
345
346 if (r->keepalive != 0) {
347 switch (error) {
348 case NGX_HTTP_BAD_REQUEST:
349 case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:
350 case NGX_HTTP_REQUEST_URI_TOO_LARGE:
351 case NGX_HTTP_TO_HTTPS:
352 case NGX_HTTPS_CERT_ERROR:
353 case NGX_HTTPS_NO_CERT:
354 case NGX_HTTP_INTERNAL_SERVER_ERROR:
355 r->keepalive = 0;
356 }
357 }
358
359 if (r->lingering_close == 1) {
360 switch (error) {
361 case NGX_HTTP_BAD_REQUEST:
362 case NGX_HTTP_TO_HTTPS:
363 case NGX_HTTPS_CERT_ERROR:
364 case NGX_HTTPS_NO_CERT:
365 r->lingering_close = 0;
366 }
367 }
368
369 r->headers_out.content_type.len = 0;
370
371 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
372
373 if (!r->error_page && clcf->error_pages) {
374
375 if (clcf->recursive_error_pages == 0) {
376 r->error_page = 1;
377 }
378
379 err_page = clcf->error_pages->elts;
380
381 for (i = 0; i < clcf->error_pages->nelts; i++) {
382 if (err_page[i].status == error) {
383 return ngx_http_send_error_page(r, &err_page[i]);
384 }
385 }
386 }
387
388 if (clcf->msie_refresh
389 && r->headers_in.msie
390 && (error == NGX_HTTP_MOVED_PERMANENTLY
391 || error == NGX_HTTP_MOVED_TEMPORARILY))
392 {
393 return ngx_http_send_refresh(r);
394 }
395
396 if (error == NGX_HTTP_CREATED) {
397 /* 201 */
398 err = 0;
399 r->header_only = 1;
400
401 } else if (error == NGX_HTTP_NO_CONTENT) {
402 /* 204 */
403 err = 0;
404
405 } else if (error < NGX_HTTP_BAD_REQUEST) {
406 /* 3XX */
407 err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200;
408
409 } else if (error < NGX_HTTP_OWN_CODES) {
410 /* 4XX */
411 err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_LEVEL_200
412 + NGX_HTTP_LEVEL_300;
413
414 } else {
415 /* 49X, 5XX */
416 err = error - NGX_HTTP_OWN_CODES + NGX_HTTP_LEVEL_200
417 + NGX_HTTP_LEVEL_300
418 + NGX_HTTP_LEVEL_400;
419 switch (error) {
420 case NGX_HTTP_TO_HTTPS:
421 case NGX_HTTPS_CERT_ERROR:
422 case NGX_HTTPS_NO_CERT:
423 r->err_status = NGX_HTTP_BAD_REQUEST;
424 break;
425 }
426 }
427
428 return ngx_http_send_special_response(r, clcf, err);
429 }
430
431
432 static ngx_int_t
433 ngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)
434 {
435 u_char ch, *p, *last;
436 ngx_str_t *uri, *args, u, a;
437 ngx_table_elt_t *location;
438 ngx_http_core_loc_conf_t *clcf;
439
440 r->err_status = err_page->overwrite;
441
442 r->method = NGX_HTTP_GET;
443 r->method_name = ngx_http_get_name;
444
445 r->zero_in_uri = 0;
446
447 args = NULL;
448
449 if (err_page->uri_lengths) {
450 if (ngx_http_script_run(r, &u, err_page->uri_lengths->elts, 0,
451 err_page->uri_values->elts)
452 == NULL)
453 {
454 return NGX_ERROR;
455 }
456
457 p = u.data;
458 uri = &u;
459
460 if (*p == '/') {
461
462 last = p + uri->len;
463
464 while (p < last) {
465
466 ch = *p++;
467
468 if (ch == '?') {
469 a.len = last - p;
470 a.data = p;
471 args = &a;
472
473 u.len = p - 1 - u.data;
474
475 while (p < last) {
476 if (*p++ == '\0') {
477 r->zero_in_uri = 1;
478 break;
479 }
480 }
481
482 break;
483 }
484
485 if (ch == '\0') {
486 r->zero_in_uri = 1;
487 continue;
488 }
489 }
490 }
491
492 } else {
493 uri = &err_page->uri;
494 }
495
496 if (uri->data[0] == '/') {
497 return ngx_http_internal_redirect(r, uri, args);
498 }
499
500 if (uri->data[0] == '@') {
501 return ngx_http_named_location(r, uri);
502 }
503
504 location = ngx_list_push(&r->headers_out.headers);
505
506 if (location == NULL) {
507 return NGX_ERROR;
508 }
509
510 r->err_status = NGX_HTTP_MOVED_TEMPORARILY;
511
512 location->hash = 1;
513 location->key.len = sizeof("Location") - 1;
514 location->key.data = (u_char *) "Location";
515 location->value = *uri;
516
517 r->headers_out.location = location;
518
519 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
520
521 if (clcf->msie_refresh && r->headers_in.msie) {
522 return ngx_http_send_refresh(r);
523 }
524
525 return ngx_http_send_special_response(r, clcf, NGX_HTTP_MOVED_TEMPORARILY
526 - NGX_HTTP_MOVED_PERMANENTLY
527 + NGX_HTTP_LEVEL_200);
528 }
529
530
531 static ngx_int_t
532 ngx_http_send_special_response(ngx_http_request_t *r,
533 ngx_http_core_loc_conf_t *clcf, ngx_uint_t err)
534 {
535 u_char *tail;
536 size_t len;
537 ngx_int_t rc;
538 ngx_buf_t *b;
539 ngx_uint_t msie_padding;
540 ngx_chain_t out[3];
541
542 if (clcf->server_tokens) {
543 len = sizeof(ngx_http_error_full_tail) - 1;
544 tail = ngx_http_error_full_tail;
545
546 } else {
547 len = sizeof(ngx_http_error_tail) - 1;
548 tail = ngx_http_error_tail;
549 }
550
551 msie_padding = 0;
552
553 if (!r->zero_body) {
554 if (ngx_http_error_pages[err].len) {
555 r->headers_out.content_length_n = ngx_http_error_pages[err].len
556 + len;
557 if (clcf->msie_padding
558 && r->headers_in.msie
559 && r->http_version >= NGX_HTTP_VERSION_10
560 && err >= NGX_HTTP_LEVEL_300)
561 {
562 r->headers_out.content_length_n +=
563 sizeof(ngx_http_msie_stub) - 1;
564 msie_padding = 1;
565 }
566
567 r->headers_out.content_type_len = sizeof("text/html") - 1;
568 r->headers_out.content_type.len = sizeof("text/html") - 1;
569 r->headers_out.content_type.data = (u_char *) "text/html";
570
571 } else {
572 r->headers_out.content_length_n = -1;
573 }
574
575 } else {
576 r->headers_out.content_length_n = 0;
577 err = 0;
578 }
579
580 if (r->headers_out.content_length) {
581 r->headers_out.content_length->hash = 0;
582 r->headers_out.content_length = NULL;
583 }
584
585 ngx_http_clear_accept_ranges(r);
586 ngx_http_clear_last_modified(r);
587
588 rc = ngx_http_send_header(r);
589
590 if (rc == NGX_ERROR || r->header_only) {
591 return rc;
592 }
593
594 if (ngx_http_error_pages[err].len == 0) {
595 return NGX_OK;
596 }
597
598 b = ngx_calloc_buf(r->pool);
599 if (b == NULL) {
600 return NGX_ERROR;
601 }
602
603 b->memory = 1;
604 b->pos = ngx_http_error_pages[err].data;
605 b->last = ngx_http_error_pages[err].data + ngx_http_error_pages[err].len;
606
607 out[0].buf = b;
608 out[0].next = &out[1];
609
610 b = ngx_calloc_buf(r->pool);
611 if (b == NULL) {
612 return NGX_ERROR;
613 }
614
615 b->memory = 1;
616
617 b->pos = tail;
618 b->last = tail + len;
619
620 out[1].buf = b;
621 out[1].next = NULL;;
622
623 if (msie_padding) {
624 b = ngx_calloc_buf(r->pool);
625 if (b == NULL) {
626 return NGX_ERROR;
627 }
628
629 b->memory = 1;
630 b->pos = ngx_http_msie_stub;
631 b->last = ngx_http_msie_stub + sizeof(ngx_http_msie_stub) - 1;
632
633 out[1].next = &out[2];
634 out[2].buf = b;
635 out[2].next = NULL;;
636 }
637
638 if (r == r->main) {
639 b->last_buf = 1;
640 }
641
642 b->last_in_chain = 1;
643
644 return ngx_http_output_filter(r, &out[0]);
645 }
646
647
648 static ngx_int_t
649 ngx_http_send_refresh(ngx_http_request_t *r)
650 {
651 u_char *p, *location;
652 size_t len, size;
653 uintptr_t escape;
654 ngx_int_t rc;
655 ngx_buf_t *b;
656 ngx_chain_t out;
657
658 len = r->headers_out.location->value.len;
659 location = r->headers_out.location->value.data;
660
661 escape = 2 * ngx_escape_uri(NULL, location, len, NGX_ESCAPE_REFRESH);
662
663 size = sizeof(ngx_http_msie_refresh_head) - 1
664 + escape + len
665 + sizeof(ngx_http_msie_refresh_tail) - 1;
666
667 r->err_status = NGX_HTTP_OK;
668
669 r->headers_out.content_type_len = sizeof("text/html") - 1;
670 r->headers_out.content_type.len = sizeof("text/html") - 1;
671 r->headers_out.content_type.data = (u_char *) "text/html";
672
673 r->headers_out.location->hash = 0;
674 r->headers_out.location = NULL;
675
676 r->headers_out.content_length_n = size;
677
678 if (r->headers_out.content_length) {
679 r->headers_out.content_length->hash = 0;
680 r->headers_out.content_length = NULL;
681 }
682
683 ngx_http_clear_accept_ranges(r);
684 ngx_http_clear_last_modified(r);
685
686 rc = ngx_http_send_header(r);
687
688 if (rc == NGX_ERROR || r->header_only) {
689 return rc;
690 }
691
692 b = ngx_create_temp_buf(r->pool, size);
693 if (b == NULL) {
694 return NGX_ERROR;
695 }
696
697 p = ngx_cpymem(b->pos, ngx_http_msie_refresh_head,
698 sizeof(ngx_http_msie_refresh_head) - 1);
699
700 if (escape == 0) {
701 p = ngx_cpymem(p, location, len);
702
703 } else {
704 p = (u_char *) ngx_escape_uri(p, location, len, NGX_ESCAPE_REFRESH);
705 }
706
707 b->last = ngx_cpymem(p, ngx_http_msie_refresh_tail,
708 sizeof(ngx_http_msie_refresh_tail) - 1);
709
710 b->last_buf = 1;
711 b->last_in_chain = 1;
712
713 out.buf = b;
714 out.next = NULL;
715
716 return ngx_http_output_filter(r, &out);
717 }
718
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.