Skip to content

Commit 238f96f

Browse files
author
Sebastiano Merlino
committed
Modified in order to standardize doxygen generation.
Altered file response management in order to avoid huge memory consumption that was due to the usage of libmicrohttpd native file readers
1 parent 3850f99 commit 238f96f

File tree

6 files changed

+51
-83
lines changed

6 files changed

+51
-83
lines changed

Makefile.am

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ ACLOCAL_AMFLAGS = -I m4
2626

2727
SUBDIRS = src $(PYTHON_DIR) $(JAVA_DIR) $(PHP_DIR) $(LUA_DIR) $(PERL_DIR) $(RUBY_DIR) $(GUILE_DIR)
2828
DIST_SUBDIRS = src $(PYTHON_DIR) $(JAVA_DIR) $(PHP_DIR) $(LUA_DIR) $(PERL_DIR) $(RUBY_DIR) $(GUILE_DIR)
29-
EXTRA_DIST = libhttpserver.pc.in $(DOXYFILE) $(PYTHON_EXTRA) $(PHP_EXTRA) $(PERL_EXTRA)
29+
EXTRA_DIST = libhttpserver.pc.in $(DX_CONFIG) $(PYTHON_EXTRA) $(PHP_EXTRA) $(PERL_EXTRA)
30+
31+
MOSTLYCLEANFILES = $(DX_CLEANFILES)
3032

3133
pkgconfigdir = $(libdir)/pkgconfig
3234
pkgconfig_DATA = libhttpserver.pc
@@ -63,7 +65,6 @@ GUILE_DIR = src/guile
6365
endif
6466

6567
include $(top_srcdir)/aminclude.am
66-
DOXYFILE = doxyconfig
6768

6869
# Update libtool, if needed.
6970
libtool: $(LIBTOOL_DEPS)

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ DX_RTF_FEATURE(OFF)
177177
DX_XML_FEATURE(OFF)
178178
DX_PDF_FEATURE(OFF)
179179
DX_PS_FEATURE(OFF)
180-
DX_INIT_DOXYGEN([$PACKAGE_NAME],[doxyconfig])
180+
DX_INIT_DOXYGEN([$PACKAGE_NAME],[doxyconfig.in])
181181

182182
if test x$swig = xtrue; then
183183
AC_CHECK_PROG([SWIG], [swig], ["yes"], ["no"])
File renamed without changes.

src/http_response.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,6 @@ const std::vector<std::pair<std::string, std::string> > http_response::get_foote
5454
return to_ret;
5555
}
5656
//RESPONSE
57-
http_file_response::http_file_response
58-
(
59-
const string& filename,
60-
int response_code,
61-
const std::string& content_type
62-
)
63-
{
64-
FILE* f;
65-
this->filename = filename;
66-
if(!(f = fopen(filename.c_str(), "r")))
67-
{
68-
this->response_type = http_response::STRING_CONTENT;
69-
this->content = NOT_FOUND_ERROR;
70-
this->response_code = http_utils::http_not_found;
71-
this->set_header(http_utils::http_header_content_type, content_type);
72-
this->fp = -1;
73-
}
74-
else
75-
{
76-
this->response_type = http_response::FILE_CONTENT;
77-
this->response_code = response_code;
78-
this->fp = fileno(f);
79-
}
80-
}
81-
8257
shoutCAST_response::shoutCAST_response
8358
(
8459
const std::string& content,

src/httpserver/http_response.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,12 @@ class http_file_response : public http_response
261261
public:
262262
http_file_response
263263
(
264-
const std::string& filename,
264+
const std::string& filename,
265265
int response_code,
266266
const std::string& content_type = "text/plain"
267-
);
267+
) : http_response(http_response::FILE_CONTENT, filename, response_code, content_type)
268+
{
269+
}
268270

269271
http_file_response(const http_file_response& b) : http_response(b) { }
270272
};

src/webserver.cpp

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdint.h>
2121
#include <inttypes.h>
2222
#include <iostream>
23+
#include <fstream>
2324
#include <stdlib.h>
2425
#include <stdio.h>
2526
#include <errno.h>
@@ -77,55 +78,26 @@ static void ignore_sigpipe ()
7778
fprintf (stderr, gettext("Failed to install SIGPIPE handler: %s\n"), strerror (errno));
7879
}
7980

