Skip to content

Truly independent web browser

LibWeb+LibWebCommon: Move file response headers to LibWebCommon

Andreas Kling68b9ce5564bf
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index 68e8172e709..fe5c45849f3 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -9,7 +9,6 @@
#include <AK/Debug.h>
#include <AK/NeverDestroyed.h>
#include <LibCore/Directory.h>
-#include <LibCore/MimeData.h>
#include <LibCore/Resource.h>
#include <LibCore/System.h>
#include <LibGC/Function.h>
@@ -29,6 +28,7 @@
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Platform/Timer.h>
+#include <LibWebCommon/Loader/FileResponseHeaders.h>
#include <LibWebCommon/Loader/UserAgent.h>
namespace Web {
@@ -219,26 +219,6 @@ bool ResourceLoader::is_known_hsts_host(Page& page, String const& host)
return page.client().page_did_is_known_hsts_host(host);
}
-static NonnullRefPtr<HTTP::HeaderList> response_headers_for_file(StringView path, size_t content_length, Optional<time_t> const& modified_time)
-{
- // For file:// and resource:// URLs, we have to guess the MIME type, since there's no HTTP header to tell us what
- // it is. We insert a fake Content-Type header here, so that clients can use it to learn the MIME type.
- auto mime_type = Core::guess_mime_type_based_on_filename(path);
-
- auto response_headers = HTTP::HeaderList::create({
- { "Access-Control-Allow-Origin"sv, "null"sv },
- { "Content-Type"sv, mime_type },
- { "Content-Length"sv, ByteString::number(content_length) },
- });
-
- if (modified_time.has_value()) {
- auto const datetime = AK::UnixDateTime::from_seconds_since_epoch(modified_time.value());
- response_headers->set({ "Last-Modified"sv, datetime.to_byte_string("%a, %d %b %Y %H:%M:%S GMT"sv, AK::UnixDateTime::LocalTime::No) });
- }
-
- return response_headers;
-}
-
static void log_request_start(LoadRequest const& request)
{
auto url_for_logging = sanitized_url_for_logging(*request.url());
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index 4e24ba9c49d..e09102428e3 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -47,6 +47,7 @@ set(SOURCES
Infra/Strings.cpp
Loader/ContentBlocking.cpp
Loader/DownloadFilename.cpp
+ Loader/FileResponseHeaders.cpp
Loader/LoaderConfigSnapshot.cpp
Loader/SiteCompatibility.cpp
MediaSession/MediaSessionState.cpp
diff --git a/Libraries/LibWebCommon/Loader/FileResponseHeaders.cpp b/Libraries/LibWebCommon/Loader/FileResponseHeaders.cpp
new file mode 100644
index 00000000000..56c3eba453d
--- /dev/null
+++ b/Libraries/LibWebCommon/Loader/FileResponseHeaders.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteString.h>
+#include <AK/Time.h>
+#include <LibCore/MimeData.h>
+#include <LibWebCommon/Loader/FileResponseHeaders.h>
+
+namespace Web {
+
+NonnullRefPtr<HTTP::HeaderList> response_headers_for_file(StringView path, size_t content_length, Optional<time_t> const& modified_time)
+{
+ // For file:// and resource:// URLs, we have to guess the MIME type, since there's no HTTP header to tell us what
+ // it is. We insert a fake Content-Type header here, so that clients can use it to learn the MIME type.
+ auto mime_type = Core::guess_mime_type_based_on_filename(path);
+
+ auto response_headers = HTTP::HeaderList::create({
+ { "Access-Control-Allow-Origin"sv, "null"sv },
+ { "Content-Type"sv, mime_type },
+ { "Content-Length"sv, ByteString::number(content_length) },
+ });
+
+ if (modified_time.has_value()) {
+ auto const datetime = AK::UnixDateTime::from_seconds_since_epoch(modified_time.value());
+ response_headers->set({ "Last-Modified"sv, datetime.to_byte_string("%a, %d %b %Y %H:%M:%S GMT"sv, AK::UnixDateTime::LocalTime::No) });
+ }
+
+ return response_headers;
+}
+
+}
diff --git a/Libraries/LibWebCommon/Loader/FileResponseHeaders.h b/Libraries/LibWebCommon/Loader/FileResponseHeaders.h
new file mode 100644
index 00000000000..3883674979b
--- /dev/null
+++ b/Libraries/LibWebCommon/Loader/FileResponseHeaders.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullRefPtr.h>
+#include <AK/Optional.h>
+#include <AK/StringView.h>
+#include <LibHTTP/HeaderList.h>
+#include <LibWebCommon/Export.h>
+#include <time.h>
+
+namespace Web {
+
+// The headers of a response to a request for a file: or resource: URL, which has no HTTP headers of its own.
+WEBCOMMON_API NonnullRefPtr<HTTP::HeaderList> response_headers_for_file(StringView path, size_t content_length, Optional<time_t> const& modified_time);
+
+}