nginx/src/http/ngx_http_special_response.c

216 lines
5.5 KiB
C
Raw Normal View History

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
2002-12-15 14:25:09 +08:00
static char error_tail[] =
"<hr><center>" NGINX_VER "</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
2003-02-07 01:21:13 +08:00
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-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
;
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
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
;
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-06-02 23:24:30 +08:00
ngx_null_string, /* 300 */
2003-03-21 00:09:44 +08:00
ngx_null_string, /* 301 */
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 */
ngx_null_string, /* 413 */
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
ngx_string(error_500_page),
ngx_null_string, /* 501 */
ngx_string(error_502_page),
ngx_null_string, /* 503 */
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
2003-03-21 00:09:44 +08:00
int ngx_http_special_response_handler(ngx_http_request_t *r, int error)
2002-12-11 02:05:12 +08:00
{
2003-06-02 23:24:30 +08:00
int err;
2002-12-15 14:25:09 +08:00
ngx_hunk_t *message, *tail;
r->headers_out.status = error;
2003-03-13 01:32:22 +08:00
if (error < NGX_HTTP_BAD_REQUEST) {
2003-05-15 01:13:13 +08:00
/* 3XX */
2002-12-15 14:25:09 +08:00
err = error - NGX_HTTP_MOVED_PERMANENTLY;
2003-06-02 23:24:30 +08:00
} else if (error < NGX_HTTP_INTERNAL_SERVER_ERROR) {
/* 4XX */
err = error - NGX_HTTP_BAD_REQUEST + 4;
2003-04-08 23:40:10 +08:00
2003-06-02 23:24:30 +08:00
} else {
/* 5XX */
err = error - NGX_HTTP_INTERNAL_SERVER_ERROR + 4 + 17;
2003-03-13 01:32:22 +08:00
}
if (r->keepalive != 0) {
switch (error) {
case NGX_HTTP_BAD_REQUEST:
case NGX_HTTP_REQUEST_URI_TOO_LARGE:
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:
case NGX_HTTP_REQUEST_URI_TOO_LARGE:
case NGX_HTTP_INTERNAL_SERVER_ERROR:
r->lingering_close = 0;
}
}
2003-06-02 23:24:30 +08:00
if (error_pages[err].len) {
2002-12-15 14:25:09 +08:00
r->headers_out.content_length = error_pages[err].len
2003-06-02 23:24:30 +08:00
+ sizeof(error_tail);
ngx_test_null(r->headers_out.content_type,
ngx_push_table(r->headers_out.headers),
NGX_HTTP_INTERNAL_SERVER_ERROR);
r->headers_out.content_type->key.len = 12;
r->headers_out.content_type->key.data = "Content-Type";
r->headers_out.content_type->value.len = 9;
r->headers_out.content_type->value.data = "text/html";
} else {
r->headers_out.content_length = -1;
2003-03-13 01:32:22 +08:00
}
2002-12-15 14:25:09 +08:00
2003-02-07 01:21:13 +08:00
if (ngx_http_send_header(r) == NGX_ERROR) {
return NGX_ERROR;
}
2002-12-15 14:25:09 +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-02-07 01:21:13 +08:00
ngx_test_null(message, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)), NGX_ERROR);
2002-12-15 14:25:09 +08:00
2003-03-12 04:38:13 +08:00
message->type = NGX_HUNK_MEMORY|NGX_HUNK_IN_MEMORY;
message->pos = error_pages[err].data;
message->last = error_pages[err].data + error_pages[err].len;
2002-12-15 14:25:09 +08:00
2003-02-07 01:21:13 +08:00
if (ngx_http_output_filter(r, message) == NGX_ERROR) {
return NGX_ERROR;
}
2002-12-11 02:05:12 +08:00
2003-02-07 01:21:13 +08:00
ngx_test_null(tail, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)), NGX_ERROR);
2002-12-11 02:05:12 +08:00
2003-03-12 04:38:13 +08:00
tail->type = NGX_HUNK_MEMORY|NGX_HUNK_LAST|NGX_HUNK_IN_MEMORY;
tail->pos = error_tail;
tail->last = error_tail + sizeof(error_tail);
2002-12-11 02:05:12 +08:00
2003-02-07 01:21:13 +08:00
return ngx_http_output_filter(r, tail);
2002-12-11 02:05:12 +08:00
}