Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : #include "uri.h" 18 : 19 : #include <fmt/format.h> 20 : #include <fmt/compile.h> 21 : 22 : namespace jami { 23 : 24 24329 : Uri::Uri(std::string_view uri) 25 : { 26 : // TODO better handling of Uri, for now it's only used for 27 : // setMessageDisplayed to differentiate swarm:xxx 28 24330 : scheme_ = Uri::Scheme::JAMI; 29 24330 : auto posSep = uri.find(':'); 30 24329 : if (posSep != std::string::npos) { 31 4948 : auto scheme_str = uri.substr(0, posSep); 32 4946 : if (scheme_str == "sip") 33 0 : scheme_ = Uri::Scheme::SIP; 34 4946 : else if (scheme_str == "swarm") 35 1590 : scheme_ = Uri::Scheme::SWARM; 36 3356 : else if (scheme_str == "jami") 37 0 : scheme_ = Uri::Scheme::JAMI; 38 3357 : else if (scheme_str == "data-transfer") 39 193 : scheme_ = Uri::Scheme::DATA_TRANSFER; 40 3163 : else if (scheme_str == "git") 41 876 : scheme_ = Uri::Scheme::GIT; 42 2288 : else if (scheme_str == "rdv") 43 9 : scheme_ = Uri::Scheme::RENDEZVOUS; 44 2279 : else if (scheme_str == "sync") 45 216 : scheme_ = Uri::Scheme::SYNC; 46 2063 : else if (scheme_str == "msg") 47 2062 : scheme_ = Uri::Scheme::MESSAGE; 48 : else 49 1 : scheme_ = Uri::Scheme::UNRECOGNIZED; 50 4947 : authority_ = uri.substr(posSep + 1); 51 : } else { 52 19381 : authority_ = uri; 53 : } 54 24330 : auto posParams = authority_.find(';'); 55 24330 : if (posParams != std::string::npos) { 56 1 : authority_ = authority_.substr(0, posParams); 57 : } 58 24330 : } 59 : 60 : const std::string& 61 332 : Uri::authority() const 62 : { 63 332 : return authority_; 64 : } 65 : 66 : Uri::Scheme 67 24428 : Uri::scheme() const 68 : { 69 24428 : return scheme_; 70 : } 71 : 72 : std::string 73 178 : Uri::toString() const 74 : { 75 534 : return fmt::format(FMT_COMPILE("{}:{}"), schemeToString(), authority_); 76 : } 77 : 78 : constexpr std::string_view 79 178 : Uri::schemeToString() const 80 : { 81 178 : switch (scheme_) { 82 0 : case Uri::Scheme::SIP: 83 0 : return "sip"sv; 84 0 : case Uri::Scheme::SWARM: 85 0 : return "swarm"sv; 86 0 : case Uri::Scheme::RENDEZVOUS: 87 0 : return "rdv"sv; 88 0 : case Uri::Scheme::GIT: 89 0 : return "git"sv; 90 0 : case Uri::Scheme::SYNC: 91 0 : return "sync"sv; 92 0 : case Uri::Scheme::MESSAGE: 93 0 : return "msg"sv; 94 178 : case Uri::Scheme::JAMI: 95 : case Uri::Scheme::UNRECOGNIZED: 96 : default: 97 178 : return "jami"sv; 98 : } 99 : } 100 : 101 : } // namespace jami