#include "ms_file.h"
+#include "ms_template.h"
+#include "ms_util.h"
+
+#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+char* get_mime(const char* _path) {
+ char* path = mandshurica_strdup(_path);
+ if(strlen(path) > 4 && memcmp(path + strlen(path) - 5, ".tmpl", 5) == 0) {
+ path[strlen(path) - 5] = 0;
+ }
+ int i;
+ for(i = strlen(path) - 1; i >= 0; i--) {
+ if(path[i] == '.') {
+ char* ext = path + i + 1;
+ char* typ = mandshurica_strdup("application/octet-stream");
+ if(strcmp(ext, "gif") == 0) {
+ free(typ);
+ typ = mandshurica_strdup("image/gif");
+ } else if(strcmp(ext, "html") == 0) {
+ free(typ);
+ typ = mandshurica_strdup("text/html");
+ }
+ free(path);
+ return typ;
+ break;
+ }
+ }
+ free(path);
+ return mandshurica_strdup("application/octet-stream");
+}
-char* mandshurica_load(const char* path, char** type, uint64_t* len) { return NULL; }
+char* mandshurica_load(const char* path, char** type, uint64_t* len) {
+ bool tmpl = false;
+ if(strlen(path) > 4 && memcmp(path + strlen(path) - 5, ".tmpl", 5) == 0) {
+ tmpl = true;
+ }
+ FILE* f = fopen(path, "rb");
+ if(f != NULL) {
+ struct stat s;
+ if(stat(path, &s) == 0) {
+ char* fdata = malloc(s.st_size + 1);
+ fread(fdata, s.st_size, 1, f);
+ fdata[s.st_size] = 0;
+ *type = get_mime(path);
+ if(tmpl) {
+ char* data = mandshurica_parse_template(fdata);
+ *len = strlen(data);
+ fclose(f);
+ return data;
+ }
+ *len = s.st_size;
+ return fdata;
+ }
+ fclose(f);
+ }
+ return NULL;
+}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
if(count == 2) {
if(strcmp(method, "GET") == 0) {
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;
+ struct stat st2;
+ if(stat(chpath, &st2) != 0) {
+ 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);
+ } else {
+ char* pth = mandshurica_path(chpath);
+ free(chpath);
+
+ bool under = false;
+ char* pth2 = mandshurica_strcat(pth, "/");
+ if(strcmp(pth, webroot) == 0) {
+ under = true;
+ } else if(strcmp(pth2, 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);
+ free(pth2);
+
+ if(under) {
+ char* type;
+ uint64_t len;
+ char* data = NULL;
+ char* s;
+
+ struct stat st;
+ 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);
+ free(s);
+ }
+ if(data == NULL) {
+ s = mandshurica_strcat(pth, "/index.html");
+ data = config->mandshurica_load(s, &type, &len);
+ free(s);
+ }
+ } else {
+ data = config->mandshurica_load(pth, &type, &len);
+ }
- send(sock, "\r\n", 2, 0);
- send(sock, "\r\n", 2, 0);
- send(sock, data, len, 0);
- free(type);
+ 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, "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);
+ }
+ } else {
+ send(sock, "HTTP/1.1 500 Internal Server Error\r\n", 15 + 9 + 12, 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);
+ }
} else {
- send(sock, "HTTP/1.1 404 Not Found\r\n", 15 + 9, 0);
+ 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);
}
-
- 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);
+ free(pth);
}
goto reset;