Avoid copying of content provider if possible (#627)

This commit is contained in:
Ivan Fefer 2020-08-26 15:56:51 +03:00 committed by GitHub
parent e5743b358d
commit f1a2ac5108
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -428,6 +428,19 @@ struct Response {
ContentProvider content_provider_;
std::function<void()> content_provider_resource_releaser_;
bool is_chunked_content_provider = false;
class ContentProviderAdapter {
public:
explicit ContentProviderAdapter(ContentProviderWithoutLength&& content_provider):
content_provider_(content_provider) {}
bool operator()(size_t offset, size_t, DataSink& sink) {
return content_provider_(offset, sink);
}
private:
ContentProviderWithoutLength content_provider_;
};
};
class Stream {
@ -3574,9 +3587,7 @@ Response::set_content_provider(size_t in_length, const char *content_type,
assert(in_length > 0);
set_header("Content-Type", content_type);
content_length_ = in_length;
content_provider_ = [provider](size_t offset, size_t length, DataSink &sink) {
return provider(offset, length, sink);
};
content_provider_ = std::move(provider);
content_provider_resource_releaser_ = resource_releaser;
is_chunked_content_provider = false;
}
@ -3587,9 +3598,7 @@ Response::set_content_provider(const char *content_type,
const std::function<void()> &resource_releaser) {
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = [provider](size_t offset, size_t, DataSink &sink) {
return provider(offset, sink);
};
content_provider_ = ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
is_chunked_content_provider = false;
}
@ -3599,9 +3608,7 @@ inline void Response::set_chunked_content_provider(
const std::function<void()> &resource_releaser) {
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = [provider](size_t offset, size_t, DataSink &sink) {
return provider(offset, sink);
};
content_provider_ = ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
is_chunked_content_provider = true;
}