diff --git a/Libraries/LibCore/MimeData.cpp b/Libraries/LibCore/MimeData.cpp index 02a4d49b88..1a3ada23cf 100644 --- a/Libraries/LibCore/MimeData.cpp +++ b/Libraries/LibCore/MimeData.cpp @@ -70,30 +70,29 @@ void MimeData::set_text(const String& text) set_data("text/plain", text.to_byte_buffer()); } -String guess_mime_type_based_on_filename(const URL& url) +String guess_mime_type_based_on_filename(const StringView& path) { - String lowercase_url = url.path().to_lowercase(); - if (lowercase_url.ends_with(".pbm")) + if (path.ends_with(".pbm", CaseSensitivity::CaseInsensitive)) return "image/x‑portable‑bitmap"; - if (url.path().ends_with(".pgm")) + if (path.ends_with(".pgm", CaseSensitivity::CaseInsensitive)) return "image/x‑portable‑graymap"; - if (url.path().ends_with(".png")) + if (path.ends_with(".png", CaseSensitivity::CaseInsensitive)) return "image/png"; - if (lowercase_url.ends_with(".ppm")) + if (path.ends_with(".ppm", CaseSensitivity::CaseInsensitive)) return "image/x‑portable‑pixmap"; - if (lowercase_url.ends_with(".gif")) + if (path.ends_with(".gif", CaseSensitivity::CaseInsensitive)) return "image/gif"; - if (lowercase_url.ends_with(".bmp")) + if (path.ends_with(".bmp", CaseSensitivity::CaseInsensitive)) return "image/bmp"; - if (lowercase_url.ends_with(".jpg") || lowercase_url.ends_with(".jpeg")) + if (path.ends_with(".jpg", CaseSensitivity::CaseInsensitive) || path.ends_with(".jpeg", CaseSensitivity::CaseInsensitive)) return "image/jpeg"; - if (lowercase_url.ends_with(".svg")) + if (path.ends_with(".svg", CaseSensitivity::CaseInsensitive)) return "image/svg+xml"; - if (lowercase_url.ends_with(".md")) + if (path.ends_with(".md", CaseSensitivity::CaseInsensitive)) return "text/markdown"; - if (lowercase_url.ends_with(".html") || lowercase_url.ends_with(".htm")) + if (path.ends_with(".html", CaseSensitivity::CaseInsensitive) || path.ends_with(".htm", CaseSensitivity::CaseInsensitive)) return "text/html"; - if (lowercase_url.ends_with("/")) + if (path.ends_with("/", CaseSensitivity::CaseInsensitive)) return "text/html"; return "text/plain"; } diff --git a/Libraries/LibCore/MimeData.h b/Libraries/LibCore/MimeData.h index 0d541fcf57..65bd56c121 100644 --- a/Libraries/LibCore/MimeData.h +++ b/Libraries/LibCore/MimeData.h @@ -61,6 +61,6 @@ private: HashMap m_data; }; -String guess_mime_type_based_on_filename(const URL&); +String guess_mime_type_based_on_filename(const StringView&); } diff --git a/Libraries/LibWeb/Loader/Resource.cpp b/Libraries/LibWeb/Loader/Resource.cpp index 696229dc76..f0a46f1a41 100644 --- a/Libraries/LibWeb/Loader/Resource.cpp +++ b/Libraries/LibWeb/Loader/Resource.cpp @@ -109,7 +109,7 @@ void Resource::did_load(Badge, const ByteBuffer& data, const Has dbg() << "No Content-Type header to go on! Guessing based on filename..."; #endif m_encoding = "utf-8"; // FIXME: This doesn't seem nice. - m_mime_type = Core::guess_mime_type_based_on_filename(url()); + m_mime_type = Core::guess_mime_type_based_on_filename(url().path()); } for_each_client([](auto& client) { diff --git a/Services/WebServer/Client.cpp b/Services/WebServer/Client.cpp index f2cdddfd37..6e8f10dba4 100644 --- a/Services/WebServer/Client.cpp +++ b/Services/WebServer/Client.cpp @@ -96,8 +96,6 @@ void Client::handle_request(ByteBuffer raw_request) path_builder.append(requested_path); auto real_path = path_builder.to_string(); - String forced_mime_type; - if (Core::File::is_directory(real_path)) { if (!request.resource().ends_with("/")) { @@ -119,7 +117,6 @@ void Client::handle_request(ByteBuffer raw_request) return; } real_path = index_html_path; - forced_mime_type = "text/html"; } auto file = Core::File::construct(real_path); @@ -128,13 +125,7 @@ void Client::handle_request(ByteBuffer raw_request) return; } - String mime_type; - if (!forced_mime_type.is_null()) - mime_type = forced_mime_type; - else - mime_type = Core::guess_mime_type_based_on_filename(request.url()); - - send_response(file->read_all(), request, mime_type); + send_response(file->read_all(), request, Core::guess_mime_type_based_on_filename(real_path)); } void Client::send_response(StringView response, const HTTP::HttpRequest& request, const String& content_type)