From 3c1ebe35ca680369335a5440bc96785178c61a33 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Wed, 22 Dec 2021 16:46:50 -0500 Subject: [PATCH] I need to make the static web server just a static web server. I hate working in webcit classic because it's such a tangled mess. The present commit just cleans up some old cruft; there is no actual change in functionality yet. The next couple of commits will attempt to only cache the templates while using a regular file open for everything else. There's no need to cache everything else because the operating system can do it better than we can. KISS principle. Coming soon to a webcit near you. --- webcit/static.c | 148 +++++++++++++----------------------------------- webcit/subst.c | 119 ++++++++++---------------------------- 2 files changed, 70 insertions(+), 197 deletions(-) diff --git a/webcit/static.c b/webcit/static.c index 58e447047..650db18f0 100644 --- a/webcit/static.c +++ b/webcit/static.c @@ -1,75 +1,28 @@ -/* - * This is the main transaction loop of the web service. It maintains a - * persistent session to the Citadel server, handling HTTP WebCit requests as - * they arrive and presenting a user interface. - */ +// The functions in this file handle static pages and objects -- a basic web server. + #include #include #include #include - #include #include #include #include - - #include "webcit.h" #include "webserver.h" unsigned char OnePixelGif[37] = { - 0x47, - 0x49, - 0x46, - 0x38, - 0x37, - 0x61, - 0x01, - 0x00, - 0x01, - 0x00, - 0x80, - 0x00, - 0x00, - 0xff, - 0xff, - 0xff, - 0xff, - 0xff, - 0xff, - 0x2c, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x00, - 0x01, - 0x00, - 0x00, - 0x02, - 0x02, - 0x44, - 0x01, - 0x00, - 0x3b + 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x01, 0x00, + 0x01, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, + 0x01, 0x00, 0x3b }; HashList *StaticFilemappings[5] = {NULL, NULL, NULL, NULL, NULL}; -/* - { - syslog(LOG_DEBUG, "Suspicious request. Ignoring."); - hprintf("HTTP/1.1 404 Security check failed\r\n"); - hprintf("Content-Type: text/plain\r\n\r\n"); - wc_printf("You have sent a malformed or invalid request.\r\n"); - end_burst(); - } -*/ - -void output_error_pic(const char *ErrMsg1, const char *ErrMsg2) -{ +void output_error_pic(const char *ErrMsg1, const char *ErrMsg2) { hprintf("HTTP/1.1 200 %s\r\n", ErrMsg1); hprintf("Content-Type: image/gif\r\n"); hprintf("x-webcit-errormessage: %s\r\n", ErrMsg2); @@ -81,8 +34,7 @@ void output_error_pic(const char *ErrMsg1, const char *ErrMsg2) /* * dump out static pages from disk */ -void output_static(const char *what) -{ +void output_static(const char *what) { int fd; struct stat statbuf; off_t bytes; @@ -90,32 +42,30 @@ void output_static(const char *what) int len; const char *Err; + syslog(LOG_DEBUG, "output_static(%s)", what); len = strlen (what); content_type = GuessMimeByFilename(what, len); fd = open(what, O_RDONLY); if (fd <= 0) { - syslog(LOG_INFO, "output_static('%s') [%s] -- NOT FOUND --\n", what, ChrPtr(WC->Hdr->this_page)); - if (strstr(content_type, "image/") != NULL) - { + syslog(LOG_INFO, "output_static('%s') [%s] : %s", what, ChrPtr(WC->Hdr->this_page), strerror(errno)); + if (strstr(content_type, "image/") != NULL) { output_error_pic("the file you requsted is gone.", strerror(errno)); } - else - { + else { hprintf("HTTP/1.1 404 %s\r\n", strerror(errno)); hprintf("Content-Type: text/plain\r\n"); begin_burst(); wc_printf("Cannot open %s: %s\r\n", what, strerror(errno)); end_burst(); } - } else { + } + else { if (fstat(fd, &statbuf) == -1) { syslog(LOG_INFO, "output_static('%s') -- FSTAT FAILED --\n", what); - if (strstr(content_type, "image/") != NULL) - { + if (strstr(content_type, "image/") != NULL) { output_error_pic("Stat failed!", strerror(errno)); } - else - { + else { hprintf("HTTP/1.1 404 %s\r\n", strerror(errno)); hprintf("Content-Type: text/plain\r\n"); begin_burst(); @@ -128,8 +78,7 @@ void output_static(const char *what) bytes = statbuf.st_size; - if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0) - { + if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0) { if (fd > 0) close(fd); syslog(LOG_INFO, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno)); hprintf("HTTP/1.1 500 internal server error \r\n"); @@ -138,7 +87,6 @@ void output_static(const char *what) return; } - close(fd); http_transmit_thing(content_type, 2); } @@ -148,8 +96,7 @@ void output_static(const char *what) } -int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir) -{ +int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir) { char dirname[PATH_MAX]; char reldir[PATH_MAX]; StrBuf *FileName = NULL; @@ -163,20 +110,17 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir) int d_namelen; int istoplevel; - if (IsEmptyStr(DirName)) - { + if (IsEmptyStr(DirName)) { return 0; } filedir = opendir (DirName); - if (filedir == NULL) - { + if (filedir == NULL) { return 0; } d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1); - if (d == NULL) - { + if (d == NULL) { closedir(filedir); return 0; } @@ -186,12 +130,9 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir) istoplevel = IsEmptyStr(RelDir); OneWebName = NewStrBuf(); - while ((readdir_r(filedir, d, &filedir_entry) == 0) && - (filedir_entry != NULL)) - { + while ((readdir_r(filedir, d, &filedir_entry) == 0) && (filedir_entry != NULL)) { #ifdef _DIRENT_HAVE_D_NAMLEN d_namelen = filedir_entry->d_namlen; - #else d_namelen = strlen(filedir_entry->d_name); #endif @@ -233,8 +174,7 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir) } } - switch (d_type) - { + switch (d_type) { case DT_DIR: /* Skip directories we are not interested in... */ if ((strcmp(filedir_entry->d_name, ".svn") == 0) || @@ -284,61 +224,53 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir) } -void output_flat_static(void) -{ +void output_flat_static(void) { void *vFile; StrBuf *File; if (WC->Hdr->HR.Handler == NULL) return; - if (GetHash(StaticFilemappings[0], SKEY(WC->Hdr->HR.Handler->Name), &vFile) && - (vFile != NULL)) - { + if (GetHash(StaticFilemappings[0], SKEY(WC->Hdr->HR.Handler->Name), &vFile) && (vFile != NULL)) { File = (StrBuf*) vFile; output_static(ChrPtr(File)); } } -void output_static_safe(HashList *DirList) -{ +void output_static_safe(HashList *DirList) { void *vFile; StrBuf *File; const char *MimeType; - if (GetHash(DirList, SKEY(WC->Hdr->HR.ReqLine), &vFile) && - (vFile != NULL)) - { + if (GetHash(DirList, SKEY(WC->Hdr->HR.ReqLine), &vFile) && (vFile != NULL)) { File = (StrBuf*) vFile; output_static(ChrPtr(File)); } else { - syslog(LOG_INFO, "output_static_safe() file %s not found. \n", - ChrPtr(WC->Hdr->HR.ReqLine)); + syslog(LOG_INFO, "output_static_safe() %s: %s", ChrPtr(WC->Hdr->HR.ReqLine), strerror(errno)); MimeType = GuessMimeByFilename(SKEY(WC->Hdr->HR.ReqLine)); - if (strstr(MimeType, "image/") != NULL) - { + if (strstr(MimeType, "image/") != NULL) { output_error_pic("the file you requested isn't known to our cache", "maybe reload webcit?"); } - else - { + else { do_404(); } } } -void output_static_0(void) -{ + + +void output_static_0(void) { output_static_safe(StaticFilemappings[0]); } -void output_static_1(void) -{ + +void output_static_1(void) { output_static_safe(StaticFilemappings[1]); } -void output_static_2(void) -{ + +void output_static_2(void) { output_static_safe(StaticFilemappings[2]); } -void output_static_3(void) -{ + +void output_static_3(void) { output_static_safe(StaticFilemappings[4]); } diff --git a/webcit/subst.c b/webcit/subst.c index b54e921ee..a3063e6f3 100644 --- a/webcit/subst.c +++ b/webcit/subst.c @@ -1,11 +1,11 @@ -#include "sysdep.h" - +// Spaghetti, technical debt, and an unmaintainable big mess. +// No one knows how this works. This is why we started over with WebCit-NG. +#include "sysdep.h" #include #include #include #include - #include #include #include @@ -219,7 +219,6 @@ void DestroySortStruct(void *vSort) void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...) { - wcsession *WCC; StrBuf *Error; StrBuf *Info; va_list arg_ptr; @@ -258,14 +257,13 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu Type, ChrPtr(Error)); } - WCC = WC; - if (WCC == NULL) { + if (WC == NULL) { FreeStrBuf(&Info); FreeStrBuf(&Error); return; } - if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf(); + if (WC->WFBuf == NULL) WC->WFBuf = NewStrBuf(); if (TP->Tokens != NULL) { /* deprecated: @@ -286,45 +284,25 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu ChrPtr(TP->Tokens->FlatToken)); - SerializeJson(WCC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName), + SerializeJson(WC->WFBuf, WildFireException(SKEY(TP->Tokens->FileName), TP->Tokens->Line, Info, 1), 1); -/* - SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName), - TP->Tokens->Line, - Error, - eERROR), 1); -*/ - } - else - { - /* deprecated. - StrBufAppendPrintf( - Target, - "
\n%s: %s\n
\n", - Type, - ChrPtr(Error)); - */ + else { StrBufPrintf(Info, "%s [%s] %s; [%s]", Type, Err, ChrPtr(Error), ChrPtr(TP->Tokens->FlatToken)); - SerializeJson(WCC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1); + SerializeJson(WC->WFBuf, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1); } FreeStrBuf(&Info); FreeStrBuf(&Error); -/* - if (dbg_backtrace_template_errors) - wc_backtrace(LOG_DEBUG); -*/ } -void LogError (StrBuf *Target, const char *Type, const char *Format, ...) -{ - wcsession *WCC; + +void LogError (StrBuf *Target, const char *Type, const char *Format, ...) { StrBuf *Error; StrBuf *Info; va_list arg_ptr; @@ -338,20 +316,15 @@ void LogError (StrBuf *Target, const char *Type, const char *Format, ...) syslog(LOG_WARNING, "%s", ChrPtr(Error)); - WCC = WC; - if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf(); + if (WC->WFBuf == NULL) WC->WFBuf = NewStrBuf(); - SerializeJson(WCC->WFBuf, WildFireException(Type, strlen(Type), + SerializeJson(WC->WFBuf, WildFireException(Type, strlen(Type), 0, Info, 1), 1); FreeStrBuf(&Info); FreeStrBuf(&Error); -/* - if (dbg_backtrace_template_errors) - wc_backtrace(LOG_DEBUG); -*/ } @@ -402,29 +375,10 @@ int CheckContext(StrBuf *Target, ContextFilter *Need, WCTemplputParams *TP, cons ContextName(TP->Filter.ContextType)); return 0; } -/* - if (TP->Tokens->nParameters < Need->nMinArgs) { - LogTemplateError(Target, ErrType, ERR_NAME, TP, - "needs at least %ld params, have %ld", - Need->nMinArgs, - TP->Tokens->nParameters); - return 0; - - } - else if (TP->Tokens->nParameters > Need->nMaxArgs) { - LogTemplateError(Target, ErrType, ERR_NAME, TP, - "just needs %ld params, you gave %ld", - Need->nMaxArgs, - TP->Tokens->nParameters); - return 0; - - } -*/ return 1; } -void FreeToken(WCTemplateToken **Token) -{ +void FreeToken(WCTemplateToken **Token) { int i; FreeStrBuf(&(*Token)->FlatToken); if ((*Token)->HaveParameters) @@ -435,9 +389,7 @@ void FreeToken(WCTemplateToken **Token) } - -void FreeWCTemplate(void *vFreeMe) -{ +void FreeWCTemplate(void *vFreeMe) { int i; WCTemplate *FreeMe = (WCTemplate*)vFreeMe; @@ -479,12 +431,7 @@ int HaveTemplateTokenString(StrBuf *Target, } } -void GetTemplateTokenString(StrBuf *Target, - WCTemplputParams *TP, - int N, - const char **Value, - long *len) -{ +void GetTemplateTokenString(StrBuf *Target, WCTemplputParams *TP, int N, const char **Value, long *len) { StrBuf *Buf; if (N >= TP->Tokens->nParameters) { @@ -583,8 +530,7 @@ void GetTemplateTokenString(StrBuf *Target, } } -long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt) -{ +long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long dflt) { long Ret; if (N >= TP->Tokens->nParameters) { LogTemplateError(Target, @@ -1293,12 +1239,11 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf, -/** - * \brief Display a variable-substituted template - * \param templatename template file to load +/* + * Display a variable-substituted template + * templatename template file to load */ -void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere) -{ +void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere) { WCTemplate *NewTemplate; NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate)); @@ -1322,12 +1267,11 @@ void *prepare_template(StrBuf *filename, StrBuf *Key, HashList *PutThere) return NewTemplate; } -/** - * \brief Display a variable-substituted template - * \param templatename template file to load +/* + * Display a variable-substituted template + * templatename template file to load */ -void *duplicate_template(WCTemplate *OldTemplate) -{ +void *duplicate_template(WCTemplate *OldTemplate) { WCTemplate *NewTemplate; NewTemplate = (WCTemplate *) malloc(sizeof(WCTemplate)); @@ -1343,16 +1287,13 @@ void *duplicate_template(WCTemplate *OldTemplate) } -void SanityCheckTemplate(StrBuf *Target, WCTemplate *CheckMe) -{ +void SanityCheckTemplate(StrBuf *Target, WCTemplate *CheckMe) { int i = 0; int j; int FoundConditionalEnd; - for (i = 0; i < CheckMe->nTokensUsed; i++) - { - switch(CheckMe->Tokens[i]->Flags) - { + for (i = 0; i < CheckMe->nTokensUsed; i++) { + switch(CheckMe->Tokens[i]->Flags) { case SV_CONDITIONAL: case SV_NEG_CONDITIONAL: FoundConditionalEnd = 0; @@ -1388,9 +1329,9 @@ void SanityCheckTemplate(StrBuf *Target, WCTemplate *CheckMe) } } -/** - * \brief Display a variable-substituted template - * \param templatename template file to load +/* + * Display a variable-substituted template + * templatename template file to load */ void *load_template(StrBuf *Target, WCTemplate *NewTemplate) { -- 2.30.2