Skip to content

Commit b3786d8

Browse files
author
Sebastiano Merlino
committed
Created new interface in order to optimize memory management
1 parent 238f96f commit b3786d8

13 files changed

+341
-74
lines changed

src/http_endpoint.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ http_endpoint::~http_endpoint()
3535
}
3636
}
3737
http_endpoint::http_endpoint(const string& url, bool family, bool registration):
38-
url_complete(string_utilities::to_lower_copy(url)),
3938
url_modded("/"),
4039
family_url(family),
4140
reg_compiled(false)
4241
{
43-
vector<string> parts = http_utils::tokenize_url(url);
42+
vector<string> parts;
43+
string_utilities::to_lower_copy(url, url_complete);
44+
http_utils::tokenize_url(url, parts);
4445
string buffered;
4546
bool first = true;
4647
if(registration)
@@ -156,7 +157,11 @@ http_endpoint& http_endpoint::operator =(const http_endpoint& h)
156157

157158
bool http_endpoint::operator <(const http_endpoint& b) const
158159
{
159-
return string_utilities::to_lower_copy(this->url_modded) < string_utilities::to_lower_copy(b.url_modded);
160+
string url_modded_l;
161+
string url_modded_r;
162+
string_utilities::to_lower_copy(this->url_modded, url_modded_l);
163+
string_utilities::to_lower_copy(b.url_modded, url_modded_r);
164+
return url_modded_l < url_modded_r;
160165
}
161166

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

src/http_request.cpp

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace httpserver
2828
//REQUEST
2929
void http_request::set_method(const std::string& method)
3030
{
31-
this->method = string_utilities::to_upper_copy(method);
31+
string_utilities::to_upper_copy(method, this->method);
3232
}
3333

3434
bool http_request::check_digest_auth(const std::string& realm, const std::string& password, int nonce_timeout, bool& reload_nonce) const
@@ -60,6 +60,26 @@ const std::vector<std::pair<std::string, std::string> > http_request::get_header
6060
#endif
6161
return to_ret;
6262
}
63+
size_t http_request::get_headers(std::vector<std::pair<std::string, std::string> >& result) const
64+
{
65+
std::map<std::string, std::string, header_comparator>::const_iterator it;
66+
for(it = headers.begin(); it != headers.end(); it++)
67+
#ifdef USE_CPP_ZEROX
68+
result.push_back(std::make_pair((*it).first,(*it).second));
69+
#else
70+
result.push_back(std::make_pair<std::string, std::string>((*it).first,(*it).second));
71+
#endif
72+
return result.size();
73+
}
74+
75+
#ifndef SWIG
76+
size_t http_request::get_headers(std::map<std::string, std::string, header_comparator>& result) const
77+
{
78+
result = this->headers;
79+
return result.size();
80+
}
81+
#endif
82+
6383
const std::vector<std::pair<std::string, std::string> > http_request::get_footers() const
6484
{
6585
std::vector<std::pair<std::string, std::string> > to_ret;
@@ -72,6 +92,26 @@ const std::vector<std::pair<std::string, std::string> > http_request::get_footer
7292
#endif
7393
return to_ret;
7494
}
95+
size_t http_request::get_footers(std::vector<std::pair<std::string, std::string> >& result) const
96+
{
97+
std::map<std::string, std::string, header_comparator>::const_iterator it;
98+
for(it = footers.begin(); it != footers.end(); it++)
99+
#ifdef USE_CPP_ZEROX
100+
result.push_back(std::make_pair((*it).first,(*it).second));
101+
#else
102+
result.push_back(std::make_pair<std::string, std::string>((*it).first,(*it).second));
103+
#endif
104+
return result.size();
105+
}
106+
107+
#ifndef SWIG
108+
size_t http_request::get_footers(std::map<std::string, std::string, header_comparator>& result) const
109+
{
110+
result = this->footers;
111+
return result.size();
112+
}
113+
#endif
114+
75115
const std::vector<std::pair<std::string, std::string> > http_request::get_cookies() const
76116
{
77117
std::vector<std::pair<std::string, std::string> > to_ret;
@@ -84,6 +124,27 @@ const std::vector<std::pair<std::string, std::string> > http_request::get_cookie
84124
#endif
85125
return to_ret;
86126
}
127+
128+
size_t http_request::get_cookies(std::vector<std::pair<std::string, std::string> >& result) const
129+
{
130+
std::map<std::string, std::string, header_comparator>::const_iterator it;
131+
for(it = cookies.begin(); it != cookies.end(); it++)
132+
#ifdef USE_CPP_ZEROX
133+
result.push_back(std::make_pair((*it).first,(*it).second));
134+
#else
135+
result.push_back(std::make_pair<std::string, std::string>((*it).first,(*it).second));
136+
#endif
137+
return result.size();
138+
}
139+
140+
#ifndef SWIG
141+
size_t http_request::get_cookies(std::map<std::string, std::string, header_comparator>& result) const
142+
{
143+
result = this->cookies;
144+
return result.size();
145+
}
146+
#endif
147+
87148
const std::vector<std::pair<std::string, std::string> > http_request::get_args() const
88149
{
89150
std::vector<std::pair<std::string, std::string> > to_ret;
@@ -97,4 +158,26 @@ const std::vector<std::pair<std::string, std::string> > http_request::get_args()
97158
return to_ret;
98159
}
99160

