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"
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 wc_printf("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");
50 wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
54 content_type = GuessMimeByFilename(what, len);
56 if (fstat(fd, &statbuf) == -1) {
57 lprintf(9, "output_static('%s') -- FSTAT FAILED --\n", what);
58 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
59 hprintf("Content-Type: text/plain\r\n");
61 wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
67 bytes = statbuf.st_size;
69 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
71 if (fd > 0) close(fd);
72 lprintf(9, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
73 hprintf("HTTP/1.1 500 internal server error \r\n");
74 hprintf("Content-Type: text/plain\r\n");
82 lprintf(9, "output_static('%s') %s\n", what, content_type);
84 http_transmit_thing(content_type, 2);
86 if (yesbstr("force_close_session")) {
92 int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
94 char dirname[PATH_MAX];
95 char reldir[PATH_MAX];
96 StrBuf *FileName = NULL;
98 StrBuf *WebDir = NULL;
99 StrBuf *OneWebName = NULL;
102 struct dirent *filedir_entry;
108 filedir = opendir (DirName);
109 if (filedir == NULL) {
113 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
118 Dir = NewStrBufPlain(DirName, -1);
119 WebDir = NewStrBufPlain(RelDir, -1);
120 istoplevel = IsEmptyStr(RelDir);
121 OneWebName = NewStrBuf();
123 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
124 (filedir_entry != NULL))
127 #ifdef _DIRENT_HAVE_D_NAMELEN
128 d_namelen = filedir_entry->d_namelen;
129 d_type = filedir_entry->d_type;
138 #define IFTODT(mode) (((mode) & 0170000) >> 12)
139 #define DTTOIF(dirtype) ((dirtype) << 12)
141 d_namelen = strlen(filedir_entry->d_name);
144 d_without_ext = d_namelen;
146 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
147 continue; /* Ignore backup files... */
149 if ((d_namelen == 1) &&
150 (filedir_entry->d_name[0] == '.'))
153 if ((d_namelen == 2) &&
154 (filedir_entry->d_name[0] == '.') &&
155 (filedir_entry->d_name[1] == '.'))
158 if (d_type == DT_UNKNOWN) {
161 snprintf(path, PATH_MAX, "%s/%s",
162 DirName, filedir_entry->d_name);
163 if (stat(path, &s) == 0) {
164 d_type = IFTODT(s.st_mode);
171 /* Skip directories we are not interested in... */
172 if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
173 (strcmp(filedir_entry->d_name, "t") == 0))
175 snprintf(dirname, PATH_MAX, "%s/%s/",
176 DirName, filedir_entry->d_name);
178 snprintf(reldir, PATH_MAX, "%s/",
179 filedir_entry->d_name);
181 snprintf(reldir, PATH_MAX, "%s/%s/",
182 RelDir, filedir_entry->d_name);
183 StripSlashes(dirname, 1);
184 StripSlashes(reldir, 1);
185 LoadStaticDir(dirname, DirList, reldir);
187 case DT_LNK: /* TODO: check whether its a file or a directory */
189 PStart = filedir_entry->d_name;
190 FileName = NewStrBufDup(Dir);
191 if (ChrPtr(FileName) [ StrLength(FileName) - 1] != '/')
192 StrBufAppendBufPlain(FileName, "/", 1, 0);
193 StrBufAppendBufPlain(FileName, filedir_entry->d_name, d_namelen, 0);
195 FlushStrBuf(OneWebName);
196 StrBufAppendBuf(OneWebName, WebDir, 0);
197 if ((StrLength(OneWebName) != 0) &&
198 (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
199 StrBufAppendBufPlain(OneWebName, "/", 1, 0);
200 StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);
202 Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
203 /* lprintf(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
215 FreeStrBuf(&OneWebName);
220 void output_flat_static(void)
226 if (WCC->Hdr->HR.Handler == NULL)
228 if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
231 File = (StrBuf*) vFile;
232 output_static(ChrPtr(vFile));
236 extern void do_404(void);
238 void output_static_safe(HashList *DirList)
244 if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
247 File = (StrBuf*) vFile;
248 output_static(ChrPtr(vFile));
251 lprintf(1, "output_static_safe() file %s not found. \n",
252 ChrPtr(WCC->Hdr->HR.ReqLine));
253 ///TODO: detect image & output blank image
257 void output_static_0(void)
259 output_static_safe(StaticFilemappings[0]);
261 void output_static_1(void)
263 output_static_safe(StaticFilemappings[1]);
265 void output_static_2(void)
267 output_static_safe(StaticFilemappings[2]);
269 void output_static_3(void)
271 output_static_safe(StaticFilemappings[3]);
278 void robots_txt(void) {
279 output_headers(0, 0, 0, 0, 0, 0);
281 hprintf("Content-type: text/plain\r\n"
283 "Connection: close\r\n",
287 wc_printf("User-agent: *\r\n"
289 "Sitemap: %s/sitemap.xml\r\n"
300 ServerStartModule_STATIC
303 StaticFilemappings[0] = NewHash(1, NULL);
304 StaticFilemappings[1] = NewHash(1, NULL);
305 StaticFilemappings[2] = NewHash(1, NULL);
306 StaticFilemappings[3] = NewHash(1, NULL);
309 ServerShutdownModule_STATIC
312 DeleteHash(&StaticFilemappings[0]);
313 DeleteHash(&StaticFilemappings[1]);
314 DeleteHash(&StaticFilemappings[2]);
315 DeleteHash(&StaticFilemappings[3]);
323 LoadStaticDir(static_dirs[0], StaticFilemappings[0], "");
324 LoadStaticDir(static_dirs[1], StaticFilemappings[1], "");
325 LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
326 LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
328 WebcitAddUrlHandler(HKEY("robots.txt"), "", 0, robots_txt, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
329 WebcitAddUrlHandler(HKEY("favicon.ico"), "", 0, output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
330 WebcitAddUrlHandler(HKEY("static"), "", 0, output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
331 WebcitAddUrlHandler(HKEY("static.local"), "", 0, output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
332 WebcitAddUrlHandler(HKEY("tinymce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
333 WebcitAddUrlHandler(HKEY("tiny_mce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);