80-
static long get_file_size (const char *filename)
81+
static size_t load_file (const char* filename, char** content)
8182
{
82-
FILE *fp;
83-
84-
fp = fopen (filename, "rb");
85-
if (fp)
86-
{
87-
long size;
88-
89-
if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
90-
size = 0;
91-
92-
fclose (fp);
93-
83+
ifstream fp(filename, ios::in | ios::binary | ios::ate);
84+
if(fp.is_open())
85+
{
86+
int size = fp.tellg();
87+
*content = (char*) malloc(size * sizeof(char));
88+
fp.seekg(0, ios::beg);
89+
fp.read(*content, size);
90+
fp.close();
9491
return size;
95-
}
96-
else
92+
}
9793
return 0;
9894
}
9995

100-
static char * load_file (const char *filename)
96+
static char* load_file (const char *filename)
10197
{
102-
FILE *fp;
103-
char *buffer;
104-
long size;
105-
106-
size = get_file_size (filename);
107-
if (size == 0)
108-
return NULL;
109-
110-
fp = fopen (filename, "rb");
111-
if (!fp)
112-
return NULL;
113-
114-
buffer = (char*) malloc (size);
115-
if (!buffer)
116-
{
117-
fclose (fp);
118-
return NULL;
119-
}
120-
121-
if (size != (int) fread (buffer, 1, size, fp))
122-
{
123-
free (buffer);
124-
buffer = NULL;
125-
}
126-
127-
fclose (fp);
128-
return buffer;
98+
char* content = NULL;
99+
load_file(filename, &content);
100+
return content;
129101
}
130102

131103
//LOGGING DELEGATE
@@ -154,19 +126,25 @@ void unescaper::unescape(char* s) const {}
154126
//WEBSERVER CREATOR
155127
create_webserver& create_webserver::https_mem_key(const std::string& https_mem_key)
156128
{
157-
_https_mem_key = load_file(https_mem_key.c_str());
129+
char* _https_mem_key_pt = load_file(https_mem_key.c_str());
130+
_https_mem_key = _https_mem_key_pt;
131+
free(_https_mem_key_pt);
158132
return *this;
159133
}
160134

161135
create_webserver& create_webserver::https_mem_cert(const std::string& https_mem_cert)
162136
{
163-
_https_mem_cert = load_file(https_mem_cert.c_str());
137+
char* _https_mem_cert_pt = load_file(https_mem_cert.c_str());
138+
_https_mem_cert = _https_mem_cert_pt;
139+
free(_https_mem_cert_pt);
164140
return *this;
165141
}
166142

167143
create_webserver& create_webserver::https_mem_trust(const std::string& https_mem_trust)
168144
{
169-
_https_mem_trust = load_file(https_mem_trust.c_str());
145+
char* _https_mem_trust_pt = load_file(https_mem_trust.c_str());
146+
_https_mem_trust = _https_mem_trust_pt;
147+
free(_https_mem_trust_pt);
170148
return *this;
171149
}
172150

@@ -787,10 +765,20 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
787765
#endif
788766
if(dhrs.response_type == http_response::FILE_CONTENT)
789767
{
790-
struct stat st;
791-
fstat(dhrs.fp, &st);
792-
size_t filesize = st.st_size;
793-
response = MHD_create_response_from_fd(filesize, dhrs.fp);
768+
char* page = NULL;
769+
size_t size = load_file(dhrs.filename.c_str(), &page);
770+
if(size)
771+
response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_FREE);
772+
else
773+
{
774+
if (user != 0x0)
775+
free (user);
776+
if (pass != 0x0)
777+
free (pass);
778+
if (digested_user != 0x0)
779+
free (digested_user);
780+
return not_found_page(cls, connection);
781+
}
794782
}
795783
else if(dhrs.response_type == http_response::SWITCH_PROTOCOL)
796784
{
@@ -800,11 +788,13 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
800788
{
801789
if(dhrs.content != "")
802790
{
791+
//this process is necessary to avoid to truncate byte strings to '\0'
803792
vector<char> v_page(dhrs.content.begin(), dhrs.content.end());
804793
size_t size = v_page.size();
805-
char page[size];
806-
memcpy( (char*) page, &v_page[0], sizeof( char ) * size );
807-
response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_COPY);
794+
char* page = (char*) malloc(sizeof(char)*size);
795+
memcpy( page, &v_page[0], sizeof( char ) * size );
796+
//end string conversion process
797+
response = MHD_create_response_from_buffer(size, page, MHD_RESPMEM_MUST_FREE);
808798
}
809799
else
810800
{

0 commit comments

Comments
 (0)