Skip to content

Commit 9590198

Browse files
committed
Use proper return (rather then paramer returns)
1 parent 64af4db commit 9590198

File tree

9 files changed

+59
-72
lines changed

9 files changed

+59
-72
lines changed

src/http_endpoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ webserver::http_endpoint::http_endpoint
5959
if(url_complete[0] != '/')
6060
url_complete = "/" + url_complete;
6161

62-
http_utils::tokenize_url(url, parts);
62+
parts = http_utils::tokenize_url(url);
6363
string buffered;
6464
bool first = true;
6565

src/http_request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace httpserver
3131

3232
void http_request::set_method(const std::string& method)
3333
{
34-
string_utilities::to_upper_copy(method, this->method);
34+
this->method = string_utilities::to_upper_copy(method);
3535
}
3636

3737
bool http_request::check_digest_auth(

src/http_utils.cpp

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,21 @@ const std::string http_utils::http_post_encoding_multipart_formdata =
210210
MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
211211

212212

213-
size_t http_utils::tokenize_url(
213+
std::vector<std::string> http_utils::tokenize_url(
214214
const std::string& str,
215-
std::vector<std::string>& result,
216215
const char separator
217216
)
218217
{
219-
string_utilities::string_split(str, result, separator);
220-
return result.size();
218+
return string_utilities::string_split(str, separator);
221219
}
222220

223-
void http_utils::standardize_url(const std::string& url, std::string& result)
221+
std::string http_utils::standardize_url(const std::string& url)
224222
{
225-
std::string n_url;
226-
string_utilities::regex_replace(url, "(\\/)+", "/", n_url);
223+
std::string n_url = string_utilities::regex_replace(url, "(\\/)+", "/");
227224
std::string::size_type n_url_length = n_url.length();
228225

226+
std::string result;
227+
229228
if (n_url_length > 1 && n_url[n_url_length - 1] == '/')
230229
{
231230
result = n_url.substr(0, n_url_length - 1);
@@ -234,14 +233,17 @@ void http_utils::standardize_url(const std::string& url, std::string& result)
234233
{
235234
result = n_url;
236235
}
236+
237+
return result;
237238
}
238239

239-
void get_ip_str(
240+
std::string get_ip_str(
240241
const struct sockaddr *sa,
241-
std::string& result,
242242
socklen_t maxlen
243243
)
244244
{
245+
std::string result;
246+
245247
if(sa)
246248
{
247249
int addrlen = sizeof(sockaddr_in);
@@ -256,16 +258,8 @@ void get_ip_str(
256258
result = to_ret;
257259
}
258260
}
259-
}
260261

261-
std::string get_ip_str_new(
262-
const struct sockaddr* sa,
263-
socklen_t maxlen
264-
)
265-
{
266-
std::string to_ret;
267-
get_ip_str(sa, to_ret, maxlen);
268-
return to_ret;
262+
return result;
269263
}
270264

271265
unsigned short get_port(const struct sockaddr* sa)
@@ -355,7 +349,7 @@ ip_representation::ip_representation(const std::string& ip)
355349
if(ip.find(':') != std::string::npos) //IPV6
356350
{
357351
ip_version = http_utils::IPV6;
358-
string_utilities::string_split(ip, parts, ':', false);
352+
parts = string_utilities::string_split(ip, ':', false);
359353
int y = 0;
360354
for(unsigned int i = 0; i < parts.size(); i++)
361355
{
@@ -385,8 +379,7 @@ ip_representation::ip_representation(const std::string& ip)
385379
}
386380
if(parts[i].find('.') != std::string::npos)
387381
{
388-
vector<string> subparts;
389-
string_utilities::string_split(parts[i], subparts, '.');
382+
vector<string> subparts = string_utilities::string_split(parts[i], '.');
390383
if(subparts.size() == 4)
391384
{
392385
for(unsigned int ii = 0; ii < subparts.size(); ii++)
@@ -447,7 +440,7 @@ ip_representation::ip_representation(const std::string& ip)
447440
else //IPV4
448441
{
449442
ip_version = http_utils::IPV4;
450-
string_utilities::string_split(ip, parts, '.');
443+
parts = string_utilities::string_split(ip, '.');
451444
if(parts.size() == 4)
452445
{
453446
for(unsigned int i = 0; i < parts.size(); i++)
@@ -487,26 +480,22 @@ bool ip_representation::operator <(const ip_representation& b) const
487480
return false;
488481
}
489482

490-
size_t load_file (const char* filename, char** content)
483+
char* load_file (const char *filename)
491484
{
485+
char* content = NULL;
486+
492487
ifstream fp(filename, ios::in | ios::binary | ios::ate);
493488
if(fp.is_open())
494489
{
495490
int size = fp.tellg();
496-
*content = (char*) malloc(size * sizeof(char));
491+
content = (char*) malloc(size * sizeof(char));
497492
fp.seekg(0, ios::beg);
498-
fp.read(*content, size);
493+
fp.read(content, size);
499494
fp.close();
500-
return size;
495+
return content;
501496
}
502497
else
503498
throw file_access_exception();
504-
}
505-
506-
char* load_file (const char *filename)
507-
{
508-
char* content = NULL;
509-
load_file(filename, &content);
510499
return content;
511500
}
512501

src/httpserver/http_request.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,7 @@ class http_request
505505
void set_path(const std::string& path)
506506
{
507507
this->path = path;
508-
std::vector<std::string> complete_path;
509-
http::http_utils::tokenize_url(this->path, complete_path);
508+
std::vector<std::string> complete_path = http::http_utils::tokenize_url(this->path);
510509
for(unsigned int i = 0; i < complete_path.size(); i++)
511510
{
512511
this->post_path.push_back(complete_path[i]);

src/httpserver/http_utils.hpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ class http_utils
240240

241241
static const std::string http_post_encoding_form_urlencoded;
242242
static const std::string http_post_encoding_multipart_formdata;
243-
static size_t tokenize_url(const std::string&,
244-
std::vector<std::string>& result, const char separator = '/'
243+
static std::vector<std::string> tokenize_url(const std::string&,
244+
const char separator = '/'
245245
);
246-
static void standardize_url(const std::string&, std::string& result);
246+
static std::string standardize_url(const std::string&);
247247
};
248248

249249
#define COMPARATOR(x, y, op) \
@@ -331,13 +331,9 @@ struct ip_representation
331331
* @param maxlen Maxlen of the address (automatically discovered if not passed)
332332
* @return string containing the ip address
333333
**/
334-
void get_ip_str(const struct sockaddr *sa,
335-
std::string& result, socklen_t maxlen = 0
336-
);
334+
std::string get_ip_str(const struct sockaddr *sa, socklen_t maxlen = 0);
337335

338-
std::string get_ip_str_new(const struct sockaddr* sa,
339-
socklen_t maxlen = 0
340-
);
336+
std::string get_ip_str_new(const struct sockaddr* sa, socklen_t maxlen = 0);
341337
/**
342338
* Method used to get a port from a sockaddr
343339
* @param sa The sockaddr object to find the port from
@@ -376,8 +372,6 @@ size_t http_unescape (char *val);
376372

377373
char* load_file (const char *filename);
378374

379-
size_t load_file (const char* filename, char** content);
380-
381375
};
382376
};
383377
#endif

src/httpserver/string_utilities.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ namespace string_utilities
3939
* @param str The string to turn uppercase
4040
* @return a string that is the uppercase version of the previous
4141
**/
42-
void to_upper_copy(const std::string& str, std::string& result);
43-
void to_lower_copy(const std::string& str, std::string& result);
44-
size_t string_split(const std::string& s,
45-
std::vector<std::string>& result,
42+
std::string to_upper_copy(const std::string& str);
43+
std::string to_lower_copy(const std::string& str);
44+
std::vector<std::string> string_split(const std::string& s,
4645
char sep = ' ', bool collapse = true
4746
);
48-
void regex_replace(const std::string& str, const std::string& pattern,
49-
const std::string& replace_str, std::string& result
47+
std::string regex_replace(const std::string& str, const std::string& pattern,
48+
const std::string& replace_str
5049
);
5150
void to_upper(std::string& str);
5251
};

src/string_utilities.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ namespace httpserver
3434
namespace string_utilities
3535
{
3636

37-
void to_upper_copy(const std::string& str, std::string& result)
37+
std::string to_upper_copy(const std::string& str)
3838
{
39-
result = str;
39+
std::string result = str;
4040
std::transform(result.begin(),
4141
result.end(),
4242
result.begin(),
4343
(int(*)(int)) std::toupper
4444
);
45+
46+
return result;
4547
}
4648

4749
void to_upper(std::string& str)
@@ -53,41 +55,44 @@ void to_upper(std::string& str)
5355
);
5456
}
5557

56-
void to_lower_copy(const std::string& str, std::string& result)
58+
std::string to_lower_copy(const std::string& str)
5759
{
58-
result = str;
60+
std::string result = str;
5961
std::transform(result.begin(),
6062
result.end(),
6163
result.begin(),
6264
(int(*)(int)) std::tolower
6365
);
66+
67+
return result;
6468
}
6569

66-
size_t string_split(
70+
std::vector<std::string> string_split(
6771
const std::string& s,
68-
std::vector<std::string>& result,
6972
char sep,
7073
bool collapse
7174
)
7275
{
76+
std::vector<std::string> result;
77+
7378
std::istringstream buf(s);
7479
for(std::string token; getline(buf, token, sep); )
7580
{
7681
if((collapse && token != "") || !collapse)
7782
result.push_back(token);
7883
}
79-
return result.size();
84+
return result;
8085
}
8186

82-
void regex_replace(const std::string& str,
87+
std::string regex_replace(const std::string& str,
8388
const std::string& pattern,
84-
const std::string& replace_str,
85-
std::string& result
89+
const std::string& replace_str
8690
)
8791
{
8892
regex_t preg;
8993
regmatch_t substmatch[1];
9094
regcomp(&preg, pattern.c_str(), REG_EXTENDED|REG_ICASE);
95+
std::string result;
9196
if ( regexec(&preg, str.c_str(), 1, substmatch, 0) == 0 )
9297
{
9398
char ns[substmatch[0].rm_so + 1 +
@@ -113,6 +118,8 @@ void regex_replace(const std::string& str,
113118
result = std::string((char*)ns);
114119
}
115120
regfree(&preg);
121+
122+
return result;
116123
}
117124

118125
};

src/webserver.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,7 @@ void webserver::end_request_construction(
775775
connection,
776776
MHD_CONNECTION_INFO_CLIENT_ADDRESS
777777
);
778-
std::string ip_str;
779-
get_ip_str(conninfo->client_addr, ip_str);
778+
std::string ip_str = get_ip_str(conninfo->client_addr);
780779
mr->dhr->set_requestor(ip_str);
781780
mr->dhr->set_requestor_port(get_port(conninfo->client_addr));
782781
if(pass != 0x0)
@@ -989,7 +988,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
989988

990989
mr->standardized_url = new string();
991990
internal_unescaper((void*) static_cast<webserver*>(cls), (char*) url);
992-
http_utils::standardize_url(url, *mr->standardized_url);
991+
mr->standardized_url = new string(http_utils::standardize_url(url));
993992

994993
bool body = false;
995994

test/unit/http_utils_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ LT_END_AUTO_TEST(unescape)
6060

6161
LT_BEGIN_AUTO_TEST(http_utils_suite, standardize_url)
6262
string url = "/", result;
63-
http::http_utils::standardize_url(url, result);
63+
result = http::http_utils::standardize_url(url);
6464
LT_CHECK_EQ(result, "/");
6565

6666
url = "/abc/", result = "";
67-
http::http_utils::standardize_url(url, result);
67+
result = http::http_utils::standardize_url(url);
6868
LT_CHECK_EQ(result, "/abc");
6969

7070
url = "/abc", result = "";
71-
http::http_utils::standardize_url(url, result);
71+
result = http::http_utils::standardize_url(url);
7272
LT_CHECK_EQ(result, "/abc");
7373

7474
url = "/abc/pqr/", result = "";
75-
http::http_utils::standardize_url(url, result);
75+
result = http::http_utils::standardize_url(url);
7676
LT_CHECK_EQ(result, "/abc/pqr");
7777

7878
url = "/abc/pqr", result = "";
79-
http::http_utils::standardize_url(url, result);
79+
result = http::http_utils::standardize_url(url);
8080
LT_CHECK_EQ(result, "/abc/pqr");
8181
LT_END_AUTO_TEST(standardize_url)
8282

@@ -88,7 +88,7 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, ip_to_str)
8888
ip4addr.sin_addr.s_addr = inet_addr("127.0.0.1");
8989

9090
string result = "";
91-
http::get_ip_str((struct sockaddr *) &ip4addr, result);
91+
result = http::get_ip_str((struct sockaddr *) &ip4addr);
9292

9393
LT_CHECK_EQ(result, "127.0.0.1");
9494
LT_END_AUTO_TEST(ip_to_str)

0 commit comments

Comments
 (0)