No more robots.txt, we now welcome spiders
[citadel.git] / webcit / static.c
1 /*
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.
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <dirent.h>
9 #include <errno.h>
10
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include <stddef.h>
15
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                         wc_printf("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                 wc_printf("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                         wc_printf("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_type = 0;
104         int d_namelen;
105         int d_without_ext;
106         int istoplevel;
107                 
108         filedir = opendir (DirName);
109         if (filedir == NULL) {
110                 return 0;
111         }
112
113         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
114         if (d == NULL) {
115                 return 0;
116         }
117
118         Dir = NewStrBufPlain(DirName, -1);
119         WebDir = NewStrBufPlain(RelDir, -1);
120         istoplevel = IsEmptyStr(RelDir);
121         OneWebName = NewStrBuf();
122
123         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
124                (filedir_entry != NULL))
125         {
126                 char *PStart;
127 #ifdef _DIRENT_HAVE_D_NAMELEN
128                 d_namelen = filedir_entry->d_namelen;
129                 d_type = filedir_entry->d_type;
130 #else
131
132 #ifndef DT_UNKNOWN
133 #define DT_UNKNOWN     0
134 #define DT_DIR         4
135 #define DT_REG         8
136 #define DT_LNK         10
137
138 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
139 #define DTTOIF(dirtype)        ((dirtype) << 12)
140 #endif
141                 d_namelen = strlen(filedir_entry->d_name);
142                 d_type = DT_UNKNOWN;
143 #endif
144                 d_without_ext = d_namelen;
145
146                 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
147                         continue; /* Ignore backup files... */
148
149                 if ((d_namelen == 1) && 
150                     (filedir_entry->d_name[0] == '.'))
151                         continue;
152
153                 if ((d_namelen == 2) && 
154                     (filedir_entry->d_name[0] == '.') &&
155                     (filedir_entry->d_name[1] == '.'))
156                         continue;
157
158                 if (d_type == DT_UNKNOWN) {
159                         struct stat s;
160                         char path[PATH_MAX];
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);
165                         }
166                 }
167
168                 switch (d_type)
169                 {
170                 case DT_DIR:
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))
174                                 break;
175                         snprintf(dirname, PATH_MAX, "%s/%s/", 
176                                  DirName, filedir_entry->d_name);
177                         if (istoplevel)
178                                 snprintf(reldir, PATH_MAX, "%s/", 
179                                          filedir_entry->d_name);
180                         else
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);                                 
186                         break;
187                 case DT_LNK: /* TODO: check whether its a file or a directory */
188                 case DT_REG:
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);
194
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);
201
202                         Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
203                         /* lprintf(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
204                         break;
205                 default:
206                         break;
207                 }
208
209
210         }
211         free(d);
212         closedir(filedir);
213         FreeStrBuf(&Dir);
214         FreeStrBuf(&WebDir);
215         FreeStrBuf(&OneWebName);
216         return 1;
217 }
218
219
220 void output_flat_static(void)
221 {
222         wcsession *WCC = WC;
223         void *vFile;
224         StrBuf *File;
225
226         if (WCC->Hdr->HR.Handler == NULL)
227                 return;
228         if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
229             (vFile != NULL))
230         {
231                 File = (StrBuf*) vFile;
232                 output_static(ChrPtr(vFile));
233         }
234 }
235
236 extern void do_404(void);
237
238 void output_static_safe(HashList *DirList)
239 {
240         wcsession *WCC = WC;
241         void *vFile;
242         StrBuf *File;
243
244         if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
245             (vFile != NULL))
246         {
247                 File = (StrBuf*) vFile;
248                 output_static(ChrPtr(vFile));
249         }
250         else {
251                 lprintf(1, "output_static_safe() file %s not found. \n", 
252                         ChrPtr(WCC->Hdr->HR.ReqLine));
253 ///TODO: detect image & output blank image
254                 do_404();
255         }
256 }
257 void output_static_0(void)
258 {
259         output_static_safe(StaticFilemappings[0]);
260 }
261 void output_static_1(void)
262 {
263         output_static_safe(StaticFilemappings[1]);
264 }
265 void output_static_2(void)
266 {
267         output_static_safe(StaticFilemappings[2]);
268 }
269 void output_static_3(void)
270 {
271         output_static_safe(StaticFilemappings[3]);
272 }
273
274 void 
275 ServerStartModule_STATIC
276 (void)
277 {
278         StaticFilemappings[0] = NewHash(1, NULL);
279         StaticFilemappings[1] = NewHash(1, NULL);
280         StaticFilemappings[2] = NewHash(1, NULL);
281         StaticFilemappings[3] = NewHash(1, NULL);
282 }
283 void 
284 ServerShutdownModule_STATIC
285 (void)
286 {
287         DeleteHash(&StaticFilemappings[0]);
288         DeleteHash(&StaticFilemappings[1]);
289         DeleteHash(&StaticFilemappings[2]);
290         DeleteHash(&StaticFilemappings[3]);
291 }
292
293
294 void 
295 InitModule_STATIC
296 (void)
297 {
298         LoadStaticDir(static_dirs[0], StaticFilemappings[0], "");
299         LoadStaticDir(static_dirs[1], StaticFilemappings[1], "");
300         LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
301         LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
302
303         WebcitAddUrlHandler(HKEY("favicon.ico"), "", 0, output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
304         WebcitAddUrlHandler(HKEY("static"), "", 0, output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
305         WebcitAddUrlHandler(HKEY("static.local"), "", 0, output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
306         WebcitAddUrlHandler(HKEY("tinymce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
307         WebcitAddUrlHandler(HKEY("tiny_mce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
308 }