/* * Copyright (C) Igor Sysoev */ #include #include #include #include static u_char error_tail[] = "
" NGINX_VER "
" CRLF "" CRLF "" CRLF ; static u_char ngx_http_msie_stub[] = "" CRLF "" CRLF "" CRLF "" CRLF "" CRLF "" CRLF ; static char error_301_page[] = "" CRLF "301 Moved Permanently" CRLF "" CRLF "

301 Moved Permanently

" CRLF ; static char error_302_page[] = "" CRLF "302 Found" CRLF "" CRLF "

302 Found

" CRLF ; static char error_400_page[] = "" CRLF "400 Bad Request" CRLF "" CRLF "

400 Bad Request

" CRLF ; static char error_401_page[] = "" CRLF "401 Authorization Required" CRLF "" CRLF "

401 Authorization Required

" CRLF ; static char error_402_page[] = "" CRLF "402 Payment Required" CRLF "" CRLF "

402 Payment Required

" CRLF ; static char error_403_page[] = "" CRLF "403 Forbidden" CRLF "" CRLF "

403 Forbidden

" CRLF ; static char error_404_page[] = "" CRLF "404 Not Found" CRLF "" CRLF "

404 Not Found

" CRLF ; static char error_405_page[] = "" CRLF "405 Not Allowed" CRLF "" CRLF "

405 Not Allowed

" CRLF ; static char error_406_page[] = "" CRLF "406 Not Acceptable" CRLF "" CRLF "

406 Not Acceptable

" CRLF ; static char error_408_page[] = "" CRLF "408 Request Time-out" CRLF "" CRLF "

408 Request Time-out

" CRLF ; static char error_410_page[] = "" CRLF "410 Gone" CRLF "" CRLF "

410 Gone

" CRLF ; static char error_411_page[] = "" CRLF "411 Length Required" CRLF "" CRLF "

411 Length Required

" CRLF ; static char error_413_page[] = "" CRLF "413 Request Entity Too Large" CRLF "" CRLF "

413 Request Entity Too Large

" CRLF ; static char error_414_page[] = "" CRLF "414 Request-URI Too Large" CRLF "" CRLF "

414 Request-URI Too Large

" CRLF ; static char error_416_page[] = "" CRLF "416 Requested Range Not Satisfiable" CRLF "" CRLF "

416 Requested Range Not Satisfiable

" CRLF ; static char error_497_page[] = "" CRLF "400 The plain HTTP request was sent to HTTPS port" CRLF "" CRLF "

400 Bad Request

" CRLF "
The plain HTTP request was sent to HTTPS port
" CRLF ; static char error_500_page[] = "" CRLF "500 Internal Server Error" CRLF "" CRLF "

500 Internal Server Error

" CRLF ; static char error_501_page[] = "" CRLF "501 Method Not Implemented" CRLF "" CRLF "

501 Method Not Implemented

" CRLF ; static char error_502_page[] = "" CRLF "502 Bad Gateway" CRLF "" CRLF "

502 Bad Gateway

" CRLF ; static char error_503_page[] = "" CRLF "503 Service Temporarily Unavailable" CRLF "" CRLF "

503 Service Temporarily Unavailable

" CRLF ; static char error_504_page[] = "" CRLF "504 Gateway Time-out" CRLF "" CRLF "

504 Gateway Time-out

