Skip to content

Commit 42e762d

Browse files
committed
Refactor ETag handling: use 'auto' for type inference and improve code readability
1 parent c5dfb10 commit 42e762d

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

httplib.h

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,8 +2976,8 @@ inline std::string from_i_to_hex(size_t n) {
29762976
inline std::string compute_etag(const FileStat &fs) {
29772977
if (!fs.is_file()) { return std::string(); }
29782978

2979-
time_t mtime_raw = fs.mtime();
2980-
size_t mtime = mtime_raw < 0 ? 0 : static_cast<size_t>(mtime_raw);
2979+
auto mtime_raw = fs.mtime();
2980+
auto mtime = mtime_raw < 0 ? 0 : static_cast<size_t>(mtime_raw);
29812981
auto size = fs.size();
29822982

29832983
return std::string("W/\"") + from_i_to_hex(mtime) + "-" +
@@ -8359,10 +8359,11 @@ inline bool Server::handle_file_request(Request &req, Response &res) {
83598359

83608360
// Compute and set weak ETag based on mtime+size.
83618361
auto etag = detail::compute_etag(stat);
8362+
if (!etag.empty()) { res.set_header("ETag", etag); }
8363+
83628364
auto mtime = stat.mtime();
8363-
auto last_modified = detail::file_mtime_to_http_date(mtime);
83648365

8365-
if (!etag.empty()) { res.set_header("ETag", etag); }
8366+
auto last_modified = detail::file_mtime_to_http_date(mtime);
83668367
if (!last_modified.empty()) {
83678368
res.set_header("Last-Modified", last_modified);
83688369
}
@@ -8372,15 +8373,16 @@ inline bool Server::handle_file_request(Request &req, Response &res) {
83728373
// 2. If-Modified-Since is checked only when If-None-Match is absent
83738374
if (req.has_header("If-None-Match")) {
83748375
if (!etag.empty()) {
8375-
auto inm = req.get_header_value("If-None-Match");
8376-
bool matched = false;
8376+
auto val = req.get_header_value("If-None-Match");
8377+
auto matched = false;
8378+
83778379
// NOTE: We use exact string matching here. This works correctly
83788380
// because our server always generates weak ETags (W/"..."), and
83798381
// clients typically send back the same ETag they received.
83808382
// RFC 9110 Section 8.8.3.2 allows weak comparison for
83818383
// If-None-Match, where W/"x" and "x" would match, but this
83828384
// simplified implementation requires exact matches.
8383-
detail::split(inm.data(), inm.data() + inm.size(), ',',
8385+
detail::split(val.data(), val.data() + val.size(), ',',
83848386
[&](const char *b, const char *e) {
83858387
if (!matched) {
83868388
auto tag = std::string(b, e);
@@ -8394,31 +8396,33 @@ inline bool Server::handle_file_request(Request &req, Response &res) {
83948396
}
83958397
}
83968398
} else if (req.has_header("If-Modified-Since")) {
8397-
auto ims = req.get_header_value("If-Modified-Since");
8398-
auto ims_time = detail::parse_http_date(ims);
8399-
if (ims_time != static_cast<time_t>(-1) && mtime <= ims_time) {
8399+
auto val = req.get_header_value("If-Modified-Since");
8400+
auto t = detail::parse_http_date(val);
8401+
8402+
if (t != static_cast<time_t>(-1) && mtime <= t) {
84008403
res.status = StatusCode::NotModified_304;
84018404
return true;
84028405
}
84038406
}
84048407

8405-
// Handle If-Range for partial content requests (RFC 9110 Section 13.1.5).
8406-
// If-Range is only evaluated when Range header is present.
8407-
// If the validator matches, serve partial content; otherwise serve full content.
8408+
// Handle If-Range for partial content requests (RFC 9110
8409+
// Section 13.1.5). If-Range is only evaluated when Range header is
8410+
// present. If the validator matches, serve partial content; otherwise
8411+
// serve full content.
84088412
if (!req.ranges.empty() && req.has_header("If-Range")) {
8409-
auto if_range = req.get_header_value("If-Range");
8413+
auto val = req.get_header_value("If-Range");
84108414
auto valid = false;
84118415

8412-
if (detail::is_strong_etag(if_range)) {
8416+
if (detail::is_strong_etag(val)) {
84138417
// RFC 9110 Section 13.1.5: If-Range requires strong ETag
84148418
// comparison.
8415-
valid = (!etag.empty() && if_range == etag);
8416-
} else if (detail::is_weak_etag(if_range)) {
8419+
valid = (!etag.empty() && val == etag);
8420+
} else if (detail::is_weak_etag(val)) {
84178421
// Weak ETags are not valid for If-Range (RFC 9110 Section 13.1.5)
84188422
valid = false;
84198423
} else {
84208424
// HTTP-date comparison
8421-
auto if_range_time = detail::parse_http_date(if_range);
8425+
auto if_range_time = detail::parse_http_date(val);
84228426
valid = (if_range_time != static_cast<time_t>(-1) &&
84238427
mtime <= if_range_time);
84248428
}

0 commit comments

Comments
 (0)