161+
size_t http_request::get_args(std::vector<std::pair<std::string, std::string> >& result) const
162+
{
163+
std::map<std::string, std::string, header_comparator>::const_iterator it;
164+
for(it = args.begin(); it != args.end(); it++)
165+
#ifdef USE_CPP_ZEROX
166+
result.push_back(std::make_pair((*it).first,(*it).second));
167+
#else
168+
result.push_back(std::make_pair<std::string, std::string>((*it).first,(*it).second));
169+
#endif
170+
return result.size();
171+
}
172+
173+
#ifndef SWIG
174+
size_t http_request::get_args(std::map<std::string, std::string, arg_comparator>& result) const
175+
{
176+
result = this->args;
177+
return result.size();
178+
}
179+
#endif
180+
181+
182+
100183
};

src/http_resource.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ http_response http_resource::render_CONNECT(const http_request& r)
114114

115115
http_response http_resource::route_request(const http_request& r)
116116
{
117-
string method = string_utilities::to_upper_copy(r.get_method());
117+
string method;
118+
r.get_method(method);
119+
string_utilities::to_upper(method);
118120

119121
http_response res;
120122

src/http_response.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,27 @@ const std::vector<std::pair<std::string, std::string> > http_response::get_heade
4141
#endif
4242
return to_ret;
4343
}
44+
size_t http_response::get_headers(std::vector<std::pair<std::string, std::string> >& result)
45+
{
46+
std::map<std::string, std::string, header_comparator>::const_iterator it;
47+
for(it = headers.begin(); it != headers.end(); it++)
48+
#ifdef USE_CPP_ZEROX
49+
result.push_back(std::make_pair((*it).first,(*it).second));
50+
#else
51+
result.push_back(std::make_pair<std::string, std::string>((*it).first,(*it).second));
52+
#endif
53+
return result.size();
54+
}
55+
size_t http_response::get_headers(std::map<std::string, std::string, header_comparator>& result)
56+
{
57+
result = this->headers;
58+
return result.size();
59+
}
60+
4461
const std::vector<std::pair<std::string, std::string> > http_response::get_footers()
4562
{
4663
std::vector<std::pair<std::string, std::string> > to_ret;
47-
std::map<std::string, std::string, arg_comparator>::const_iterator it;
64+
std::map<std::string, std::string, header_comparator>::const_iterator it;
4865
for(it = footers.begin(); it != footers.end(); it++)
4966
#ifdef USE_CPP_ZEROX
5067
to_ret.push_back(std::make_pair((*it).first,(*it).second));
@@ -53,6 +70,23 @@ const std::vector<std::pair<std::string, std::string> > http_response::get_foote
5370
#endif
5471
return to_ret;
5572
}
73+
size_t http_response::get_footers(std::vector<std::pair<std::string, std::string> >& result)
74+
{
75+
std::map<std::string, std::string, arg_comparator>::const_iterator it;
76+
for(it = footers.begin(); it != footers.end(); it++)
77+
#ifdef USE_CPP_ZEROX
78+
result.push_back(std::make_pair((*it).first,(*it).second));
79+
#else
80+
result.push_back(std::make_pair<std::string, std::string>((*it).first,(*it).second));
81+
#endif
82+
return result.size();
83+
}
84+
size_t http_response::get_footers(std::map<std::string, std::string, header_comparator>& result)
85+
{
86+
result = this->footers;
87+
return result.size();
88+
}
89+
5690
//RESPONSE
5791
shoutCAST_response::shoutCAST_response
5892
(

src/http_utils.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,27 @@ const std::string http_utils::http_post_encoding_form_urlencoded = MHD_HTTP_POST
162162
const std::string http_utils::http_post_encoding_multipart_formdata = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
163163

164164

165-
const std::vector<std::string> http_utils::tokenize_url(const std::string& str, const char separator)
165+
size_t http_utils::tokenize_url(const std::string& str, std::vector<std::string>& result, const char separator)
166166
{
167-
return string_utilities::string_split(str, separator);
167+
string_utilities::string_split(str, result, separator);
168+
return result.size();
168169
}
169170

170-
std::string http_utils::standardize_url(const std::string& url)
171+
void http_utils::standardize_url(const std::string& url, std::string& result)
171172
{
172-
std::string n_url = string_utilities::regex_replace(url, "(\\/)+", "/");
173+
std::string n_url;
174+
string_utilities::regex_replace(url, "(\\/)+", "/", n_url);
173175
if(n_url[n_url.size() - 1] == '/')
174176
{
175-
return n_url.substr(0, n_url.size() -1);
177+
result = n_url.substr(0, n_url.size() -1);
176178
}
177179
else
178180
{
179-
return n_url;
181+
result = n_url;
180182
}
181183
}
182184

183-
std::string get_ip_str(const struct sockaddr *sa, socklen_t maxlen)
185+
void get_ip_str(const struct sockaddr *sa, std::string& result, socklen_t maxlen)
184186
{
185187
char to_ret[INET6_ADDRSTRLEN] = { '\0' };
186188
switch(sa->sa_family)
@@ -198,9 +200,9 @@ std::string get_ip_str(const struct sockaddr *sa, socklen_t maxlen)
198200
break;
199201
default:
200202
strncpy(to_ret, "Unknown AF", 11);
201-
return NULL;
203+
return;
202204
}
203-
return to_ret;
205+
result = to_ret;
204206
}
205207

