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
#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>
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) {
--- /dev/null
+/* $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; }
#include "ms_config.h"
+#include <stdint.h>
+
#define MANDSHURICA_VERSION "0.0"
#define MS_MOD_LOG "LOG"
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;
};
--- /dev/null
+/* $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
#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>
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;
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;
}
}
goquit:;
+ free(webroot);
if(method != NULL) {
free(method);
method = NULL;
+* TMPL *
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>