From aafd9bb61f398cf8bff33b9350a8cd769bfb6de0 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 8 May 2024 15:57:14 +0000 Subject: [PATCH] login system works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@66 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Module/http.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/Module/http.c b/Module/http.c index e9b9f06..bb3e991 100644 --- a/Module/http.c +++ b/Module/http.c @@ -191,6 +191,7 @@ void http_handler(int sock) { } unsigned long long conlen = 0; unsigned long long conincr = 0; + bool nolen = false; char* postbuf = NULL; while(true) { int len = recv(sock, buf, BUFFER_SIZE, 0); @@ -198,11 +199,12 @@ void http_handler(int sock) { int i; for(i = 0; i < len; i++) { cbuf[0] = buf[i]; - if(conlen > 0) { +force_back:; + if(conlen > 0 || nolen) { conlen--; postbuf[conincr] = cbuf[0]; conincr++; - if(conlen == 0) { + if(conlen == 0 || nolen) { if(headers != NULL) { int j; bool hastype = false; @@ -238,6 +240,7 @@ void http_handler(int sock) { free(token); } if(did_logout){ + param.login = false; char* type; uint64_t len; char* data = NULL; @@ -281,6 +284,63 @@ void http_handler(int sock) { send(sock, lenstr, strlen(lenstr), 0); free(lenstr); + send(sock, "\r\n", 2, 0); + send(sock, "Set-Cookie: TOKEN=; HttpOnly; MaxAge=0\r\n", 40, 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, "Set-Cookie: TOKEN=; HttpOnly; MaxAge=0\r\n", 40, 0); + send(sock, "\r\n", 2, 0); + send(sock, "\r\n", 2, 0); + } + }else{ + char* type; + uint64_t len; + char* data = NULL; + char* s; + + struct stat st; + char* lastmod = NULL; + const char* pth = mandshurica_strcat(webroot, "/logout-fail/"); + 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, param); + free(s); + } + if(data == NULL) { + s = mandshurica_strcat(pth, "/index.html"); + data = config->mandshurica_load(s, &type, &lastmod, &len, param); + free(s); + } + } else { + data = config->mandshurica_load(pth, &type, &lastmod, &len, param); + } + } + 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); @@ -309,6 +369,7 @@ void http_handler(int sock) { char* token = mandshurica_strdup(httppath); int res = mod_auth(usr, pwd, &token); if(res == MS_AUTH_OK) { + param.login = true; char* type; uint64_t len; char* data = NULL; @@ -515,6 +576,7 @@ void http_handler(int sock) { if(headers != NULL) { int j; bool haslen = false; + nolen = false; for(j = 0; headers[j] != NULL; j += 2) { if(mandshurica_strcaseequ(headers[j], "Content-Length")) { haslen = true; @@ -523,6 +585,11 @@ void http_handler(int sock) { postbuf = malloc(conlen + 1); postbuf[conlen] = 0; count = 0; + if(conlen == 0) { + nolen = true; + cbuf[0] = 0; + goto force_back; + } break; } } -- 2.43.0