From ac2d3abf3b913a4befd969765220e1af7b99009b Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 20 Apr 2024 00:58:35 +0000 Subject: [PATCH] cache works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@47 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Mandshurica/crypto.c | 2 +- Mandshurica/file.c | 8 +++++++- Mandshurica/mandshurica.h | 2 +- Mandshurica/ms_file.h | 2 +- Module/http.c | 13 ++++++++++--- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Mandshurica/crypto.c b/Mandshurica/crypto.c index 8a6a092..75d9866 100644 --- a/Mandshurica/crypto.c +++ b/Mandshurica/crypto.c @@ -31,6 +31,7 @@ #include "ms_crypto.h" #include +#include #include @@ -52,6 +53,5 @@ char* mandshurica_sha512(const char* pwd) { buf[i * 2 + 1] = num2hex((buffer[i] & 0x0f) >> 0); } buf[128] = 0; - printf("%s\n", buf); return buf; } diff --git a/Mandshurica/file.c b/Mandshurica/file.c index 9a21d1f..28ae8f5 100644 --- a/Mandshurica/file.c +++ b/Mandshurica/file.c @@ -39,6 +39,7 @@ #include #include #include +#include char* get_mime(const char* _path) { char* path = mandshurica_strdup(_path); @@ -66,8 +67,9 @@ char* get_mime(const char* _path) { return mandshurica_strdup("application/octet-stream"); } -char* mandshurica_load(const char* path, char** type, uint64_t* len) { +char* mandshurica_load(const char* path, char** type, char** lastmod, uint64_t* len) { bool tmpl = false; + *lastmod = NULL; if(strlen(path) > 4 && memcmp(path + strlen(path) - 5, ".tmpl", 5) == 0) { tmpl = true; } @@ -84,6 +86,10 @@ char* mandshurica_load(const char* path, char** type, uint64_t* len) { *len = strlen(data); fclose(f); return data; + } else { + struct tm* tm = gmtime(&s.st_mtime); + *lastmod = malloc(513); + strftime(*lastmod, 512, "%a, %d %b %Y %H:%M:%S GMT", tm); } *len = s.st_size; return fdata; diff --git a/Mandshurica/mandshurica.h b/Mandshurica/mandshurica.h index a9534bb..9db8df4 100644 --- a/Mandshurica/mandshurica.h +++ b/Mandshurica/mandshurica.h @@ -46,7 +46,7 @@ struct mandshurica_config { void (*mandshurica_log)(const char*, const char*); char* (*mandshurica_get_param)(const char* param); char* (*mandshurica_parse_template)(const char* data); - char* (*mandshurica_load)(const char* path, char** type, uint64_t* len); + char* (*mandshurica_load)(const char* path, char** type, char** lastmod, uint64_t* len); struct mandshurica_mod*** libs; }; diff --git a/Mandshurica/ms_file.h b/Mandshurica/ms_file.h index b286dbc..66e0fc7 100644 --- a/Mandshurica/ms_file.h +++ b/Mandshurica/ms_file.h @@ -33,6 +33,6 @@ #include -char* mandshurica_load(const char* path, char** type, uint64_t* len); +char* mandshurica_load(const char* path, char** type, char** lastmod, uint64_t* len); #endif diff --git a/Module/http.c b/Module/http.c index a7188b5..f8d2413 100644 --- a/Module/http.c +++ b/Module/http.c @@ -222,20 +222,21 @@ void http_handler(int sock) { char* s; struct stat st; + char* lastmod = NULL; if(stat(pth, &st) == 0) { if(S_ISDIR(st.st_mode)) { if(data == NULL) { s = mandshurica_strcat(pth, "/index.html.tmpl"); - data = config->mandshurica_load(s, &type, &len); + data = config->mandshurica_load(s, &type, &lastmod, &len); free(s); } if(data == NULL) { s = mandshurica_strcat(pth, "/index.html"); - data = config->mandshurica_load(s, &type, &len); + data = config->mandshurica_load(s, &type, &lastmod, &len); free(s); } } else { - data = config->mandshurica_load(pth, &type, &len); + data = config->mandshurica_load(pth, &type, &lastmod, &len); } if(data != NULL) { @@ -244,6 +245,12 @@ void http_handler(int sock) { send(sock, "Content-Type: ", 14, 0); send(sock, type, strlen(type), 0); send(sock, "\r\n", 2, 0); + if(lastmod != NULL) { + send(sock, "Last-Modified: ", 15, 0); + send(sock, lastmod, strlen(lastmod), 0); + send(sock, "\r\n", 2, 0); + free(lastmod); + } send(sock, "Content-Length: ", 16, 0); char* lenstr = malloc(513); -- 2.43.0