mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibHTTP+LibWeb: Remove unused HTTP::HTTPResponse
The only thing in HTTPResponse being used is reason_phrase_for_code, which is just a static helper method. Move it to its own file and remove HTTPResponse. This is just one less thing to have to port to an upcoming HTTP header refactor.
This commit is contained in:
parent
bf75f52ce0
commit
0480934afb
Notes:
github-actions[bot]
2025-11-27 13:59:13 +00:00
Author: https://github.com/trflynn89
Commit: 0480934afb
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6944
Reviewed-by: https://github.com/gmta ✅
6 changed files with 82 additions and 130 deletions
|
|
@ -1,6 +1,5 @@
|
|||
set(SOURCES
|
||||
HttpRequest.cpp
|
||||
HttpResponse.cpp
|
||||
)
|
||||
|
||||
ladybird_lib(LibHTTP http)
|
||||
|
|
|
|||
|
|
@ -1,88 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
|
||||
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<int, StringView> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibCore/NetworkResponse.h>
|
||||
#include <LibHTTP/HeaderMap.h>
|
||||
|
||||
namespace HTTP {
|
||||
|
||||
class HttpResponse : public Core::NetworkResponse {
|
||||
public:
|
||||
virtual ~HttpResponse() override = default;
|
||||
static NonnullRefPtr<HttpResponse> 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 };
|
||||
};
|
||||
|
||||
}
|
||||
80
Libraries/LibHTTP/Status.h
Normal file
80
Libraries/LibHTTP/Status.h
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -10,7 +10,6 @@
|
|||
#include <LibCore/MimeData.h>
|
||||
#include <LibCore/Resource.h>
|
||||
#include <LibGC/Function.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <LibRequests/Request.h>
|
||||
#include <LibRequests/RequestClient.h>
|
||||
#include <LibURL/Parser.h>
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
#include <AK/StringBuilder.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Time.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <LibHTTP/Status.h>
|
||||
#include <LibWeb/WebDriver/Client.h>
|
||||
|
||||
namespace Web::WebDriver {
|
||||
|
|
@ -326,7 +326,7 @@ ErrorOr<void, Client::WrappedError> 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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue