Skip to content

Commit c742465

Browse files
authored
Fixes get pending when body is provided. (#166)
Body is ignored on get (and get-like) operations.
1 parent 2a74f9e commit c742465

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

src/httpserver/details/modded_request.hpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ struct modded_request
4343
http_request* dhr;
4444
std::shared_ptr<http_response> dhrs;
4545
bool second;
46+
bool has_body;
4647

4748
modded_request():
4849
pp(0x0),
4950
complete_uri(0x0),
5051
standardized_url(0x0),
5152
ws(0x0),
5253
dhr(0x0),
53-
second(false)
54+
second(false),
55+
has_body(false)
5456
{
5557
}
5658

@@ -60,7 +62,8 @@ struct modded_request
6062
standardized_url(b.standardized_url),
6163
ws(b.ws),
6264
dhr(b.dhr),
63-
second(b.second)
65+
second(b.second),
66+
has_body(b.has_body)
6467
{
6568
}
6669

@@ -70,7 +73,8 @@ struct modded_request
7073
standardized_url(std::move(b.standardized_url)),
7174
ws(std::move(b.ws)),
7275
dhr(std::move(b.dhr)),
73-
second(b.second)
76+
second(b.second),
77+
has_body(b.has_body)
7478
{
7579
}
7680

@@ -84,6 +88,7 @@ struct modded_request
8488
this->ws = b.ws;
8589
this->dhr = b.dhr;
8690
this->second = b.second;
91+
this->has_body = b.has_body;
8792

8893
return *this;
8994
}
@@ -98,6 +103,7 @@ struct modded_request
98103
this->ws = std::move(b.ws);
99104
this->dhr = std::move(b.dhr);
100105
this->second = b.second;
106+
this->has_body = b.has_body;
101107

102108
return *this;
103109
}

src/httpserver/webserver.hpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,11 @@ class webserver
222222
void **con_cls, int upgrade_socket
223223
);
224224

225-
int bodyless_requests_answer(MHD_Connection* connection,
226-
const char* method, const char* version,
227-
struct details::modded_request* mr
228-
);
229-
230-
int bodyfull_requests_answer_first_step(MHD_Connection* connection,
225+
int requests_answer_first_step(MHD_Connection* connection,
231226
struct details::modded_request* mr
232227
);
233228

234-
int bodyfull_requests_answer_second_step(MHD_Connection* connection,
229+
int requests_answer_second_step(MHD_Connection* connection,
235230
const char* method, const char* version, const char* upload_data,
236231
size_t* upload_data_size, struct details::modded_request* mr
237232
);

src/webserver.cpp

+23-22
Original file line numberDiff line numberDiff line change
@@ -511,23 +511,19 @@ const std::shared_ptr<http_response> webserver::internal_error_page(details::mod
511511
}
512512
}
513513

514-
int webserver::bodyless_requests_answer(
515-
MHD_Connection* connection, const char* method,
516-
const char* version, struct details::modded_request* mr
517-
)
518-
{
519-
http_request req(connection, unescaper);
520-
mr->dhr = &(req);
521-
return complete_request(connection, mr, version, method);
522-
}
523-
524-
int webserver::bodyfull_requests_answer_first_step(
514+
int webserver::requests_answer_first_step(
525515
MHD_Connection* connection,
526516
struct details::modded_request* mr
527517
)
528518
{
529519
mr->second = true;
530520
mr->dhr = new http_request(connection, unescaper);
521+
522+
if (!mr->has_body)
523+
{
524+
return MHD_YES;
525+
}
526+
531527
mr->dhr->set_content_size_limit(content_size_limit);
532528
const char *encoding = MHD_lookup_connection_value (
533529
connection,
@@ -567,20 +563,25 @@ int webserver::bodyfull_requests_answer_first_step(
567563
return MHD_YES;
568564
}
569565

570-
int webserver::bodyfull_requests_answer_second_step(
566+
int webserver::requests_answer_second_step(
571567
MHD_Connection* connection, const char* method,
572568
const char* version, const char* upload_data,
573569
size_t* upload_data_size, struct details::modded_request* mr
574570
)
575571
{
576572
if (0 == *upload_data_size) return complete_request(connection, mr, version, method);
577573

574+
if (mr->has_body)
575+
{
576+
578577
#ifdef DEBUG
579-
cout << "Writing content: " << upload_data << endl;
578+
cout << "Writing content: " << upload_data << endl;
580579
#endif //DEBUG
581-
mr->dhr->grow_content(upload_data, *upload_data_size);
580+
mr->dhr->grow_content(upload_data, *upload_data_size);
581+
582+
if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
583+
}
582584

583-
if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
584585
*upload_data_size = 0;
585586
return MHD_YES;
586587
}
@@ -750,7 +751,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
750751
if(mr->second != false)
751752
{
752753
return static_cast<webserver*>(cls)->
753-
bodyfull_requests_answer_second_step(
754+
requests_answer_second_step(
754755
connection,
755756
method,
756757
version,
@@ -776,7 +777,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
776777
base_unescaper(t_url, static_cast<webserver*>(cls)->unescaper);
777778
mr->standardized_url = new string(http_utils::standardize_url(t_url));
778779

779-
bool body = false;
780+
mr->has_body = false;
780781

781782
access_log(
782783
static_cast<webserver*>(cls),
@@ -790,22 +791,22 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
790791
else if (0 == strcmp(method, http_utils::http_method_post.c_str()))
791792
{
792793
mr->callback = &http_resource::render_POST;
793-
body = true;
794+
mr->has_body = true;
794795
}
795796
else if (0 == strcasecmp(method, http_utils::http_method_put.c_str()))
796797
{
797798
mr->callback = &http_resource::render_PUT;
798-
body = true;
799+
mr->has_body = true;
799800
}
800801
else if (0 == strcasecmp(method,http_utils::http_method_delete.c_str()))
801802
{
802803
mr->callback = &http_resource::render_DELETE;
803-
body = true;
804+
mr->has_body = true;
804805
}
805806
else if (0 == strcasecmp(method, http_utils::http_method_patch.c_str()))
806807
{
807808
mr->callback = &http_resource::render_PATCH;
808-
body = true;
809+
mr->has_body = true;
809810
}
810811
else if (0 == strcasecmp(method, http_utils::http_method_head.c_str()))
811812
{
@@ -824,7 +825,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
824825
mr->callback = &http_resource::render_OPTIONS;
825826
}
826827

827-
return body ? static_cast<webserver*>(cls)->bodyfull_requests_answer_first_step(connection, mr) : static_cast<webserver*>(cls)->bodyless_requests_answer(connection, method, version, mr);
828+
return static_cast<webserver*>(cls)->requests_answer_first_step(connection, mr);
828829
}
829830

830831
};

0 commit comments

Comments
 (0)