1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:27:45 +00:00

WebServer: Propagate more errors

Use try_append() instead of append().
This commit is contained in:
Sam Atkins 2023-03-27 16:16:14 +01:00 committed by Sam Atkins
parent 8dd0038f47
commit 97b3f1230b

View file

@ -128,26 +128,15 @@ ErrorOr<bool> Client::handle_request(HTTP::HttpRequest const& request)
auto requested_path = TRY(String::from_deprecated_string(LexicalPath::join("/"sv, resource_decoded).string())); auto requested_path = TRY(String::from_deprecated_string(LexicalPath::join("/"sv, resource_decoded).string()));
dbgln_if(WEBSERVER_DEBUG, "Canonical requested path: '{}'", requested_path); dbgln_if(WEBSERVER_DEBUG, "Canonical requested path: '{}'", requested_path);
StringBuilder path_builder; auto real_path = TRY(String::formatted("{}{}", Configuration::the().document_root_path(), requested_path));
path_builder.append(Configuration::the().document_root_path());
path_builder.append(requested_path);
auto real_path = TRY(path_builder.to_string());
if (FileSystem::is_directory(real_path.bytes_as_string_view())) { if (FileSystem::is_directory(real_path.bytes_as_string_view())) {
if (!resource_decoded.ends_with('/')) { if (!resource_decoded.ends_with('/')) {
StringBuilder red; TRY(send_redirect(TRY(String::formatted("{}/", requested_path)), request));
red.append(requested_path);
red.append("/"sv);
TRY(send_redirect(red.to_deprecated_string(), request));
return true; return true;
} }
StringBuilder index_html_path_builder; auto index_html_path = TRY(String::formatted("{}/index.html", real_path));
index_html_path_builder.append(real_path);
index_html_path_builder.append("/index.html"sv);
auto index_html_path = TRY(index_html_path_builder.to_string());
if (!FileSystem::exists(index_html_path)) { if (!FileSystem::exists(index_html_path)) {
TRY(handle_directory_listing(requested_path, real_path, request)); TRY(handle_directory_listing(requested_path, real_path, request));
return true; return true;
@ -179,17 +168,17 @@ ErrorOr<bool> Client::handle_request(HTTP::HttpRequest const& request)
ErrorOr<void> Client::send_response(Stream& response, HTTP::HttpRequest const& request, ContentInfo content_info) ErrorOr<void> Client::send_response(Stream& response, HTTP::HttpRequest const& request, ContentInfo content_info)
{ {
StringBuilder builder; StringBuilder builder;
builder.append("HTTP/1.0 200 OK\r\n"sv); TRY(builder.try_append("HTTP/1.0 200 OK\r\n"sv));
builder.append("Server: WebServer (SerenityOS)\r\n"sv); TRY(builder.try_append("Server: WebServer (SerenityOS)\r\n"sv));
builder.append("X-Frame-Options: SAMEORIGIN\r\n"sv); TRY(builder.try_append("X-Frame-Options: SAMEORIGIN\r\n"sv));
builder.append("X-Content-Type-Options: nosniff\r\n"sv); TRY(builder.try_append("X-Content-Type-Options: nosniff\r\n"sv));
builder.append("Pragma: no-cache\r\n"sv); TRY(builder.try_append("Pragma: no-cache\r\n"sv));
if (content_info.type == "text/plain") if (content_info.type == "text/plain")
builder.appendff("Content-Type: {}; charset=utf-8\r\n", content_info.type); TRY(builder.try_appendff("Content-Type: {}; charset=utf-8\r\n", content_info.type));
else else
builder.appendff("Content-Type: {}\r\n", content_info.type); TRY(builder.try_appendff("Content-Type: {}\r\n", content_info.type));
builder.appendff("Content-Length: {}\r\n", content_info.length); TRY(builder.try_appendff("Content-Length: {}\r\n", content_info.length));
builder.append("\r\n"sv); TRY(builder.try_append("\r\n"sv));
auto builder_contents = TRY(builder.to_byte_buffer()); auto builder_contents = TRY(builder.to_byte_buffer());
TRY(m_socket->write_until_depleted(builder_contents)); TRY(m_socket->write_until_depleted(builder_contents));
@ -227,11 +216,11 @@ ErrorOr<void> Client::send_response(Stream& response, HTTP::HttpRequest const& r
ErrorOr<void> Client::send_redirect(StringView redirect_path, HTTP::HttpRequest const& request) ErrorOr<void> Client::send_redirect(StringView redirect_path, HTTP::HttpRequest const& request)
{ {
StringBuilder builder; StringBuilder builder;
builder.append("HTTP/1.0 301 Moved Permanently\r\n"sv); TRY(builder.try_append("HTTP/1.0 301 Moved Permanently\r\n"sv));
builder.append("Location: "sv); TRY(builder.try_append("Location: "sv));
builder.append(redirect_path); TRY(builder.try_append(redirect_path));
builder.append("\r\n"sv); TRY(builder.try_append("\r\n"sv));
builder.append("\r\n"sv); TRY(builder.try_append("\r\n"sv));
auto builder_contents = TRY(builder.to_byte_buffer()); auto builder_contents = TRY(builder.to_byte_buffer());
TRY(m_socket->write_until_depleted(builder_contents)); TRY(m_socket->write_until_depleted(builder_contents));
@ -266,41 +255,41 @@ ErrorOr<void> Client::handle_directory_listing(String const& requested_path, Str
{ {
StringBuilder builder; StringBuilder builder;
builder.append("<!DOCTYPE html>\n"sv); TRY(builder.try_append("<!DOCTYPE html>\n"sv));
builder.append("<html>\n"sv); TRY(builder.try_append("<html>\n"sv));
builder.append("<head><meta charset=\"utf-8\">\n"sv); TRY(builder.try_append("<head><meta charset=\"utf-8\">\n"sv));
builder.append("<title>Index of "sv); TRY(builder.try_append("<title>Index of "sv));
builder.append(escape_html_entities(requested_path)); TRY(builder.try_append(escape_html_entities(requested_path)));
builder.append("</title><style>\n"sv); TRY(builder.try_append("</title><style>\n"sv));
builder.append(".folder { width: 16px; height: 16px; background-image: url('data:image/png;base64,"sv); TRY(builder.try_append(".folder { width: 16px; height: 16px; background-image: url('data:image/png;base64,"sv));
builder.append(folder_image_data()); TRY(builder.try_append(folder_image_data()));
builder.append("'); }\n"sv); TRY(builder.try_append("'); }\n"sv));
builder.append(".file { width: 16px; height: 16px; background-image: url('data:image/png;base64,"sv); TRY(builder.try_append(".file { width: 16px; height: 16px; background-image: url('data:image/png;base64,"sv));
builder.append(file_image_data()); TRY(builder.try_append(file_image_data()));
builder.append("'); }\n"sv); TRY(builder.try_append("'); }\n"sv));
builder.append("</style></head><body>\n"sv); TRY(builder.try_append("</style></head><body>\n"sv));
builder.append("<h1>Index of "sv); TRY(builder.try_append("<h1>Index of "sv));
builder.append(escape_html_entities(requested_path)); TRY(builder.try_append(escape_html_entities(requested_path)));
builder.append("</h1>\n"sv); TRY(builder.try_append("</h1>\n"sv));
builder.append("<hr>\n"sv); TRY(builder.try_append("<hr>\n"sv));
builder.append("<code><table>\n"sv); TRY(builder.try_append("<code><table>\n"sv));
Core::DirIterator dt(real_path.bytes_as_string_view()); Core::DirIterator dt(real_path.bytes_as_string_view());
Vector<DeprecatedString> names; Vector<DeprecatedString> names;
while (dt.has_next()) while (dt.has_next())
names.append(dt.next_path()); TRY(names.try_append(dt.next_path()));
quick_sort(names); quick_sort(names);
for (auto& name : names) { for (auto& name : names) {
StringBuilder path_builder; StringBuilder path_builder;
path_builder.append(real_path); TRY(path_builder.try_append(real_path));
path_builder.append('/'); TRY(path_builder.try_append('/'));
// NOTE: In the root directory of the webserver, ".." should be equal to ".", since we don't want // NOTE: In the root directory of the webserver, ".." should be equal to ".", since we don't want
// the user to see e.g. the size of the parent directory (and it isn't unveiled, so stat fails). // the user to see e.g. the size of the parent directory (and it isn't unveiled, so stat fails).
if (requested_path == "/" && name == "..") if (requested_path == "/" && name == "..")
path_builder.append("."sv); TRY(path_builder.try_append("."sv));
else else
path_builder.append(name); TRY(path_builder.try_append(name));
struct stat st; struct stat st;
memset(&st, 0, sizeof(st)); memset(&st, 0, sizeof(st));
@ -311,30 +300,30 @@ ErrorOr<void> Client::handle_directory_listing(String const& requested_path, Str
bool is_directory = S_ISDIR(st.st_mode); bool is_directory = S_ISDIR(st.st_mode);
builder.append("<tr>"sv); TRY(builder.try_append("<tr>"sv));
builder.appendff("<td><div class=\"{}\"></div></td>", is_directory ? "folder" : "file"); TRY(builder.try_appendff("<td><div class=\"{}\"></div></td>", is_directory ? "folder" : "file"));
builder.append("<td><a href=\"./"sv); TRY(builder.try_append("<td><a href=\"./"sv));
builder.append(URL::percent_encode(name)); TRY(builder.try_append(URL::percent_encode(name)));
// NOTE: For directories, we append a slash so we don't always hit the redirect case, // NOTE: For directories, we append a slash so we don't always hit the redirect case,
// which adds a slash anyways. // which adds a slash anyways.
if (is_directory) if (is_directory)
builder.append('/'); TRY(builder.try_append('/'));
builder.append("\">"sv); TRY(builder.try_append("\">"sv));
builder.append(escape_html_entities(name)); TRY(builder.try_append(escape_html_entities(name)));
builder.append("</a></td><td>&nbsp;</td>"sv); TRY(builder.try_append("</a></td><td>&nbsp;</td>"sv));
builder.appendff("<td>{:10}</td><td>&nbsp;</td>", st.st_size); TRY(builder.try_appendff("<td>{:10}</td><td>&nbsp;</td>", st.st_size));
builder.append("<td>"sv); TRY(builder.try_append("<td>"sv));
builder.append(Core::DateTime::from_timestamp(st.st_mtime).to_deprecated_string()); TRY(builder.try_append(TRY(Core::DateTime::from_timestamp(st.st_mtime).to_string())));
builder.append("</td>"sv); TRY(builder.try_append("</td>"sv));
builder.append("</tr>\n"sv); TRY(builder.try_append("</tr>\n"sv));
} }
builder.append("</table></code>\n"sv); TRY(builder.try_append("</table></code>\n"sv));
builder.append("<hr>\n"sv); TRY(builder.try_append("<hr>\n"sv));
builder.append("<i>Generated by WebServer (SerenityOS)</i>\n"sv); TRY(builder.try_append("<i>Generated by WebServer (SerenityOS)</i>\n"sv));
builder.append("</body>\n"sv); TRY(builder.try_append("</body>\n"sv));
builder.append("</html>\n"sv); TRY(builder.try_append("</html>\n"sv));
auto response = builder.to_deprecated_string(); auto response = builder.to_deprecated_string();
FixedMemoryStream stream { response.bytes() }; FixedMemoryStream stream { response.bytes() };
@ -346,23 +335,23 @@ ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const
auto reason_phrase = HTTP::HttpResponse::reason_phrase_for_code(code); auto reason_phrase = HTTP::HttpResponse::reason_phrase_for_code(code);
StringBuilder content_builder; StringBuilder content_builder;
content_builder.append("<!DOCTYPE html><html><body><h1>"sv); TRY(content_builder.try_append("<!DOCTYPE html><html><body><h1>"sv));
content_builder.appendff("{} ", code); TRY(content_builder.try_appendff("{} ", code));
content_builder.append(reason_phrase); TRY(content_builder.try_append(reason_phrase));
content_builder.append("</h1></body></html>"sv); TRY(content_builder.try_append("</h1></body></html>"sv));
StringBuilder header_builder; StringBuilder header_builder;
header_builder.appendff("HTTP/1.0 {} ", code); TRY(header_builder.try_appendff("HTTP/1.0 {} ", code));
header_builder.append(reason_phrase); TRY(header_builder.try_append(reason_phrase));
header_builder.append("\r\n"sv); TRY(header_builder.try_append("\r\n"sv));
for (auto& header : headers) { for (auto& header : headers) {
header_builder.append(header); TRY(header_builder.try_append(header));
header_builder.append("\r\n"sv); TRY(header_builder.try_append("\r\n"sv));
} }
header_builder.append("Content-Type: text/html; charset=UTF-8\r\n"sv); TRY(header_builder.try_append("Content-Type: text/html; charset=UTF-8\r\n"sv));
header_builder.appendff("Content-Length: {}\r\n", content_builder.length()); TRY(header_builder.try_appendff("Content-Length: {}\r\n", content_builder.length()));
header_builder.append("\r\n"sv); TRY(header_builder.try_append("\r\n"sv));
TRY(m_socket->write_until_depleted(TRY(header_builder.to_byte_buffer()))); TRY(m_socket->write_until_depleted(TRY(header_builder.to_byte_buffer())));
TRY(m_socket->write_until_depleted(TRY(content_builder.to_byte_buffer()))); TRY(m_socket->write_until_depleted(TRY(content_builder.to_byte_buffer())));