Skip to content

Commit 5568e5a

Browse files
committed
Fixed load_file method (considers EOF)
1 parent 754fdc6 commit 5568e5a

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ AC_SUBST(LDFLAGS)
308308
AC_SUBST(EXT_LIB_PATH)
309309
AC_SUBST(EXT_LIBS)
310310

311-
AC_CONFIG_LINKS([test/run_tests:test/run_tests])
311+
AC_CONFIG_LINKS([test/test_content:test/test_content])
312312

313313
AC_OUTPUT(
314314
libhttpserver.pc

src/http_utils.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,21 +524,24 @@ bool ip_representation::operator <(const ip_representation& b) const
524524

525525
char* load_file (const char *filename)
526526
{
527-
char* content = NULL;
528-
529527
ifstream fp(filename, ios::in | ios::binary | ios::ate);
530528
if(fp.is_open())
531529
{
530+
fp.seekg(0, fp.end);
532531
int size = fp.tellg();
533-
content = (char*) malloc(size * sizeof(char));
534-
fp.seekg(0, ios::beg);
532+
fp.seekg(0, fp.beg);
533+
534+
char* content = new char[size];
535535
fp.read(content, size);
536536
fp.close();
537+
538+
content[size - 1] = 0;
537539
return content;
538540
}
539541
else
542+
{
540543
throw std::invalid_argument("Unable to open file");
541-
return content;
544+
}
542545
}
543546

544547
void dump_header_map(std::ostream &os, const std::string &prefix,

src/httpserver/create_webserver.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,21 @@ class create_webserver
201201
{
202202
char* _https_mem_key_pt = http::load_file(https_mem_key.c_str());
203203
_https_mem_key = _https_mem_key_pt;
204-
free(_https_mem_key_pt);
204+
delete _https_mem_key_pt;
205205
return *this;
206206
}
207207
create_webserver& https_mem_cert(const std::string& https_mem_cert)
208208
{
209209
char* _https_mem_cert_pt = http::load_file(https_mem_cert.c_str());
210210
_https_mem_cert = _https_mem_cert_pt;
211-
free(_https_mem_cert_pt);
211+
delete _https_mem_cert_pt;
212212
return *this;
213213
}
214214
create_webserver& https_mem_trust(const std::string& https_mem_trust)
215215
{
216216
char* _https_mem_trust_pt = http::load_file(https_mem_trust.c_str());
217217
_https_mem_trust = _https_mem_trust_pt;
218-
free(_https_mem_trust_pt);
218+
delete _https_mem_trust_pt;
219219
return *this;
220220
}
221221
create_webserver& raw_https_mem_key(const std::string& https_mem_key)

test/run_tests

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/test_content

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test content of file

test/unit/http_utils_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,14 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation6_sockaddr)
468468
LT_CHECK_EQ(test_ip.mask, 0xFFFF);
469469
LT_END_AUTO_TEST(ip_representation6_sockaddr)
470470

471+
LT_BEGIN_AUTO_TEST(http_utils_suite, load_file)
472+
LT_CHECK_EQ(std::string(http::load_file("test_content")), "test content of file");
473+
LT_END_AUTO_TEST(load_file)
474+
475+
LT_BEGIN_AUTO_TEST(http_utils_suite, load_file_invalid)
476+
LT_CHECK_THROW(http::load_file("test_content_invalid"));
477+
LT_END_AUTO_TEST(load_file_invalid)
478+
471479
LT_BEGIN_AUTO_TEST_ENV()
472480
AUTORUN_TESTS()
473481
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)