]> Nishi Git Mirror - mandshurica.git/commitdiff
add functions for file processing
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Fri, 19 Apr 2024 12:16:46 +0000 (12:16 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Fri, 19 Apr 2024 12:16:46 +0000 (12:16 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@40 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Mandshurica/Makefile
Mandshurica/config.c
Mandshurica/file.c [new file with mode: 0644]
Mandshurica/mandshurica.h
Mandshurica/ms_file.h [new file with mode: 0644]
Module/http.c
Webroot/index.html

index 2db8d5d2e915123357cbaa9bf85b6e79a75214d1..9e356ac726cd3ccf19e2cb8f118a65025c513d07 100644 (file)
@@ -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
index 86df1dce53425ebde1c07d16080f64de1cb43f99..35dd6b1f4e1371c8e0f27c069a8dd51d9b4fc837 100644 (file)
@@ -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 <dlfcn.h>
@@ -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 (file)
index 0000000..839f835
--- /dev/null
@@ -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 <stdio.h>
+
+char* mandshurica_load(const char* path, char** type, uint64_t* len) { return NULL; }
index a5953ac301689421fb3fe8a29b884a4c4a83bada..a9534bbecd341be37d7038edcf6ff9a8e89926dc 100644 (file)
@@ -33,6 +33,8 @@
 
 #include "ms_config.h"
 
+#include <stdint.h>
+
 #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 (file)
index 0000000..b286dbc
--- /dev/null
@@ -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 <stdint.h>
+
+char* mandshurica_load(const char* path, char** type, uint64_t* len);
+
+#endif
index c181056dceee17beedae95cd6d40523b7c0395ea..8da4d831d998fa6eeeb7ff8deb6376ae5f6cf762 100644 (file)
 #include "../Mandshurica/ms_util.h"
 
 #include <dlfcn.h>
+#include <limits.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stddef.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
@@ -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;
index 21f656cfa280b1d136fc6ed0abcfc33eac2bceeb..cd4c8c8203f88a9341873eb8c9aed52f9663574f 100644 (file)
@@ -1,3 +1,4 @@
+* TMPL *
 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 <html>
        <head>