mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:17:36 +00:00
WebServer: Try to send an appopriate Content-Type header
Use Core::guess_mime_type_based_on_filename() for this. It's obviously not perfect, but it works better than just sending "text/html" for everything no matter what. :^)
This commit is contained in:
parent
78518d230c
commit
7f70a6f0cb
2 changed files with 9 additions and 6 deletions
|
@ -25,12 +25,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include <AK/URLParser.h>
|
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
|
#include <AK/URLParser.h>
|
||||||
#include <LibCore/DateTime.h>
|
#include <LibCore/DateTime.h>
|
||||||
#include <LibCore/DirIterator.h>
|
#include <LibCore/DirIterator.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/MimeData.h>
|
||||||
#include <LibHTTP/HttpRequest.h>
|
#include <LibHTTP/HttpRequest.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -122,15 +123,17 @@ void Client::handle_request(ByteBuffer raw_request)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
send_response(file->read_all(), request);
|
send_response(file->read_all(), request, Core::guess_mime_type_based_on_filename(request.url()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::send_response(StringView response, const HTTP::HttpRequest& request)
|
void Client::send_response(StringView response, const HTTP::HttpRequest& request, const String& content_type)
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append("HTTP/1.0 200 OK\r\n");
|
builder.append("HTTP/1.0 200 OK\r\n");
|
||||||
builder.append("Server: WebServer (SerenityOS)\r\n");
|
builder.append("Server: WebServer (SerenityOS)\r\n");
|
||||||
builder.append("Content-Type: text/html\r\n");
|
builder.append("Content-Type: ");
|
||||||
|
builder.append(content_type);
|
||||||
|
builder.append("\r\n");
|
||||||
builder.append("\r\n");
|
builder.append("\r\n");
|
||||||
|
|
||||||
m_socket->write(builder.to_string());
|
m_socket->write(builder.to_string());
|
||||||
|
@ -201,7 +204,7 @@ void Client::handle_directory_listing(const String& requested_path, const String
|
||||||
builder.append("</body>\n");
|
builder.append("</body>\n");
|
||||||
builder.append("</html>\n");
|
builder.append("</html>\n");
|
||||||
|
|
||||||
send_response(builder.to_string(), request);
|
send_response(builder.to_string(), request, "text/html");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::send_error_response(unsigned code, const StringView& message, const HTTP::HttpRequest& request)
|
void Client::send_error_response(unsigned code, const StringView& message, const HTTP::HttpRequest& request)
|
||||||
|
|
|
@ -42,7 +42,7 @@ private:
|
||||||
Client(NonnullRefPtr<Core::TCPSocket>, const String&, Core::Object* parent);
|
Client(NonnullRefPtr<Core::TCPSocket>, const String&, Core::Object* parent);
|
||||||
|
|
||||||
void handle_request(ByteBuffer);
|
void handle_request(ByteBuffer);
|
||||||
void send_response(StringView, const HTTP::HttpRequest&);
|
void send_response(StringView, const HTTP::HttpRequest&, const String& content_type);
|
||||||
void send_redirect(StringView redirect, const HTTP::HttpRequest& request);
|
void send_redirect(StringView redirect, const HTTP::HttpRequest& request);
|
||||||
void send_error_response(unsigned code, const StringView& message, const HTTP::HttpRequest&);
|
void send_error_response(unsigned code, const StringView& message, const HTTP::HttpRequest&);
|
||||||
void die();
|
void die();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue