From 88dbe155e4415182dee503d90942367495bcfa14 Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sun, 21 Mar 2021 09:25:41 -0400 Subject: [PATCH] wsd: http: support sending http::Response directly to the socket The Socket now accepts http::Response and serializes it before sending it. Change-Id: Id36e2d91b21d168da72ccdbd7e509ec08021b78e Signed-off-by: Ashod Nakashian --- net/Socket.cpp | 7 +++++++ net/Socket.hpp | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/net/Socket.cpp b/net/Socket.cpp index f95b0af5b..da03727c1 100644 --- a/net/Socket.cpp +++ b/net/Socket.cpp @@ -38,6 +38,7 @@ #include #endif #include "WebSocketHandler.hpp" +#include // Bug in pre C++17 where static constexpr must be defined. Fixed in C++17. constexpr std::chrono::microseconds SocketPoll::DefaultPollTimeoutMicroS; @@ -596,6 +597,12 @@ void StreamSocket::send(Poco::Net::HTTPResponse& response) send(oss.str()); } +void StreamSocket::send(const http::Response& response) +{ + response.writeData(_outBuffer); + flush(); +} + void SocketPoll::dumpState(std::ostream& os) { // FIXME: NOT thread-safe! _pollSockets is modified from the polling thread! diff --git a/net/Socket.hpp b/net/Socket.hpp index 8477bb4b5..2168520db 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -39,6 +39,10 @@ #define LOG_SOCKET_DATA +namespace http +{ +class Response; +} namespace Poco { class MemoryInputStream; @@ -946,6 +950,10 @@ public: /// Adds Date and User-Agent. void send(Poco::Net::HTTPResponse& response); + /// Send an http::Response and flush. + /// Does not add any fields to the header. + void send(const http::Response& response); + /// Safely flush any outgoing data. inline void flush() {