From 208d71561aec026967f2dd90149978917c87c48e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 4 Nov 2016 02:18:42 +0900 Subject: [PATCH] src: Add missing mandatory SP after status code --- src/HttpServer.cc | 7 +- src/asio_server_request_handler.cc | 11 +++- src/http2.cc | 102 ++++++++++++++--------------- src/http2.h | 6 +- src/shrpx_http.cc | 7 +- src/shrpx_https_upstream.cc | 13 ++-- src/shrpx_spdy_upstream.cc | 18 +++-- 7 files changed, 94 insertions(+), 70 deletions(-) diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 09f33d63..272a42df 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -1921,13 +1921,18 @@ namespace { FileEntry make_status_body(int status, uint16_t port) { BlockAllocator balloc(1024, 1024); - auto status_string = http2::get_status_string(balloc, status); + auto status_string = http2::stringify_status(balloc, status); + auto reason_pharase = http2::get_reason_phrase(status); std::string body; body = ""; body += status_string; + body += ' '; + body += reason_pharase; body += "

"; body += status_string; + body += ' '; + body += reason_pharase; body += "


"; body += NGHTTPD_SERVER; body += " at port "; diff --git a/src/asio_server_request_handler.cc b/src/asio_server_request_handler.cc index 6e0f0744..b7012d28 100644 --- a/src/asio_server_request_handler.cc +++ b/src/asio_server_request_handler.cc @@ -36,11 +36,16 @@ std::string create_html(int status_code) { BlockAllocator balloc(1024, 1024); std::string res; res.reserve(512); - auto status = ::nghttp2::http2::get_status_string(balloc, status_code); + auto status_string = ::nghttp2::http2::stringify_status(balloc, status_code); + auto reason_phrase = ::nghttp2::http2::get_reason_phrase(status_code); res += R"()"; - res += status; + res += status_string; + res += ' '; + res += reason_phrase; res += "

"; - res += status; + res += status_string; + res += ' '; + res += reason_phrase; res += "

"; return res; } diff --git a/src/http2.cc b/src/http2.cc index 458ec8a7..fb790677 100644 --- a/src/http2.cc +++ b/src/http2.cc @@ -30,107 +30,107 @@ namespace nghttp2 { namespace http2 { -StringRef get_status_string(BlockAllocator &balloc, unsigned int status_code) { +StringRef get_reason_phrase(unsigned int status_code) { switch (status_code) { case 100: - return StringRef::from_lit("100 Continue"); + return StringRef::from_lit("Continue"); case 101: - return StringRef::from_lit("101 Switching Protocols"); + return StringRef::from_lit("Switching Protocols"); case 200: - return StringRef::from_lit("200 OK"); + return StringRef::from_lit("OK"); case 201: - return StringRef::from_lit("201 Created"); + return StringRef::from_lit("Created"); case 202: - return StringRef::from_lit("202 Accepted"); + return StringRef::from_lit("Accepted"); case 203: - return StringRef::from_lit("203 Non-Authoritative Information"); + return StringRef::from_lit("Non-Authoritative Information"); case 204: - return StringRef::from_lit("204 No Content"); + return StringRef::from_lit("No Content"); case 205: - return StringRef::from_lit("205 Reset Content"); + return StringRef::from_lit("Reset Content"); case 206: - return StringRef::from_lit("206 Partial Content"); + return StringRef::from_lit("Partial Content"); case 300: - return StringRef::from_lit("300 Multiple Choices"); + return StringRef::from_lit("Multiple Choices"); case 301: - return StringRef::from_lit("301 Moved Permanently"); + return StringRef::from_lit("Moved Permanently"); case 302: - return StringRef::from_lit("302 Found"); + return StringRef::from_lit("Found"); case 303: - return StringRef::from_lit("303 See Other"); + return StringRef::from_lit("See Other"); case 304: - return StringRef::from_lit("304 Not Modified"); + return StringRef::from_lit("Not Modified"); case 305: - return StringRef::from_lit("305 Use Proxy"); - // case 306: return StringRef::from_lit("306 (Unused)"); + return StringRef::from_lit("Use Proxy"); + // case 306: return StringRef::from_lit("(Unused)"); case 307: - return StringRef::from_lit("307 Temporary Redirect"); + return StringRef::from_lit("Temporary Redirect"); case 308: - return StringRef::from_lit("308 Permanent Redirect"); + return StringRef::from_lit("Permanent Redirect"); case 400: - return StringRef::from_lit("400 Bad Request"); + return StringRef::from_lit("Bad Request"); case 401: - return StringRef::from_lit("401 Unauthorized"); + return StringRef::from_lit("Unauthorized"); case 402: - return StringRef::from_lit("402 Payment Required"); + return StringRef::from_lit("Payment Required"); case 403: - return StringRef::from_lit("403 Forbidden"); + return StringRef::from_lit("Forbidden"); case 404: - return StringRef::from_lit("404 Not Found"); + return StringRef::from_lit("Not Found"); case 405: - return StringRef::from_lit("405 Method Not Allowed"); + return StringRef::from_lit("Method Not Allowed"); case 406: - return StringRef::from_lit("406 Not Acceptable"); + return StringRef::from_lit("Not Acceptable"); case 407: - return StringRef::from_lit("407 Proxy Authentication Required"); + return StringRef::from_lit("Proxy Authentication Required"); case 408: - return StringRef::from_lit("408 Request Timeout"); + return StringRef::from_lit("Request Timeout"); case 409: - return StringRef::from_lit("409 Conflict"); + return StringRef::from_lit("Conflict"); case 410: - return StringRef::from_lit("410 Gone"); + return StringRef::from_lit("Gone"); case 411: - return StringRef::from_lit("411 Length Required"); + return StringRef::from_lit("Length Required"); case 412: - return StringRef::from_lit("412 Precondition Failed"); + return StringRef::from_lit("Precondition Failed"); case 413: - return StringRef::from_lit("413 Payload Too Large"); + return StringRef::from_lit("Payload Too Large"); case 414: - return StringRef::from_lit("414 URI Too Long"); + return StringRef::from_lit("URI Too Long"); case 415: - return StringRef::from_lit("415 Unsupported Media Type"); + return StringRef::from_lit("Unsupported Media Type"); case 416: - return StringRef::from_lit("416 Requested Range Not Satisfiable"); + return StringRef::from_lit("Requested Range Not Satisfiable"); case 417: - return StringRef::from_lit("417 Expectation Failed"); + return StringRef::from_lit("Expectation Failed"); case 421: - return StringRef::from_lit("421 Misdirected Request"); + return StringRef::from_lit("Misdirected Request"); case 426: - return StringRef::from_lit("426 Upgrade Required"); + return StringRef::from_lit("Upgrade Required"); case 428: - return StringRef::from_lit("428 Precondition Required"); + return StringRef::from_lit("Precondition Required"); case 429: - return StringRef::from_lit("429 Too Many Requests"); + return StringRef::from_lit("Too Many Requests"); case 431: - return StringRef::from_lit("431 Request Header Fields Too Large"); + return StringRef::from_lit("Request Header Fields Too Large"); case 451: - return StringRef::from_lit("451 Unavailable For Legal Reasons"); + return StringRef::from_lit("Unavailable For Legal Reasons"); case 500: - return StringRef::from_lit("500 Internal Server Error"); + return StringRef::from_lit("Internal Server Error"); case 501: - return StringRef::from_lit("501 Not Implemented"); + return StringRef::from_lit("Not Implemented"); case 502: - return StringRef::from_lit("502 Bad Gateway"); + return StringRef::from_lit("Bad Gateway"); case 503: - return StringRef::from_lit("503 Service Unavailable"); + return StringRef::from_lit("Service Unavailable"); case 504: - return StringRef::from_lit("504 Gateway Timeout"); + return StringRef::from_lit("Gateway Timeout"); case 505: - return StringRef::from_lit("505 HTTP Version Not Supported"); + return StringRef::from_lit("HTTP Version Not Supported"); case 511: - return StringRef::from_lit("511 Network Authentication Required"); + return StringRef::from_lit("Network Authentication Required"); default: - return util::make_string_ref_uint(balloc, status_code); + return StringRef{}; } } diff --git a/src/http2.h b/src/http2.h index e996dd5c..6f12d2cd 100644 --- a/src/http2.h +++ b/src/http2.h @@ -94,9 +94,9 @@ using HeaderRefs = std::vector; namespace http2 { -// Returns string version of |status code| followed by reason -// string. (e.g., "404 Not Found"). -StringRef get_status_string(BlockAllocator &balloc, unsigned int status_code); +// Returns reason-phrase for given |status code|. If there is no +// known reason-phrase for the given code, returns empty string. +StringRef get_reason_phrase(unsigned int status_code); // Returns string version of |status_code|. (e.g., "404") StringRef stringify_status(BlockAllocator &balloc, unsigned int status_code); diff --git a/src/shrpx_http.cc b/src/shrpx_http.cc index 77a641e3..c7ade2d1 100644 --- a/src/shrpx_http.cc +++ b/src/shrpx_http.cc @@ -45,11 +45,14 @@ StringRef create_error_html(BlockAllocator &balloc, unsigned int http_status) { } } - auto status_string = http2::get_status_string(balloc, http_status); + auto status_string = http2::stringify_status(balloc, http_status); + auto reason_phrase = http2::get_reason_phrase(http_status); return concat_string_ref( balloc, StringRef::from_lit(R"()"), - status_string, StringRef::from_lit("

"), status_string, + status_string, StringRef::from_lit(" "), reason_phrase, + StringRef::from_lit("

"), status_string, + StringRef::from_lit(" "), reason_phrase, StringRef::from_lit("

")); } diff --git a/src/shrpx_https_upstream.cc b/src/shrpx_https_upstream.cc index a55c6bab..25e23603 100644 --- a/src/shrpx_https_upstream.cc +++ b/src/shrpx_https_upstream.cc @@ -863,7 +863,9 @@ int HttpsUpstream::send_reply(Downstream *downstream, const uint8_t *body, auto output = downstream->get_response_buf(); output->append("HTTP/1.1 "); - output->append(http2::get_status_string(balloc, resp.http_status)); + output->append(http2::stringify_status(balloc, resp.http_status)); + output->append(' '); + output->append(http2::get_reason_phrase(resp.http_status)); output->append("\r\n"); for (auto &kv : resp.fs.headers()) { @@ -923,8 +925,9 @@ void HttpsUpstream::error_reply(unsigned int status_code) { auto output = downstream->get_response_buf(); output->append("HTTP/1.1 "); - auto status_str = http2::get_status_string(balloc, status_code); - output->append(status_str); + output->append(http2::stringify_status(balloc, status_code)); + output->append(' '); + output->append(http2::get_reason_phrase(status_code)); output->append("\r\nServer: "); output->append(get_config()->http.server_name); output->append("\r\nContent-Length: "); @@ -1011,7 +1014,9 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) { buf->append("."); buf->append(util::utos(req.http_minor)); buf->append(" "); - buf->append(http2::get_status_string(balloc, resp.http_status)); + buf->append(http2::stringify_status(balloc, resp.http_status)); + buf->append(' '); + buf->append(http2::get_reason_phrase(resp.http_status)); buf->append("\r\n"); auto config = get_config(); diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index f476dc05..b6129e09 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -918,7 +918,9 @@ int SpdyUpstream::send_reply(Downstream *downstream, const uint8_t *body, const auto &resp = downstream->response(); auto &balloc = downstream->get_block_allocator(); - auto status_string = http2::get_status_string(balloc, resp.http_status); + auto status_line = concat_string_ref( + balloc, http2::stringify_status(balloc, resp.http_status), + StringRef::from_lit(" "), http2::get_reason_phrase(resp.http_status)); const auto &headers = resp.fs.headers(); @@ -931,7 +933,7 @@ int SpdyUpstream::send_reply(Downstream *downstream, const uint8_t *body, httpconf.add_response_headers.size() * 2 + 1); nva.push_back(":status"); - nva.push_back(status_string.c_str()); + nva.push_back(status_line.c_str()); nva.push_back(":version"); nva.push_back("HTTP/1.1"); @@ -999,9 +1001,11 @@ int SpdyUpstream::error_reply(Downstream *downstream, lgconf->update_tstamp(std::chrono::system_clock::now()); auto content_length = util::make_string_ref_uint(balloc, html.size()); - auto status_string = http2::get_status_string(balloc, status_code); + auto status_line = concat_string_ref( + balloc, http2::stringify_status(balloc, status_code), + StringRef::from_lit(" "), http2::get_reason_phrase(status_code)); - const char *nv[] = {":status", status_string.c_str(), + const char *nv[] = {":status", status_line.c_str(), ":version", "http/1.1", "content-type", "text/html; charset=UTF-8", "server", get_config()->http.server_name.c_str(), @@ -1104,9 +1108,11 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) { size_t hdidx = 0; std::string via_value; - auto status_string = http2::get_status_string(balloc, resp.http_status); + auto status_line = concat_string_ref( + balloc, http2::stringify_status(balloc, resp.http_status), + StringRef::from_lit(" "), http2::get_reason_phrase(resp.http_status)); nv[hdidx++] = ":status"; - nv[hdidx++] = status_string.c_str(); + nv[hdidx++] = status_line.c_str(); nv[hdidx++] = ":version"; nv[hdidx++] = "HTTP/1.1"; for (auto &hd : resp.fs.headers()) {