From dc4dc0e87708b1e754bc706d30211da998f65dbc Mon Sep 17 00:00:00 2001 From: nishi Date: Fri, 19 Apr 2024 12:16:46 +0000 Subject: [PATCH] add functions for file processing git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@40 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Mandshurica/Makefile | 2 +- Mandshurica/config.c | 4 ++- Mandshurica/file.c | 35 +++++++++++++++++++ Mandshurica/mandshurica.h | 4 +++ Mandshurica/ms_file.h | 38 +++++++++++++++++++++ Module/http.c | 72 ++++++++++++++++++++++++++++++++++++++- Webroot/index.html | 1 + 7 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 Mandshurica/file.c create mode 100644 Mandshurica/ms_file.h diff --git a/Mandshurica/Makefile b/Mandshurica/Makefile index 2db8d5d..9e356ac 100644 --- a/Mandshurica/Makefile +++ b/Mandshurica/Makefile @@ -12,7 +12,7 @@ endif all: ./mandshurica -./mandshurica: ./main.o ./log.o ./config.o ./util.o ./template.o +./mandshurica: ./main.o ./log.o ./config.o ./util.o ./template.o ./file.o $(CC) $(LDFLAGS) $(EXTRA_LDFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS) ./main.o: ./main.c ./mandshurica.h diff --git a/Mandshurica/config.c b/Mandshurica/config.c index 86df1dc..35dd6b1 100644 --- a/Mandshurica/config.c +++ b/Mandshurica/config.c @@ -32,7 +32,9 @@ #include "ms_config.h" #include "mandshurica.h" +#include "ms_file.h" #include "ms_log.h" +#include "ms_template.h" #include "ms_util.h" #include @@ -86,7 +88,7 @@ char* mandshurica_get_param(const char* param) { return NULL; } -struct mandshurica_config config = {.mandshurica_log = mandshurica_log, .mandshurica_get_param = mandshurica_get_param}; +struct mandshurica_config config = {.mandshurica_log = mandshurica_log, .mandshurica_get_param = mandshurica_get_param, .mandshurica_parse_template = mandshurica_parse_template, .mandshurica_load = mandshurica_load}; int mandshurica_load_config(const char* path) { if(server_root == NULL) { diff --git a/Mandshurica/file.c b/Mandshurica/file.c new file mode 100644 index 0000000..839f835 --- /dev/null +++ b/Mandshurica/file.c @@ -0,0 +1,35 @@ +/* $Id$ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* Mandshurica - Build Automation */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Nishi. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are without specific prior written permission. */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */ +/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#include "ms_file.h" + +#include + +char* mandshurica_load(const char* path, char** type, uint64_t* len) { return NULL; } diff --git a/Mandshurica/mandshurica.h b/Mandshurica/mandshurica.h index a5953ac..a9534bb 100644 --- a/Mandshurica/mandshurica.h +++ b/Mandshurica/mandshurica.h @@ -33,6 +33,8 @@ #include "ms_config.h" +#include + #define MANDSHURICA_VERSION "0.0" #define MS_MOD_LOG "LOG" @@ -43,6 +45,8 @@ 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); struct mandshurica_mod*** libs; }; diff --git a/Mandshurica/ms_file.h b/Mandshurica/ms_file.h new file mode 100644 index 0000000..b286dbc --- /dev/null +++ b/Mandshurica/ms_file.h @@ -0,0 +1,38 @@ +/* $Id$ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* Mandshurica - Build Automation */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Nishi. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are without specific prior written permission. */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */ +/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#ifndef __MANDSHURICA_MS_FILE_H__ +#define __MANDSHURICA_MS_FILE_H__ + +#include + +char* mandshurica_load(const char* path, char** type, uint64_t* len); + +#endif diff --git a/Module/http.c b/Module/http.c index c181056..8da4d83 100644 --- a/Module/http.c +++ b/Module/http.c @@ -34,9 +34,11 @@ #include "../Mandshurica/ms_util.h" #include +#include #include #include #include +#include #include #include #include @@ -111,6 +113,12 @@ int mod_init(struct mandshurica_config* _config) { void http_handler(int sock) { char* root = config->mandshurica_get_param("HTTPRoot"); if(root == NULL) root = WEBROOT_PREFIX; + char* webroot; + if(root[strlen(root) - 1] == '/') { + webroot = mandshurica_strdup(root); + } else { + webroot = mandshurica_strcat(root, "/"); + } char* buf = malloc(BUFFER_SIZE); char* method = NULL; @@ -176,9 +184,70 @@ void http_handler(int sock) { count++; if(count == 2) { if(strcmp(method, "GET") == 0) { - char* chpath = mandshurica_strcat3(root, "/", path); + char* chpath = mandshurica_strcat(webroot, path); chdir(chpath); free(chpath); + + char pth[PATH_MAX + 1]; + getcwd(pth, PATH_MAX); + + bool under = false; + if(pth[strlen(pth) - 1] != '/') { + pth[strlen(pth) + 1] = 0; + pth[strlen(pth)] = '/'; + } + if(strcmp(pth, webroot) == 0) { + under = true; + } else if(strlen(pth) >= strlen(webroot)) { + int j; + under = true; + for(j = 0; webroot[j] != 0; j++) { + if(webroot[j] != pth[j]) { + under = false; + break; + } + } + } + + if(under) { + char* s = mandshurica_strcat(pth, "/index.html"); + + char* type; + uint64_t len; + char* data = config->mandshurica_load(s, &type, &len); + if(data != NULL) { + send(sock, "HTTP/1.1 200 OK\r\n", 8 + 9, 0); + send(sock, "Connection: keep-alive\r\n", 24, 0); + send(sock, "\r\n", 2, 0); + send(sock, "Content-Type: ", 14, 0); + send(sock, type, strlen(type), 0); + send(sock, "\r\n", 2, 0); + send(sock, "Content-Length: ", 16, 0); + + char* lenstr = malloc(513); + sprintf(lenstr, "%llu", len); + send(sock, lenstr, strlen(lenstr), 0); + free(lenstr); + + send(sock, "\r\n", 2, 0); + send(sock, "\r\n", 2, 0); + send(sock, data, len, 0); + free(type); + } else { + send(sock, "HTTP/1.1 404 Not Found\r\n", 15 + 9, 0); + send(sock, "Connection: keep-alive\r\n", 24, 0); + send(sock, "Content-Length: 0\r\n", 19, 0); + send(sock, "\r\n", 2, 0); + } + + free(s); + } else { + send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0); + send(sock, "Connection: keep-alive\r\n", 24, 0); + send(sock, "Content-Length: 0\r\n", 19, 0); + send(sock, "\r\n", 2, 0); + } + goto reset; } state = 0; @@ -229,6 +298,7 @@ void http_handler(int sock) { } } goquit:; + free(webroot); if(method != NULL) { free(method); method = NULL; diff --git a/Webroot/index.html b/Webroot/index.html index 21f656c..cd4c8c8 100644 --- a/Webroot/index.html +++ b/Webroot/index.html @@ -1,3 +1,4 @@ +* TMPL * -- 2.43.0