Skip to content

Commit 4fd86b4

Browse files
committed
refactor: improve FileCache Open method and enhance HttpHandler header handling
1 parent 16c24e7 commit 4fd86b4

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

http/server/FileCache.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,19 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
102102
// Lock order: LRUCache mutex → fc->mutex (never reverse).
103103
{
104104
std::lock_guard<std::mutex> lock(fc->mutex);
105-
// Sync local stat result into cached entry
106-
fc->st = st;
107-
if (S_ISREG(fc->st.st_mode)) {
108-
param->filesize = fc->st.st_size;
105+
if (S_ISREG(st.st_mode)) {
106+
param->filesize = st.st_size;
109107
// FILE
110108
if (param->need_read) {
111-
if (fc->st.st_size > param->max_read) {
109+
if (st.st_size > param->max_read) {
112110
param->error = ERR_OVER_LIMIT;
113-
// Don't cache incomplete entries
111+
// Leave existing cache entry's state untouched
114112
return NULL;
115113
}
114+
}
115+
// Validation passed — commit new stat into cached entry
116+
fc->st = st;
117+
if (param->need_read) {
116118
fc->resize_buf(fc->st.st_size, max_header_length);
117119
// Loop to handle partial reads (EINTR, etc.)
118120
char* dst = fc->filebuf.base;
@@ -145,8 +147,9 @@ file_cache_ptr FileCache::Open(const char* filepath, OpenParam* param) {
145147
fc->content_type = http_content_type_str_by_suffix(suffix + 1);
146148
}
147149
}
148-
} else if (S_ISDIR(fc->st.st_mode)) {
150+
} else if (S_ISDIR(st.st_mode)) {
149151
// DIR
152+
fc->st = st;
150153
std::string page;
151154
make_index_of_page(filepath, page, param->path);
152155
fc->resize_buf(page.size(), max_header_length);

http/server/HttpHandler.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,13 @@ int HttpHandler::GetSendData(char** data, size_t* len) {
801801
// FileCache
802802
// NOTE: no copy filebuf, more efficient
803803
header = pResp->Dump(true, false);
804-
fc->prepend_header(header.c_str(), header.size());
805-
*data = fc->httpbuf.base;
806-
*len = fc->httpbuf.len;
807-
state = SEND_DONE;
808-
return *len;
804+
if (fc->prepend_header(header.c_str(), header.size())) {
805+
*data = fc->httpbuf.base;
806+
*len = fc->httpbuf.len;
807+
state = SEND_DONE;
808+
return *len;
809+
}
810+
// Header too large for reserved space — fall through to normal path
809811
}
810812
// API service
811813
content_length = pResp->ContentLength();

0 commit comments

Comments
 (0)