Skip to content

Fixes get pending when body is provided. #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/httpserver/details/modded_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ struct modded_request
http_request* dhr;
std::shared_ptr<http_response> dhrs;
bool second;
bool has_body;

modded_request():
pp(0x0),
complete_uri(0x0),
standardized_url(0x0),
ws(0x0),
dhr(0x0),
second(false)
second(false),
has_body(false)
{
}

Expand All @@ -60,7 +62,8 @@ struct modded_request
standardized_url(b.standardized_url),
ws(b.ws),
dhr(b.dhr),
second(b.second)
second(b.second),
has_body(b.has_body)
{
}

Expand All @@ -70,7 +73,8 @@ struct modded_request
standardized_url(std::move(b.standardized_url)),
ws(std::move(b.ws)),
dhr(std::move(b.dhr)),
second(b.second)
second(b.second),
has_body(b.has_body)
{
}

Expand All @@ -84,6 +88,7 @@ struct modded_request
this->ws = b.ws;
this->dhr = b.dhr;
this->second = b.second;
this->has_body = b.has_body;

return *this;
}
Expand All @@ -98,6 +103,7 @@ struct modded_request
this->ws = std::move(b.ws);
this->dhr = std::move(b.dhr);
this->second = b.second;
this->has_body = b.has_body;

return *this;
}
Expand Down
9 changes: 2 additions & 7 deletions src/httpserver/webserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,11 @@ class webserver
void **con_cls, int upgrade_socket
);

int bodyless_requests_answer(MHD_Connection* connection,
const char* method, const char* version,
struct details::modded_request* mr
);

int bodyfull_requests_answer_first_step(MHD_Connection* connection,
int requests_answer_first_step(MHD_Connection* connection,
struct details::modded_request* mr
);

int bodyfull_requests_answer_second_step(MHD_Connection* connection,
int requests_answer_second_step(MHD_Connection* connection,
const char* method, const char* version, const char* upload_data,
size_t* upload_data_size, struct details::modded_request* mr
);
Expand Down
45 changes: 23 additions & 22 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,23 +511,19 @@ const std::shared_ptr<http_response> webserver::internal_error_page(details::mod
}
}

int webserver::bodyless_requests_answer(
MHD_Connection* connection, const char* method,
const char* version, struct details::modded_request* mr
)
{
http_request req(connection, unescaper);
mr->dhr = &(req);
return complete_request(connection, mr, version, method);
}

int webserver::bodyfull_requests_answer_first_step(
int webserver::requests_answer_first_step(
MHD_Connection* connection,
struct details::modded_request* mr
)
{
mr->second = true;
mr->dhr = new http_request(connection, unescaper);

if (!mr->has_body)
{
return MHD_YES;
}

mr->dhr->set_content_size_limit(content_size_limit);
const char *encoding = MHD_lookup_connection_value (
connection,
Expand Down Expand Up @@ -567,20 +563,25 @@ int webserver::bodyfull_requests_answer_first_step(
return MHD_YES;
}

int webserver::bodyfull_requests_answer_second_step(
int webserver::requests_answer_second_step(
MHD_Connection* connection, const char* method,
const char* version, const char* upload_data,
size_t* upload_data_size, struct details::modded_request* mr
)
{
if (0 == *upload_data_size) return complete_request(connection, mr, version, method);

if (mr->has_body)
{

#ifdef DEBUG
cout << "Writing content: " << upload_data << endl;
cout << "Writing content: " << upload_data << endl;
#endif //DEBUG
mr->dhr->grow_content(upload_data, *upload_data_size);
mr->dhr->grow_content(upload_data, *upload_data_size);

if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
}

if (mr->pp != NULL) MHD_post_process(mr->pp, upload_data, *upload_data_size);
*upload_data_size = 0;
return MHD_YES;
}
Expand Down Expand Up @@ -750,7 +751,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
if(mr->second != false)
{
return static_cast<webserver*>(cls)->
bodyfull_requests_answer_second_step(
requests_answer_second_step(
connection,
method,
version,
Expand All @@ -776,7 +777,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
base_unescaper(t_url, static_cast<webserver*>(cls)->unescaper);
mr->standardized_url = new string(http_utils::standardize_url(t_url));

bool body = false;
mr->has_body = false;

access_log(
static_cast<webserver*>(cls),
Expand All @@ -790,22 +791,22 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
else if (0 == strcmp(method, http_utils::http_method_post.c_str()))
{
mr->callback = &http_resource::render_POST;
body = true;
mr->has_body = true;
}
else if (0 == strcasecmp(method, http_utils::http_method_put.c_str()))
{
mr->callback = &http_resource::render_PUT;
body = true;
mr->has_body = true;
}
else if (0 == strcasecmp(method,http_utils::http_method_delete.c_str()))
{
mr->callback = &http_resource::render_DELETE;
body = true;
mr->has_body = true;
}
else if (0 == strcasecmp(method, http_utils::http_method_patch.c_str()))
{
mr->callback = &http_resource::render_PATCH;
body = true;
mr->has_body = true;
}
else if (0 == strcasecmp(method, http_utils::http_method_head.c_str()))
{
Expand All @@ -824,7 +825,7 @@ int webserver::answer_to_connection(void* cls, MHD_Connection* connection,
mr->callback = &http_resource::render_OPTIONS;
}

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

};