Skip to content

Commit

Permalink
HTTP/2: the "421 Misdirected Request" response (closes #848).
Browse files Browse the repository at this point in the history
Since 4fbef397c753 nginx rejects with the 400 error any attempts of
requesting different host over the same connection, if the relevant
virtual server requires verification of a client certificate.

While requesting hosts other than negotiated isn't something legal
in HTTP/1.x, the HTTP/2 specification explicitly permits such requests
for connection reuse and has introduced a special response code 421.

According to RFC 7540 Section 9.1.2 this code can be sent by a server
that is not configured to produce responses for the combination of
scheme and authority that are included in the request URI.  And the
client may retry the request over a different connection.

Now this code is used for requests that aren't authorized in current
connection.  After receiving the 421 response a client will be able
to open a new connection, provide the required certificate and retry
the request.

Unfortunately, not all clients currently are able to handle it well.
Notably Chrome just shows an error, while at least the latest version
of Firefox retries the request over a new connection.
  • Loading branch information
VBart committed May 20, 2016
1 parent 61fbcd1 commit f7673bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/http/ngx_http_header_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,28 @@ static ngx_str_t ngx_http_status_lines[] = {
ngx_string("414 Request-URI Too Large"),
ngx_string("415 Unsupported Media Type"),
ngx_string("416 Requested Range Not Satisfiable"),
ngx_null_string, /* "417 Expectation Failed" */
ngx_null_string, /* "418 unused" */
ngx_null_string, /* "419 unused" */
ngx_null_string, /* "420 unused" */
ngx_string("421 Misdirected Request"),

/* ngx_null_string, */ /* "417 Expectation Failed" */
/* ngx_null_string, */ /* "418 unused" */
/* ngx_null_string, */ /* "419 unused" */
/* ngx_null_string, */ /* "420 unused" */
/* ngx_null_string, */ /* "421 unused" */
/* ngx_null_string, */ /* "422 Unprocessable Entity" */
/* ngx_null_string, */ /* "423 Locked" */
/* ngx_null_string, */ /* "424 Failed Dependency" */

#define NGX_HTTP_LAST_4XX 417
#define NGX_HTTP_LAST_4XX 422
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)

ngx_string("500 Internal Server Error"),
ngx_string("501 Not Implemented"),
ngx_string("502 Bad Gateway"),
ngx_string("503 Service Temporarily Unavailable"),
ngx_string("504 Gateway Time-out"),

ngx_null_string, /* "505 HTTP Version Not Supported" */
ngx_null_string, /* "506 Variant Also Negotiates" */
ngx_string("507 Insufficient Storage"),

/* ngx_null_string, */ /* "508 unused" */
/* ngx_null_string, */ /* "509 unused" */
/* ngx_null_string, */ /* "510 Not Extended" */
Expand Down
2 changes: 1 addition & 1 deletion src/http/ngx_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ ngx_http_set_virtual_server(ngx_http_request_t *r, ngx_str_t *host)
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client attempted to request the server name "
"different from that one was negotiated");
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
ngx_http_finalize_request(r, NGX_HTTP_MISDIRECTED_REQUEST);
return NGX_ERROR;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/http/ngx_http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#define NGX_HTTP_REQUEST_URI_TOO_LARGE 414
#define NGX_HTTP_UNSUPPORTED_MEDIA_TYPE 415
#define NGX_HTTP_RANGE_NOT_SATISFIABLE 416
#define NGX_HTTP_MISDIRECTED_REQUEST 421


/* Our own HTTP codes */
Expand Down
15 changes: 14 additions & 1 deletion src/http/ngx_http_special_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ static char ngx_http_error_416_page[] =
;


static char ngx_http_error_421_page[] =
"<html>" CRLF
"<head><title>421 Misdirected Request</title></head>" CRLF
"<body bgcolor=\"white\">" CRLF
"<center><h1>421 Misdirected Request</h1></center>" CRLF
;


static char ngx_http_error_494_page[] =
"<html>" CRLF
"<head><title>400 Request Header Or Cookie Too Large</title></head>"
Expand Down Expand Up @@ -334,8 +342,13 @@ static ngx_str_t ngx_http_error_pages[] = {
ngx_string(ngx_http_error_414_page),
ngx_string(ngx_http_error_415_page),
ngx_string(ngx_http_error_416_page),
ngx_null_string, /* 417 */
ngx_null_string, /* 418 */
ngx_null_string, /* 419 */
ngx_null_string, /* 420 */
ngx_string(ngx_http_error_421_page),

#define NGX_HTTP_LAST_4XX 417
#define NGX_HTTP_LAST_4XX 422
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)

ngx_string(ngx_http_error_494_page), /* 494, request header too large */
Expand Down

0 comments on commit f7673bb

Please sign in to comment.