1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 20:17:42 +00:00

LibWeb: Heap-allocate returned Fetch::Infrastructure::{Request,Response}

A Request/Response instance should always be heap-allocated and have
clear ownership, so let's also wrap it in a NonnullOwnPtr instead of
putting them on the stack.
This commit is contained in:
Linus Groh 2022-09-24 00:28:11 +01:00
parent 88ee15a497
commit a602a4c780
4 changed files with 25 additions and 25 deletions

View file

@ -158,14 +158,14 @@ ErrorOr<ByteBuffer> Request::byte_serialize_origin() const
} }
// https://fetch.spec.whatwg.org/#concept-request-clone // https://fetch.spec.whatwg.org/#concept-request-clone
Request Request::clone() const NonnullOwnPtr<Request> Request::clone() const
{ {
// To clone a request request, run these steps: // To clone a request request, run these steps:
// 1. Let newRequest be a copy of request, except for its body. // 1. Let newRequest be a copy of request, except for its body.
BodyType body; BodyType body;
swap(body, const_cast<BodyType&>(m_body)); swap(body, const_cast<BodyType&>(m_body));
auto new_request = *this; auto new_request = adopt_own(*new Infrastructure::Request(*this));
swap(body, const_cast<BodyType&>(m_body)); swap(body, const_cast<BodyType&>(m_body));
// FIXME: 2. If requests body is non-null, set newRequests body to the result of cloning requests body. // FIXME: 2. If requests body is non-null, set newRequests body to the result of cloning requests body.

View file

@ -284,7 +284,7 @@ public:
[[nodiscard]] String serialize_origin() const; [[nodiscard]] String serialize_origin() const;
[[nodiscard]] ErrorOr<ByteBuffer> byte_serialize_origin() const; [[nodiscard]] ErrorOr<ByteBuffer> byte_serialize_origin() const;
[[nodiscard]] Request clone() const; [[nodiscard]] NonnullOwnPtr<Request> clone() const;
[[nodiscard]] ErrorOr<void> add_range_reader(u64 first, Optional<u64> const& last); [[nodiscard]] ErrorOr<void> add_range_reader(u64 first, Optional<u64> const& last);

View file

@ -12,19 +12,19 @@ namespace Web::Fetch::Infrastructure {
// A network error is a response whose status is always 0, status message is always // A network error is a response whose status is always 0, status message is always
// the empty byte sequence, header list is always empty, and body is always null. // the empty byte sequence, header list is always empty, and body is always null.
Response Response::aborted_network_error() NonnullOwnPtr<Response> Response::aborted_network_error()
{ {
auto response = network_error(); auto response = network_error();
response.set_aborted(true); response->set_aborted(true);
return response; return response;
} }
Response Response::network_error() NonnullOwnPtr<Response> Response::network_error()
{ {
Response response; auto response = make<Response>();
response.set_status(0); response->set_status(0);
response.set_type(Type::Error); response->set_type(Type::Error);
VERIFY(!response.body().has_value()); VERIFY(!response->body().has_value());
return response; return response;
} }
@ -85,7 +85,7 @@ FilteredResponse::~FilteredResponse()
{ {
} }
ErrorOr<BasicFilteredResponse> BasicFilteredResponse::create(Response& internal_response) ErrorOr<NonnullOwnPtr<BasicFilteredResponse>> BasicFilteredResponse::create(Response& internal_response)
{ {
// A basic filtered response is a filtered response whose type is "basic" and header list excludes // A basic filtered response is a filtered response whose type is "basic" and header list excludes
// any headers in internal responses header list whose name is a forbidden response-header name. // any headers in internal responses header list whose name is a forbidden response-header name.
@ -95,7 +95,7 @@ ErrorOr<BasicFilteredResponse> BasicFilteredResponse::create(Response& internal_
TRY(header_list.append(header)); TRY(header_list.append(header));
} }
return BasicFilteredResponse(internal_response, move(header_list)); return adopt_own(*new BasicFilteredResponse(internal_response, move(header_list)));
} }
BasicFilteredResponse::BasicFilteredResponse(Response& internal_response, HeaderList header_list) BasicFilteredResponse::BasicFilteredResponse(Response& internal_response, HeaderList header_list)
@ -104,7 +104,7 @@ BasicFilteredResponse::BasicFilteredResponse(Response& internal_response, Header
{ {
} }
ErrorOr<CORSFilteredResponse> CORSFilteredResponse::create(Response& internal_response) ErrorOr<NonnullOwnPtr<CORSFilteredResponse>> CORSFilteredResponse::create(Response& internal_response)
{ {
// A CORS filtered response is a filtered response whose type is "cors" and header list excludes // A CORS filtered response is a filtered response whose type is "cors" and header list excludes
// any headers in internal responses header list whose name is not a CORS-safelisted response-header // any headers in internal responses header list whose name is not a CORS-safelisted response-header
@ -119,7 +119,7 @@ ErrorOr<CORSFilteredResponse> CORSFilteredResponse::create(Response& internal_re
TRY(header_list.append(header)); TRY(header_list.append(header));
} }
return CORSFilteredResponse(internal_response, move(header_list)); return adopt_own(*new CORSFilteredResponse(internal_response, move(header_list)));
} }
CORSFilteredResponse::CORSFilteredResponse(Response& internal_response, HeaderList header_list) CORSFilteredResponse::CORSFilteredResponse(Response& internal_response, HeaderList header_list)
@ -128,11 +128,11 @@ CORSFilteredResponse::CORSFilteredResponse(Response& internal_response, HeaderLi
{ {
} }
OpaqueFilteredResponse OpaqueFilteredResponse::create(Response& internal_response) NonnullOwnPtr<OpaqueFilteredResponse> OpaqueFilteredResponse::create(Response& internal_response)
{ {
// An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", // An opaque-redirect filtered response is a filtered response whose type is "opaqueredirect",
// status is 0, status message is the empty byte sequence, header list is empty, and body is null. // status is 0, status message is the empty byte sequence, header list is empty, and body is null.
return OpaqueFilteredResponse(internal_response); return adopt_own(*new OpaqueFilteredResponse(internal_response));
} }
OpaqueFilteredResponse::OpaqueFilteredResponse(Response& internal_response) OpaqueFilteredResponse::OpaqueFilteredResponse(Response& internal_response)
@ -140,9 +140,9 @@ OpaqueFilteredResponse::OpaqueFilteredResponse(Response& internal_response)
{ {
} }
OpaqueRedirectFilteredResponse OpaqueRedirectFilteredResponse::create(Response& internal_response) NonnullOwnPtr<OpaqueRedirectFilteredResponse> OpaqueRedirectFilteredResponse::create(Response& internal_response)
{ {
return OpaqueRedirectFilteredResponse(internal_response); return adopt_own(*new OpaqueRedirectFilteredResponse(internal_response));
} }
OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(Response& internal_response) OpaqueRedirectFilteredResponse::OpaqueRedirectFilteredResponse(Response& internal_response)

View file

@ -44,8 +44,8 @@ public:
u64 decoded_size { 0 }; u64 decoded_size { 0 };
}; };
[[nodiscard]] static Response aborted_network_error(); [[nodiscard]] static NonnullOwnPtr<Response> aborted_network_error();
[[nodiscard]] static Response network_error(); [[nodiscard]] static NonnullOwnPtr<Response> network_error();
Response() = default; Response() = default;
virtual ~Response() = default; virtual ~Response() = default;
@ -157,7 +157,7 @@ private:
}; };
// https://fetch.spec.whatwg.org/#concept-filtered-response // https://fetch.spec.whatwg.org/#concept-filtered-response
class FilteredResponse : protected Response { class FilteredResponse : public Response {
public: public:
explicit FilteredResponse(Response&); explicit FilteredResponse(Response&);
virtual ~FilteredResponse() = 0; virtual ~FilteredResponse() = 0;
@ -187,7 +187,7 @@ protected:
// https://fetch.spec.whatwg.org/#concept-filtered-response-basic // https://fetch.spec.whatwg.org/#concept-filtered-response-basic
class BasicFilteredResponse final : public FilteredResponse { class BasicFilteredResponse final : public FilteredResponse {
public: public:
static ErrorOr<BasicFilteredResponse> create(Response&); static ErrorOr<NonnullOwnPtr<BasicFilteredResponse>> create(Response&);
[[nodiscard]] virtual Type type() const override { return Type::Basic; } [[nodiscard]] virtual Type type() const override { return Type::Basic; }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; } [[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; }
@ -201,7 +201,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-filtered-response-cors // https://fetch.spec.whatwg.org/#concept-filtered-response-cors
class CORSFilteredResponse final : public FilteredResponse { class CORSFilteredResponse final : public FilteredResponse {
public: public:
static ErrorOr<CORSFilteredResponse> create(Response&); static ErrorOr<NonnullOwnPtr<CORSFilteredResponse>> create(Response&);
[[nodiscard]] virtual Type type() const override { return Type::CORS; } [[nodiscard]] virtual Type type() const override { return Type::CORS; }
[[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; } [[nodiscard]] virtual HeaderList const& header_list() const override { return m_header_list; }
@ -215,7 +215,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-filtered-response-opaque // https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
class OpaqueFilteredResponse final : public FilteredResponse { class OpaqueFilteredResponse final : public FilteredResponse {
public: public:
static OpaqueFilteredResponse create(Response&); static NonnullOwnPtr<OpaqueFilteredResponse> create(Response&);
[[nodiscard]] virtual Type type() const override { return Type::Opaque; } [[nodiscard]] virtual Type type() const override { return Type::Opaque; }
[[nodiscard]] virtual Vector<AK::URL> const& url_list() const override { return m_url_list; } [[nodiscard]] virtual Vector<AK::URL> const& url_list() const override { return m_url_list; }
@ -235,7 +235,7 @@ private:
// https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect // https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect
class OpaqueRedirectFilteredResponse final : public FilteredResponse { class OpaqueRedirectFilteredResponse final : public FilteredResponse {
public: public:
static OpaqueRedirectFilteredResponse create(Response&); static NonnullOwnPtr<OpaqueRedirectFilteredResponse> create(Response&);
[[nodiscard]] virtual Type type() const override { return Type::OpaqueRedirect; } [[nodiscard]] virtual Type type() const override { return Type::OpaqueRedirect; }
[[nodiscard]] virtual Status status() const override { return 0; } [[nodiscard]] virtual Status status() const override { return 0; }