mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Redirect a directory request with a relative Location (#2630)
* Redirect a directory request with a relative Location The trailing-slash redirect used the request path as-is, so a path starting with "//" produced a Location that clients read as a network-path reference to another host. Name only the last segment, percent-encoded and prefixed with "./", so the target always resolves against the request URL, whatever the mount point or any prefix a proxy put in front of it. * Shorten the relative redirect comment
This commit is contained in:
1 parent
924b5cecf0
commit
2c918f27ee
2 files changed
+13
-2
No files matched your search
@@ -13869,7 +13869,11 @@ inline bool Server::handle_file_request(Request &req, Response &res) {
|
||||
detail::FileStat stat(path);
|
||||
|
||||
if (stat.is_dir()) {
|
||||
res.set_redirect(req.path + "/", StatusCode::MovedPermanently_301);
|
||||
// Relative, so "//dir" cannot name another host; "./" keeps a ':'
|
||||
// in the name from reading as a scheme.
|
||||
auto name = req.path.substr(req.path.rfind('/') + 1);
|
||||
res.set_redirect("./" + encode_path_component(name) + "/",
|
||||
StatusCode::MovedPermanently_301);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user