Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpinet] Serve index HTML file from WebServer if available #7780

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 42 additions & 30 deletions wpinet/src/main/native/cpp/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,39 +289,51 @@ void MyHttpConnection::ProcessRequest() {
wpi::json{{"dirs", std::move(dirs)}, {"files", std::move(files)}}
.dump());
} else {
wpi::StringMap<std::string> dirs;
wpi::StringMap<std::string> files;
for (auto&& entry : fs::directory_iterator{fullpath}) {
bool subdir = entry.is_directory(ec);
std::string name = entry.path().filename().string();
wpi::SmallString<128> nameUriBuf, nameHtmlBuf;
if (subdir) {
dirs.emplace(
name, fmt::format(
"<tr><td><a href=\"{}/\">{}/</a></td><td></td></tr>",
EscapeURI(name, nameUriBuf),
EscapeHTML(name, nameHtmlBuf)));
} else {
files.emplace(
name, fmt::format(
"<tr><td><a href=\"{}\">{}</a></td><td>{}</td></tr>",
EscapeURI(name, nameUriBuf),
EscapeHTML(name, nameHtmlBuf), entry.file_size(ec)));
fs::path indexpath = fmt::format("{}index.html", fullpath);
jwbonner marked this conversation as resolved.
Show resolved Hide resolved
if (fs::exists(indexpath)) {
jwbonner marked this conversation as resolved.
Show resolved Hide resolved
wpi::SmallString<128> extraHeadersBuf;
wpi::raw_svector_ostream os{extraHeadersBuf};
os << "Content-Disposition: filename=\"";
os.write_escaped(indexpath.filename().string());
jwbonner marked this conversation as resolved.
Show resolved Hide resolved
os << "\"\r\n";
SendFileResponse(200, "OK", GetMimeType("html"), indexpath, os.str());
} else {
wpi::StringMap<std::string> dirs;
wpi::StringMap<std::string> files;
for (auto&& entry : fs::directory_iterator{fullpath}) {
bool subdir = entry.is_directory(ec);
std::string name = entry.path().filename().string();
wpi::SmallString<128> nameUriBuf, nameHtmlBuf;
if (subdir) {
dirs.emplace(
name,
fmt::format(
"<tr><td><a href=\"{}/\">{}/</a></td><td></td></tr>",
EscapeURI(name, nameUriBuf),
EscapeHTML(name, nameHtmlBuf)));
} else {
files.emplace(
name,
fmt::format(
"<tr><td><a href=\"{}\">{}</a></td><td>{}</td></tr>",
EscapeURI(name, nameUriBuf),
EscapeHTML(name, nameHtmlBuf), entry.file_size(ec)));
}
}
}

std::string html = fmt::format(
"<html><head><title>{}</title></head><body>"
"<table><tr><th>Name</th><th>Size</th></tr>\n",
path);
for (auto&& str : dirs) {
html += str.second;
}
for (auto&& str : files) {
html += str.second;
std::string html = fmt::format(
"<html><head><title>{}</title></head><body>"
"<table><tr><th>Name</th><th>Size</th></tr>\n",
path);
for (auto&& str : dirs) {
html += str.second;
}
for (auto&& str : files) {
html += str.second;
}
html += "</table></body></html>";
SendResponse(200, "OK", "text/html", html);
}
html += "</table></body></html>";
SendResponse(200, "OK", "text/html", html);
}
} else {
wpi::SmallString<128> extraHeadersBuf;
Expand Down
Loading