From 67515abc9b585ac7fff034cbb0c78d7778de2f40 Mon Sep 17 00:00:00 2001 From: jadit19 Date: Sun, 8 Sep 2024 15:03:18 +0530 Subject: [PATCH] etag in res.sendFile --- include/expresso/helpers/response.h | 3 +++ src/core/response.cpp | 1 + src/helpers/response.cpp | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/include/expresso/helpers/response.h b/include/expresso/helpers/response.h index 0d5ccb2..7222b12 100644 --- a/include/expresso/helpers/response.h +++ b/include/expresso/helpers/response.h @@ -5,6 +5,7 @@ #include #include +#include namespace expresso { @@ -14,6 +15,8 @@ static const short CHUNK_SIZE = 1024; std::string getAvailableFile(const std::string &path); +const std::string generateETag(const std::string &data); + bool sendChunkedData(const int &socket, const std::string &data); bool sendFileInChunks(const int &socket, const std::string &path); diff --git a/src/core/response.cpp b/src/core/response.cpp index 4a1b862..68364cf 100644 --- a/src/core/response.cpp +++ b/src/core/response.cpp @@ -89,6 +89,7 @@ void expresso::core::Response::sendFile(const std::string &path, int64_t start, this->set("content-type", brewtils::os::file::getMimeType(fileName)); this->set("content-disposition", "inline; filename=\"" + fileName + "\""); this->set("accept-ranges", "bytes"); + this->set("etag", expresso::helpers::generateETag(availableFile)); if (!isPartial) { start = 0; diff --git a/src/helpers/response.cpp b/src/helpers/response.cpp index 42325da..73eb935 100644 --- a/src/helpers/response.cpp +++ b/src/helpers/response.cpp @@ -18,6 +18,14 @@ std::string expresso::helpers::getAvailableFile(const std::string &path) { return ""; } +const std::string expresso::helpers::generateETag(const std::string &data) { + const std::string availableFile = expresso::helpers::getAvailableFile(data); + uint32_t crc = zippuccino::crc::compute(availableFile); + std::ostringstream etag; + etag << "\"" << std::hex << std::setw(8) << std::setfill('0') << crc << "\""; + return etag.str(); +} + bool expresso::helpers::sendChunkedData(const int &socket, const std::string &data) { std::ostringstream dataSizeHex;