Skip to content

Commit

Permalink
Redirect to url with trailing slash, WITHOUT the protocol and host.
Browse files Browse the repository at this point in the history
Crow does not know the correct protocol when behind a reverse proxy.
So instead, simply redirect to the same url and add a trailing slash.

Note, it is now valid for the "Location" header field to be relative.
From wikipedia:
An obsolete version of the HTTP 1.1 specifications (IETF RFC 2616)
required a complete absolute URI for redirection.
The IETF HTTP working group found that the most popular web browsers
tolerate the passing of a relative URL[3] and, consequently, the updated
HTTP 1.1 specifications (IETF RFC 7231) relaxed the original constraint,
allowing the use of relative URLs in Location headers.
  • Loading branch information
paulharris committed Nov 13, 2024
1 parent 5a972fe commit 7aef5b3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 32 deletions.
12 changes: 0 additions & 12 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,6 @@ namespace crow
}
}
#endif
//if there is a redirection with a partial URL, treat the URL as a route.
std::string location = res.get_header_value("Location");
if (!location.empty() && location.find("://", 0) == std::string::npos)
{
#ifdef CROW_ENABLE_SSL
if (handler_->ssl_used())
location.insert(0, "https://" + req_.get_header_value("Host"));
else
#endif
location.insert(0, "http://" + req_.get_header_value("Host"));
res.set_header("location", location);
}

prepare_buffers();

Expand Down
22 changes: 2 additions & 20 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -1462,16 +1462,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
{
CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url;
res = response(301);

// TODO(ipkn) absolute url building
if (req.get_header_value("Host").empty())
{
res.add_header("Location", req.url + "/");
}
else
{
res.add_header("Location", (using_ssl ? "https://" : "http://") + req.get_header_value("Host") + req.url + "/");
}
res.add_header("Location", req.url + "/");
res.end();
return;
}
Expand Down Expand Up @@ -1710,16 +1701,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
{
CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url;
res = response(301);

// TODO(ipkn) absolute url building
if (req.get_header_value("Host").empty())
{
res.add_header("Location", req.url + "/");
}
else
{
res.add_header("Location", (using_ssl ? "https://" : "http://") + req.get_header_value("Host") + req.url + "/");
}
res.add_header("Location", req.url + "/");
res.end();
return;
}
Expand Down

0 comments on commit 7aef5b3

Please sign in to comment.