diff --git a/Makefile b/Makefile index d3f1b4a..d7d7846 100644 --- a/Makefile +++ b/Makefile @@ -47,16 +47,16 @@ bin/sesimos: bin/server.o bin/client.o bin/logger.o \ $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) -bin/server.o: src/server.h src/defs.h src/client.h src/lib/cache.h src/lib/config.h src/lib/sock.h \ +bin/server.o: src/server.h src/defs.h src/client.h src/cache_handler.h src/lib/config.h src/lib/sock.h \ src/lib/proxy.h src/lib/geoip.h src/lib/utils.h src/logger.h bin/client.o: src/client.h src/defs.h src/server.h src/lib/utils.h src/lib/config.h src/lib/sock.h \ - src/lib/http.h src/lib/proxy.h src/lib/fastcgi.h src/lib/cache.h src/lib/geoip.h src/lib/compress.h \ + src/lib/http.h src/lib/proxy.h src/lib/fastcgi.h src/cache_handler.h src/lib/geoip.h src/lib/compress.h \ src/lib/websocket.h src/logger.h bin/logger.o: src/logger.h -bin/lib/cache.o: src/lib/cache.h src/lib/utils.h src/lib/uri.h src/lib/compress.h src/logger.h +bin/lib/cache.o: src/cache_handler.h src/lib/utils.h src/lib/uri.h src/lib/compress.h src/logger.h bin/lib/compress.o: src/lib/compress.h diff --git a/src/lib/cache.c b/src/cache_handler.c similarity index 93% rename from src/lib/cache.c rename to src/cache_handler.c index 1257e56..76bd7a4 100644 --- a/src/lib/cache.c +++ b/src/cache_handler.c @@ -1,23 +1,22 @@ /** * sesimos - secure, simple, modern web server * @brief File cache implementation - * @file src/lib/cache.c + * @file src/cache_handler.c * @author Lorenz Stechauner * @date 2020-12-19 */ -#include "../server.h" -#include "../logger.h" -#include "cache.h" -#include "utils.h" -#include "compress.h" -#include "config.h" +#include "server.h" +#include "logger.h" +#include "cache_handler.h" +#include "lib/utils.h" +#include "lib/compress.h" +#include "lib/config.h" #include #include #include #include -#include #include #include #include @@ -26,8 +25,7 @@ #define CACHE_BUF_SIZE 16 -magic_t magic; - +static magic_t magic; static pthread_t thread; static sem_t sem_free, sem_used, sem_lock; @@ -39,8 +37,6 @@ typedef struct { static buf_t buffer; - - static int magic_init(void) { if ((magic = magic_open(MAGIC_MIME)) == NULL) { critical("Unable to open magic cookie"); @@ -323,7 +319,7 @@ static void cache_mark_entry_dirty(cache_entry_t *entry) { sem_post(&sem_used); } -static int cache_update_entry(cache_entry_t *entry, const char *filename, const char *webroot) { +static void cache_update_entry(cache_entry_t *entry, const char *filename, const char *webroot) { struct stat statbuf; stat(filename, &statbuf); memcpy(&entry->meta.stat, &statbuf, sizeof(statbuf)); @@ -348,8 +344,6 @@ static int cache_update_entry(cache_entry_t *entry, const char *filename, const strcpy(entry->meta.charset, magic_file(magic, filename)); cache_mark_entry_dirty(entry); - - return 0; } void cache_mark_dirty(cache_t *cache, const char *filename) { @@ -357,18 +351,15 @@ void cache_mark_dirty(cache_t *cache, const char *filename) { if (entry) cache_mark_entry_dirty(entry); } -int cache_init_uri(cache_t *cache, http_uri *uri) { - if (uri->filename == NULL) - return 0; +void cache_init_uri(cache_t *cache, http_uri *uri) { + if (!uri->filename) + return; cache_entry_t *entry = cache_get_entry(cache, uri->filename); - if (entry == NULL) { + if (!entry) { // no entry found -> create new entry - entry = cache_get_new_entry(cache); - if (entry) { - if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) { - return -1; - } + if ((entry = cache_get_new_entry(cache))) { + cache_update_entry(entry, uri->filename, uri->webroot); uri->meta = &entry->meta; } else { warning("No empty cache entry slot found"); @@ -376,18 +367,14 @@ int cache_init_uri(cache_t *cache, http_uri *uri) { } else { uri->meta = &entry->meta; if (entry->flags & CACHE_DIRTY) - return 0; + return; // check, if file has changed struct stat statbuf; stat(uri->filename, &statbuf); if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) { // modify time has changed - if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) { - return -1; - } + cache_update_entry(entry, uri->filename, uri->webroot); } } - - return 0; } diff --git a/src/lib/cache.h b/src/cache_handler.h similarity index 76% rename from src/lib/cache.h rename to src/cache_handler.h index f348a9b..1489607 100644 --- a/src/lib/cache.h +++ b/src/cache_handler.h @@ -1,15 +1,15 @@ /** * sesimos - secure, simple, modern web server * @brief File cache implementation (header file) - * @file src/lib/cache.h + * @file src/cache_handler.h * @author Lorenz Stechauner * @date 2020-12-19 */ -#ifndef SESIMOS_CACHE_H -#define SESIMOS_CACHE_H +#ifndef SESIMOS_CACHE_HANDLER_H +#define SESIMOS_CACHE_HANDLER_H -#include "uri.h" +#include "lib/uri.h" #define CACHE_ENTRIES 1024 @@ -39,6 +39,6 @@ int cache_join(void); void cache_mark_dirty(cache_t *cache, const char *filename); -int cache_init_uri(cache_t *cache, http_uri *uri); +void cache_init_uri(cache_t *cache, http_uri *uri); -#endif //SESIMOS_CACHE_H +#endif //SESIMOS_CACHE_HANDLER_H diff --git a/src/client.c b/src/client.c index e8cbc60..300a0e1 100644 --- a/src/client.c +++ b/src/client.c @@ -17,7 +17,7 @@ #include "lib/http.h" #include "lib/proxy.h" #include "lib/fastcgi.h" -#include "lib/cache.h" +#include "cache_handler.h" #include "lib/geoip.h" #include "lib/compress.h" #include "lib/websocket.h" @@ -252,11 +252,8 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien goto respond; } - if ((ret = cache_init_uri(conf->cache, &uri)) != 0) { - res.status = http_get_status(500); - sprintf(err_msg, "Unable to communicate with internal file cache."); - goto respond; - } + cache_init_uri(conf->cache, &uri); + const char *last_modified = http_format_date(uri.meta->stat.st_mtime, buf0, sizeof(buf0)); http_add_header_field(&res.hdr, "Last-Modified", last_modified); sprintf(buf1, "%s; charset=%s", uri.meta->type, uri.meta->charset); diff --git a/src/lib/config.h b/src/lib/config.h index a34c059..5ab22d5 100644 --- a/src/lib/config.h +++ b/src/lib/config.h @@ -10,7 +10,7 @@ #define SESIMOS_CONFIG_H #include "uri.h" -#include "cache.h" +#include "../cache_handler.h" #define CONFIG_MAX_HOST_CONFIG 64 #define CONFIG_MAX_CERT_CONFIG 64 diff --git a/src/server.c b/src/server.c index a185145..fd8bbdb 100644 --- a/src/server.c +++ b/src/server.c @@ -11,7 +11,7 @@ #include "client.h" #include "logger.h" -#include "lib/cache.h" +#include "cache_handler.h" #include "lib/config.h" #include "lib/sock.h" #include "lib/proxy.h"