diff --git a/Libraries/LibHTTP/CMakeLists.txt b/Libraries/LibHTTP/CMakeLists.txt index 172f488307..139cbdb729 100644 --- a/Libraries/LibHTTP/CMakeLists.txt +++ b/Libraries/LibHTTP/CMakeLists.txt @@ -1,6 +1,5 @@ set(SOURCES HttpRequest.cpp - HttpResponse.cpp ) ladybird_lib(LibHTTP http) diff --git a/Libraries/LibHTTP/HttpResponse.cpp b/Libraries/LibHTTP/HttpResponse.cpp deleted file mode 100644 index 82ff75d3bf..0000000000 --- a/Libraries/LibHTTP/HttpResponse.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2022, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include - -namespace HTTP { - -HttpResponse::HttpResponse(int code, HeaderMap&& headers, size_t size) - : m_code(code) - , m_headers(move(headers)) - , m_downloaded_size(size) -{ -} - -StringView HttpResponse::reason_phrase_for_code(int code) -{ - VERIFY(code >= 100 && code <= 599); - - static HashMap s_reason_phrases = { - { 100, "Continue"sv }, - { 101, "Switching Protocols"sv }, - { 200, "OK"sv }, - { 201, "Created"sv }, - { 202, "Accepted"sv }, - { 203, "Non-Authoritative Information"sv }, - { 204, "No Content"sv }, - { 205, "Reset Content"sv }, - { 206, "Partial Content"sv }, - { 300, "Multiple Choices"sv }, - { 301, "Moved Permanently"sv }, - { 302, "Found"sv }, - { 303, "See Other"sv }, - { 304, "Not Modified"sv }, - { 305, "Use Proxy"sv }, - { 307, "Temporary Redirect"sv }, - { 400, "Bad Request"sv }, - { 401, "Unauthorized"sv }, - { 402, "Payment Required"sv }, - { 403, "Forbidden"sv }, - { 404, "Not Found"sv }, - { 405, "Method Not Allowed"sv }, - { 406, "Not Acceptable"sv }, - { 407, "Proxy Authentication Required"sv }, - { 408, "Request Timeout"sv }, - { 409, "Conflict"sv }, - { 410, "Gone"sv }, - { 411, "Length Required"sv }, - { 412, "Precondition Failed"sv }, - { 413, "Payload Too Large"sv }, - { 414, "URI Too Long"sv }, - { 415, "Unsupported Media Type"sv }, - { 416, "Range Not Satisfiable"sv }, - { 417, "Expectation Failed"sv }, - { 418, "I'm a teapot"sv }, - { 421, "Misdirected Request"sv }, - { 422, "Unprocessable Content"sv }, - { 423, "Locked"sv }, - { 424, "Failed Dependency"sv }, - { 425, "Too Early"sv }, - { 426, "Upgrade Required"sv }, - { 428, "Precondition Required"sv }, - { 429, "Too Many Requests"sv }, - { 431, "Request Header Fields Too Large"sv }, - { 451, "Unavailable For Legal Reasons"sv }, - { 500, "Internal Server Error"sv }, - { 501, "Not Implemented"sv }, - { 502, "Bad Gateway"sv }, - { 503, "Service Unavailable"sv }, - { 504, "Gateway Timeout"sv }, - { 505, "HTTP Version Not Supported"sv } - }; - - if (s_reason_phrases.contains(code)) - return s_reason_phrases.ensure(code); - - // NOTE: "A client MUST understand the class of any status code, as indicated by the first - // digit, and treat an unrecognized status code as being equivalent to the x00 status - // code of that class." (RFC 7231, section 6) - auto generic_code = (code / 100) * 100; - VERIFY(s_reason_phrases.contains(generic_code)); - return s_reason_phrases.ensure(generic_code); -} - -} diff --git a/Libraries/LibHTTP/HttpResponse.h b/Libraries/LibHTTP/HttpResponse.h deleted file mode 100644 index 556ae154cf..0000000000 --- a/Libraries/LibHTTP/HttpResponse.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * Copyright (c) 2022, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include - -namespace HTTP { - -class HttpResponse : public Core::NetworkResponse { -public: - virtual ~HttpResponse() override = default; - static NonnullRefPtr create(int code, HeaderMap&& headers, size_t downloaded_size) - { - return adopt_ref(*new HttpResponse(code, move(headers), downloaded_size)); - } - - int code() const { return m_code; } - size_t downloaded_size() const { return m_downloaded_size; } - StringView reason_phrase() const { return reason_phrase_for_code(m_code); } - HeaderMap const& headers() const { return m_headers; } - - static StringView reason_phrase_for_code(int code); - -private: - HttpResponse(int code, HeaderMap&&, size_t size); - - int m_code { 0 }; - HeaderMap m_headers; - size_t m_downloaded_size { 0 }; -}; - -} diff --git a/Libraries/LibHTTP/Status.h b/Libraries/LibHTTP/Status.h new file mode 100644 index 0000000000..7056e2db56 --- /dev/null +++ b/Libraries/LibHTTP/Status.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace HTTP { + +constexpr StringView reason_phrase_for_code(u32 code) +{ + VERIFY(code >= 100 && code <= 599); + + // clang-format off + switch (code) { + case 100: return "Continue"sv; + case 101: return "Switching Protocols"sv; + case 200: return "OK"sv; + case 201: return "Created"sv; + case 202: return "Accepted"sv; + case 203: return "Non-Authoritative Information"sv; + case 204: return "No Content"sv; + case 205: return "Reset Content"sv; + case 206: return "Partial Content"sv; + case 300: return "Multiple Choices"sv; + case 301: return "Moved Permanently"sv; + case 302: return "Found"sv; + case 303: return "See Other"sv; + case 304: return "Not Modified"sv; + case 305: return "Use Proxy"sv; + case 307: return "Temporary Redirect"sv; + case 400: return "Bad Request"sv; + case 401: return "Unauthorized"sv; + case 402: return "Payment Required"sv; + case 403: return "Forbidden"sv; + case 404: return "Not Found"sv; + case 405: return "Method Not Allowed"sv; + case 406: return "Not Acceptable"sv; + case 407: return "Proxy Authentication Required"sv; + case 408: return "Request Timeout"sv; + case 409: return "Conflict"sv; + case 410: return "Gone"sv; + case 411: return "Length Required"sv; + case 412: return "Precondition Failed"sv; + case 413: return "Payload Too Large"sv; + case 414: return "URI Too Long"sv; + case 415: return "Unsupported Media Type"sv; + case 416: return "Range Not Satisfiable"sv; + case 417: return "Expectation Failed"sv; + case 418: return "I'm a teapot"sv; + case 421: return "Misdirected Request"sv; + case 422: return "Unprocessable Content"sv; + case 423: return "Locked"sv; + case 424: return "Failed Dependency"sv; + case 425: return "Too Early"sv; + case 426: return "Upgrade Required"sv; + case 428: return "Precondition Required"sv; + case 429: return "Too Many Requests"sv; + case 431: return "Request Header Fields Too Large"sv; + case 451: return "Unavailable For Legal Reasons"sv; + case 500: return "Internal Server Error"sv; + case 501: return "Not Implemented"sv; + case 502: return "Bad Gateway"sv; + case 503: return "Service Unavailable"sv; + case 504: return "Gateway Timeout"sv; + case 505: return "HTTP Version Not Supported"sv; + default: break; + } + // clang-format on + + // A client MUST understand the class of any status code, as indicated by the first digit, and treat an unrecognized + // status code as being equivalent to the x00 status code of that class. (RFC 7231, section 6) + return reason_phrase_for_code((code / 100) * 100); +} + +} diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp index 51f48e59d9..647644061c 100644 --- a/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include diff --git a/Libraries/LibWeb/WebDriver/Client.cpp b/Libraries/LibWeb/WebDriver/Client.cpp index ebfd663dc8..539d11c55c 100644 --- a/Libraries/LibWeb/WebDriver/Client.cpp +++ b/Libraries/LibWeb/WebDriver/Client.cpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include namespace Web::WebDriver { @@ -326,7 +326,7 @@ ErrorOr Client::send_error_response(HTTP::HttpReques { // FIXME: Implement to spec. dbgln_if(WEBDRIVER_DEBUG, "Sending error response: {} {}: {}", error.http_status, error.error, error.message); - auto reason = HTTP::HttpResponse::reason_phrase_for_code(error.http_status); + auto reason = HTTP::reason_phrase_for_code(error.http_status); JsonObject error_response; error_response.set("error"sv, error.error);