@@ -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