X Tutup
Skip to content

Commit fbc8bd0

Browse files
author
Sebastiano Merlino
committed
Add logic to avoid error on empty args
Created also test to enforce the logic
1 parent 7763937 commit fbc8bd0

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/webserver.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,10 +749,11 @@ int webserver::build_request_args (
749749
void *cls,
750750
enum MHD_ValueKind kind,
751751
const char *key,
752-
const char *value
752+
const char *arg_value
753753
)
754754
{
755755
details::modded_request* mr = static_cast<details::modded_request*>(cls);
756+
char* value = (char*) ((arg_value == NULL) ? "" : arg_value);
756757
{
757758
char buf[strlen(key) + strlen(value) + 3];
758759
if(mr->dhr->querystring == "")
@@ -766,7 +767,7 @@ int webserver::build_request_args (
766767
mr->dhr->querystring += string(buf);
767768
}
768769
}
769-
int size = internal_unescaper((void*) mr->ws, (char*) value);
770+
int size = internal_unescaper((void*) mr->ws, value);
770771
mr->dhr->set_arg(key, string(value, size));
771772
return MHD_YES;
772773
}
@@ -820,6 +821,8 @@ size_t unescaper_func(void * cls, struct MHD_Connection *c, char *s)
820821

821822
size_t internal_unescaper(void* cls, char* s)
822823
{
824+
if(strlen(s) == 0) return 0;
825+
823826
webserver* dws = static_cast<webserver*>(cls);
824827
if(dws->unescaper != 0x0)
825828
{

test/basic.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ size_t headerfunc(void *ptr, size_t size, size_t nmemb, map<string, string>* ss)
2121
string s_ptr((char*)ptr, size*nmemb);
2222
size_t pos = s_ptr.find(":");
2323
if(pos != string::npos)
24-
(*ss)[s_ptr.substr(0, pos)] =
24+
(*ss)[s_ptr.substr(0, pos)] =
2525
s_ptr.substr(pos + 2, s_ptr.size() - pos - 4);
2626
return size*nmemb;
2727
}
@@ -276,6 +276,19 @@ LT_BEGIN_AUTO_TEST(basic_suite, postprocessor)
276276
curl_easy_cleanup(curl);
277277
LT_END_AUTO_TEST(postprocessor)
278278

279+
LT_BEGIN_AUTO_TEST(basic_suite, empty_arg)
280+
simple_resource* resource = new simple_resource();
281+
ws->register_resource("base", resource);
282+
curl_global_init(CURL_GLOBAL_ALL);
283+
CURL *curl = curl_easy_init();
284+
CURLcode res;
285+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
286+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "arg1");
287+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
288+
res = curl_easy_perform(curl);
289+
LT_ASSERT_EQ(res, 0);
290+
curl_easy_cleanup(curl);
291+
LT_END_AUTO_TEST(empty_arg)
279292

280293
LT_BEGIN_AUTO_TEST_ENV()
281294
AUTORUN_TESTS()

0 commit comments

Comments
 (0)
X Tutup