Repository navigation
Expand file tree
/
Copy pathHostClass.cpp
More file actions
52 lines (44 loc) · 1.5 KB
/
Copy pathHostClass.cpp
File metadata and controls
52 lines (44 loc) · 1.5 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
#include <cctype>
#include <string>
#include "ns/http/ModuleFetch.h"
namespace nativescript::common::http {
namespace {
std::string LowercaseHostOf(const std::string& url) {
const size_t schemeEnd = url.find("://");
if (schemeEnd == std::string::npos) {
return "";
}
const size_t start = schemeEnd + 3;
const size_t end = url.find_first_of("/?#", start);
std::string authority = url.substr(
start, end == std::string::npos ? std::string::npos : end - start);
const size_t at = authority.rfind('@');
if (at != std::string::npos) {
authority.erase(0, at + 1);
}
std::string host;
if (!authority.empty() && authority[0] == '[') {
const size_t close = authority.find(']');
host =
close == std::string::npos ? authority : authority.substr(0, close + 1);
} else {
host = authority.substr(0, authority.find(':'));
}
for (char& c : host) {
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return host;
}
} // namespace
ModuleFetchHostClass ModuleFetchHostClassOf(const std::string& url) {
const std::string host = LowercaseHostOf(url);
if (host == "127.0.0.1" || host == "[::1]" || host == "localhost" ||
host == "10.0.2.2" || host == "10.0.3.2") {
return ModuleFetchHostClass::kLoopback;
}
return ModuleFetchHostClass::kRemote;
}
const char* ModuleFetchHostClassName(ModuleFetchHostClass hostClass) {
return hostClass == ModuleFetchHostClass::kLoopback ? "loopback" : "remote";
}
} // namespace nativescript::common::http