Check multiple host prefix formats, /user/{user}, @{user}

FossilOrigin-Name: 96effaf566d917d695bbaea799a594df9c932478803d5aa16b5f83ff51e83c48
This commit is contained in:
nekobit 2022-10-10 03:48:42 +00:00
parent 2ff4ce015f
commit c914ff1f2d

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "instance/instance.h"
#include "jsonhelper.h"
#include <stdexcept>
#include <utility>
@ -55,14 +56,18 @@ namespace
// TODO a lot
HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
DESTRUCT_WORMHOLE_ARGS(args);
// auto args = std::any_cast<RouteArgs>(_args);
using namespace std::string_literals;
rjson::Document root(rjson::kObjectType);
rjson::Document::AllocatorType& a = root.GetAllocator();
rjson::Value links(rjson::kArrayType);
// Resource to read into
std::string resource;
// Stored result of account string, stripped from resource
std::string acct_str;
User user;
try
@ -73,24 +78,39 @@ HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const
return HTTP::Error::make_json_error_response("Missing resource parameter", HTTP::Code::BAD_REQUEST);
}
// MUST have acct: at beginning
std::string acct_str;
constexpr std::string_view prefix = "acct:";
if (!resource.compare(0, prefix.size(), prefix))
// Perform multiple different Resource formats
// TODO separate into functions?
constexpr std::string_view acct_prefix = "acct:";
static const std::string local_host_prefix = Instance::instance_host_https(true) + "user/";
static const std::string at_prefix = Instance::instance_host_https(true) + "@";
// Standard "acct:" prefix check
if (!resource.compare(0, acct_prefix.size(), acct_prefix))
{
// Get data after acct:
acct_str = resource.substr(prefix.size());
acct_str = resource.substr(acct_prefix.size());
}
// Host prefix check
else if (!resource.compare(0, local_host_prefix.size(), local_host_prefix))
{
// Get data after acct:
acct_str = resource.substr(local_host_prefix.size());
}
// At prefix check
else if (!resource.compare(0, at_prefix.size(), at_prefix))
{
// Get data after acct:
acct_str = resource.substr(at_prefix.size());
}
else {
return HTTP::Error::make_json_error_response("Missing \"acct:\" prefix", HTTP::Code::BAD_REQUEST);
return HTTP::Error::make_json_error_response("Invalid prefix", HTTP::Code::METHOD_NOT_ALLOWED);
}
try
{
user = db->get_user(acct_str);
}
catch (...)
{
catch (...) {
return HTTP::Error::make_json_error_response("Couldn't find user", HTTP::Code::NOT_FOUND);
}