From eb7686249433896df0455e0fef18522572793664 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 20 Apr 2024 08:52:46 +0000 Subject: [PATCH] works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@54 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Module/http.c | 86 +++++++++++++++++++++++++-- Webroot/bar.html | 2 +- Webroot/login-success/index.html.tmpl | 10 ++++ 3 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 Webroot/login-success/index.html.tmpl diff --git a/Module/http.c b/Module/http.c index 9550b6c..860f13a 100644 --- a/Module/http.c +++ b/Module/http.c @@ -199,6 +199,59 @@ void http_handler(int sock) { if(usr != NULL && pwd != NULL) { int res = mod_auth(usr, pwd); if(res == MS_AUTH_OK) { + char* type; + uint64_t len; + char* data = NULL; + char* s; + + struct stat st; + char* lastmod = NULL; + const char* pth = mandshurica_strcat(webroot, "/login-success/"); + 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, &lastmod, &len); + free(s); + } + if(data == NULL) { + s = mandshurica_strcat(pth, "/index.html"); + data = config->mandshurica_load(s, &type, &lastmod, &len); + free(s); + } + } else { + data = config->mandshurica_load(pth, &type, &lastmod, &len); + } + } + if(data != NULL) { + send(sock, "HTTP/1.1 200 OK\r\n", 9 + 8, 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); + 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); + 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 200 OK\r\n", 9 + 8, 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 if(res == MS_AUTH_ERROR) { send(sock, "HTTP/1.1 400 Bad Request\r\n", 17 + 9, 0); send(sock, "Connection: keep-alive\r\n", 24, 0); @@ -229,10 +282,35 @@ void http_handler(int sock) { data = config->mandshurica_load(pth, &type, &lastmod, &len); } } - send(sock, "HTTP/1.1 403 Forbidden\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); + if(data != NULL) { + send(sock, "HTTP/1.1 403 Forbidden\r\n", 24, 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); + 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); + 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 403 Forbidden\r\n", 24, 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 400 Bad Request\r\n", 17 + 9, 0); diff --git a/Webroot/bar.html b/Webroot/bar.html index 08c3f66..cadeedb 100644 --- a/Webroot/bar.html +++ b/Webroot/bar.html @@ -1,3 +1,3 @@
-Home @ifnot login@Login@endif@ +Home@ifnot login@ Login@endif@@if login@ Logout@endif@
diff --git a/Webroot/login-success/index.html.tmpl b/Webroot/login-success/index.html.tmpl new file mode 100644 index 0000000..7da3637 --- /dev/null +++ b/Webroot/login-success/index.html.tmpl @@ -0,0 +1,10 @@ + +@include head-start.html@ +Mandshurica - Login successful +@include body-start.html@ +

Mandshurica - Login successful

+@include bar.html@ +Welcome back +@include end.html@ -- 2.43.0