uriparser.h
3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef OSDEV_COMPONENTS_MQTT_URIPARSER_H
#define OSDEV_COMPONENTS_MQTT_URIPARSER_H
// std
#include <string>
#include "commondefs.h"
namespace osdev {
namespace components {
namespace mqtt {
/**
* @brief Helper class to parse, normalize, and replace the port service name by portnumber in a uri.
* example:
* opc.tcp://user:secret\@processmodel:processmodel/path1/path2?query3=value4#fragment5
* will result in
* opc.tcp://user:secret\@processmodel:12345/path1/path2?query3=value4#fragment5
* @note This assumes that the port service name was registered in the etc/services file under protocol tcp.
* Lookup of a portnumber for protocols other than tcp are not supported.
*
* IPv6 addresses in a uri are only parsable when they are wrapped in brackets ([]) (RFC 3986, section 3.2.2)
*
* @note This class is designed to handle a subset of all possible uris. The scheme and the authority part are
* expected not to be empty. The authority part can contain user and password information.
*
* @note Only scheme, hostname and port are converted to lowercase (see RFC 3986 6.2.2.1. Case Normalization)
*
* @note Special characters are escaped with percent encoding. This applies to user, password, path, query and fragment parts.
* The path and query part is escaped on a per element basis. This means that ParsedUri contains the raw query and path strings.
* The decoding is done when the methods parseQuery and parsePath are called.
*/
class UriParser
{
public:
/**
* @brief Parses the specified uri string.
* @param uri The uri string to parse.
* @return A map containing the parsed uri.
* @note The path and query parts can still contain percent encoded special characters.
*/
static ParsedUri parse(const std::string& uri);
/**
* @brief Parse the query part of a parsed uri. Percent encoded special characters are decoded.
* @param parsedUri A ParsedUri object.
* @return key/value map.
*/
static ParsedQuery parseQuery(const ParsedUri& parsedUri);
/**
* @brief Parse the path part of a parsed uri. Percent encoded special characters are decoded.
* @param parsedUri A ParsedUri object.
* @return vector of path elements.
*/
static ParsedPath parsePath(const ParsedUri& parsedUri);
/**
* @brief Parses, normalizes, and replaces the port service name by portnumber in the specified uri string.
* @param uri The uri string to parse and normalize.
* @return The normalized uri string, with the port service name replaced by the portnumber.
*/
static std::string normalize(const std::string& uri);
/**
* @brief Converts a parsed uri back to a string.
* @param parsedUri The parsed uri to convert to string.
* @return The uri as string.
*/
static std::string toString(const ParsedUri& parsedUri);
/**
* @brief Get portnumber associated with a service.
* @param serviceName Name of the service for which a portnumber is searched.
* @param protocolName Name of the protocol for which the portname was registered.
* @return portnumber.
* @throws InvalidArgumentException when service is unknown.
* @throws SystemException when underlying systemcall fails.
*/
static int getPortnumber(const std::string& serviceName, const std::string& protocolName = "tcp");
};
} // End namespace mqtt
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_MQTT_URIPARSER_H