Skip to content

Commit cccacb6

Browse files
author
Sebastiano Merlino
committed
Linearized code to make it more readable
1 parent d3b3027 commit cccacb6

File tree

1 file changed

+40
-106
lines changed

1 file changed

+40
-106
lines changed

src/details/http_endpoint.cpp

Lines changed: 40 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ http_endpoint::http_endpoint
5050
family_url(family),
5151
reg_compiled(false)
5252
{
53-
if(use_regex)
54-
this->url_modded = "^/";
55-
else
56-
this->url_modded = "/";
53+
this->url_modded = use_regex ? "^/" : "/";
5754
vector<string> parts;
5855

5956
#ifdef CASE_INSENSITIVE
@@ -68,103 +65,49 @@ http_endpoint::http_endpoint
6865
http_utils::tokenize_url(url, parts);
6966
string buffered;
7067
bool first = true;
71-
if(registration)
68+
69+
for (unsigned int i = 0; i < parts.size(); i++)
7270
{
73-
for(unsigned int i = 0; i< parts.size(); i++)
71+
if(!registration)
7472
{
75-
if((parts[i] != "") && (parts[i][0] != '{'))
76-
{
77-
if(first)
78-
{
79-
if(parts[i][0] == '^')
80-
{
81-
this->url_modded = parts[i];
82-
}
83-
else
84-
{
85-
this->url_modded += parts[i];
86-
}
87-
first = false;
88-
}
89-
else
90-
{
91-
this->url_modded += "/" + parts[i];
92-
}
93-
}
94-
else
95-
{
96-
if(
97-
(parts[i].size() >= 3) &&
98-
(parts[i][0] == '{') &&
99-
(parts[i][parts[i].size() - 1] == '}')
100-
)
101-
{
102-
std::string::size_type bar = parts[i].find_first_of('|');
103-
if(bar != string::npos)
104-
{
105-
this->url_pars.push_back(parts[i].substr(1, bar - 1));
106-
if(first)
107-
{
108-
this->url_modded += parts[i].substr(
109-
bar + 1, parts[i].size() - bar - 2
110-
);
111-
first = false;
112-
}
113-
else
114-
{
115-
this->url_modded += "/"+parts[i].substr(
116-
bar + 1, parts[i].size() - bar - 2
117-
);
118-
}
119-
}
120-
else
121-
{
122-
this->url_pars.push_back(
123-
parts[i].substr(1,parts[i].size() - 2)
124-
);
125-
if(first)
126-
{
127-
this->url_modded += "([^\\/]+)";
128-
first = false;
129-
}
130-
else
131-
{
132-
this->url_modded += "/([^\\/]+)";
133-
}
134-
}
135-
this->chunk_positions.push_back(i);
136-
}
137-
else
138-
{
139-
throw bad_http_endpoint();
140-
}
141-
}
73+
this->url_modded += (first ? "" : "/") + parts[i];
74+
first = false;
75+
14276
this->url_pieces.push_back(parts[i]);
77+
78+
continue;
14379
}
144-
}
145-
else
146-
{
147-
for(unsigned int i = 0; i< parts.size(); i++)
80+
81+
if((parts[i] != "") && (parts[i][0] != '{'))
14882
{
149-
if(first)
150-
{
151-
this->url_modded += parts[i];
152-
first = false;
153-
}
154-
else
155-
{
156-
this->url_modded += "/" + parts[i];
157-
}
83+
this->url_modded = (first ? "" : "/") + (parts[i][0] == '^' ? "" : this->url_modded) + parts[i];
84+
first = false;
15885
this->url_pieces.push_back(parts[i]);
86+
87+
continue;
15988
}
89+
90+
if((parts[i].size() < 3) || (parts[i][0] != '{') || (parts[i][parts[i].size() - 1] != '}'))
91+
throw bad_http_endpoint();
92+
93+
std::string::size_type bar = parts[i].find_first_of('|');
94+
this->url_pars.push_back(parts[i].substr(1, bar != string::npos ? bar - 1 : parts[i].size() - bar - 2));
95+
this->url_modded += (first ? "" : "/") + (bar != string::npos ? parts[i].substr(bar + 1, parts[i].size() - bar - 2) : "([^\\/]+)");
96+
97+
first = false;
98+
99+
this->chunk_positions.push_back(i);
100+
101+
this->url_pieces.push_back(parts[i]);
160102
}
103+
161104
if(use_regex)
162105
{
163106
this->url_modded += "$";
164107
regcomp(&(this->re_url_modded), url_modded.c_str(),
165108
REG_EXTENDED|REG_ICASE|REG_NOSUB
166109
);
167-
reg_compiled = true;
110+
this->reg_compiled = true;
168111
}
169112
}
170113

@@ -206,27 +149,18 @@ bool http_endpoint::operator <(const http_endpoint& b) const
206149

207150
bool http_endpoint::match(const http_endpoint& url) const
208151
{
209-
if(this->family_url && (url.url_pieces.size() >= this->url_pieces.size()))
152+
153+
if(!this->family_url || url.url_pieces.size() < this->url_pieces.size())
154+
return regexec(&(this->re_url_modded), url.url_complete.c_str(), 0, NULL, 0) == 0;
155+
156+
string nn = "/";
157+
bool first = true;
158+
for(unsigned int i = 0; i < this->url_pieces.size(); i++)
210159
{
211-
string nn = "/";
212-
bool first = true;
213-
for(unsigned int i = 0; i < this->url_pieces.size(); i++)
214-
{
215-
if(first)
216-
{
217-
nn += url.url_pieces[i];
218-
first = false;
219-
}
220-
else
221-
{
222-
nn += "/" + url.url_pieces[i];
223-
}
224-
}
225-
return regexec(&(this->re_url_modded), nn.c_str(), 0, NULL, 0) == 0;
160+
nn += (first ? "" : "/") + url.url_pieces[i];
161+
first = false;
226162
}
227-
else
228-
return regexec(&(this->re_url_modded),
229-
url.url_complete.c_str(), 0, NULL, 0) == 0;
163+
return regexec(&(this->re_url_modded), nn.c_str(), 0, NULL, 0) == 0;
230164
}
231165

232166
};

0 commit comments

Comments
 (0)