Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
endpoint_uri.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_address/endpoint_uri.h
10//! @brief Network endpoint URI.
11
12#ifndef ROC_ADDRESS_ENDPOINT_URI_H_
13#define ROC_ADDRESS_ENDPOINT_URI_H_
14
18#include "roc_core/stddefs.h"
21
22namespace roc {
23namespace address {
24
25//! Network endpoint URI.
27public:
28 //! URI subset.
29 enum Subset {
30 Subset_Full, //!< Entire URI.
31 Subset_Resource //!< Absolute path and query.
32 };
33
34 //! Initialize empty URI.
36
37 //! Check if URI is equivalent to another URI.
38 bool is_equal(const EndpointUri& other) const;
39
40 //! Copy data from another URI.
41 bool assign(const EndpointUri& other);
42
43 //! Check given subset of the URI.
44 bool verify(Subset subset) const;
45
46 //! Clear given subset of the URI.
47 void clear(Subset subset);
48
49 //! Invalidate given subset of the URI.
50 void invalidate(Subset subset);
51
52 //! Set protocol ID (URI scheme).
54
55 //! Protocol ID (URI scheme).
56 Protocol proto() const;
57
58 //! Get protocol ID (URI scheme).
59 bool get_proto(Protocol& proto) const;
60
61 //! Get URI proto.
63
64 //! Set URI host.
65 //! String should be zero-terminated.
66 bool set_host(const char* str);
67
68 //! Set URI host.
69 //! String should not be zero-terminated.
70 bool set_host(const char* str, size_t str_len);
71
72 //! Hostname or IP address.
73 const char* host() const;
74
75 //! Get URI host.
77
78 //! Set port.
79 bool set_port(int);
80
81 //! TCP or UDP port.
82 int port() const;
83
84 //! Get URI port.
85 bool get_port(int& port) const;
86
87 //! Get string representation of port.
88 //! If port is not set, default port for the protocol is used.
89 //! This string is suitable for passing to getaddrinfo().
90 //! @returns NULL if both port and default port are not set.
91 const char* service() const;
92
93 //! Set decoded URI path.
94 bool set_path(const char* str);
95
96 //! Set decoded URI path.
97 //! String should not be zero-terminated.
98 bool set_path(const char* str, size_t str_len);
99
100 //! Set encoded URI path.
101 //! String should be percent-encoded.
102 bool set_encoded_path(const char* str);
103
104 //! Set encoded URI path.
105 //! String should be percent-encoded.
106 //! String should not be zero-terminated.
107 bool set_encoded_path(const char* str, size_t str_len);
108
109 //! Decoded path.
110 const char* path() const;
111
112 //! Get URI path.
113 //! String will be percent-encoded.
115
116 //! Set query.
117 //! String should be percent-encoded.
118 bool set_encoded_query(const char* str);
119
120 //! Set query.
121 //! String should be percent-encoded.
122 //! String should not be zero-terminated.
123 bool set_encoded_query(const char* str, size_t str_len);
124
125 //! Raw query.
126 const char* encoded_query() const;
127
128 //! Get URI query.
129 //! String will be percent-encoded.
131
132private:
133 void set_service_from_port_(int port);
134 bool set_service_from_proto_(Protocol proto);
135
136 enum Part {
137 PartProto = (1 << 0),
138 PartHost = (1 << 1),
139 PartPort = (1 << 2),
140 PartPath = (1 << 3),
141 PartQuery = (1 << 4)
142 };
143
144 bool part_is_valid_(Part part) const;
145 void set_valid_(Part part);
146 void set_invalid_(Part part);
147
148 int invalid_parts_;
149
150 Protocol proto_;
151
153 int port_;
154 char service_[6];
155
158};
159
160//! Parse EndpointUri from string.
161//!
162//! The URI should be in the following form:
163//! - PROTOCOL://HOST[:PORT][/PATH][?QUERY]
164//!
165//! Examples:
166//! - rtp+rs8m://localhost
167//! - rtsp://localhost:123/path?query
168//! - rtp://127.0.0.1:123
169//! - rtp://[::1]:123
170//!
171//! The URI syntax is defined by RFC 3986.
172//!
173//! The path and query fields are allowed only for some protocols.
174//!
175//! The port field can be omitted if the protocol have standard port. Otherwise,
176//! the port can not be omitted.
177//!
178//! The path and host fields of the URI are percent-decoded. (But the set of allowed
179//! unencoded characters is different for path and host).
180//!
181//! The query fields of the URI is kept as is. The user is responsible
182//! to percent-decode it when necessary.
183//!
184//! This parser does not try to perform full URI validation. For example, it does not
185//! check that path contains only allowed symbols. If it can be parsed, it will be.
186bool parse_endpoint_uri(const char* str, EndpointUri::Subset subset, EndpointUri& result);
187
188//! Format EndpointUri to string.
189//!
190//! Formats a normalized form of the URI.
191//!
192//! The path and host parts of the URI are percent-encoded if necessary.
193//! The query field is stored in the encoded form, so it is just copied as is.
194//!
195//! @returns
196//! true on success or false if the buffer is too small.
198 EndpointUri::Subset subset,
200
201} // namespace address
202} // namespace roc
203
204#endif // ROC_ADDRESS_ENDPOINT_URI_H_
Network endpoint URI.
const char * encoded_query() const
Raw query.
bool set_proto(Protocol)
Set protocol ID (URI scheme).
bool set_encoded_query(const char *str, size_t str_len)
Set query. String should be percent-encoded. String should not be zero-terminated.
bool format_proto(core::StringBuilder &dst) const
Get URI proto.
@ Subset_Resource
Absolute path and query.
void invalidate(Subset subset)
Invalidate given subset of the URI.
void clear(Subset subset)
Clear given subset of the URI.
bool get_proto(Protocol &proto) const
Get protocol ID (URI scheme).
const char * host() const
Hostname or IP address.
bool set_encoded_path(const char *str, size_t str_len)
Set encoded URI path. String should be percent-encoded. String should not be zero-terminated.
int port() const
TCP or UDP port.
bool is_equal(const EndpointUri &other) const
Check if URI is equivalent to another URI.
bool format_encoded_path(core::StringBuilder &dst) const
Get URI path. String will be percent-encoded.
const char * path() const
Decoded path.
bool verify(Subset subset) const
Check given subset of the URI.
bool set_path(const char *str, size_t str_len)
Set decoded URI path. String should not be zero-terminated.
EndpointUri(core::IAllocator &)
Initialize empty URI.
bool set_encoded_path(const char *str)
Set encoded URI path. String should be percent-encoded.
bool format_host(core::StringBuilder &dst) const
Get URI host.
bool set_port(int)
Set port.
bool set_path(const char *str)
Set decoded URI path.
bool set_host(const char *str, size_t str_len)
Set URI host. String should not be zero-terminated.
bool set_encoded_query(const char *str)
Set query. String should be percent-encoded.
Protocol proto() const
Protocol ID (URI scheme).
bool format_encoded_query(core::StringBuilder &dst) const
Get URI query. String will be percent-encoded.
bool assign(const EndpointUri &other)
Copy data from another URI.
bool set_host(const char *str)
Set URI host. String should be zero-terminated.
bool get_port(int &port) const
Get URI port.
const char * service() const
Get string representation of port. If port is not set, default port for the protocol is used....
Memory allocator interface.
Definition iallocator.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
Interface ID.
bool parse_endpoint_uri(const char *str, EndpointUri::Subset subset, EndpointUri &result)
Parse EndpointUri from string.
Protocol
Protocol ID.
Definition protocol.h:19
bool format_endpoint_uri(const EndpointUri &uri, EndpointUri::Subset subset, core::StringBuilder &dst)
Format EndpointUri to string.
Root namespace.
Non-copyable object.
Protocol ID.
Commonly used types and functions.
String buffer.
String builder.