cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
uri.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <ostream>
23
24#include <absl/strings/string_view.h>
25#include <fmt/format.h>
26#include <re2/re2.h>
27
31
32namespace msgpack_rpc::addresses {
33
34URI::URI(std::string_view scheme, std::string_view host_or_path,
35 std::optional<std::uint16_t> port_number)
37
38std::string_view URI::scheme() const noexcept { return scheme_; }
39
40std::string_view URI::host_or_path() const noexcept { return host_or_path_; }
41
42std::optional<std::uint16_t> URI::port_number() const noexcept {
43 return port_number_;
44}
45
46bool URI::operator==(const URI& right) const {
47 return (scheme_ == right.scheme_) &&
48 (host_or_path_ == right.host_or_path_) &&
49 (port_number_ == right.port_number_);
50}
51
52bool URI::operator!=(const URI& right) const { return !operator==(right); }
53
54URI URI::parse(std::string_view uri_string) {
55 static re2::RE2 ip_regex{R"((tcp)://([a-zA-Z0-9+-.]+):(\d+))"};
56 static re2::RE2 ipv6_regex{R"((tcp)://\[([a-zA-Z0-9+-.:]+)\]:(\d+))"};
57 static re2::RE2 file_path_regex{R"((unix)://(.+))"};
58
59 std::string scheme{};
60 std::string host{};
61 auto port = static_cast<std::uint16_t>(0);
62
63 const auto absl_uri_string =
64 absl::string_view(uri_string.data(), uri_string.size());
65 if (!re2::RE2::FullMatch(
66 absl_uri_string, ip_regex, &scheme, &host, &port) &&
67 !re2::RE2::FullMatch(
68 absl_uri_string, ipv6_regex, &scheme, &host, &port) &&
69 !re2::RE2::FullMatch(
70 absl_uri_string, file_path_regex, &scheme, &host)) {
71 throw MsgpackRPCException(StatusCode::INVALID_ARGUMENT,
72 fmt::format("Invalid URI string: \"{}\".", uri_string));
73 }
74 if (scheme == TCP_SCHEME) {
75 return URI(scheme, host, port);
76 }
78 return URI(scheme, host);
79 }
81 StatusCode::UNEXPECTED_ERROR, "Unexpected line is executed.");
82}
83
84} // namespace msgpack_rpc::addresses
85
86namespace fmt {
87
88format_context::iterator
89formatter<msgpack_rpc::addresses::URI>::format( // NOLINT
90 const msgpack_rpc::addresses::URI& val, format_context& context) const {
91 auto out = context.out();
92 out = fmt::format_to(out, "{}://", val.scheme());
94 if (val.host_or_path().find(':') == std::string_view::npos) {
95 out = fmt::format_to(out, "{}", val.host_or_path());
96 } else {
97 out = fmt::format_to(out, "[{}]", val.host_or_path());
98 }
99 } else {
100 out = fmt::format_to(out, "{}", val.host_or_path());
101 }
102 if (val.port_number()) {
103 out = fmt::format_to(out, ":{}", *(val.port_number()));
104 }
105 return out;
106}
107
108} // namespace fmt
109
110std::ostream& operator<<(
111 std::ostream& stream, const msgpack_rpc::addresses::URI& uri) {
112 stream << fmt::format("{}", uri);
113 return stream;
114}
Class of URIs (Uniform Resource Identifiers) to specify endpoints in this library.
Definition uri.h:38
std::string_view scheme() const noexcept
Get the scheme.
Definition uri.cpp:38
std::string scheme_
Scheme.
Definition uri.h:102
std::optional< std::uint16_t > port_number() const noexcept
Get the port number.
Definition uri.cpp:42
std::string host_or_path_
Host name or file path.
Definition uri.h:105
std::optional< std::uint16_t > port_number_
Port.
Definition uri.h:108
std::string_view host_or_path() const noexcept
Get the host name or file path.
Definition uri.cpp:40
static URI parse(std::string_view uri_string)
Parse a string to create a URI.
Definition uri.cpp:54
bool operator==(const URI &right) const
Compare with a URI.
Definition uri.cpp:46
bool operator!=(const URI &right) const
Compare with a URI.
Definition uri.cpp:52
URI(std::string_view scheme, std::string_view host_or_path, std::optional< std::uint16_t > port_number=std::optional< std::uint16_t >())
Constructor.
Definition uri.cpp:34
Class of exceptions in cpp-msgpack-rpc library.
Definition of MsgpackRPCException class.
Namespace of fmt library.
Definition uri.h:113
Namespace of addresses.
Definition i_address.h:26
constexpr std::string_view UNIX_SOCKET_SCHEME
Scheme of unix sockets (streaming protocol).
Definition schemes.h:30
constexpr std::string_view TCP_SCHEME
Scheme of TCP.
Definition schemes.h:27
Definition of constants of schemes.
Definition of StatusCode enumeration.
Definition of URI class.
std::ostream & operator<<(std::ostream &stream, const msgpack_rpc::addresses::URI &uri)
Format a URI.
Definition uri.cpp:110