diff options
author | dewana-dewan <iit2015097@iiita.ac.in> | 2017-03-19 21:15:29 +0530 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2017-03-30 12:09:12 +0100 |
commit | 432204566764e88266370738686ed6a8d1dd31c4 (patch) | |
tree | d59cae8916bf6b3e67b8cb5898ee54efcdbdcc33 | |
parent | 1d335b92ce771e311e8c0faaeaf112208bea75f6 (diff) |
tdf#106579 - serving gzipped file content
Change-Id: I320b22babf1bf65a0f1d4b1809a6ffb1f5ec8344
-rw-r--r-- | net/Socket.cpp | 86 | ||||
-rw-r--r-- | net/Socket.hpp | 6 | ||||
-rw-r--r-- | wsd/FileServer.cpp | 3 |
3 files changed, 65 insertions, 30 deletions
diff --git a/net/Socket.cpp b/net/Socket.cpp index cb863b1f4..62896921a 100644 --- a/net/Socket.cpp +++ b/net/Socket.cpp @@ -12,7 +12,8 @@ #include <stdio.h> #include <ctype.h> #include <iomanip> - +#include <zlib.h> + #include <Poco/DateTime.h> #include <Poco/DateTimeFormat.h> #include <Poco/DateTimeFormatter.h> @@ -181,7 +182,7 @@ void SocketPoll::dumpState(std::ostream& os) namespace HttpHelper { void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path, - Poco::Net::HTTPResponse& response, bool noCache) + Poco::Net::HTTPResponse& response, bool noCache, bool deflate) { struct stat st; if (stat(path.c_str(), &st) != 0) @@ -191,14 +192,6 @@ namespace HttpHelper return; } - int bufferSize = std::min(st.st_size, (off_t)Socket::MaximumSendBufferSize); - if (st.st_size >= socket->getSendBufferSize()) - { - socket->setSocketBufferSize(bufferSize); - bufferSize = socket->getSendBufferSize(); - } - - response.setContentLength(st.st_size); response.set("User-Agent", HTTP_AGENT_STRING); response.set("Date", Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT)); if (!noCache) @@ -208,26 +201,67 @@ namespace HttpHelper response.set("ETag", "\"" LOOLWSD_VERSION_HASH "\""); } - std::ostringstream oss; - response.write(oss); - const std::string header = oss.str(); - LOG_TRC("#" << socket->getFD() << ": Sending file [" << path << "]: " << header); - socket->send(header); + if(!deflate) + { + int bufferSize = std::min(st.st_size, (off_t)Socket::MaximumSendBufferSize); + if (st.st_size >= socket->getSendBufferSize()) + { + socket->setSocketBufferSize(bufferSize); + bufferSize = socket->getSendBufferSize(); + } + + response.setContentLength(st.st_size); + std::ostringstream oss; + response.write(oss); + const std::string header = oss.str(); + LOG_TRC("#" << socket->getFD() << ": Sending file [" << path << "]: " << header); + socket->send(header); - std::ifstream file(path, std::ios::binary); - bool flush = true; - do + std::ifstream file(path, std::ios::binary); + bool flush = true; + do + { + char buf[bufferSize]; + file.read(buf, sizeof(buf)); + const int size = file.gcount(); + if (size > 0) + socket->send(buf, size, flush); + else + break; + flush = false; + } + while (file); + } + else { + response.set("Content-Encoding", "deflate"); + std::ostringstream oss; + response.write(oss); + const std::string header = oss.str(); + LOG_TRC("#" << socket->getFD() << ": Sending file [" << path << "]: " << header); + socket->send(header); + + std::ifstream file(path, std::ios::binary); + uLong bufferSize; + bufferSize = st.st_size; char buf[bufferSize]; - file.read(buf, sizeof(buf)); - const int size = file.gcount(); - if (size > 0) - socket->send(buf, size, flush); - else - break; - flush = false; + bool flush = true; + do + { + unsigned int a = 9; + file.read(buf, sizeof(buf)); + long unsigned int size = file.gcount(); + long unsigned int compSize = compressBound(size); + char cbuf[compSize]; + compress2((Bytef *)&cbuf, &compSize, (Bytef *)&buf, size, a) ; + if (size > 0) + socket->send(cbuf, compSize, flush); + else + break; + flush = false; + } + while(file); } - while (file); } } diff --git a/net/Socket.hpp b/net/Socket.hpp index 111b32df5..4767d26d3 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -856,14 +856,14 @@ protected: namespace HttpHelper { void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path, - Poco::Net::HTTPResponse& response, bool noCache = false); + Poco::Net::HTTPResponse& response, bool noCache = false, bool deflate = false); inline void sendFile(const std::shared_ptr<StreamSocket>& socket, const std::string& path, - const std::string& mediaType, bool noCache = false) + const std::string& mediaType, bool noCache = false, bool deflate = false) { Poco::Net::HTTPResponse response; response.setContentType(mediaType); - sendFile(socket, path, response, noCache); + sendFile(socket, path, response, noCache, deflate); } }; diff --git a/wsd/FileServer.cpp b/wsd/FileServer.cpp index 58fe90c28..0f987579e 100644 --- a/wsd/FileServer.cpp +++ b/wsd/FileServer.cpp @@ -194,7 +194,8 @@ void FileServerRequestHandler::handleRequest(const HTTPRequest& request, Poco::M } response.setContentType(mimeType); - HttpHelper::sendFile(socket, filepath, response, noCache); + bool deflate = request.hasToken("Accept-Encoding", "deflate"); + HttpHelper::sendFile(socket, filepath, response, noCache, deflate); } } catch (const Poco::Net::NotAuthenticatedException& exc) |