Skip to content
This repository was archived by the owner on Jul 14, 2019. It is now read-only.

Commit 38b4dd6

Browse files
Andrew ThompsonLior Amram
authored andcommitted
Per-file-extension cache-timeout added to file server
1 parent adaf26c commit 38b4dd6

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

include/http_file_server.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ struct http_file_server {
3232
/* internal use */
3333
size_t base_dir_len;
3434
struct hashtable ht_ext_whitelist;
35+
struct hashtable ht_ext_max_age;
3536
};
3637

37-
#define HTTP_FILE_SERVER_INITIALIZER { NULL, 0, 0, 0, HASHTABLE_INITIALIZER }
38+
#define HTTP_FILE_SERVER_INITIALIZER { NULL, 0, 0, 0, HASHTABLE_INITIALIZER, HASHTABLE_INITIALIZER }
3839

3940
int http_file_server_init(struct http_file_server *fs);
4041
int http_file_server_run(struct http_file_server *fs);
4142
int http_file_server_run2(struct http_file_server *fs, struct http_headers *headers, const char *file);
4243
static inline void http_file_server_gzip_ext(struct http_file_server *fs, const char *ext);
4344

45+
/* If the cache expiration for an extension is not set here, http_file_server->max_age will be used */
46+
static inline void http_file_server_max_age_ext(struct http_file_server *fs, const char *ext, int max_age);
47+
4448
#include "../src/_http_file_server.c"
4549

4650
#endif // _HTTP_FILE_SERVER__H_

src/_http_file_server.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ static inline void http_file_server_gzip_ext(struct http_file_server *fs, const
2121
hashtable_insert(&fs->ht_ext_whitelist, ext, strlen(ext), NULL, 0);
2222
}
2323

24+
static inline void http_file_server_max_age_ext(struct http_file_server *fs, const char *ext, int32_t max_age) {
25+
hashtable_insert(&fs->ht_ext_max_age, ext, strlen(ext), &max_age, sizeof(int32_t));
26+
}

src/http_file_server.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,25 @@ int http_file_server_init(struct http_file_server *fs) {
9494
if (NULL == realpath(fs->base_dir ? fs->base_dir : ".", realname))
9595
return -1;
9696
fs->base_dir = strdup(realname);
97-
if (0 > hashtable_init(&fs->ht_ext_whitelist, 0))
97+
if ((0 > hashtable_init(&fs->ht_ext_whitelist, 0)) ||
98+
(0 > hashtable_init(&fs->ht_ext_max_age, 0)))
9899
return -1;
100+
99101
fs->base_dir_len = strlen(fs->base_dir);
100102
return 0;
101103
}
102104

105+
static inline int32_t find_max_age(struct http_file_server *fs, const char *ext) {
106+
int32_t rv = fs->max_age;
107+
108+
if(0 < hashtable_get_size(&fs->ht_ext_max_age)) {
109+
uint32_t ofs = hashtable_lookup(&fs->ht_ext_max_age, ext, strlen(ext));
110+
if(ofs)
111+
memcpy(&rv, hashtable_get_val(&fs->ht_ext_max_age, ofs), sizeof(int32_t));
112+
}
113+
return rv;
114+
}
115+
103116
#define HTTP_FILE_SERVER_ERROR(code) \
104117
http_server_response_sprintf(HTTP_STATUS_##code, HTTP_CONTENT_TYPE_TEXT_PLAIN, "%s\n", HTTP_STATUS_##code)
105118

@@ -212,6 +225,8 @@ int http_file_server_run2(struct http_file_server *fs, struct http_headers *head
212225
gmtime_r(&orig_st.st_mtime, &tm);
213226
char etag[128];
214227
intmax_t size = st.st_size;
228+
int32_t max_age = find_max_age(fs, ext);
229+
215230
int n = strftime(etag, sizeof(etag), "\r\nETag: \"%d%m%Y%H%M%S", &tm);
216231
if (0 == n || (int)sizeof(etag) - n <= snprintf(etag + n, sizeof(etag) - n, "%jd\"", size))
217232
return HTTP_FILE_SERVER_ERROR(500), close(ffd), -1;
@@ -223,11 +238,11 @@ int http_file_server_run2(struct http_file_server *fs, struct http_headers *head
223238
vmbuf_strcpy(&ctx->header, etag);
224239
if (compressed && include_payload)
225240
vmbuf_strcpy(&ctx->header, "\r\nContent-Encoding: gzip");
226-
vmbuf_sprintf(&ctx->header, "\r\nCache-Control: max-age=%d", fs->max_age);
241+
vmbuf_sprintf(&ctx->header, "\r\nCache-Control: max-age=%d", max_age);
227242
time_t t = time(NULL);
228243
gmtime_r(&t, &tm);
229244
vmbuf_strftime(&ctx->header, "\r\nDate: %a, %d %b %Y %H:%M:%S GMT", &tm);
230-
t += fs->max_age;
245+
t += max_age;
231246
gmtime_r(&t, &tm);
232247
vmbuf_strftime(&ctx->header, "\r\nExpires: %a, %d %b %Y %H:%M:%S GMT", &tm);
233248
gmtime_r(&orig_st.st_mtime, &tm);

0 commit comments

Comments
 (0)