Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 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 27442 : 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 27440 : scheme_ = Uri::Scheme::JAMI;
29 27440 : auto posSep = uri.find(':');
30 27448 : if (posSep != std::string::npos) {
31 10271 : auto scheme_str = uri.substr(0, posSep);
32 10277 : if (scheme_str == "sip")
33 0 : scheme_ = Uri::Scheme::SIP;
34 10274 : else if (scheme_str == "swarm")
35 1523 : scheme_ = Uri::Scheme::SWARM;
36 8753 : else if (scheme_str == "jami")
37 0 : scheme_ = Uri::Scheme::JAMI;
38 8751 : else if (scheme_str == "data-transfer")
39 229 : scheme_ = Uri::Scheme::DATA_TRANSFER;
40 8521 : else if (scheme_str == "git")
41 986 : scheme_ = Uri::Scheme::GIT;
42 7539 : else if (scheme_str == "rdv")
43 9 : scheme_ = Uri::Scheme::RENDEZVOUS;
44 7532 : else if (scheme_str == "sync")
45 203 : scheme_ = Uri::Scheme::SYNC;
46 7324 : else if (scheme_str == "msg")
47 3684 : scheme_ = Uri::Scheme::MESSAGE;
48 3646 : else if (scheme_str == "auth")
49 4 : scheme_ = Uri::Scheme::AUTH;
50 3642 : else if (scheme_str == "svcdisc")
51 3639 : scheme_ = Uri::Scheme::SVC_DISCOVERY;
52 1 : else if (scheme_str == "svc")
53 0 : scheme_ = Uri::Scheme::SVC_TUNNEL;
54 : else
55 1 : scheme_ = Uri::Scheme::UNRECOGNIZED;
56 10278 : authority_ = uri.substr(posSep + 1);
57 : } else {
58 17177 : authority_ = uri;
59 : }
60 27425 : auto posParams = authority_.find(';');
61 27438 : if (posParams != std::string::npos) {
62 1 : authority_ = authority_.substr(0, posParams);
63 : }
64 27438 : }
65 :
66 : const std::string&
67 315 : Uri::authority() const
68 : {
69 315 : return authority_;
70 : }
71 :
72 : Uri::Scheme
73 27539 : Uri::scheme() const
74 : {
75 27539 : return scheme_;
76 : }
77 :
78 : std::string
79 166 : Uri::toString() const
80 : {
81 664 : return fmt::format(FMT_COMPILE("{}:{}"), schemeToString(), authority_);
82 : }
83 :
84 : constexpr std::string_view
85 166 : Uri::schemeToString() const
86 : {
87 166 : switch (scheme_) {
88 0 : case Uri::Scheme::SIP:
89 0 : return "sip"sv;
90 0 : case Uri::Scheme::SWARM:
91 0 : return "swarm"sv;
92 0 : case Uri::Scheme::RENDEZVOUS:
93 0 : return "rdv"sv;
94 0 : case Uri::Scheme::GIT:
95 0 : return "git"sv;
96 0 : case Uri::Scheme::SYNC:
97 0 : return "sync"sv;
98 0 : case Uri::Scheme::MESSAGE:
99 0 : return "msg"sv;
100 0 : case Uri::Scheme::AUTH:
101 0 : return "auth"sv;
102 0 : case Uri::Scheme::SVC_DISCOVERY:
103 0 : return "svcdisc"sv;
104 0 : case Uri::Scheme::SVC_TUNNEL:
105 0 : return "svc"sv;
106 166 : case Uri::Scheme::JAMI:
107 : case Uri::Scheme::UNRECOGNIZED:
108 : default:
109 166 : return "jami"sv;
110 : }
111 : }
112 :
113 : } // namespace jami
|