diff --git a/Makefile b/Makefile index 3a8fb0a..7798abf 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ packages: compile: @mkdir -p bin - gcc src/necronda-server.c -o bin/necronda-server -std=c11 -lssl -lcrypto + gcc src/necronda-server.c -o bin/necronda-server -std=c11 -lssl -lcrypto -lmagic install: | packages update compile @echo "Finished!" diff --git a/src/cache.c b/src/cache.c index 9288371..36ddbd4 100644 --- a/src/cache.c +++ b/src/cache.c @@ -6,3 +6,26 @@ */ #include "cache.h" +#include "uri.h" + +int magic_init() { + magic = magic_open(MAGIC_MIME); + magic_load(magic, "/usr/share/misc/magic.mgc"); + return 0; +} + +int cache_init() { + magic_init(); + + return 0; +} + +int uri_cache_init(http_uri *uri) { + if (uri->filename == NULL) { + return -1; + } + + + + return 0; +} diff --git a/src/cache.h b/src/cache.h index 365e069..599dc0c 100644 --- a/src/cache.h +++ b/src/cache.h @@ -8,4 +8,25 @@ #ifndef NECRONDA_SERVER_CACHE_H #define NECRONDA_SERVER_CACHE_H +#include + +magic_t magic; + +typedef struct { + char *etag; + char *type; + char *subtype; + char *filename_comp; + struct stat stat; +} meta_data; + +typedef struct { + char *filename; + unsigned short filename_len; + meta_data meta; +} cache_entry; + +cache_entry cache[FILE_CACHE_SIZE]; +int cache_entries = 0; + #endif //NECRONDA_SERVER_CACHE_H diff --git a/src/client.c b/src/client.c index f8d3266..a6b57f6 100644 --- a/src/client.c +++ b/src/client.c @@ -155,9 +155,9 @@ int client_request_handler(sock *client, int req_num) { sprintf(err_msg, "It is not allowed to list the contents of this directory."); goto respond; } else if (uri.filename == NULL && (int) !uri.is_static && (int) uri.is_dir && strlen(uri.pathinfo) == 0) { + // TODO list directory contents res.status = http_get_status(501); sprintf(err_msg, "Listing contents of an directory is currently not implemented."); - // TODO list directory contents goto respond; } else if (uri.filename == NULL || (strlen(uri.pathinfo) > 0 && (int) uri.is_static)) { res.status = http_get_status(404); @@ -312,7 +312,7 @@ int client_handler(sock *client, long client_num, struct sockaddr_in6 *client_ad log_req_prefix = malloc(256); log_client_prefix = malloc(256); - sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", client->enc ? HTTPS_STR : HTTP_STR, + sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR, ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET_ADDRSTRLEN, client_addr_str, ntohs(client_addr->sin6_port), CLR_STR); diff --git a/src/necronda-server.c b/src/necronda-server.c index d8f8759..f9c6aee 100644 --- a/src/necronda-server.c +++ b/src/necronda-server.c @@ -11,6 +11,7 @@ #include "utils.c" #include "uri.c" +#include "cache.c" #include "http.c" #include "client.c" @@ -238,6 +239,7 @@ int main(int argc, const char *argv[]) { signal(SIGINT, terminate); signal(SIGTERM, terminate); + cache_init(); openssl_init(); client.ctx = SSL_CTX_new(TLS_server_method()); diff --git a/src/necronda-server.h b/src/necronda-server.h index bc71265..904a78b 100644 --- a/src/necronda-server.h +++ b/src/necronda-server.h @@ -34,6 +34,7 @@ #define CLIENT_TIMEOUT 3600 #define CLIENT_MAX_HEADER_SIZE 8192 +#define FILE_CACHE_SIZE 1024 #define ERR_STR "\x1B[1;31m" #define CLR_STR "\x1B[0m" diff --git a/src/uri.c b/src/uri.c index ef68e0c..7f73ccb 100644 --- a/src/uri.c +++ b/src/uri.c @@ -35,9 +35,7 @@ int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mo uri->pathinfo = NULL; uri->query = NULL; uri->filename = NULL; - uri->filename_comp = NULL; uri->uri = NULL; - uri->etag = NULL; uri->is_static = 1; uri->is_dir = 0; if (uri_str[0] != '/') { @@ -168,7 +166,5 @@ void uri_free(http_uri *uri) { if (uri->pathinfo != NULL) free(uri->pathinfo); if (uri->query != NULL) free(uri->query); if (uri->filename != NULL) free(uri->filename); - if (uri->filename_comp != NULL) free(uri->filename_comp); - if (uri->etag != NULL) free(uri->etag); if (uri->uri != NULL) free(uri->uri); } diff --git a/src/uri.h b/src/uri.h index 88716df..b7af308 100644 --- a/src/uri.h +++ b/src/uri.h @@ -9,6 +9,7 @@ #define NECRONDA_SERVER_URI_H #include +#include "cache.h" #define URI_DIR_MODE_FORBIDDEN 0 #define URI_DIR_MODE_LIST 1 @@ -21,10 +22,8 @@ typedef struct { char *pathinfo; // "login" char *query; // "username=test" char *filename; // "/account/index.php" - char *filename_comp; // "/srv/www/www.test.org/res/.file.css.compressed" char *uri; // "/account/login?username=test" - char *etag; - struct stat stat; + meta_data meta; unsigned int is_static:1; unsigned int is_dir:1; } http_uri;