rest: don't copy data when sending binary response

Also, change `HTTPRequest::WriteReply` to accept `std::span`.
This commit is contained in:
Roman Zeyde
2024-06-22 11:46:10 +03:00
parent 538363738e
commit 1556d21599
3 changed files with 17 additions and 17 deletions

View File

@@ -26,6 +26,7 @@
#include <deque>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <unordered_map>
@@ -634,7 +635,7 @@ void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
* Replies must be sent in the main loop in the main http thread,
* this cannot be done from worker threads.
*/
void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
void HTTPRequest::WriteReply(int nStatus, std::span<const std::byte> reply)
{
assert(!replySent && req);
if (m_interrupt) {
@@ -643,7 +644,7 @@ void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
// Send event to main http thread to send reply message
struct evbuffer* evb = evhttp_request_get_output_buffer(req);
assert(evb);
evbuffer_add(evb, strReply.data(), strReply.size());
evbuffer_add(evb, reply.data(), reply.size());
auto req_copy = req;
HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{
evhttp_send_reply(req_copy, nStatus, nullptr, nullptr);