move lib/cache to cache_handler

This commit is contained in:
2022-12-19 15:27:38 +01:00
parent dd4f768cc4
commit ce658ac965
6 changed files with 31 additions and 47 deletions

View File

@ -47,16 +47,16 @@ bin/sesimos: bin/server.o bin/client.o bin/logger.o \
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(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 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 \ 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 src/lib/websocket.h src/logger.h
bin/logger.o: 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 bin/lib/compress.o: src/lib/compress.h

View File

@ -1,23 +1,22 @@
/** /**
* sesimos - secure, simple, modern web server * sesimos - secure, simple, modern web server
* @brief File cache implementation * @brief File cache implementation
* @file src/lib/cache.c * @file src/cache_handler.c
* @author Lorenz Stechauner * @author Lorenz Stechauner
* @date 2020-12-19 * @date 2020-12-19
*/ */
#include "../server.h" #include "server.h"
#include "../logger.h" #include "logger.h"
#include "cache.h" #include "cache_handler.h"
#include "utils.h" #include "lib/utils.h"
#include "compress.h" #include "lib/compress.h"
#include "config.h" #include "lib/config.h"
#include <stdio.h> #include <stdio.h>
#include <magic.h> #include <magic.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <fcntl.h> #include <fcntl.h>
@ -26,8 +25,7 @@
#define CACHE_BUF_SIZE 16 #define CACHE_BUF_SIZE 16
magic_t magic; static magic_t magic;
static pthread_t thread; static pthread_t thread;
static sem_t sem_free, sem_used, sem_lock; static sem_t sem_free, sem_used, sem_lock;
@ -39,8 +37,6 @@ typedef struct {
static buf_t buffer; static buf_t buffer;
static int magic_init(void) { static int magic_init(void) {
if ((magic = magic_open(MAGIC_MIME)) == NULL) { if ((magic = magic_open(MAGIC_MIME)) == NULL) {
critical("Unable to open magic cookie"); critical("Unable to open magic cookie");
@ -323,7 +319,7 @@ static void cache_mark_entry_dirty(cache_entry_t *entry) {
sem_post(&sem_used); 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; struct stat statbuf;
stat(filename, &statbuf); stat(filename, &statbuf);
memcpy(&entry->meta.stat, &statbuf, sizeof(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)); strcpy(entry->meta.charset, magic_file(magic, filename));
cache_mark_entry_dirty(entry); cache_mark_entry_dirty(entry);
return 0;
} }
void cache_mark_dirty(cache_t *cache, const char *filename) { 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); if (entry) cache_mark_entry_dirty(entry);
} }
int cache_init_uri(cache_t *cache, http_uri *uri) { void cache_init_uri(cache_t *cache, http_uri *uri) {
if (uri->filename == NULL) if (!uri->filename)
return 0; return;
cache_entry_t *entry = cache_get_entry(cache, uri->filename); cache_entry_t *entry = cache_get_entry(cache, uri->filename);
if (entry == NULL) { if (!entry) {
// no entry found -> create new entry // no entry found -> create new entry
entry = cache_get_new_entry(cache); if ((entry = cache_get_new_entry(cache))) {
if (entry) { cache_update_entry(entry, uri->filename, uri->webroot);
if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) {
return -1;
}
uri->meta = &entry->meta; uri->meta = &entry->meta;
} else { } else {
warning("No empty cache entry slot found"); warning("No empty cache entry slot found");
@ -376,18 +367,14 @@ int cache_init_uri(cache_t *cache, http_uri *uri) {
} else { } else {
uri->meta = &entry->meta; uri->meta = &entry->meta;
if (entry->flags & CACHE_DIRTY) if (entry->flags & CACHE_DIRTY)
return 0; return;
// check, if file has changed // check, if file has changed
struct stat statbuf; struct stat statbuf;
stat(uri->filename, &statbuf); stat(uri->filename, &statbuf);
if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) { if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) {
// modify time has changed // modify time has changed
if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) { cache_update_entry(entry, uri->filename, uri->webroot);
return -1;
}
} }
} }
return 0;
} }

View File

@ -1,15 +1,15 @@
/** /**
* sesimos - secure, simple, modern web server * sesimos - secure, simple, modern web server
* @brief File cache implementation (header file) * @brief File cache implementation (header file)
* @file src/lib/cache.h * @file src/cache_handler.h
* @author Lorenz Stechauner * @author Lorenz Stechauner
* @date 2020-12-19 * @date 2020-12-19
*/ */
#ifndef SESIMOS_CACHE_H #ifndef SESIMOS_CACHE_HANDLER_H
#define SESIMOS_CACHE_H #define SESIMOS_CACHE_HANDLER_H
#include "uri.h" #include "lib/uri.h"
#define CACHE_ENTRIES 1024 #define CACHE_ENTRIES 1024
@ -39,6 +39,6 @@ int cache_join(void);
void cache_mark_dirty(cache_t *cache, const char *filename); 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

View File

@ -17,7 +17,7 @@
#include "lib/http.h" #include "lib/http.h"
#include "lib/proxy.h" #include "lib/proxy.h"
#include "lib/fastcgi.h" #include "lib/fastcgi.h"
#include "lib/cache.h" #include "cache_handler.h"
#include "lib/geoip.h" #include "lib/geoip.h"
#include "lib/compress.h" #include "lib/compress.h"
#include "lib/websocket.h" #include "lib/websocket.h"
@ -252,11 +252,8 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
goto respond; goto respond;
} }
if ((ret = cache_init_uri(conf->cache, &uri)) != 0) { cache_init_uri(conf->cache, &uri);
res.status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with internal file cache.");
goto respond;
}
const char *last_modified = http_format_date(uri.meta->stat.st_mtime, buf0, sizeof(buf0)); 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); http_add_header_field(&res.hdr, "Last-Modified", last_modified);
sprintf(buf1, "%s; charset=%s", uri.meta->type, uri.meta->charset); sprintf(buf1, "%s; charset=%s", uri.meta->type, uri.meta->charset);

View File

@ -10,7 +10,7 @@
#define SESIMOS_CONFIG_H #define SESIMOS_CONFIG_H
#include "uri.h" #include "uri.h"
#include "cache.h" #include "../cache_handler.h"
#define CONFIG_MAX_HOST_CONFIG 64 #define CONFIG_MAX_HOST_CONFIG 64
#define CONFIG_MAX_CERT_CONFIG 64 #define CONFIG_MAX_CERT_CONFIG 64

View File

@ -11,7 +11,7 @@
#include "client.h" #include "client.h"
#include "logger.h" #include "logger.h"
#include "lib/cache.h" #include "cache_handler.h"
#include "lib/config.h" #include "lib/config.h"
#include "lib/sock.h" #include "lib/sock.h"
#include "lib/proxy.h" #include "lib/proxy.h"