2 * $Id: webcit.c 7459 2009-05-17 08:34:33Z dothebart $
4 * This is the main transaction loop of the web service. It maintains a
5 * persistent session to the Citadel server, handling HTTP WebCit requests as
6 * they arrive and presenting a user interface.
18 #include "webserver.h"
21 HashList *StaticFilemappings[4] = {NULL, NULL, NULL, NULL};
24 lprintf(9, "Suspicious request. Ignoring.");
25 hprintf("HTTP/1.1 404 Security check failed\r\n");
26 hprintf("Content-Type: text/plain\r\n\r\n");
27 wprintf("You have sent a malformed or invalid request.\r\n");
32 * dump out static pages from disk
34 void output_static(const char *what)
40 const char *content_type;
44 fd = open(what, O_RDONLY);
46 lprintf(9, "output_static('%s') [%s] -- NOT FOUND --\n", what, ChrPtr(WC->Hdr->this_page));
47 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
48 hprintf("Content-Type: text/plain\r\n");
49 wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
53 content_type = GuessMimeByFilename(what, len);
55 if (fstat(fd, &statbuf) == -1) {
56 lprintf(9, "output_static('%s') -- FSTAT FAILED --\n", what);
57 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
58 hprintf("Content-Type: text/plain\r\n");
59 wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
65 bytes = statbuf.st_size;
67 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
69 if (fd > 0) close(fd);
70 lprintf(9, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
71 hprintf("HTTP/1.1 500 internal server error \r\n");
72 hprintf("Content-Type: text/plain\r\n");
80 lprintf(9, "output_static('%s') %s\n", what, content_type);
82 http_transmit_thing(content_type, 2);
84 if (yesbstr("force_close_session")) {
90 int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
92 char dirname[PATH_MAX];
93 char reldir[PATH_MAX];
94 StrBuf *FileName = NULL;
96 StrBuf *WebDir = NULL;
97 StrBuf *OneWebName = NULL;
100 struct dirent *filedir_entry;
105 filedir = opendir (DirName);
106 if (filedir == NULL) {
110 Dir = NewStrBufPlain(DirName, -1);
111 WebDir = NewStrBufPlain(RelDir, -1);
112 istoplevel = IsEmptyStr(RelDir);
113 OneWebName = NewStrBuf();
115 while ((readdir_r(filedir, &d, &filedir_entry) == 0) &&
116 (filedir_entry != NULL))
119 #ifdef _DIRENT_HAVE_D_NAMELEN
120 d_namelen = filedir_entry->d_namelen;
122 d_namelen = strlen(filedir_entry->d_name);
124 d_without_ext = d_namelen;
126 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
127 continue; /* Ignore backup files... */
129 if ((d_namelen == 1) &&
130 (filedir_entry->d_name[0] == '.'))
133 if ((d_namelen == 2) &&
134 (filedir_entry->d_name[0] == '.') &&
135 (filedir_entry->d_name[1] == '.'))
138 switch (filedir_entry->d_type)
141 /* Skip directories we are not interested in... */
142 if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
143 (strcmp(filedir_entry->d_name, "t") == 0))
145 snprintf(dirname, PATH_MAX, "%s/%s/",
146 DirName, filedir_entry->d_name);
148 snprintf(reldir, PATH_MAX, "%s/",
149 filedir_entry->d_name);
151 snprintf(reldir, PATH_MAX, "%s/%s/",
152 RelDir, filedir_entry->d_name);
153 StripSlashes(dirname, 1);
154 StripSlashes(reldir, 1);
155 LoadStaticDir(dirname, DirList, reldir);
157 case DT_LNK: /* TODO: check whether its a file or a directory */
159 PStart = filedir_entry->d_name;
160 FileName = NewStrBufDup(Dir);
161 if (ChrPtr(FileName) [ StrLength(FileName) - 1] != '/')
162 StrBufAppendBufPlain(FileName, "/", 1, 0);
163 StrBufAppendBufPlain(FileName, filedir_entry->d_name, d_namelen, 0);
165 FlushStrBuf(OneWebName);
166 StrBufAppendBuf(OneWebName, WebDir, 0);
167 if ((StrLength(OneWebName) != 0) &&
168 (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
169 StrBufAppendBufPlain(OneWebName, "/", 1, 0);
170 StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);
172 Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
173 /* printf("[%s | %s] \n", ChrPtr(OneWebName), ChrPtr(FileName));*/
184 FreeStrBuf(&OneWebName);
189 void output_flat_static(void)
195 if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
198 File = (StrBuf*) vFile;
199 output_static(ChrPtr(vFile));
203 extern void do_404(void);
205 void output_static_safe(HashList *DirList)
211 if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
214 File = (StrBuf*) vFile;
215 output_static(ChrPtr(vFile));
218 lprintf(1, "output_static_safe() file %s not found. \n",
219 ChrPtr(WCC->Hdr->HR.ReqLine));
220 ///TODO: detect image & output blank image
224 void output_static_0(void)
226 output_static_safe(StaticFilemappings[0]);
228 void output_static_1(void)
230 output_static_safe(StaticFilemappings[1]);
232 void output_static_2(void)
234 output_static_safe(StaticFilemappings[2]);
236 void output_static_3(void)
238 output_static_safe(StaticFilemappings[3]);
242 ServerStartModule_STATIC
245 StaticFilemappings[0] = NewHash(1, NULL);
246 StaticFilemappings[1] = NewHash(1, NULL);
247 StaticFilemappings[2] = NewHash(1, NULL);
248 StaticFilemappings[3] = NewHash(1, NULL);
251 ServerShutdownModule_STATIC
254 DeleteHash(&StaticFilemappings[0]);
255 DeleteHash(&StaticFilemappings[1]);
256 DeleteHash(&StaticFilemappings[2]);
257 DeleteHash(&StaticFilemappings[3]);
265 LoadStaticDir(static_dirs[0], StaticFilemappings[0], "");
266 LoadStaticDir(static_dirs[1], StaticFilemappings[1], "");
267 LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
268 LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
270 WebcitAddUrlHandler(HKEY("robots.txt"), output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
271 WebcitAddUrlHandler(HKEY("favicon.ico"), output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
272 WebcitAddUrlHandler(HKEY("static"), output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
273 WebcitAddUrlHandler(HKEY("static.local"), output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
274 WebcitAddUrlHandler(HKEY("tinymce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
275 WebcitAddUrlHandler(HKEY("tiny_mce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);