Skip to content

Commit a598cb4

Browse files
committed
http_endpoint to use C++ regex
1 parent 0bafefc commit a598cb4

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/details/http_endpoint.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ using namespace http;
3434

3535
http_endpoint::~http_endpoint()
3636
{
37-
if(reg_compiled)
38-
{
39-
regfree(&(this->re_url_normalized));
40-
}
4137
}
4238

4339
http_endpoint::http_endpoint
@@ -118,9 +114,7 @@ http_endpoint::http_endpoint
118114
if(use_regex)
119115
{
120116
this->url_normalized += "$";
121-
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
122-
REG_EXTENDED|REG_ICASE|REG_NOSUB
123-
);
117+
this->re_url_normalized = std::regex(url_normalized, std::regex_constants::icase & std::regex_constants::nosubs & std::regex_constants::extended);
124118
this->reg_compiled = true;
125119
}
126120
}
@@ -131,35 +125,50 @@ http_endpoint::http_endpoint(const http_endpoint& h):
131125
url_pars(h.url_pars),
132126
url_pieces(h.url_pieces),
133127
chunk_positions(h.chunk_positions),
128+
re_url_normalized(h.re_url_normalized),
129+
family_url(h.family_url),
130+
reg_compiled(h.reg_compiled)
131+
{
132+
}
133+
134+
http_endpoint::http_endpoint(http_endpoint&& h):
135+
url_complete(std::move(h.url_complete)),
136+
url_normalized(std::move(h.url_normalized)),
137+
url_pars(std::move(h.url_pars)),
138+
url_pieces(std::move(h.url_pieces)),
139+
chunk_positions(std::move(h.chunk_positions)),
140+
re_url_normalized(std::move(h.re_url_normalized)),
134141
family_url(h.family_url),
135142
reg_compiled(h.reg_compiled)
136143
{
137-
if(this->reg_compiled)
138-
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
139-
REG_EXTENDED|REG_ICASE|REG_NOSUB
140-
);
141144
}
142145

143-
http_endpoint& http_endpoint::operator =(const http_endpoint& h)
146+
http_endpoint& http_endpoint::operator=(const http_endpoint& h)
144147
{
145148
this->url_complete = h.url_complete;
146149
this->url_normalized = h.url_normalized;
147150
this->family_url = h.family_url;
148151
this->reg_compiled = h.reg_compiled;
149-
if(this->reg_compiled)
150-
{
151-
regfree(&(this->re_url_normalized));
152-
153-
regcomp(&(this->re_url_normalized), url_normalized.c_str(),
154-
REG_EXTENDED|REG_ICASE|REG_NOSUB
155-
);
156-
}
152+
this->re_url_normalized = h.re_url_normalized;
157153
this->url_pars = h.url_pars;
158154
this->url_pieces = h.url_pieces;
159155
this->chunk_positions = h.chunk_positions;
160156
return *this;
161157
}
162158

159+
http_endpoint& http_endpoint::operator=(http_endpoint&& h)
160+
{
161+
this->url_complete = std::move(h.url_complete);
162+
this->url_normalized = std::move(h.url_normalized);
163+
this->family_url = h.family_url;
164+
this->reg_compiled = h.reg_compiled;
165+
this->re_url_normalized = std::move(h.re_url_normalized);
166+
this->url_pars = std::move(h.url_pars);
167+
this->url_pieces = std::move(h.url_pieces);
168+
this->chunk_positions = std::move(h.chunk_positions);
169+
return *this;
170+
}
171+
163172
bool http_endpoint::operator <(const http_endpoint& b) const
164173
{
165174
COMPARATOR(this->url_normalized, b.url_normalized, std::toupper);
@@ -170,7 +179,7 @@ bool http_endpoint::match(const http_endpoint& url) const
170179
if (!this->reg_compiled) throw std::invalid_argument("Cannot run match. Regex suppressed.");
171180

172181
if(!this->family_url || url.url_pieces.size() < this->url_pieces.size())
173-
return regexec(&(this->re_url_normalized), url.url_complete.c_str(), 0, NULL, 0) == 0;
182+
return regex_match(url.url_complete, this->re_url_normalized);
174183

175184
string nn = "/";
176185
bool first = true;
@@ -179,7 +188,7 @@ bool http_endpoint::match(const http_endpoint& url) const
179188
nn += (first ? "" : "/") + url.url_pieces[i];
180189
first = false;
181190
}
182-
return regexec(&(this->re_url_normalized), nn.c_str(), 0, NULL, 0) == 0;
191+
return regex_match(nn, this->re_url_normalized);
183192
}
184193

185194
};

src/httpserver/details/http_endpoint.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <vector>
2929
#include <utility>
30-
#include <regex.h>
30+
#include <regex>
3131
#include <string>
3232
#include <stdexcept>
3333

@@ -51,6 +51,8 @@ class http_endpoint
5151
**/
5252
http_endpoint(const http_endpoint& h);
5353

54+
http_endpoint(http_endpoint&& h);
55+
5456
/**
5557
* Class Destructor
5658
**/
@@ -71,6 +73,8 @@ class http_endpoint
7173
**/
7274
http_endpoint& operator =(const http_endpoint& h);
7375

76+
http_endpoint& operator =(http_endpoint&& h);
77+
7478
/**
7579
* Method indicating if this endpoint 'matches' with the one passed. A passed endpoint matches a registered endpoint if
7680
* the regex represented by the registered endpoint matches the passed one.
@@ -186,7 +190,7 @@ class http_endpoint
186190
/**
187191
* Regex used in comparisons
188192
**/
189-
regex_t re_url_normalized;
193+
std::regex re_url_normalized;
190194

191195
/**
192196
* Boolean indicating wheter the endpoint represents a family

0 commit comments

Comments
 (0)