Skip to content

Commit 80c391e

Browse files
authored
[wpinet] WebServer: Unescape URI (#7552)
Also provide Content-Disposition filename header in response. This fixes e.g. filenames with spaces in them.
1 parent 70f36cc commit 80c391e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

wpinet/src/main/native/cpp/WebServer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include <fmt/format.h>
1616
#include <wpi/DenseMap.h>
1717
#include <wpi/MemoryBuffer.h>
18+
#include <wpi/SmallString.h>
1819
#include <wpi/Signal.h>
1920
#include <wpi/StringMap.h>
2021
#include <wpi/fs.h>
2122
#include <wpi/json.h>
2223
#include <wpi/print.h>
24+
#include <wpi/raw_ostream.h>
2325

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

244+
wpi::SmallString<128> pathBuf;
245+
bool error;
246+
path = UnescapeURI(path, pathBuf, &error);
247+
if (error) {
248+
SendError(400);
249+
return;
250+
}
251+
242252
std::string_view query;
243253
if (url.HasQuery()) {
244254
query = url.GetQuery();
@@ -314,8 +324,13 @@ void MyHttpConnection::ProcessRequest() {
314324
SendResponse(200, "OK", "text/html", html);
315325
}
316326
} else {
327+
wpi::SmallString<128> extraHeadersBuf;
328+
wpi::raw_svector_ostream os{extraHeadersBuf};
329+
os << "Content-Disposition: filename=\"";
330+
os.write_escaped(fullpath.filename().string());
331+
os << "\"\r\n";
317332
SendFileResponse(200, "OK", GetMimeType(wpi::rsplit(path, '.').second),
318-
fullpath);
333+
fullpath, os.str());
319334
}
320335
} else {
321336
SendError(404, "Resource not found");

0 commit comments

Comments
 (0)