-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuri.h
More file actions
83 lines (70 loc) · 2.02 KB
/
uri.h
File metadata and controls
83 lines (70 loc) · 2.02 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
#pragma once
#include <string>
namespace Uri
{
struct Uri
{
std::string uri;
std::string path;
std::string protocol;
std::string host;
uint16_t port = 0;
};
inline Uri Parse(const std::string &uri)
{
static const std::string protocolEndString = "://";
static const std::string portStartString = ":";
static const std::string slashString = "/";
Uri result;
result.uri = uri;
result.port = 0;
if (uri.length() == 0)
return result;
// protocol
std::string::size_type protocolEnd = uri.find(protocolEndString, 0);
std::string::size_type hostStart = 0;
std::string::size_type portStart = 0;
std::string::size_type hostEnd = 0;
if (protocolEnd != std::string::npos)
{
result.protocol = uri.substr(0, protocolEnd);
hostStart = protocolEnd + protocolEndString.size();
}
else
{
return result;
}
// port
portStart = uri.find(portStartString, hostStart);
if (portStart != std::string::npos)
{
hostEnd = uri.find(slashString, portStart);
if(hostEnd != std::string::npos)
{
std::string port = uri.substr(portStart+1, hostEnd-portStart-1);
result.port = static_cast<uint16_t>(std::stoi(port));
}
else
{
std::string port = uri.substr(portStart+1);
result.port = static_cast<uint16_t>(std::stoi(port));
}
hostEnd = portStart;
}
else
{
hostEnd = uri.find(slashString, hostStart);
if(hostEnd == std::string::npos)
{
return result;
}
}
// host
result.host = uri.substr(hostStart, hostEnd-hostStart);
return result;
}
inline bool IsLocal(Uri* uri)
{
return uri->protocol.empty();
}
}