cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
*/
20
#include "
msgpack_rpc/addresses/uri.h
"
21
22
#include <ostream>
23
24
#include <absl/strings/string_view.h>
25
#include <fmt/format.h>
26
#include <re2/re2.h>
27
28
#include "
msgpack_rpc/addresses/schemes.h
"
29
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
30
#include "
msgpack_rpc/common/status_code.h
"
31
32
namespace
msgpack_rpc::addresses
{
33
34
URI::URI
(std::string_view
scheme
, std::string_view
host_or_path
,
35
std::optional<std::uint16_t>
port_number
)
36
:
scheme_
(
scheme
),
host_or_path_
(
host_or_path
),
port_number_
(
port_number
) {}
37
38
std::string_view
URI::scheme
() const noexcept {
return
scheme_
; }
39
40
std::string_view
URI::host_or_path
() const noexcept {
return
host_or_path_
; }
41
42
std::optional<std::uint16_t>
URI::port_number
() const noexcept {
43
return
port_number_
;
44
}
45
46
bool
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
52
bool
URI::operator!=
(
const
URI
& right)
const
{
return
!
operator==
(right); }
53
54
URI
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
}
77
if
(
scheme
==
UNIX_SOCKET_SCHEME
) {
78
return
URI
(
scheme
, host);
79
}
80
throw
MsgpackRPCException
(
81
StatusCode::UNEXPECTED_ERROR,
"Unexpected line is executed."
);
82
}
83
84
}
// namespace msgpack_rpc::addresses
85
86
namespace
fmt
{
87
88
format_context::iterator
89
formatter<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
());
93
if
(val.
scheme
() ==
msgpack_rpc::addresses::TCP_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
110
std::ostream&
operator<<
(
111
std::ostream& stream,
const
msgpack_rpc::addresses::URI
& uri) {
112
stream << fmt::format(
"{}"
, uri);
113
return
stream;
114
}
msgpack_rpc::addresses::URI
Class of URIs (Uniform Resource Identifiers) to specify endpoints in this library.
Definition
uri.h:38
msgpack_rpc::addresses::URI::scheme
std::string_view scheme() const noexcept
Get the scheme.
Definition
uri.cpp:38
msgpack_rpc::addresses::URI::scheme_
std::string scheme_
Scheme.
Definition
uri.h:102
msgpack_rpc::addresses::URI::port_number
std::optional< std::uint16_t > port_number() const noexcept
Get the port number.
Definition
uri.cpp:42
msgpack_rpc::addresses::URI::host_or_path_
std::string host_or_path_
Host name or file path.
Definition
uri.h:105
msgpack_rpc::addresses::URI::port_number_
std::optional< std::uint16_t > port_number_
Port.
Definition
uri.h:108
msgpack_rpc::addresses::URI::host_or_path
std::string_view host_or_path() const noexcept
Get the host name or file path.
Definition
uri.cpp:40
msgpack_rpc::addresses::URI::parse
static URI parse(std::string_view uri_string)
Parse a string to create a URI.
Definition
uri.cpp:54
msgpack_rpc::addresses::URI::operator==
bool operator==(const URI &right) const
Compare with a URI.
Definition
uri.cpp:46
msgpack_rpc::addresses::URI::operator!=
bool operator!=(const URI &right) const
Compare with a URI.
Definition
uri.cpp:52
msgpack_rpc::addresses::URI::URI
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
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
fmt
Namespace of fmt library.
Definition
uri.h:113
msgpack_rpc::addresses
Namespace of addresses.
Definition
i_address.h:26
msgpack_rpc::addresses::UNIX_SOCKET_SCHEME
constexpr std::string_view UNIX_SOCKET_SCHEME
Scheme of unix sockets (streaming protocol).
Definition
schemes.h:30
msgpack_rpc::addresses::TCP_SCHEME
constexpr std::string_view TCP_SCHEME
Scheme of TCP.
Definition
schemes.h:27
schemes.h
Definition of constants of schemes.
status_code.h
Definition of StatusCode enumeration.
uri.h
Definition of URI class.
operator<<
std::ostream & operator<<(std::ostream &stream, const msgpack_rpc::addresses::URI &uri)
Format a URI.
Definition
uri.cpp:110
src
msgpack_rpc
addresses
uri.cpp
Generated on
for cpp-msgpack-rpc by
1.18.0