2 * This is the main transaction loop of the web service. It maintains a
3 * persistent session to the Citadel server, handling HTTP WebCit requests as
4 * they arrive and presenting a user interface.
18 #include "webserver.h"
20 unsigned char OnePixelGif[37] = {
59 HashList *StaticFilemappings[4] = {NULL, NULL, NULL, NULL};
62 syslog(9, "Suspicious request. Ignoring.");
63 hprintf("HTTP/1.1 404 Security check failed\r\n");
64 hprintf("Content-Type: text/plain\r\n\r\n");
65 wc_printf("You have sent a malformed or invalid request.\r\n");
71 void output_error_pic(const char *ErrMsg1, const char *ErrMsg2)
73 hprintf("HTTP/1.1 200 %s\r\n", ErrMsg1);
74 hprintf("Content-Type: image/gif\r\n");
75 hprintf("x-webcit-errormessage: %s\r\n", ErrMsg2);
77 StrBufPlain(WC->WBuf, (const char *)OnePixelGif, sizeof(OnePixelGif));
82 * dump out static pages from disk
84 void output_static(const char *what)
89 const char *content_type;
94 content_type = GuessMimeByFilename(what, len);
95 fd = open(what, O_RDONLY);
97 syslog(9, "output_static('%s') [%s] -- NOT FOUND --\n", what, ChrPtr(WC->Hdr->this_page));
98 if (strstr(content_type, "image/") != NULL)
100 output_error_pic("the file you requsted is gone.", strerror(errno));
104 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
105 hprintf("Content-Type: text/plain\r\n");
107 wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
111 if (fstat(fd, &statbuf) == -1) {
112 syslog(9, "output_static('%s') -- FSTAT FAILED --\n", what);
113 if (strstr(content_type, "image/") != NULL)
115 output_error_pic("Stat failed!", strerror(errno));
119 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
120 hprintf("Content-Type: text/plain\r\n");
122 wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
125 if (fd > 0) close(fd);
129 bytes = statbuf.st_size;
131 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
133 if (fd > 0) close(fd);
134 syslog(9, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
135 hprintf("HTTP/1.1 500 internal server error \r\n");
136 hprintf("Content-Type: text/plain\r\n");
143 http_transmit_thing(content_type, 2);
145 if (yesbstr("force_close_session")) {
146 end_webcit_session();
151 int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
153 char dirname[PATH_MAX];
154 char reldir[PATH_MAX];
155 StrBuf *FileName = NULL;
157 StrBuf *WebDir = NULL;
158 StrBuf *OneWebName = NULL;
161 struct dirent *filedir_entry;
166 filedir = opendir (DirName);
167 if (filedir == NULL) {
171 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
176 Dir = NewStrBufPlain(DirName, -1);
177 WebDir = NewStrBufPlain(RelDir, -1);
178 istoplevel = IsEmptyStr(RelDir);
179 OneWebName = NewStrBuf();
181 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
182 (filedir_entry != NULL))
184 #ifdef _DIRENT_HAVE_D_NAMELEN
185 d_namelen = filedir_entry->d_namelen;
186 d_type = filedir_entry->d_type;
195 #define IFTODT(mode) (((mode) & 0170000) >> 12)
196 #define DTTOIF(dirtype) ((dirtype) << 12)
198 d_namelen = strlen(filedir_entry->d_name);
201 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
202 continue; /* Ignore backup files... */
204 if ((d_namelen == 1) &&
205 (filedir_entry->d_name[0] == '.'))
208 if ((d_namelen == 2) &&
209 (filedir_entry->d_name[0] == '.') &&
210 (filedir_entry->d_name[1] == '.'))
213 if (d_type == DT_UNKNOWN) {
216 snprintf(path, PATH_MAX, "%s/%s",
217 DirName, filedir_entry->d_name);
218 if (stat(path, &s) == 0) {
219 d_type = IFTODT(s.st_mode);
226 /* Skip directories we are not interested in... */
227 if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
228 (strcmp(filedir_entry->d_name, "t") == 0))
230 snprintf(dirname, PATH_MAX, "%s/%s/",
231 DirName, filedir_entry->d_name);
233 snprintf(reldir, PATH_MAX, "%s/",
234 filedir_entry->d_name);
236 snprintf(reldir, PATH_MAX, "%s/%s/",
237 RelDir, filedir_entry->d_name);
238 StripSlashes(dirname, 1);
239 StripSlashes(reldir, 1);
240 LoadStaticDir(dirname, DirList, reldir);
242 case DT_LNK: /* TODO: check whether its a file or a directory */
244 FileName = NewStrBufDup(Dir);
245 if (ChrPtr(FileName) [ StrLength(FileName) - 1] != '/')
246 StrBufAppendBufPlain(FileName, "/", 1, 0);
247 StrBufAppendBufPlain(FileName, filedir_entry->d_name, d_namelen, 0);
249 FlushStrBuf(OneWebName);
250 StrBufAppendBuf(OneWebName, WebDir, 0);
251 if ((StrLength(OneWebName) != 0) &&
252 (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
253 StrBufAppendBufPlain(OneWebName, "/", 1, 0);
254 StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);
256 Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
257 /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
269 FreeStrBuf(&OneWebName);
274 void output_flat_static(void)
280 if (WCC->Hdr->HR.Handler == NULL)
282 if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
285 File = (StrBuf*) vFile;
286 output_static(ChrPtr(File));
290 void output_static_safe(HashList *DirList)
295 const char *MimeType;
297 if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
300 File = (StrBuf*) vFile;
301 output_static(ChrPtr(File));
304 syslog(1, "output_static_safe() file %s not found. \n",
305 ChrPtr(WCC->Hdr->HR.ReqLine));
306 MimeType = GuessMimeByFilename(SKEY(WCC->Hdr->HR.ReqLine));
307 if (strstr(MimeType, "image/") != NULL)
309 output_error_pic("the file you requested isn't known to our cache", "maybe reload webcit?");
317 void output_static_0(void)
319 output_static_safe(StaticFilemappings[0]);
321 void output_static_1(void)
323 output_static_safe(StaticFilemappings[1]);
325 void output_static_2(void)
327 output_static_safe(StaticFilemappings[2]);
329 void output_static_3(void)
331 output_static_safe(StaticFilemappings[3]);
338 void robots_txt(void) {
339 output_headers(0, 0, 0, 0, 0, 0);
341 hprintf("Content-type: text/plain\r\n"
343 "Connection: close\r\n",
347 wc_printf("User-agent: *\r\n"
348 "Disallow: /printmsg\r\n"
349 "Disallow: /msgheaders\r\n"
350 "Disallow: /groupdav\r\n"
351 "Disallow: /do_template\r\n"
352 "Disallow: /static\r\n"
353 "Disallow: /display_page\r\n"
354 "Disallow: /readnew\r\n"
355 "Disallow: /display_enter\r\n"
356 "Disallow: /skip\r\n"
357 "Disallow: /ungoto\r\n"
358 "Sitemap: %s/sitemap.xml\r\n"
369 ServerStartModule_STATIC
372 StaticFilemappings[0] = NewHash(1, NULL);
373 StaticFilemappings[1] = NewHash(1, NULL);
374 StaticFilemappings[2] = NewHash(1, NULL);
375 StaticFilemappings[3] = NewHash(1, NULL);
378 ServerShutdownModule_STATIC
381 DeleteHash(&StaticFilemappings[0]);
382 DeleteHash(&StaticFilemappings[1]);
383 DeleteHash(&StaticFilemappings[2]);
384 DeleteHash(&StaticFilemappings[3]);
392 LoadStaticDir(static_dirs[0], StaticFilemappings[0], "");
393 LoadStaticDir(static_dirs[1], StaticFilemappings[1], "");
394 LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
395 LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
397 WebcitAddUrlHandler(HKEY("robots.txt"), "", 0, robots_txt, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
398 WebcitAddUrlHandler(HKEY("favicon.ico"), "", 0, output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
399 WebcitAddUrlHandler(HKEY("static"), "", 0, output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
400 WebcitAddUrlHandler(HKEY("static.local"), "", 0, output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
401 WebcitAddUrlHandler(HKEY("tinymce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
402 WebcitAddUrlHandler(HKEY("tiny_mce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);