" CRLF ; static ngx_str_t error_pages[] = { ngx_null_string, /* 204 */ #define NGX_HTTP_LEVEL_200 1 /* ngx_null_string, */ /* 300 */ ngx_string(error_301_page), ngx_string(error_302_page), ngx_null_string, /* 303 */ #define NGX_HTTP_LEVEL_300 3 ngx_string(error_400_page), ngx_string(error_401_page), ngx_string(error_402_page), ngx_string(error_403_page), ngx_string(error_404_page), ngx_string(error_405_page), ngx_string(error_406_page), ngx_null_string, /* 407 */ ngx_string(error_408_page), ngx_null_string, /* 409 */ ngx_string(error_410_page), ngx_string(error_411_page), ngx_null_string, /* 412 */ ngx_string(error_413_page), ngx_string(error_414_page), ngx_null_string, /* 415 */ ngx_string(error_416_page), #define NGX_HTTP_LEVEL_400 17 ngx_string(error_497_page), /* 497, http to https */ ngx_string(error_404_page), /* 498, invalid host name */ ngx_null_string, /* 499, client had closed connection */ ngx_string(error_500_page), ngx_string(error_501_page), ngx_string(error_502_page), ngx_string(error_503_page), ngx_string(error_504_page) }; ngx_int_t ngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error) { ngx_int_t rc; ngx_uint_t i, err, msie_padding; ngx_buf_t *b; ngx_chain_t *out, *cl; ngx_http_err_page_t *err_page; ngx_http_core_loc_conf_t *clcf; ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http special response: %d, \"%V\"", error, &r->uri); rc = ngx_http_discard_body(r); if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) { error = NGX_HTTP_INTERNAL_SERVER_ERROR; } r->headers_out.status = error; if (r->keepalive != 0) { switch (error) { case NGX_HTTP_BAD_REQUEST: case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE: case NGX_HTTP_REQUEST_URI_TOO_LARGE: case NGX_HTTP_TO_HTTPS: case NGX_HTTP_INTERNAL_SERVER_ERROR: r->keepalive = 0; } } if (r->lingering_close == 1) { switch (error) { case NGX_HTTP_BAD_REQUEST: case NGX_HTTP_TO_HTTPS: r->lingering_close = 0; } } clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); if (r->err_ctx == NULL && clcf->error_pages) { err_page = clcf->error_pages->elts; for (i = 0; i < clcf->error_pages->nelts; i++) { if (err_page[i].status == error) { r->err_status = err_page[i].overwrite; r->err_ctx = r->ctx; r->method = NGX_HTTP_GET; if (err_page[i].uri.data[0] == '/') { return ngx_http_internal_redirect(r, &err_page[i].uri, NULL); } r->headers_out.location = ngx_list_push(&r->headers_out.headers); if (r->headers_out.location) { r->err_status = NGX_HTTP_MOVED_TEMPORARILY; error = NGX_HTTP_MOVED_TEMPORARILY; r->headers_out.location->hash = 1; r->headers_out.location->key.len = sizeof("Location") - 1; r->headers_out.location->key.data = (u_char *) "Location"; r->headers_out.location->value = err_page[i].uri; } else { error = NGX_HTTP_INTERNAL_SERVER_ERROR; } } } } if (error == NGX_HTTP_NO_CONTENT) { /* 204 */ err = 0; } else if (error < NGX_HTTP_BAD_REQUEST) { /* 3XX */ err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200; } else if (error < NGX_HTTP_OWN_CODES) { /* 4XX */ err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_LEVEL_200 + NGX_HTTP_LEVEL_300; } else { /* 49X, 5XX */ err = error - NGX_HTTP_OWN_CODES + NGX_HTTP_LEVEL_200 + NGX_HTTP_LEVEL_300 + NGX_HTTP_LEVEL_400; switch (error) { case NGX_HTTP_TO_HTTPS: r->headers_out.status = NGX_HTTP_BAD_REQUEST; error = NGX_HTTP_BAD_REQUEST; break; } } msie_padding = 0; if (error_pages[err].len) { r->headers_out.content_length_n = error_pages[err].len + sizeof(error_tail) - 1; if (clcf->msie_padding && r->headers_in.msie && 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(ngx_http_msie_stub) - 1; msie_padding = 1; } r->headers_out.content_type.len = sizeof("text/html") - 1; r->headers_out.content_type.data = (u_char *) "text/html"; } else { r->headers_out.content_length_n = -1; } if (r->headers_out.content_length) { r->headers_out.content_length->hash = 0; r->headers_out.content_length = NULL; } ngx_http_clear_accept_ranges(r); ngx_http_clear_last_modified(r); rc = ngx_http_send_header(r); if (rc == NGX_ERROR || r->header_only) { return rc; } if (error_pages[err].len == 0) { return NGX_OK; } b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } b->memory = 1; b->pos = error_pages[err].data; b->last = error_pages[err].data + error_pages[err].len; cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; out = cl; b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } b->memory = 1; b->pos = error_tail; b->last = error_tail + sizeof(error_tail) - 1; cl->next = ngx_alloc_chain_link(r->pool); if (cl->next == NULL) { return NGX_ERROR; } cl = cl->next; cl->buf = b; if (msie_padding) { b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } b->memory = 1; b->pos = ngx_http_msie_stub; b->last = ngx_http_msie_stub + sizeof(ngx_http_msie_stub) - 1; cl->next = ngx_alloc_chain_link(r->pool); if (cl->next == NULL) { return NGX_ERROR; } cl = cl->next; cl->buf = b; } if (r == r->main) { b->last_buf = 1; } b->last_in_chain = 1; cl->next = NULL; return ngx_http_output_filter(r, out); }