-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestURI.cpp
More file actions
73 lines (65 loc) · 1.48 KB
/
RequestURI.cpp
File metadata and controls
73 lines (65 loc) · 1.48 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
#include "RequestURI.hpp"
RequestURI::RequestURI(const string &uri)
{
uriStr = uri;
size_t start = 0;
size_t end = uriStr.find_first_of("?#", 0);
if (end == string::npos)
{
path = uriStr;
return;
}
path = uriStr.substr(start, end - start);
start = end;
if (uriStr[start] == '?')
{
end = uriStr.find_first_of("#", 0);
if (end == string::npos)
{
query = uriStr.substr(start + 1, end - start - 1);
return;
}
}
fragment = uriStr.substr(end + 1, uriStr.size() - end - 1);
}
RequestURI::RequestURI(const string &path, const string &query, const string &fragment)
{
this->path = path;
this->query = query;
this->fragment = fragment;
updateUriStr();
}
RequestURI::~RequestURI() {}
const string &RequestURI::getPath() const
{
return path;
}
void RequestURI::updateUriStr()
{
uriStr = path;
if (query.size() != 0)
uriStr += "?" + query;
if (fragment.size() != 0)
uriStr += "#" + fragment;
}
const string &RequestURI::getQuery() const
{
return query;
}
const string &RequestURI::getFragment() const
{
return fragment;
}
const string &RequestURI::getUriStr() const
{
return uriStr;
}
ostream &operator<<(ostream &out, const RequestURI &uri)
{
out << uri.getPath();
if (uri.getQuery().size() != 0)
out << "?" << uri.getQuery();
if (uri.getFragment().size() != 0)
out << "#" << uri.getFragment();
return out;
}