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