16 size_t schemeEnd = rawUri.find(
':');
17 if (schemeEnd == std::string::npos)
return;
18 scheme =
String(rawUri.substr(0, schemeEnd).c_str());
21 size_t authStart = schemeEnd + 1;
22 if (authStart + 1 < rawUri.length() && rawUri.substr(authStart, 2) ==
"//") {
24 size_t authEnd = rawUri.find_first_of(
"/?#", authStart);
25 authority = (authEnd == std::string::npos)
26 ?
String(rawUri.substr(authStart).c_str())
27 :
String(rawUri.substr(authStart, authEnd - authStart).c_str());
33 size_t pathStart = rawUri.find(
"//");
34 if (pathStart != std::string::npos) {
35 pathStart = rawUri.find(
'/', pathStart + 2);
37 size_t colon = rawUri.find(
':');
38 pathStart = (colon != std::string::npos)
39 ? (scheme.
Equals(
"mailto") ? colon + 1 : rawUri.find(
'/', colon + 1))
44 if (pathStart != std::string::npos) {
45 size_t pathEnd = rawUri.find_first_of(
"?#", pathStart);
46 path = (pathEnd == std::string::npos)
47 ?
String(rawUri.substr(pathStart).c_str())
48 :
String(rawUri.substr(pathStart, pathEnd - pathStart).c_str());
55 size_t qStart = rawUri.find(
'?');
56 if (qStart != std::string::npos) {
57 size_t qEnd = rawUri.find(
'#', qStart);
58 query = (qEnd == std::string::npos)
59 ?
String(rawUri.substr(qStart + 1).c_str())
60 :
String(rawUri.substr(qStart + 1, qEnd - qStart - 1).c_str());
64 size_t fStart = rawUri.find(
'#');
65 if (fStart != std::string::npos) {
66 fragment =
String(rawUri.substr(fStart + 1).c_str());
72 std::string authStr = (
const char*)authority;
73 if (authStr.empty())
return;
76 size_t userEnd = authStr.find(
'@');
77 if (userEnd != std::string::npos) {
78 userInfo =
String(authStr.substr(0, userEnd).c_str());
79 authStr = authStr.substr(userEnd + 1);
83 size_t portStart = authStr.find(
':');
84 if (portStart != std::string::npos) {
85 host =
String(authStr.substr(0, portStart).c_str());
86 port = std::stoi(authStr.substr(portStart + 1));
88 host =
String(authStr.c_str());
89 port = (scheme.
ToLower() ==
"https") ? 443 : 80;
93 void Uri::ParseUri() {
95 std::string rawUri = (
const char*)_uriString;
120 if (_scheme.Equals(
"http") && _port == 80)
return true;
121 if (_scheme.Equals(
"https") && _port == 443)
return true;
127 return _scheme.
Equals(
"file");
132 return _host.Equals(
"localhost") || _host.Equals(
"127.0.0.1") || _host.Equals(
"::1");
137 std::string s = (
const char*)stringToEscape;
140 if (isalnum(
static_cast<unsigned char>(c)) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
144 snprintf(buf, 4,
"%%%02X",
static_cast<unsigned char>(c));
148 return String(escaped.c_str());
155 if (sscanf_s(s.substr(i + 1, 2).c_str(),
"%x", &value) == 1) {
157 if (sscanf(s.substr(i + 1, 2).c_str(),
"%x", &value) == 1) {
159 unescaped +=
static_cast<char>(value);
168 std::string s = (
const char*)stringToUnescape;
169 std::string unescaped;
170 for (
size_t i = 0; i < s.length(); ++i) {
171 if (s[i] ==
'%' && i + 2 < s.length()) {
177 return String(unescaped.c_str());
Provides an object representation of a Uniform Resource Identifier (URI) and easy access to its parts...
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
String()
Initializes a new instance of the String class to an empty string.
static bool Equals(const String &sStr1, const String &sStr2)
String GetHost() const
Gets the host component of this instance.
String GetFragment() const
Gets the escaped URI fragment.
String GetOriginalString() const
Gets the original URI string passed to the constructor.
bool IsFile() const
Determines whether the specified Uri is a file URI.
int GetPort() const
Gets the port number of this URI.
String GetUserInfo() const
Gets the user name, password, or other user-specific information associated with the specified URI.
bool IsLoopback() const
Determines whether the specified Uri references the local host.
String GetScheme() const
Gets the scheme name for this URI.
Uri(const String &uriString)
Initializes a new instance of the Uri class with the specified URI string.
static String UnescapeDataString(const String &stringToUnescape)
Converts a string to its unescaped representation according to RFC 3986.
String GetQuery() const
Gets any query information included in the specified URI.
bool IsDefaultPort() const
Determines whether the port value of the URI is the default for this scheme.
String GetAbsoluteUri() const
Gets the absolute URI string representation.
String GetAuthority() const
Gets the authority (host, optional port, and optional user information).
String ToString() const
Gets the canonical string representation for the specified Uri instance.
String GetAbsolutePath() const
Gets the absolute path of the URI.
static String EscapeDataString(const String &stringToEscape)
Converts a string to its escaped representation according to RFC 3986.
static void ParseAuthorityComponents(const String &authority, const String &scheme, String &userInfo, String &host, int &port)
static void ParseQueryAndFragment(const std::string &rawUri, String &query, String &fragment)
static void ParseSchemeAndAuthority(const std::string &rawUri, String &scheme, String &authority)
static void ParsePath(const std::string &rawUri, const String &scheme, String &path)
static void UnescapePercentChar(const std::string &s, size_t &i, std::string &unescaped)