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