summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authordewana-dewan <iit2015097@iiita.ac.in>2017-03-19 21:15:29 +0530
committerAshod Nakashian <ashod.nakashian@collabora.co.uk>2017-04-03 21:38:46 -0400
commitbd155ac96ddf218a26ecef96a60026866a96c1dd (patch)
tree9aeca7e0914b7ae8d0aca1f7b95b92f78e6622ce /net
parent5cf2cb3f929026122fb4d8713357c8e85b58c012 (diff)
tdf#106579 - serving gzipped file content
Change-Id: I320b22babf1bf65a0f1d4b1809a6ffb1f5ec8344 (cherry picked from commit 432204566764e88266370738686ed6a8d1dd31c4)
Diffstat (limited to 'net')
-rw-r--r--net/Socket.cpp86
-rw-r--r--net/Socket.hpp6
2 files changed, 63 insertions, 29 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 33e1091cf..0b7657312 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);
}
};