Added magic file type detection

This commit is contained in:
2020-12-19 22:09:03 +01:00
parent f6ac55adaf
commit f0338209f5
8 changed files with 52 additions and 10 deletions

View File

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

View File

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

View File

@ -8,4 +8,25 @@
#ifndef NECRONDA_SERVER_CACHE_H
#define NECRONDA_SERVER_CACHE_H
#include <magic.h>
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

View File

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

View File

@ -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());

View File

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

View File

@ -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);
}

View File

@ -9,6 +9,7 @@
#define NECRONDA_SERVER_URI_H
#include <sys/stat.h>
#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;