tdf#106579 - serving gzipped file content

Change-Id: I320b22babf1bf65a0f1d4b1809a6ffb1f5ec8344
This commit is contained in:
dewana-dewan
2017-03-19 21:15:29 +05:30
committed by Michael Meeks
parent 1d335b92ce
commit 4322045667
3 changed files with 67 additions and 32 deletions

View File

@ -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);
std::ifstream file(path, std::ios::binary);
bool flush = true;
do
if(!deflate)
{
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;
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
{
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];
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);
}
}

View File

@ -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);
}
};

View File

@ -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)