nginx/src/http/ngx_http_special_response.c

365 lines
9.5 KiB
C
Raw Normal View History

2002-12-11 02:05:12 +08:00
/*
* Copyright (C) Igor Sysoev
*/
2002-12-11 02:05:12 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
2003-06-02 23:24:30 +08:00
#include <nginx.h>
2003-02-07 01:21:13 +08:00
2002-12-11 02:05:12 +08:00
2004-03-16 15:10:12 +08:00
static u_char error_tail[] =
2002-12-15 14:25:09 +08:00
"<hr><center>" NGINX_VER "</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
2003-02-07 01:21:13 +08:00
2004-03-16 15:10:12 +08:00
static u_char msie_stub[] =
2003-06-11 23:28:34 +08:00
"<!-- The padding to disable MSIE's friendly error page -->" CRLF
"<!-- The padding to disable MSIE's friendly error page -->" CRLF
"<!-- The padding to disable MSIE's friendly error page -->" CRLF
"<!-- The padding to disable MSIE's friendly error page -->" CRLF
"<!-- The padding to disable MSIE's friendly error page -->" CRLF
"<!-- The padding to disable MSIE's friendly error page -->" CRLF
;
2003-10-13 00:49:16 +08:00
static char error_301_page[] =
"<html>" CRLF
"<head><title>301 Moved Permanently</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>301 Moved Permanently</h1></center>" CRLF
;
2003-06-02 23:24:30 +08:00
static char error_302_page[] =
"<html>" CRLF
"<head><title>302 Found</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>302 Found</h1></center>" CRLF
;
2002-12-15 14:25:09 +08:00
static char error_400_page[] =
"<html>" CRLF
"<head><title>400 Bad Request</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>400 Bad Request</h1></center>" CRLF
;
2003-02-07 01:21:13 +08:00
2003-01-10 14:09:20 +08:00
static char error_403_page[] =
"<html>" CRLF
"<head><title>403 Forbidden</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>403 Forbidden</h1></center>" CRLF
;
2003-02-07 01:21:13 +08:00
2002-12-15 14:25:09 +08:00
static char error_404_page[] =
"<html>" CRLF
"<head><title>404 Not Found</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>404 Not Found</h1></center>" CRLF
;
2003-02-07 01:21:13 +08:00
2003-05-27 20:18:54 +08:00
static char error_405_page[] =
"<html>" CRLF
"<head><title>405 Not Allowed</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>405 Not Allowed</h1></center>" CRLF
;
2003-03-21 00:09:44 +08:00
static char error_408_page[] =
"<html>" CRLF
"<head><title>408 Request Time-out</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>408 Request Time-out</h1></center>" CRLF
;
2003-10-28 05:01:00 +08:00
static char error_413_page[] =
"<html>" CRLF
"<head><title>413 Request Entity Too Large</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>413 Request Entity Too Large</h1></center>" CRLF
;
2003-03-12 04:38:13 +08:00
static char error_414_page[] =
"<html>" CRLF
"<head><title>414 Request-URI Too Large</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>414 Request-URI Too Large</h1></center>" CRLF
;
2003-06-02 23:24:30 +08:00
static char error_416_page[] =
"<html>" CRLF
"<head><title>416 Requested Range Not Satisfiable</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>416 Requested Range Not Satisfiable</h1></center>" CRLF
;
2004-07-09 15:12:14 +08:00
static char error_497_page[] =
"<html>" CRLF
2004-07-19 03:11:20 +08:00
"<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>"
CRLF
2004-07-09 15:12:14 +08:00
"<body bgcolor=\"white\">" CRLF
2004-07-19 03:11:20 +08:00
"<center><h1>400 Bad Request</h1></center>" CRLF
"<center>The plain HTTP request was sent to HTTPS port</center>" CRLF
2004-07-09 15:12:14 +08:00
;
2003-01-15 15:02:27 +08:00
static char error_500_page[] =
"<html>" CRLF
"<head><title>500 Internal Server Error</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>500 Internal Server Error</h1></center>" CRLF
;
2002-12-15 14:25:09 +08:00
2004-01-20 02:09:14 +08:00
static char error_501_page[] =
"<html>" CRLF
"<head><title>501 Method Not Implemented</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>500 Method Not Implemented</h1></center>" CRLF
;
2003-03-21 00:09:44 +08:00
static char error_502_page[] =
"<html>" CRLF
"<head><title>502 Bad Gateway</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>502 Bad Gateway</h1></center>" CRLF
;
2003-10-28 05:01:00 +08:00
static char error_503_page[] =
"<html>" CRLF
"<head><title>503 Service Temporarily Unavailable</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>503 Service Temporarily Unavailable</h1></center>" CRLF
;
2003-03-21 00:09:44 +08:00
static char error_504_page[] =
"<html>" CRLF
"<head><title>504 Gateway Time-out</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>504 Gateway Time-out</h1></center>" CRLF
;
2002-12-15 14:25:09 +08:00
static ngx_str_t error_pages[] = {
2003-10-13 00:49:16 +08:00
/* ngx_null_string, */ /* 300 */
ngx_string(error_301_page),
2003-06-02 23:24:30 +08:00
ngx_string(error_302_page),
2003-03-21 00:09:44 +08:00
ngx_null_string, /* 303 */
ngx_string(error_400_page),
ngx_null_string, /* 401 */
ngx_null_string, /* 402 */
ngx_string(error_403_page),
ngx_string(error_404_page),
2003-05-27 20:18:54 +08:00
ngx_string(error_405_page),
2003-03-21 00:09:44 +08:00
ngx_null_string, /* 406 */
ngx_null_string, /* 407 */
ngx_string(error_408_page),
ngx_null_string, /* 409 */
ngx_null_string, /* 410 */
ngx_null_string, /* 411 */
ngx_null_string, /* 412 */
2003-10-28 05:01:00 +08:00
ngx_string(error_413_page),
2003-03-21 00:09:44 +08:00
ngx_string(error_414_page),
ngx_null_string, /* 415 */
2003-06-02 23:24:30 +08:00
ngx_string(error_416_page),
2003-03-21 00:09:44 +08:00
2004-07-19 03:11:20 +08:00
ngx_string(error_497_page), /* 497, http to https */
2004-07-09 15:12:14 +08:00
ngx_string(error_404_page), /* 498, invalid host name */
ngx_null_string, /* 499, client closed connection */
2004-05-15 00:51:47 +08:00
2003-03-21 00:09:44 +08:00
ngx_string(error_500_page),
2004-01-20 02:09:14 +08:00
ngx_string(error_501_page),
2003-03-21 00:09:44 +08:00
ngx_string(error_502_page),
2003-10-28 05:01:00 +08:00
ngx_string(error_503_page),
2003-03-21 00:09:44 +08:00
ngx_string(error_504_page)
2002-12-15 14:25:09 +08:00
};
2002-12-11 02:05:12 +08:00
2003-02-07 01:21:13 +08:00
2004-06-16 23:32:11 +08:00
ngx_int_t ngx_http_special_response_handler(ngx_http_request_t *r, int error)
2002-12-11 02:05:12 +08:00
{
2004-03-16 15:10:12 +08:00
ngx_int_t rc;
2004-06-16 23:32:11 +08:00
ngx_uint_t err, i, msie_padding;
ngx_buf_t *b;
2003-10-24 14:53:41 +08:00
ngx_chain_t *out, **ll, *cl;
ngx_http_err_page_t *err_page;
2003-10-24 14:53:41 +08:00
ngx_http_core_loc_conf_t *clcf;
2002-12-15 14:25:09 +08:00
2003-10-28 00:16:17 +08:00
rc = ngx_http_discard_body(r);
2004-03-23 14:01:52 +08:00
if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
error = NGX_HTTP_INTERNAL_SERVER_ERROR;
2003-10-28 00:16:17 +08:00
}
2002-12-15 14:25:09 +08:00
r->headers_out.status = error;
2003-03-13 01:32:22 +08:00
if (r->keepalive != 0) {
switch (error) {
case NGX_HTTP_BAD_REQUEST:
2003-10-28 05:01:00 +08:00
case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:
2003-03-13 01:32:22 +08:00
case NGX_HTTP_REQUEST_URI_TOO_LARGE:
2004-07-09 15:12:14 +08:00
case NGX_HTTP_TO_HTTPS:
2003-03-13 01:32:22 +08:00
case NGX_HTTP_INTERNAL_SERVER_ERROR:
r->keepalive = 0;
}
}
2002-12-15 14:25:09 +08:00
2003-05-15 01:13:13 +08:00
if (r->lingering_close == 1) {
switch (error) {
case NGX_HTTP_BAD_REQUEST:
2004-07-09 15:12:14 +08:00
case NGX_HTTP_TO_HTTPS:
2003-05-15 01:13:13 +08:00
r->lingering_close = 0;
}
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2003-11-12 02:13:43 +08:00
if (r->err_ctx == NULL && clcf->error_pages) {
err_page = clcf->error_pages->elts;
for (i = 0; i < clcf->error_pages->nelts; i++) {
2004-04-26 04:13:21 +08:00
if (err_page[i].status == error) {
2004-04-22 02:54:33 +08:00
if (err_page[i].overwrite) {
r->err_status = err_page[i].overwrite;
} else {
r->err_status = error;
}
2003-11-12 02:13:43 +08:00
r->err_ctx = r->ctx;
return ngx_http_internal_redirect(r, &err_page[i].uri, NULL);
}
}
}
if (error < NGX_HTTP_BAD_REQUEST) {
/* 3XX */
err = error - NGX_HTTP_MOVED_PERMANENTLY;
2004-05-15 00:51:47 +08:00
} else if (error < NGX_HTTP_NGX_CODES) {
/* 4XX */
err = error - NGX_HTTP_BAD_REQUEST + 3;
} else {
2004-05-15 00:51:47 +08:00
/* 49X, 5XX */
err = error - NGX_HTTP_NGX_CODES + 3 + 17;
switch (error) {
2004-07-09 15:12:14 +08:00
case NGX_HTTP_TO_HTTPS:
r->headers_out.status = NGX_HTTP_BAD_REQUEST;
error = NGX_HTTP_BAD_REQUEST;
break;
2004-05-15 00:51:47 +08:00
case NGX_HTTP_INVALID_HOST:
r->headers_out.status = NGX_HTTP_NOT_FOUND;
error = NGX_HTTP_NOT_FOUND;
break;
}
}
2004-06-16 23:32:11 +08:00
msie_padding = 0;
2003-06-02 23:24:30 +08:00
if (error_pages[err].len) {
2003-10-22 00:49:56 +08:00
r->headers_out.content_length_n = error_pages[err].len
2004-05-05 01:56:58 +08:00
+ sizeof(error_tail) - 1;
if (clcf->msie_padding
2004-06-16 23:32:11 +08:00
&& r->headers_in.msie
2004-05-05 01:56:58 +08:00
&& r->http_version >= NGX_HTTP_VERSION_10
&& error >= NGX_HTTP_BAD_REQUEST
&& error != NGX_HTTP_REQUEST_URI_TOO_LARGE)
{
r->headers_out.content_length_n += sizeof(msie_stub) - 1;
2004-06-16 23:32:11 +08:00
msie_padding = 1;
2004-05-05 01:56:58 +08:00
}
2003-06-02 23:24:30 +08:00
r->headers_out.content_type = ngx_list_push(&r->headers_out.headers);
if (r->headers_out.content_type == NULL) {
2003-10-29 16:30:44 +08:00
return NGX_ERROR;
}
2003-06-02 23:24:30 +08:00
2003-10-30 01:39:05 +08:00
r->headers_out.content_type->key.len = sizeof("Content-Type") - 1;
2004-03-17 05:26:01 +08:00
r->headers_out.content_type->key.data = (u_char *) "Content-Type";
2003-10-30 01:39:05 +08:00
r->headers_out.content_type->value.len = sizeof("text/html") - 1;
2004-03-17 05:26:01 +08:00
r->headers_out.content_type->value.data = (u_char *) "text/html";
2003-06-02 23:24:30 +08:00
} else {
2003-10-22 00:49:56 +08:00
r->headers_out.content_length_n = -1;
2003-10-30 01:39:05 +08:00
}
if (r->headers_out.content_length) {
r->headers_out.content_length->key.len = 0;
2003-10-22 00:49:56 +08:00
r->headers_out.content_length = NULL;
2003-03-13 01:32:22 +08:00
}
2002-12-15 14:25:09 +08:00
2003-06-11 23:28:34 +08:00
rc = ngx_http_send_header(r);
2003-10-09 15:00:45 +08:00
2003-10-10 23:10:50 +08:00
if (rc == NGX_ERROR || r->header_only) {
return rc;
2003-06-11 23:28:34 +08:00
}
2003-02-07 01:21:13 +08:00
if (error_pages[err].len == 0) {
2002-12-15 14:25:09 +08:00
return NGX_OK;
2003-02-07 01:21:13 +08:00
}
2002-12-15 14:25:09 +08:00
2003-10-22 00:49:56 +08:00
out = NULL;
2003-10-23 00:38:26 +08:00
ll = NULL;
2002-12-15 14:25:09 +08:00
if (!(b = ngx_calloc_buf(r->pool))) {
2003-10-29 16:30:44 +08:00
return NGX_ERROR;
}
b->memory = 1;
b->pos = error_pages[err].data;
b->last = error_pages[err].data + error_pages[err].len;
2002-12-15 14:25:09 +08:00
ngx_alloc_link_and_set_buf(cl, b, r->pool, NGX_ERROR);
2003-10-23 00:38:26 +08:00
ngx_chain_add_link(out, ll, cl);
2002-12-11 02:05:12 +08:00
2003-06-11 23:28:34 +08:00
if (!(b = ngx_calloc_buf(r->pool))) {
2003-10-29 16:30:44 +08:00
return NGX_ERROR;
}
b->memory = 1;
b->pos = error_tail;
b->last = error_tail + sizeof(error_tail) - 1;
2003-06-11 23:28:34 +08:00
ngx_alloc_link_and_set_buf(cl, b, r->pool, NGX_ERROR);
2003-10-23 00:38:26 +08:00
ngx_chain_add_link(out, ll, cl);
2003-10-22 00:49:56 +08:00
2004-06-16 23:32:11 +08:00
if (msie_padding) {
if (!(b = ngx_calloc_buf(r->pool))) {
2003-10-29 16:30:44 +08:00
return NGX_ERROR;
}
b->memory = 1;
b->pos = msie_stub;
b->last = msie_stub + sizeof(msie_stub) - 1;
2003-10-22 00:49:56 +08:00
ngx_alloc_link_and_set_buf(cl, b, r->pool, NGX_ERROR);
2003-10-23 00:38:26 +08:00
ngx_chain_add_link(out, ll, cl);
2003-06-11 23:28:34 +08:00
}
b->last_buf = 1;
2003-06-11 23:28:34 +08:00
2003-10-22 00:49:56 +08:00
return ngx_http_output_filter(r, out);
2002-12-11 02:05:12 +08:00
}