Fix crash in extractBodyContent when using large headers (#10636)

If you send a request with a body and a header greater than 255 characters long there will be an exception. The session->fetch will not return the body in the synchronous way expected.
This commit is contained in:
eoghanbdoyle 2022-07-23 05:27:11 +01:00 committed by GitHub
parent 2e6cdb5196
commit 153cfeb7ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 17 deletions

View File

@ -75,8 +75,6 @@ protected:
// Override these for customization //
//////////////////////////////////////
virtual std::string extractBodyContent(const std::shared_ptr<restbed::Session>& session);
{{#hasPathParams}}
{{#pathParams}}
{{#isPrimitiveType}}

View File

@ -128,7 +128,13 @@ void {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource::handler_
{
const auto request = session->get_request();
{{#hasBodyParam}}
std::string bodyContent = extractBodyContent(session);
int content_length = request->get_header("Content-Length", 0);
session->fetch(content_length,
[this](const std::shared_ptr<restbed::Session> session,
const restbed::Bytes& body) {
std::string bodyContent = restbed::String::format(
"%.*s\n", (int)body.size(), body.data());
// Get body params or form params here from the body content string
{{#allParams}}
@ -224,6 +230,10 @@ void {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource::handler_
}
{{/responses}}
defaultSessionClose(session, status_code, result);
{{#hasBodyParam}}
});
{{/hasBodyParam}}
}
{{#vendorExtensions.x-codegen-other-methods}}
@ -232,7 +242,13 @@ void {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource::handler_
const auto request = session->get_request();
{{#hasBodyParam}}
std::string bodyContent = extractBodyContent(session);
int content_length = request->get_header("Content-Length", 0);
session->fetch(content_length,
[this](const std::shared_ptr<restbed::Session> session,
const restbed::Bytes& body) {
std::string bodyContent = restbed::String::format(
"%.*s\n", (int)body.size(), body.data());
// body params or form params here from the body content string
{{#allParams}}
@ -334,6 +350,9 @@ void {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource::handler_
}
{{/responses}}
defaultSessionClose(session, status_code, result);
{{#hasBodyParam}}
});
{{/hasBodyParam}}
}
{{/vendorExtensions.x-codegen-other-methods}}
@ -350,19 +369,6 @@ void {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource::handler_
throw {{classname}}Exception(501, "Not implemented");
}
{{/vendorExtensions.x-codegen-other-methods}}
std::string {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource::extractBodyContent(const std::shared_ptr<restbed::Session>& session) {
const auto request = session->get_request();
int content_length = request->get_header("Content-Length", 0);
std::string bodyContent;
session->fetch(content_length,
[&bodyContent](const std::shared_ptr<restbed::Session> session,
const restbed::Bytes &body) {
bodyContent = restbed::String::format(
"%.*s\n", (int)body.size(), body.data());
});
return bodyContent;
}
{{/operation}}
{{classname}}::{{classname}}(std::shared_ptr<restbed::Service> const& restbedService)