diff options
author | Michael Meeks <michael.meeks@collabora.com> | 2020-06-03 16:06:10 +0100 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2020-06-03 18:15:33 +0200 |
commit | 714640993bb09f34cef97e878fd8e8b13ea4fb2b (patch) | |
tree | 44b130f2d4f04d690339866b0ca3f87103e1a180 /net | |
parent | a3fc39e3253ab3b0be4567ce7a71d1ad9c594cbb (diff) |
Remember to shutdown the socket after serving files.
re-factor to make it hard not to.
Change-Id: I26ebc48b4660276ede64a22167ac4779cebf5cd4
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/95440
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Diffstat (limited to 'net')
-rw-r--r-- | net/Socket.cpp | 38 | ||||
-rw-r--r-- | net/Socket.hpp | 8 |
2 files changed, 26 insertions, 20 deletions
diff --git a/net/Socket.cpp b/net/Socket.cpp index e388d412a..ee76a5a1a 100644 --- a/net/Socket.cpp +++ b/net/Socket.cpp @@ -1022,14 +1022,19 @@ namespace HttpHelper } } - void sendFile(const std::shared_ptr<StreamSocket>& socket, - const std::string& path, - const std::string& mediaType, - Poco::Net::HTTPResponse& response, - const bool noCache, - const bool deflate, - const bool headerOnly) + void sendFileAndShutdown(const std::shared_ptr<StreamSocket>& socket, + const std::string& path, + const std::string& mediaType, + Poco::Net::HTTPResponse *optResponse, + const bool noCache, + const bool deflate, + const bool headerOnly) { + Poco::Net::HTTPResponse *response = optResponse; + Poco::Net::HTTPResponse localResponse; + if (!response) + response = &localResponse; + struct stat st; if (stat(path.c_str(), &st) != 0) { @@ -1040,16 +1045,16 @@ namespace HttpHelper if (!noCache) { // 60 * 60 * 24 * 128 (days) = 11059200 - response.set("Cache-Control", "max-age=11059200"); - response.set("ETag", "\"" LOOLWSD_VERSION_HASH "\""); + response->set("Cache-Control", "max-age=11059200"); + response->set("ETag", "\"" LOOLWSD_VERSION_HASH "\""); } else { - response.set("Cache-Control", "no-cache"); + response->set("Cache-Control", "no-cache"); } - response.setContentType(mediaType); - response.add("X-Content-Type-Options", "nosniff"); + response->setContentType(mediaType); + response->add("X-Content-Type-Options", "nosniff"); int bufferSize = std::min(st.st_size, (off_t)Socket::MaximumSendBufferSize); if (st.st_size >= socket->getSendBufferSize()) @@ -1063,24 +1068,25 @@ namespace HttpHelper // IE/Edge before enabling the deflate again if (!deflate || true) { - response.setContentLength(st.st_size); + response->setContentLength(st.st_size); LOG_TRC('#' << socket->getFD() << ": Sending " << (headerOnly ? "header for " : "") << " file [" << path << "]."); - socket->send(response); + socket->send(*response); if (!headerOnly) sendUncompressedFileContent(socket, path, bufferSize); } else { - response.set("Content-Encoding", "deflate"); + response->set("Content-Encoding", "deflate"); LOG_TRC('#' << socket->getFD() << ": Sending " << (headerOnly ? "header for " : "") << " file [" << path << "]."); - socket->send(response); + socket->send(*response); if (!headerOnly) sendDeflatedFileContent(socket, path, st.st_size); } + socket->shutdown(); } } diff --git a/net/Socket.hpp b/net/Socket.hpp index 7e011bb9d..55cfe05bb 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -1278,10 +1278,10 @@ enum class WSOpCode : unsigned char { namespace HttpHelper { - /// Sends file as HTTP response. - void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path, const std::string& mediaType, - Poco::Net::HTTPResponse& response, bool noCache = false, bool deflate = false, - const bool headerOnly = false); + /// Sends file as HTTP response and shutdown the socket. + void sendFileAndShutdown(const std::shared_ptr<StreamSocket>& socket, const std::string& path, const std::string& mediaType, + Poco::Net::HTTPResponse *optResponse = nullptr, bool noCache = false, bool deflate = false, + const bool headerOnly = false); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |