nginx/src/http/ngx_http_special_response.c

116 lines
2.7 KiB
C
Raw Normal View History

2002-12-11 02:05:12 +08:00
2002-12-15 14:25:09 +08:00
#include <nginx.h>
2002-12-11 02:05:12 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2002-12-15 14:25:09 +08:00
#include <ngx_string.h>
2002-12-11 02:05:12 +08:00
#include <ngx_http.h>
2003-02-07 01:21:13 +08:00
#include <ngx_http_output_filter.h>
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
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-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
static ngx_str_t error_pages[] = {
{ 0, NULL}, /* 301 */
{ 0, NULL}, /* 302 */
{ 0, NULL}, /* 303 */
{ 0, NULL}, /* 304 */
{ sizeof(error_400_page) - 1, error_400_page },
{ 0, NULL}, /* 401 */
{ 0, NULL}, /* 402 */
2003-01-10 14:09:20 +08:00
{ sizeof(error_403_page) - 1, error_403_page },
2002-12-15 14:25:09 +08:00
{ sizeof(error_404_page) - 1, error_404_page },
2003-01-15 15:02:27 +08:00
{ sizeof(error_500_page) - 1, error_500_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
2002-12-11 02:05:12 +08:00
int ngx_http_special_response(ngx_http_request_t *r, int error)
{
2003-02-07 01:21:13 +08:00
int err, len;
2002-12-15 14:25:09 +08:00
ngx_hunk_t *message, *tail;
len = 0;
r->headers_out.status = error;
if (error < NGX_HTTP_BAD_REQUEST)
err = error - NGX_HTTP_MOVED_PERMANENTLY;
else if (error < NGX_HTTP_INTERNAL_SERVER_ERROR)
err = error - NGX_HTTP_BAD_REQUEST + 4;
else
2003-01-15 15:02:27 +08:00
err = error - NGX_HTTP_INTERNAL_SERVER_ERROR + 4 + 5;
2002-12-15 14:25:09 +08:00
if (error_pages[err].len == 0)
r->headers_out.content_length = -1;
else
r->headers_out.content_length = error_pages[err].len
+ len + sizeof(error_tail);
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
message->type = NGX_HUNK_MEMORY;
message->pos.mem = error_pages[err].data;
message->last.mem = error_pages[err].data + error_pages[err].len;
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
2002-12-15 14:25:09 +08:00
tail->type = NGX_HUNK_MEMORY|NGX_HUNK_LAST;
tail->pos.mem = error_tail;
tail->last.mem = 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
}