-/*
- * 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 <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
-
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
-
-
#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);
/*
* 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;
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();
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");
return;
}
-
close(fd);
http_transmit_thing(content_type, 2);
}
}
-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;
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;
}
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
}
}
- switch (d_type)
- {
+ switch (d_type) {
case DT_DIR:
/* Skip directories we are not interested in... */
if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
}
-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]);
}
-#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 <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
-
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplputParams *TP, const char *Format, ...)
{
- wcsession *WCC;
StrBuf *Error;
StrBuf *Info;
va_list arg_ptr;
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:
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,
- "<pre>\n%s: %s\n</pre>\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;
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);
-*/
}
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)
}
-
-void FreeWCTemplate(void *vFreeMe)
-{
+void FreeWCTemplate(void *vFreeMe) {
int i;
WCTemplate *FreeMe = (WCTemplate*)vFreeMe;
}
}
-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) {
}
}
-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,
-/**
- * \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));
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));
}
-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;
}
}
-/**
- * \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)
{