X Tutup
Skip to content

Commit 0e6bd48

Browse files
author
Sebastiano Merlino
committed
Avoid server to crash on empty resources.
This prevents the server to crash in case of a resource not building any response.
1 parent 10a4ddc commit 0e6bd48

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/webserver.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ MHD_socket create_socket (int domain, int type, int protocol)
357357
{
358358
int sock_cloexec = SOCK_CLOEXEC;
359359
int ctype = SOCK_STREAM | sock_cloexec;
360-
360+
361361
/* use SOCK_STREAM rather than ai_socktype: some getaddrinfo
362362
* implementations do not set ai_socktype, e.g. RHL6.2. */
363363
MHD_socket fd = socket(domain, ctype, protocol);
364-
364+
365365
#ifdef _WINDOWS
366366
if (fd == INVALID_SOCKET)
367367
#else
@@ -521,7 +521,7 @@ bool webserver::start(bool blocking)
521521
}
522522
#ifdef _WINDOWS
523523
unsigned long ioarg = 1;
524-
ioctlsocket(bind_socket, FIONBIO, &ioarg);
524+
ioctlsocket(bind_socket, FIONBIO, &ioarg);
525525
#else
526526
int flags = fcntl (bind_socket, F_GETFL);
527527
flags |= O_NONBLOCK;
@@ -1132,7 +1132,10 @@ int webserver::finalize_answer(
11321132
try
11331133
{
11341134
if(hrm->is_allowed(method))
1135+
{
11351136
((hrm)->*(mr->callback))(*mr->dhr, &dhrs);
1137+
if (dhrs == 0x0) internal_error_page(&dhrs, mr);
1138+
}
11361139
else
11371140
{
11381141
method_not_allowed_page(&dhrs, mr);

test/integ/basic.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ class nok_resource : public http_resource<nok_resource>
132132
}
133133
};
134134

135+
class no_response_resource : public http_resource<no_response_resource>
136+
{
137+
public:
138+
void render_GET(const http_request& req, http_response** res)
139+
{
140+
}
141+
};
142+
135143
LT_BEGIN_SUITE(basic_suite)
136144

137145
webserver* ws;
@@ -373,6 +381,25 @@ LT_BEGIN_AUTO_TEST(basic_suite, empty_arg)
373381
curl_easy_cleanup(curl);
374382
LT_END_AUTO_TEST(empty_arg)
375383

384+
LT_BEGIN_AUTO_TEST(basic_suite, no_response)
385+
no_response_resource* resource = new no_response_resource();
386+
ws->register_resource("base", resource);
387+
curl_global_init(CURL_GLOBAL_ALL);
388+
std::string s;
389+
CURL* curl;
390+
CURLcode res;
391+
392+
curl = curl_easy_init();
393+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
394+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
395+
res = curl_easy_perform(curl);
396+
LT_ASSERT_EQ(res, 0);
397+
long http_code = 0;
398+
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
399+
LT_ASSERT_EQ(http_code, 500);
400+
curl_easy_cleanup(curl);
401+
LT_END_AUTO_TEST(no_response)
402+
376403
LT_BEGIN_AUTO_TEST_ENV()
377404
AUTORUN_TESTS()
378405
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)
X Tutup