diff --git a/src/protocol/webfinger/webfinger.cpp b/src/protocol/webfinger/webfinger.cpp index c430ac0..685e913 100644 --- a/src/protocol/webfinger/webfinger.cpp +++ b/src/protocol/webfinger/webfinger.cpp @@ -26,6 +26,7 @@ #include "http/responsecode.h" #include "user.h" #include "webfinger.h" +#include "stringhelper.h" #include "http/httpserver.h" #include "http/request.h" #include "logger.h" @@ -80,12 +81,20 @@ HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const // Perform multiple different Resource formats // TODO separate into functions? constexpr std::string_view acct_prefix = "acct:"; + static const std::string acct_suffix = "@" + Config::instance().config.instance.host; static const std::string local_host_prefix = Instance::instance_host_https(true) + "users/"; static const std::string at_prefix = Instance::instance_host_https(true) + "@"; // Standard "acct:" prefix check if (!resource.compare(0, acct_prefix.size(), acct_prefix)) + { + if (StringHelper::ends_with(resource, acct_suffix)) + { + size_t n = resource.find(acct_suffix); + resource = resource.substr(0, n); + } acct_str = resource.substr(acct_prefix.size()); + } else if (!resource.compare(0, local_host_prefix.size(), local_host_prefix)) // Instance host acct_str = resource.substr(local_host_prefix.size()); else if (!resource.compare(0, at_prefix.size(), at_prefix)) // At prefix diff --git a/src/stringhelper.h b/src/stringhelper.h new file mode 100644 index 0000000..f05845e --- /dev/null +++ b/src/stringhelper.h @@ -0,0 +1,28 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once + +namespace StringHelper +{ + inline bool ends_with(const std::string& value, const std::string& ending) + { + if (ending.size() > value.size()) return false; + return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); + } +}