nginx 0.7.59
[nginx-catap.git] / src / http / ngx_http_special_response.c
blob62f71a3ef7e72503e5b7adce94cb979f73802b65
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10 #include <nginx.h>
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);
20 static u_char ngx_http_error_full_tail[] =
21 "<hr><center>" NGINX_VER "</center>" CRLF
22 "</body>" CRLF
23 "</html>" CRLF
27 static u_char ngx_http_error_tail[] =
28 "<hr><center>nginx</center>" CRLF
29 "</body>" CRLF
30 "</html>" CRLF
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
44 static u_char ngx_http_msie_refresh_head[] =
45 "<html><head><meta http-equiv=\"Refresh\" content=\"0; URL=";
48 static u_char ngx_http_msie_refresh_tail[] =
49 "\"></head><body></body></html>" CRLF;
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
274 static ngx_str_t ngx_http_error_pages[] = {
276 ngx_null_string, /* 201, 204 */
278 #define NGX_HTTP_LAST_LEVEL_200 202
279 #define NGX_HTTP_LEVEL_200 (NGX_HTTP_LAST_LEVEL_200 - 201)
281 /* ngx_null_string, */ /* 300 */
282 ngx_string(ngx_http_error_301_page),
283 ngx_string(ngx_http_error_302_page),
284 ngx_null_string, /* 303 */
286 #define NGX_HTTP_LAST_LEVEL_300 304
287 #define NGX_HTTP_LEVEL_300 (NGX_HTTP_LAST_LEVEL_300 - 301)
289 ngx_string(ngx_http_error_400_page),
290 ngx_string(ngx_http_error_401_page),
291 ngx_string(ngx_http_error_402_page),
292 ngx_string(ngx_http_error_403_page),
293 ngx_string(ngx_http_error_404_page),
294 ngx_string(ngx_http_error_405_page),
295 ngx_string(ngx_http_error_406_page),
296 ngx_null_string, /* 407 */
297 ngx_string(ngx_http_error_408_page),
298 ngx_string(ngx_http_error_409_page),
299 ngx_string(ngx_http_error_410_page),
300 ngx_string(ngx_http_error_411_page),
301 ngx_string(ngx_http_error_412_page),
302 ngx_string(ngx_http_error_413_page),
303 ngx_string(ngx_http_error_414_page),
304 ngx_string(ngx_http_error_415_page),
305 ngx_string(ngx_http_error_416_page),
307 #define NGX_HTTP_LAST_LEVEL_400 417
308 #define NGX_HTTP_LEVEL_400 (NGX_HTTP_LAST_LEVEL_400 - 400)
310 ngx_string(ngx_http_error_495_page), /* 495, https certificate error */
311 ngx_string(ngx_http_error_496_page), /* 496, https no certificate */
312 ngx_string(ngx_http_error_497_page), /* 497, http to https */
313 ngx_string(ngx_http_error_404_page), /* 498, canceled */
314 ngx_null_string, /* 499, client has closed connection */
316 ngx_string(ngx_http_error_500_page),
317 ngx_string(ngx_http_error_501_page),
318 ngx_string(ngx_http_error_502_page),
319 ngx_string(ngx_http_error_503_page),
320 ngx_string(ngx_http_error_504_page),
321 ngx_null_string, /* 505 */
322 ngx_null_string, /* 506 */
323 ngx_string(ngx_http_error_507_page)
325 #define NGX_HTTP_LAST_LEVEL_500 508
330 static ngx_str_t ngx_http_get_name = { 3, (u_char *) "GET " };
333 ngx_int_t
334 ngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error)
336 ngx_uint_t i, err;
337 ngx_http_err_page_t *err_page;
338 ngx_http_core_loc_conf_t *clcf;
340 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
341 "http special response: %d, \"%V?%V\"",
342 error, &r->uri, &r->args);
344 r->err_status = error;
346 if (r->keepalive) {
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;
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;
369 r->headers_out.content_type.len = 0;
371 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
373 if (!r->error_page && clcf->error_pages && r->uri_changes != 0) {
375 if (clcf->recursive_error_pages == 0) {
376 r->error_page = 1;
379 err_page = clcf->error_pages->elts;
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]);
388 r->expect_tested = 1;
390 if (ngx_http_discard_request_body(r) != NGX_OK) {
391 error = NGX_HTTP_INTERNAL_SERVER_ERROR;
394 if (clcf->msie_refresh
395 && r->headers_in.msie
396 && (error == NGX_HTTP_MOVED_PERMANENTLY
397 || error == NGX_HTTP_MOVED_TEMPORARILY))
399 return ngx_http_send_refresh(r);
402 if (error == NGX_HTTP_CREATED) {
403 /* 201 */
404 err = 0;
405 r->header_only = 1;
407 } else if (error == NGX_HTTP_NO_CONTENT) {
408 /* 204 */
409 err = 0;
411 } else if (error >= NGX_HTTP_MOVED_PERMANENTLY
412 && error < NGX_HTTP_LAST_LEVEL_300)
414 /* 3XX */
415 err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200;
417 } else if (error >= NGX_HTTP_BAD_REQUEST
418 && error < NGX_HTTP_LAST_LEVEL_400)
420 /* 4XX */
421 err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_LEVEL_200
422 + NGX_HTTP_LEVEL_300;
424 } else if (error >= NGX_HTTP_OWN_CODES
425 && error < NGX_HTTP_LAST_LEVEL_500)
427 /* 49X, 5XX */
428 err = error - NGX_HTTP_OWN_CODES + NGX_HTTP_LEVEL_200
429 + NGX_HTTP_LEVEL_300
430 + NGX_HTTP_LEVEL_400;
431 switch (error) {
432 case NGX_HTTP_TO_HTTPS:
433 case NGX_HTTPS_CERT_ERROR:
434 case NGX_HTTPS_NO_CERT:
435 r->err_status = NGX_HTTP_BAD_REQUEST;
436 break;
439 } else {
440 /* unknown code, zero body */
441 err = 0;
444 return ngx_http_send_special_response(r, clcf, err);
448 ngx_int_t
449 ngx_http_filter_finalize_request(ngx_http_request_t *r, ngx_module_t *m,
450 ngx_int_t error)
452 void *ctx;
453 ngx_int_t rc;
455 ngx_http_clean_header(r);
457 ctx = NULL;
459 if (m) {
460 ctx = r->ctx[m->ctx_index];
463 /* clear the modules contexts */
464 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
466 if (m) {
467 r->ctx[m->ctx_index] = ctx;
470 r->filter_finalize = 1;
472 rc = ngx_http_special_response_handler(r, error);
474 /* NGX_ERROR resets any pending data */
476 switch (rc) {
478 case NGX_OK:
479 case NGX_DONE:
480 return NGX_ERROR;
482 default:
483 return rc;
488 void
489 ngx_http_clean_header(ngx_http_request_t *r)
491 ngx_memzero(&r->headers_out.status,
492 sizeof(ngx_http_headers_out_t)
493 - offsetof(ngx_http_headers_out_t, status));
495 r->headers_out.headers.part.nelts = 0;
496 r->headers_out.headers.part.next = NULL;
497 r->headers_out.headers.last = &r->headers_out.headers.part;
499 r->headers_out.content_length_n = -1;
500 r->headers_out.last_modified_time = -1;
504 static ngx_int_t
505 ngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)
507 ngx_int_t overwrite;
508 ngx_str_t uri, args;
509 ngx_table_elt_t *location;
510 ngx_http_core_loc_conf_t *clcf;
512 overwrite = err_page->overwrite;
514 if (overwrite && overwrite != NGX_HTTP_OK) {
515 r->expect_tested = 1;
518 r->err_status = overwrite;
520 r->zero_in_uri = 0;
522 if (ngx_http_complex_value(r, &err_page->value, &uri) != NGX_OK) {
523 return NGX_ERROR;
526 if (uri.data[0] == '/') {
528 if (err_page->value.lengths) {
529 ngx_http_split_args(r, &uri, &args);
531 } else {
532 args = err_page->args;
535 if (r->method != NGX_HTTP_HEAD) {
536 r->method = NGX_HTTP_GET;
537 r->method_name = ngx_http_get_name;
540 return ngx_http_internal_redirect(r, &uri, &args);
543 if (uri.data[0] == '@') {
544 return ngx_http_named_location(r, &uri);
547 location = ngx_list_push(&r->headers_out.headers);
549 if (location == NULL) {
550 return NGX_ERROR;
553 r->err_status = NGX_HTTP_MOVED_TEMPORARILY;
555 location->hash = 1;
556 location->key.len = sizeof("Location") - 1;
557 location->key.data = (u_char *) "Location";
558 location->value = uri;
560 r->headers_out.location = location;
562 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
564 if (clcf->msie_refresh && r->headers_in.msie) {
565 return ngx_http_send_refresh(r);
568 return ngx_http_send_special_response(r, clcf, NGX_HTTP_MOVED_TEMPORARILY
569 - NGX_HTTP_MOVED_PERMANENTLY
570 + NGX_HTTP_LEVEL_200);
574 static ngx_int_t
575 ngx_http_send_special_response(ngx_http_request_t *r,
576 ngx_http_core_loc_conf_t *clcf, ngx_uint_t err)
578 u_char *tail;
579 size_t len;
580 ngx_int_t rc;
581 ngx_buf_t *b;
582 ngx_uint_t msie_padding;
583 ngx_chain_t out[3];
585 if (clcf->server_tokens) {
586 len = sizeof(ngx_http_error_full_tail) - 1;
587 tail = ngx_http_error_full_tail;
589 } else {
590 len = sizeof(ngx_http_error_tail) - 1;
591 tail = ngx_http_error_tail;
594 msie_padding = 0;
596 if (!r->zero_body) {
597 if (ngx_http_error_pages[err].len) {
598 r->headers_out.content_length_n = ngx_http_error_pages[err].len
599 + len;
600 if (clcf->msie_padding
601 && r->headers_in.msie
602 && r->http_version >= NGX_HTTP_VERSION_10
603 && err >= NGX_HTTP_LEVEL_300)
605 r->headers_out.content_length_n +=
606 sizeof(ngx_http_msie_stub) - 1;
607 msie_padding = 1;
610 r->headers_out.content_type_len = sizeof("text/html") - 1;
611 r->headers_out.content_type.len = sizeof("text/html") - 1;
612 r->headers_out.content_type.data = (u_char *) "text/html";
613 r->headers_out.content_type_lowcase = NULL;
615 } else {
616 r->headers_out.content_length_n = -1;
619 } else {
620 r->headers_out.content_length_n = 0;
621 err = 0;
624 if (r->headers_out.content_length) {
625 r->headers_out.content_length->hash = 0;
626 r->headers_out.content_length = NULL;
629 ngx_http_clear_accept_ranges(r);
630 ngx_http_clear_last_modified(r);
632 rc = ngx_http_send_header(r);
634 if (rc == NGX_ERROR || r->header_only) {
635 return rc;
638 if (ngx_http_error_pages[err].len == 0) {
639 return NGX_OK;
642 b = ngx_calloc_buf(r->pool);
643 if (b == NULL) {
644 return NGX_ERROR;
647 b->memory = 1;
648 b->pos = ngx_http_error_pages[err].data;
649 b->last = ngx_http_error_pages[err].data + ngx_http_error_pages[err].len;
651 out[0].buf = b;
652 out[0].next = &out[1];
654 b = ngx_calloc_buf(r->pool);
655 if (b == NULL) {
656 return NGX_ERROR;
659 b->memory = 1;
661 b->pos = tail;
662 b->last = tail + len;
664 out[1].buf = b;
665 out[1].next = NULL;
667 if (msie_padding) {
668 b = ngx_calloc_buf(r->pool);
669 if (b == NULL) {
670 return NGX_ERROR;
673 b->memory = 1;
674 b->pos = ngx_http_msie_stub;
675 b->last = ngx_http_msie_stub + sizeof(ngx_http_msie_stub) - 1;
677 out[1].next = &out[2];
678 out[2].buf = b;
679 out[2].next = NULL;
682 if (r == r->main) {
683 b->last_buf = 1;
686 b->last_in_chain = 1;
688 return ngx_http_output_filter(r, &out[0]);
692 static ngx_int_t
693 ngx_http_send_refresh(ngx_http_request_t *r)
695 u_char *p, *location;
696 size_t len, size;
697 uintptr_t escape;
698 ngx_int_t rc;
699 ngx_buf_t *b;
700 ngx_chain_t out;
702 len = r->headers_out.location->value.len;
703 location = r->headers_out.location->value.data;
705 escape = 2 * ngx_escape_uri(NULL, location, len, NGX_ESCAPE_REFRESH);
707 size = sizeof(ngx_http_msie_refresh_head) - 1
708 + escape + len
709 + sizeof(ngx_http_msie_refresh_tail) - 1;
711 r->err_status = NGX_HTTP_OK;
713 r->headers_out.content_type_len = sizeof("text/html") - 1;
714 r->headers_out.content_type.len = sizeof("text/html") - 1;
715 r->headers_out.content_type.data = (u_char *) "text/html";
716 r->headers_out.content_type_lowcase = NULL;
718 r->headers_out.location->hash = 0;
719 r->headers_out.location = NULL;
721 r->headers_out.content_length_n = size;
723 if (r->headers_out.content_length) {
724 r->headers_out.content_length->hash = 0;
725 r->headers_out.content_length = NULL;
728 ngx_http_clear_accept_ranges(r);
729 ngx_http_clear_last_modified(r);
731 rc = ngx_http_send_header(r);
733 if (rc == NGX_ERROR || r->header_only) {
734 return rc;
737 b = ngx_create_temp_buf(r->pool, size);
738 if (b == NULL) {
739 return NGX_ERROR;
742 p = ngx_cpymem(b->pos, ngx_http_msie_refresh_head,
743 sizeof(ngx_http_msie_refresh_head) - 1);
745 if (escape == 0) {
746 p = ngx_cpymem(p, location, len);
748 } else {
749 p = (u_char *) ngx_escape_uri(p, location, len, NGX_ESCAPE_REFRESH);
752 b->last = ngx_cpymem(p, ngx_http_msie_refresh_tail,
753 sizeof(ngx_http_msie_refresh_tail) - 1);
755 b->last_buf = 1;
756 b->last_in_chain = 1;
758 out.buf = b;
759 out.next = NULL;
761 return ngx_http_output_filter(r, &out);