Skip to content

Commit

Permalink
[wpinet] WebServer: Unescape URI (#7552)
Browse files Browse the repository at this point in the history
Also provide Content-Disposition filename header in response.

This fixes e.g. filenames with spaces in them.
  • Loading branch information
PeterJohnson authored Dec 15, 2024
1 parent 70f36cc commit 80c391e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion wpinet/src/main/native/cpp/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include <fmt/format.h>
#include <wpi/DenseMap.h>
#include <wpi/MemoryBuffer.h>
#include <wpi/SmallString.h>
#include <wpi/Signal.h>
#include <wpi/StringMap.h>
#include <wpi/fs.h>
#include <wpi/json.h>
#include <wpi/print.h>
#include <wpi/raw_ostream.h>

#include "wpinet/EventLoopRunner.h"
#include "wpinet/HttpServerConnection.h"
Expand Down Expand Up @@ -239,6 +241,14 @@ void MyHttpConnection::ProcessRequest() {
}
// fmt::print(stderr, "path: \"{}\"\n", path);

wpi::SmallString<128> pathBuf;
bool error;
path = UnescapeURI(path, pathBuf, &error);
if (error) {
SendError(400);
return;
}

std::string_view query;
if (url.HasQuery()) {
query = url.GetQuery();
Expand Down Expand Up @@ -314,8 +324,13 @@ void MyHttpConnection::ProcessRequest() {
SendResponse(200, "OK", "text/html", html);
}
} else {
wpi::SmallString<128> extraHeadersBuf;
wpi::raw_svector_ostream os{extraHeadersBuf};
os << "Content-Disposition: filename=\"";
os.write_escaped(fullpath.filename().string());
os << "\"\r\n";
SendFileResponse(200, "OK", GetMimeType(wpi::rsplit(path, '.').second),
fullpath);
fullpath, os.str());
}
} else {
SendError(404, "Resource not found");
Expand Down

0 comments on commit 80c391e

Please sign in to comment.