-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.cpp
More file actions
119 lines (107 loc) · 2.72 KB
/
Location.cpp
File metadata and controls
119 lines (107 loc) · 2.72 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Location.hpp"
#include <iostream>
#include "Utils.hpp"
Location::Location(const vector<string> &starts_with,
const vector<string> &ends_with,
const vector<string> &methods,
const string &alias,
const vector<string> &index,
const string & cgi_script,
unsigned long long max_body)
{
this->starts_with = starts_with;
this->ends_with = ends_with;
this->methods = methods;
this->alias = alias;
this->index = index;
this->cgi_script = cgi_script;
this->max_body = max_body;
}
const string &Location::getAlias() const
{
return alias;
}
const vector<string> &Location::getStartWith() const
{
return starts_with;
}
const vector<string> &Location::getEndtWith() const
{
return ends_with;
}
const vector<string> &Location::getMethods() const
{
return methods;
}
const vector<string> &Location::getIndex() const
{
return index;
}
unsigned long long Location::getMaxBody() const
{
return max_body;
}
const string &Location::getCgiScript() const
{
return cgi_script;
}
bool Location::isMatchMethod(const string &method) const
{
bool matched = false;
for(auto const &m:methods)
{
if(m == method)
{
matched = true;
break;
}
}
return matched;
}
string normalisePath(const string &path)
{
string normPath;
normPath.reserve(path.size());
normPath.push_back(path[0]);
for(size_t j = 1; j < path.size(); j++)
{
if(path[j - 1] == '/' && path[j] == '/')
continue;
normPath.push_back(path[j]);
}
return normPath;
}
vector<string> Location::matchesUri(const string &uri) const
{
vector<string> res;
for(auto const &s:starts_with)
{
string t = uri.substr(0, s.length());
if(s == t)
{
for(auto const &e:ends_with)
{
if(uri.length() < e.length())
continue;
if(e == uri.substr(uri.length() - e.length() , e.length()))
{
string new_uri = alias + uri.substr(s.length());
new_uri = normalisePath(new_uri);
res.push_back(new_uri);
}
}
}
}
return res;
}
ostream &operator<<(ostream &out, const Location &l)
{
out << "location" << endl;
out << "\tstarts_with = " << join(";", l.getStartWith()) << endl;
out << "\tends_with = " << join(";", l.getEndtWith()) << endl;
out << "\tmethods_with = " << join(";", l.getMethods()) << endl;
out << "\tindex = " << join(";", l.getIndex()) << endl;
out << "\talias = " << l.getAlias() << endl;
out << "\tcgi_script = " << l.getCgiScript() << endl;
return out;
}