From 1c30c629a56403302a68eae3f40469da98d9468f Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 5 Dec 2025 00:13:51 -0500 Subject: [PATCH] Fix type for mtime in FileStat and improve ETag handling comments --- httplib.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 5f0537d..fd233b4 100644 --- a/httplib.h +++ b/httplib.h @@ -2976,8 +2976,8 @@ inline std::string from_i_to_hex(size_t n) { inline std::string compute_etag(const FileStat &fs) { if (!fs.is_file()) { return std::string(); } - size_t mtime = fs.mtime(); - size_t size = fs.size(); + auto mtime = static_cast(fs.mtime()); + auto size = fs.size(); return std::string("W/\"") + from_i_to_hex(mtime) + "-" + from_i_to_hex(size) + "\""; @@ -3161,8 +3161,8 @@ inline bool FileStat::is_dir() const { return ret_ >= 0 && S_ISDIR(st_.st_mode); } -inline size_t FileStat::mtime() const { - return ret_ >= 0 ? static_cast(st_.st_mtime) : 0; +inline time_t FileStat::mtime() const { + return ret_ >= 0 ? static_cast(st_.st_mtime) : 0; } inline size_t FileStat::size() const { @@ -8373,6 +8373,12 @@ inline bool Server::handle_file_request(Request &req, Response &res) { if (!etag.empty()) { auto inm = req.get_header_value("If-None-Match"); bool matched = false; + // NOTE: We use exact string matching here. This works correctly + // because our server always generates weak ETags (W/"..."), and + // clients typically send back the same ETag they received. + // RFC 9110 Section 8.8.3.2 allows weak comparison for + // If-None-Match, where W/"x" and "x" would match, but this + // simplified implementation requires exact matches. detail::split(inm.data(), inm.data() + inm.size(), ',', [&](const char *b, const char *e) { if (!matched) {