206208
const struct sockaddr str_to_ip(const std::string& src)
@@ -298,7 +300,7 @@ ip_representation::ip_representation(const std::string& ip)
298300
if(ip.find(':') != std::string::npos) //IPV6
299301
{
300302
ip_version = http_utils::IPV6;
301-
parts = string_utilities::string_split(ip, ':', false);
303+
string_utilities::string_split(ip, parts, ':', false);
302304
int y = 0;
303305
for(unsigned int i = 0; i < parts.size(); i++)
304306
{
@@ -324,7 +326,8 @@ ip_representation::ip_representation(const std::string& ip)
324326
}
325327
if(parts[i].find('.') != std::string::npos)
326328
{
327-
vector<string> subparts = string_utilities::string_split(parts[i], '.');
329+
vector<string> subparts;
330+
string_utilities::string_split(parts[i], subparts, '.');
328331
if(subparts.size() == 4)
329332
{
330333
for(unsigned int ii = 0; ii < subparts.size(); ii++)
@@ -381,7 +384,7 @@ ip_representation::ip_representation(const std::string& ip)
381384
else //IPV4
382385
{
383386
ip_version = http_utils::IPV4;
384-
parts = string_utilities::string_split(ip, '.');
387+
string_utilities::string_split(ip, parts, '.');
385388
if(parts.size() == 4)
386389
{
387390
for(unsigned int i = 0; i < parts.size(); i++)

src/httpserver.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,7 @@ namespace std {
5353
%module(directors="1") libhttpserver_python
5454
#endif
5555

56-
//%feature("director") webserver;
57-
//%feature("director") http_request;
58-
//%feature("director") http_response;
59-
//%feature("director") http_string_response;
60-
//%feature("director") http_file_response;
61-
//%feature("director") http_basic_auth_fail_response;
62-
//%feature("director") http_digest_auth_fail_response;
63-
//%feature("director") shoutCAST_response;
6456
%feature("director") http_resource;
65-
//%feature("director") http_endpoint;
66-
//%feature("director") http_utils;
6757

6858
#ifdef SWIGPYTHON
6959
%typemap(out) string {

src/httpserver/http_endpoint.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class http_endpoint
7373
{
7474
return this->url_complete;
7575
}
76+
void get_url_complete(std::string& result) const
77+
{
78+
result = this->url_complete;
79+
}
80+
size_t get_url_complete_size() const
81+
{
82+
return this->url_complete.size();
83+
}
7684
/**
7785
* Method used to get all pars defined inside an url.
7886
* @return a vector of strings representing all found pars.
@@ -81,6 +89,11 @@ class http_endpoint
8189
{
8290
return this->url_pars;
8391
}
92+
size_t get_url_pars(std::vector<std::string>& result) const
93+
{
94+
result = this->url_pars;
95+
return result.size();
96+
}
8497
/**
8598
* Method used to get all pieces of an url; considering an url splitted by '/'.
8699
* @return a vector of strings representing all found pieces.
@@ -89,6 +102,15 @@ class http_endpoint
89102
{
90103
return this->url_pieces;
91104
}
105+
size_t get_url_pieces(std::vector<std::string>& result) const
106+
{
107+
result = this->url_pieces;
108+
return result.size();
109+
}
110+
size_t get_url_pieces_num() const
111+
{
112+
return this->url_pieces.size();
113+
}
92114
/**
93115
* Method used to get indexes of all parameters inside url
94116
* @return a vector of int indicating all positions.
@@ -97,6 +119,11 @@ class http_endpoint
97119
{
98120
return this->chunk_positions;
99121
}
122+
size_t get_chunk_positions(std::vector<int>& result) const
123+
{
124+
result = this->chunk_positions;
125+
return result.size();
126+
}
100127
private:
101128
/**
102129
* Default constructor of the class.

0 commit comments

Comments
 (0)