X Tutup
Skip to content

Commit 888350d

Browse files
author
Sebastiano Merlino
committed
Cleaned some code in order to avoid unuseful malloc and new.
1 parent 874dcd5 commit 888350d

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

src/HttpUtils.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,30 +184,25 @@ std::string HttpUtils::standardizeUrl(const std::string& url)
184184

185185
std::string get_ip_str(const struct sockaddr *sa, socklen_t maxlen)
186186
{
187-
char* to_ret;
187+
char to_ret[INET6_ADDRSTRLEN] = { '\0' };
188188
switch(sa->sa_family)
189189
{
190190
case AF_INET:
191191
if(maxlen == 0)
192192
maxlen = INET_ADDRSTRLEN;
193-
to_ret = (char*)malloc(maxlen*sizeof(char));
194193
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), to_ret, maxlen);
195194
break;
196195

197196
case AF_INET6:
198197
if(maxlen == 0)
199198
maxlen = INET6_ADDRSTRLEN;
200-
to_ret = (char*)malloc(maxlen*sizeof(char));
201199
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), to_ret, maxlen);
202200
break;
203201
default:
204-
to_ret = (char*)malloc(11*sizeof(char));
205202
strncpy(to_ret, "Unknown AF", 11);
206203
return NULL;
207204
}
208-
std::string res(to_ret);
209-
free(to_ret);
210-
return std::string(res);
205+
return to_ret;
211206
}
212207

213208
const struct sockaddr str_to_ip(const std::string& src)

src/Webserver.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
623623
string st_url = HttpUtils::standardizeUrl(url);
624624

625625
mr = (struct ModdedRequest*) *con_cls;
626-
access_log(dws, *(mr->completeUri) + " METHOD: " + method);
626+
if(mr->second == false)
627+
access_log(dws, *(mr->completeUri) + " METHOD: " + method);
627628
mr->ws = dws;
628629
if (0 == strcmp (method, MHD_HTTP_METHOD_POST) || 0 == strcmp(method, MHD_HTTP_METHOD_PUT))
629630
{
@@ -712,9 +713,6 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
712713
}
713714
HttpEndpoint endpoint = HttpEndpoint(st_url);
714715
HttpResponse dhrs;
715-
void* page;
716-
size_t size = 0;
717-
bool to_free = false;
718716
const HttpEndpoint* matchingEndpoint = 0x0;
719717
if(!(dws->registeredResources.count(endpoint) > 0))
720718
{
@@ -789,23 +787,22 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
789787
}
790788
else if(dhrs.responseType == HttpResponse::SWITCH_PROTOCOL)
791789
{
792-
response = MHD_create_response_for_upgrade(&upgrade_handler, (void*)dhrs.getSwitchCallback());
790+
// response = MHD_create_response_for_upgrade(&upgrade_handler, (void*)dhrs.getSwitchCallback());
793791
}
794792
else
795793
{
796794
if(dhrs.content != "")
797795
{
798796
vector<char> v_page(dhrs.content.begin(), dhrs.content.end());
799-
size = v_page.size();
800-
page = (void*) malloc(size*sizeof(char));
801-
memcpy( page, &v_page[0], sizeof( char ) * size );
802-
to_free = true;
797+
size_t size = v_page.size();
798+
char page[size];
799+
memcpy( (char*) page, &v_page[0], sizeof( char ) * size );
800+
response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_COPY);
803801
}
804802
else
805803
{
806-
page = (void*)"";
804+
response = MHD_create_response_from_buffer(0, (void*)"", MHD_RESPMEM_MUST_COPY);
807805
}
808-
response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_COPY);
809806
}
810807
vector<pair<string,string> > response_headers = dhrs.getHeaders();
811808
vector<pair<string,string> > response_footers = dhrs.getFooters();
@@ -829,8 +826,6 @@ int Webserver::answerToConnection(void* cls, MHD_Connection* connection,
829826
if (digestedUser != 0x0)
830827
free (digestedUser);
831828
MHD_destroy_response (response);
832-
if(to_free)
833-
free(page);
834829
return to_ret;
835830
}
836831

src/string_utilities.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,12 @@ std::string regex_replace(std::string str, std::string pattern, std::string repl
7373
regcomp(&preg, pattern.c_str(), REG_EXTENDED|REG_ICASE);
7474
if ( regexec(&preg, str.c_str(), 1, substmatch, 0) == 0 )
7575
{
76-
//fprintf(stderr, "%d, %d\n", substmatch[0].rm_so, substmatch[0].rm_eo);
77-
char *ns = (char*)malloc(substmatch[0].rm_so + 1 + replace_str.size() + (str.size() - substmatch[0].rm_eo) + 2);
76+
char ns[substmatch[0].rm_so + 1 + replace_str.size() + (str.size() - substmatch[0].rm_eo) + 2];
7877
memcpy(ns, str.c_str(), substmatch[0].rm_so+1);
7978
memcpy(&ns[substmatch[0].rm_so], replace_str.c_str(), replace_str.size());
8079
memcpy(&ns[substmatch[0].rm_so+replace_str.size()], &str[substmatch[0].rm_eo], strlen(&str[substmatch[0].rm_eo]));
8180
ns[ substmatch[0].rm_so + replace_str.size() + strlen(&str[substmatch[0].rm_eo]) ] = 0;
82-
to_ret = std::string(ns);
83-
free(ns);
81+
to_ret = std::string((char*)ns);
8482
}
8583
regfree(&preg);
8684
return to_ret;

0 commit comments

Comments
 (0)
X Tutup