Skip to content

etag in res.sendFile #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/expresso/helpers/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <brewtils/os.h>
#include <brewtils/sys.h>
#include <zippuccino/crc.h>

namespace expresso {

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/core/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading