mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:07:45 +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
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=");
|
auto offset = content_type.index_of("charset=");
|
||||||
if (offset.has_value()) {
|
if (offset.has_value()) {
|
||||||
|
@ -53,7 +53,7 @@ static String encoding_from_content_type(const String& content_type)
|
||||||
return encoding;
|
return encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "utf-8";
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
static String mime_type_from_content_type(const String& content_type)
|
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;
|
m_loaded = true;
|
||||||
|
|
||||||
auto content_type = headers.get("Content-Type");
|
auto content_type = headers.get("Content-Type");
|
||||||
|
|
||||||
if (content_type.has_value()) {
|
if (content_type.has_value()) {
|
||||||
dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.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());
|
m_mime_type = mime_type_from_content_type(content_type.value());
|
||||||
} else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
|
} 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());
|
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();
|
m_mime_type = url().data_mime_type();
|
||||||
} else {
|
} 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());
|
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) {
|
for_each_client([](auto& client) {
|
||||||
client.resource_did_load();
|
client.resource_did_load();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue