Skip to content

Commit 90e7a0d

Browse files
author
Sebastiano Merlino
committed
Created COMPARATOR macro to avoid repetition in http_utils code to
compare headers and arguments. Changed http_endpoint to use comparator to compare string.
1 parent 4b333e7 commit 90e7a0d

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

src/http_endpoint.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,7 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h)
192192

193193
bool http_endpoint::operator <(const http_endpoint& b) const
194194
{
195-
string url_modded_l;
196-
string url_modded_r;
197-
string_utilities::to_lower_copy(this->url_modded, url_modded_l);
198-
string_utilities::to_lower_copy(b.url_modded, url_modded_r);
199-
return url_modded_l < url_modded_r;
195+
COMPARATOR(this->url_modded, b.url_modded, toupper);
200196
}
201197

202198
bool http_endpoint::match(const http_endpoint& url) const

src/httpserver/http_utils.hpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ class http_utils
230230
static void standardize_url(const std::string&, std::string& result);
231231
};
232232

233+
#define COMPARATOR(x, y, op) \
234+
{ \
235+
size_t l1 = (x).size();\
236+
size_t l2 = (y).size();\
237+
if (l1 < l2) return true;\
238+
if (l1 > l2) return false;\
239+
\
240+
for (size_t n = 0; n < l1; n++)\
241+
{\
242+
char xc = op((x)[n]);\
243+
char yc = op((y)[n]);\
244+
if (xc < yc) return true;\
245+
if (xc > yc) return false;\
246+
}\
247+
return false;\
248+
}
249+
233250
class header_comparator {
234251
public:
235252
/**
@@ -239,19 +256,7 @@ class header_comparator {
239256
**/
240257
bool operator()(const std::string& x,const std::string& y) const
241258
{
242-
size_t l1 = x.size();
243-
size_t l2 = y.size();
244-
if (l1 < l2) return true;
245-
if (l1 > l2) return false;
246-
247-
for (size_t n = 0; n < l1; n++)
248-
{
249-
char a = toupper(x[n]);
250-
char b = toupper(y[n]);
251-
if (a < b) return true;
252-
if (a > b) return false;
253-
}
254-
return false;
259+
COMPARATOR(x, y, toupper);
255260
}
256261
};
257262

@@ -269,24 +274,11 @@ class arg_comparator {
269274
**/
270275
bool operator()(const std::string& x,const std::string& y) const
271276
{
272-
size_t l1 = x.size();
273-
size_t l2 = y.size();
274-
if (l1 < l2) return true;
275-
if (l1 > l2) return false;
276-
277-
for (size_t n = 0; n < l1; n++)
278-
{
279277
#ifdef CASE_INSENSITIVE
280-
char a = toupper(x[n]);
281-
char b = toupper(y[n]);
278+
COMPARATOR(x, y, toupper);
282279
#else
283-
char a = x[n];
284-
char b = y[n];
280+
COMPARATOR(x, y, );
285281
#endif
286-
if (a < b) return true;
287-
if (a > b) return false;
288-
}
289-
return false;
290282
}
291283
};
292284

0 commit comments

Comments
 (0)