* add logging if auth-basic session connecting fails with weird conditions
[citadel.git] / webcit / static.c
1 /*
2  * $Id: webcit.c 7459 2009-05-17 08:34:33Z dothebart $
3  *
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.
7  */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <dirent.h>
11 #include <errno.h>
12
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <stdarg.h>
16
17 #include "webcit.h"
18 #include "webserver.h"
19
20
21 HashList *StaticFilemappings[4] = {NULL, NULL, NULL, NULL};
22 /*
23                 {
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");
28                         end_burst();
29                 }
30 */
31 /*
32  * dump out static pages from disk
33  */
34 void output_static(const char *what)
35 {
36         int fd;
37         struct stat statbuf;
38         off_t bytes;
39         off_t count = 0;
40         const char *content_type;
41         int len;
42         const char *Err;
43
44         fd = open(what, O_RDONLY);
45         if (fd <= 0) {
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                 begin_burst();
50                 wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
51                 end_burst();
52         } else {
53                 len = strlen (what);
54                 content_type = GuessMimeByFilename(what, len);
55
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");
60                         begin_burst();
61                         wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
62                         end_burst();
63                         return;
64                 }
65
66                 count = 0;
67                 bytes = statbuf.st_size;
68
69                 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
70                 {
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");
75                                 end_burst();
76                                 return;
77                 }
78
79
80                 close(fd);
81 #ifndef TECH_PREVIEW
82                 lprintf(9, "output_static('%s')  %s\n", what, content_type);
83 #endif
84                 http_transmit_thing(content_type, 2);
85         }
86         if (yesbstr("force_close_session")) {
87                 end_webcit_session();
88         }
89 }
90
91
92 int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
93 {
94         char dirname[PATH_MAX];
95         char reldir[PATH_MAX];
96         StrBuf *FileName = NULL;
97         StrBuf *Dir = NULL;
98         StrBuf *WebDir = NULL;
99         StrBuf *OneWebName = NULL;
100         DIR *filedir = NULL;
101         struct dirent d;
102         struct dirent *filedir_entry;
103         int d_namelen;
104         int d_without_ext;
105         int istoplevel;
106                 
107         filedir = opendir (DirName);
108         if (filedir == NULL) {
109                 return 0;
110         }
111
112         Dir = NewStrBufPlain(DirName, -1);
113         WebDir = NewStrBufPlain(RelDir, -1);
114         istoplevel = IsEmptyStr(RelDir);
115         OneWebName = NewStrBuf();
116
117         while ((readdir_r(filedir, &d, &filedir_entry) == 0) &&
118                (filedir_entry != NULL))
119         {
120                 char *PStart;
121 #ifdef _DIRENT_HAVE_D_NAMELEN
122                 d_namelen = filedir_entry->d_namelen;
123 #else
124                 d_namelen = strlen(filedir_entry->d_name);
125 #endif
126                 d_without_ext = d_namelen;
127
128                 if (filedir_entry->d_type == DT_UNKNOWN) {
129                         struct stat s;
130                         char path[PATH_MAX];
131                         snprintf(path, PATH_MAX, "%s/%s", 
132                                 DirName, filedir_entry->d_name);
133                         if (stat(path, &s) == 0) {
134                                 filedir_entry->d_type = IFTODT(s.st_mode);
135                         }
136                 }
137
138                 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
139                         continue; /* Ignore backup files... */
140
141                 if ((d_namelen == 1) && 
142                     (filedir_entry->d_name[0] == '.'))
143                         continue;
144
145                 if ((d_namelen == 2) && 
146                     (filedir_entry->d_name[0] == '.') &&
147                     (filedir_entry->d_name[1] == '.'))
148                         continue;
149
150                 switch (filedir_entry->d_type)
151                 {
152                 case DT_DIR:
153                         /* Skip directories we are not interested in... */
154                         if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
155                             (strcmp(filedir_entry->d_name, "t") == 0))
156                                 break;
157                         snprintf(dirname, PATH_MAX, "%s/%s/", 
158                                  DirName, filedir_entry->d_name);
159                         if (istoplevel)
160                                 snprintf(reldir, PATH_MAX, "%s/", 
161                                          filedir_entry->d_name);
162                         else
163                                 snprintf(reldir, PATH_MAX, "%s/%s/", 
164                                          RelDir, filedir_entry->d_name);
165                         StripSlashes(dirname, 1);
166                         StripSlashes(reldir, 1);
167                         LoadStaticDir(dirname, DirList, reldir);                                 
168                         break;
169                 case DT_LNK: /* TODO: check whether its a file or a directory */
170                 case DT_REG:
171                         PStart = filedir_entry->d_name;
172                         FileName = NewStrBufDup(Dir);
173                         if (ChrPtr(FileName) [ StrLength(FileName) - 1] != '/')
174                                 StrBufAppendBufPlain(FileName, "/", 1, 0);
175                         StrBufAppendBufPlain(FileName, filedir_entry->d_name, d_namelen, 0);
176
177                         FlushStrBuf(OneWebName);
178                         StrBufAppendBuf(OneWebName, WebDir, 0);
179                         if ((StrLength(OneWebName) != 0) && 
180                             (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
181                                 StrBufAppendBufPlain(OneWebName, "/", 1, 0);
182                         StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);
183
184                         Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
185                         /* lprintf(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
186                         break;
187                 default:
188                         break;
189                 }
190
191
192         }
193         closedir(filedir);
194         FreeStrBuf(&Dir);
195         FreeStrBuf(&WebDir);
196         FreeStrBuf(&OneWebName);
197         return 1;
198 }
199
200
201 void output_flat_static(void)
202 {
203         wcsession *WCC = WC;
204         void *vFile;
205         StrBuf *File;
206
207         if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
208             (vFile != NULL))
209         {
210                 File = (StrBuf*) vFile;
211                 output_static(ChrPtr(vFile));
212         }
213 }
214
215 extern void do_404(void);
216
217 void output_static_safe(HashList *DirList)
218 {
219         wcsession *WCC = WC;
220         void *vFile;
221         StrBuf *File;
222
223         if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
224             (vFile != NULL))
225         {
226                 File = (StrBuf*) vFile;
227                 output_static(ChrPtr(vFile));
228         }
229         else {
230                 lprintf(1, "output_static_safe() file %s not found. \n", 
231                         ChrPtr(WCC->Hdr->HR.ReqLine));
232 ///TODO: detect image & output blank image
233                 do_404();
234         }
235 }
236 void output_static_0(void)
237 {
238         output_static_safe(StaticFilemappings[0]);
239 }
240 void output_static_1(void)
241 {
242         output_static_safe(StaticFilemappings[1]);
243 }
244 void output_static_2(void)
245 {
246         output_static_safe(StaticFilemappings[2]);
247 }
248 void output_static_3(void)
249 {
250         output_static_safe(StaticFilemappings[3]);
251 }
252
253 void 
254 ServerStartModule_STATIC
255 (void)
256 {
257         StaticFilemappings[0] = NewHash(1, NULL);
258         StaticFilemappings[1] = NewHash(1, NULL);
259         StaticFilemappings[2] = NewHash(1, NULL);
260         StaticFilemappings[3] = NewHash(1, NULL);
261 }
262 void 
263 ServerShutdownModule_STATIC
264 (void)
265 {
266         DeleteHash(&StaticFilemappings[0]);
267         DeleteHash(&StaticFilemappings[1]);
268         DeleteHash(&StaticFilemappings[2]);
269         DeleteHash(&StaticFilemappings[3]);
270 }
271
272
273 void 
274 InitModule_STATIC
275 (void)
276 {
277         LoadStaticDir(static_dirs[0], StaticFilemappings[0], "");
278         LoadStaticDir(static_dirs[1], StaticFilemappings[1], "");
279         LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
280         LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
281
282         WebcitAddUrlHandler(HKEY("robots.txt"), output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
283         WebcitAddUrlHandler(HKEY("favicon.ico"), output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
284         WebcitAddUrlHandler(HKEY("static"), output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
285         WebcitAddUrlHandler(HKEY("static.local"), output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
286         WebcitAddUrlHandler(HKEY("tinymce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
287         WebcitAddUrlHandler(HKEY("tiny_mce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
288 }