Skip to content

Commit f9b7691

Browse files
author
Sebastiano Merlino
committed
Use MHD_create_response_from_fd for files
Now the library makes use of MHD_create_response_from_fd for files. This improves the performances because allow to use sendfile on the socket.
1 parent c9db9b3 commit f9b7691

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

src/http_response.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
1515
You should have received a copy of the GNU Lesser General Public
1616
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
1818
USA
1919
*/
2020

2121
#include <cstdio>
2222
#include <functional>
2323
#include <iostream>
2424
#include <sstream>
25+
#include <fcntl.h>
26+
#include <unistd.h>
2527
#include "http_utils.hpp"
2628
#include "webserver.hpp"
2729
#include "http_response.hpp"
@@ -155,20 +157,20 @@ void http_response::get_raw_response_file(
155157
webserver* ws
156158
)
157159
{
158-
char* page = NULL;
159-
size_t size = http::load_file(filename.c_str(), &page);
160+
int fd = open(filename.c_str(), O_RDONLY);
161+
size_t size = lseek(fd, 0, SEEK_END);
160162
if(size)
161-
*response = MHD_create_response_from_buffer(
162-
size,
163-
page,
164-
MHD_RESPMEM_MUST_FREE
165-
);
163+
{
164+
*response = MHD_create_response_from_fd(size, fd);
165+
}
166166
else
167+
{
167168
*response = MHD_create_response_from_buffer(
168-
size,
169+
0,
169170
(void*) "",
170171
MHD_RESPMEM_PERSISTENT
171172
);
173+
}
172174
}
173175

174176
void http_response::get_raw_response_cache(
@@ -184,7 +186,7 @@ void http_response::get_raw_response_cache(
184186
webserver::get_response(ce, &r);
185187
r->get_raw_response(response, ws);
186188
r->decorate_response(*response); //It is done here to avoid to search two times for the same element
187-
189+
188190
//TODO: Check if element is not in cache and throw exception
189191
}
190192

@@ -253,7 +255,7 @@ ssize_t long_polling_receive_response::data_generator(
253255
size_t max
254256
)
255257
{
256-
long_polling_receive_response* _this =
258+
long_polling_receive_response* _this =
257259
static_cast<long_polling_receive_response*>(cls);
258260

259261
if(_this->ws->pop_signaled(_this->connection_id))

0 commit comments

Comments
 (0)