mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibWeb: Use Optional<String> for encoding_from_content_type
This patch changes the encoding_from_content_type function to only return an encoding if it actually finds one, and leave it up to the caller to decided on a default to use. It also modifies the caller to expect an Optional<String> (instead of relying on the null state of the String class) as a return value and separates the encoding and MIME type determination. This will be built upon in a further commit.
This commit is contained in:
parent
d325403cb5
commit
ce6d6706a6
Notes:
sideshowbarker
2024-07-18 17:48:47 +09:00
Author: https://github.com/MaxWipfli Commit: https://github.com/SerenityOS/serenity/commit/ce6d6706a67 Pull-request: https://github.com/SerenityOS/serenity/pull/7055 Issue: https://github.com/SerenityOS/serenity/issues/6910 Reviewed-by: https://github.com/Dexesttp Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/linusg
1 changed files with 14 additions and 6 deletions
|
@ -41,7 +41,7 @@ void Resource::for_each_client(Function<void(ResourceClient&)> callback)
|
|||
}
|
||||
}
|
||||
|
||||
static String encoding_from_content_type(const String& content_type)
|
||||
static Optional<String> encoding_from_content_type(const String& content_type)
|
||||
{
|
||||
auto offset = content_type.index_of("charset=");
|
||||
if (offset.has_value()) {
|
||||
|
@ -53,7 +53,7 @@ static String encoding_from_content_type(const String& content_type)
|
|||
return encoding;
|
||||
}
|
||||
|
||||
return "utf-8";
|
||||
return {};
|
||||
}
|
||||
|
||||
static String mime_type_from_content_type(const String& content_type)
|
||||
|
@ -74,20 +74,28 @@ void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap
|
|||
m_loaded = true;
|
||||
|
||||
auto content_type = headers.get("Content-Type");
|
||||
|
||||
if (content_type.has_value()) {
|
||||
dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.value());
|
||||
m_encoding = encoding_from_content_type(content_type.value());
|
||||
m_mime_type = mime_type_from_content_type(content_type.value());
|
||||
} else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
|
||||
dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type());
|
||||
m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
|
||||
m_mime_type = url().data_mime_type();
|
||||
} else {
|
||||
dbgln_if(RESOURCE_DEBUG, "No Content-Type header to go on! Guessing based on filename...");
|
||||
m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
|
||||
m_mime_type = Core::guess_mime_type_based_on_filename(url().path());
|
||||
}
|
||||
|
||||
if (content_type.has_value()) {
|
||||
auto encoding = encoding_from_content_type(content_type.value());
|
||||
if (encoding.has_value()) {
|
||||
dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.has_value());
|
||||
m_encoding = encoding.value();
|
||||
} else {
|
||||
// FIXME: This doesn't seem nice.
|
||||
m_encoding = "utf-8";
|
||||
}
|
||||
}
|
||||
|
||||
for_each_client([](auto& client) {
|
||||
client.resource_did_load();
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue