* svn id tag
[citadel.git] / webcit / static.c
index 0d0eea948b23c423204d232760f887a6ba4bbdc8..b5ea7965d928c00b42d519df49398d6373161cc0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: webcit.c 7459 2009-05-17 08:34:33Z dothebart $
+ * $Id$
  *
  * This is the main transaction loop of the web service.  It maintains a
  * persistent session to the Citadel server, handling HTTP WebCit requests as
 #include <unistd.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <stddef.h>
+
 
 #include "webcit.h"
 #include "webserver.h"
 
 
 HashList *StaticFilemappings[4] = {NULL, NULL, NULL, NULL};
-
-
 /*
-  for ( a = 0; a < 9; ++a)
-  {
-  extract_token(index[a], ChrPtr(ReqLine), a + 1, '/', sizes[a]);
-  if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
-  if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
-  if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
-  if ((index[a][0] == '.') && (index[a][1] == '.'))
-  nBackDots++;
-  if (index[a][0] == '\0')
-  nEmpty++;
-  }
-*/
-
-/* TODO: staticdata
-{
-
-
-       /** Figure out the action * /
-       index[0] = action;
-       sizes[0] = sizeof action;
-       for (a=1; a<9; a++)
-       {
-               index[a] = arg[a-1];
-               sizes[a] = sizeof arg[a-1];
-       }
-       nBackDots = 0;
-       nEmpty = 0;
-
-
-       /* Static content can be sent without connecting to Citadel. * /
-       is_static = 0;
-       for (a=0; a<ndirs && ! is_static; ++a) {
-               if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location * /
-                       is_static = 1;
-                       n_static = a;
-               }
-       }
-       if (is_static) {
-               if (nBackDots < 2)
-               {
-                       snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
-                                static_dirs[n_static], 
-                                index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
-                       for (a=0; a<8; ++a) {
-                               if (buf[strlen(buf)-1] == '/') {
-                                       buf[strlen(buf)-1] = 0;
-                               }
-                       }
-                       for (a = 0; a < strlen(buf); ++a) {
-                               if (isspace(buf[a])) {
-                                       buf[a] = 0;
-                               }
-                       }
-                       output_static(buf);
-               }
-               else 
                {
                        lprintf(9, "Suspicious request. Ignoring.");
                        hprintf("HTTP/1.1 404 Security check failed\r\n");
                        hprintf("Content-Type: text/plain\r\n\r\n");
-                       wprintf("You have sent a malformed or invalid request.\r\n");
+                       wc_printf("You have sent a malformed or invalid request.\r\n");
                        end_burst();
                }
-               goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect * /
-       }
-       }*/
-
-
+*/
 /*
  * dump out static pages from disk
  */
@@ -108,7 +48,8 @@ void output_static(const char *what)
                lprintf(9, "output_static('%s') [%s]  -- NOT FOUND --\n", what, ChrPtr(WC->Hdr->this_page));
                hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
                hprintf("Content-Type: text/plain\r\n");
-               wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
+               begin_burst();
+               wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
                end_burst();
        } else {
                len = strlen (what);
@@ -118,7 +59,8 @@ void output_static(const char *what)
                        lprintf(9, "output_static('%s')  -- FSTAT FAILED --\n", what);
                        hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
                        hprintf("Content-Type: text/plain\r\n");
-                       wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
+                       begin_burst();
+                       wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
                        end_burst();
                        return;
                }
@@ -141,35 +83,13 @@ void output_static(const char *what)
 #ifndef TECH_PREVIEW
                lprintf(9, "output_static('%s')  %s\n", what, content_type);
 #endif
-               http_transmit_thing(content_type, 1);
+               http_transmit_thing(content_type, 2);
        }
        if (yesbstr("force_close_session")) {
                end_webcit_session();
        }
 }
 
-       /* TODO: integrate this into the static startup logic
-
-        * While we're at it, gracefully handle requests for the
-        * robots.txt and favicon.ico files.
-        * /
-       if ((StrLength(ReqLine) >= 11) &&
-           !strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
-               StrBufPlain(ReqLine, 
-                           HKEY("/static/robots.txt"
-                                "?force_close_session=yes HTTP/1.1"));
-               Hdr.eReqType = eGET;
-       }
-       else if ((StrLength(ReqLine) >= 11) &&
-                !strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
-               StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
-               Hdr.eReqType = eGET;
-       }
-
-*/
-
-
-
 
 int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
 {
@@ -180,28 +100,48 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
        StrBuf *WebDir = NULL;
        StrBuf *OneWebName = NULL;
        DIR *filedir = NULL;
-       struct dirent d;
+       struct dirent *d;
        struct dirent *filedir_entry;
-       int d_namelen;
+       int d_type = 0;
+        int d_namelen;
        int d_without_ext;
+       int istoplevel;
                
        filedir = opendir (DirName);
        if (filedir == NULL) {
                return 0;
        }
 
+       d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
+       if (d == NULL) {
+               return 0;
+       }
+
        Dir = NewStrBufPlain(DirName, -1);
        WebDir = NewStrBufPlain(RelDir, -1);
+       istoplevel = IsEmptyStr(RelDir);
        OneWebName = NewStrBuf();
 
-               while ((readdir_r(filedir, &d, &filedir_entry) == 0) &&
+       while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
               (filedir_entry != NULL))
        {
                char *PStart;
 #ifdef _DIRENT_HAVE_D_NAMELEN
                d_namelen = filedir_entry->d_namelen;
+               d_type = filedir_entry->d_type;
 #else
+
+#ifndef DT_UNKNOWN
+#define DT_UNKNOWN     0
+#define DT_DIR         4
+#define DT_REG         8
+#define DT_LNK         10
+
+#define IFTODT(mode)   (((mode) & 0170000) >> 12)
+#define DTTOIF(dirtype)        ((dirtype) << 12)
+#endif
                d_namelen = strlen(filedir_entry->d_name);
+               d_type = DT_UNKNOWN;
 #endif
                d_without_ext = d_namelen;
 
@@ -217,17 +157,33 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
                    (filedir_entry->d_name[1] == '.'))
                        continue;
 
-               switch (filedir_entry->d_type)
+               if (d_type == DT_UNKNOWN) {
+                       struct stat s;
+                       char path[PATH_MAX];
+                       snprintf(path, PATH_MAX, "%s/%s", 
+                               DirName, filedir_entry->d_name);
+                       if (stat(path, &s) == 0) {
+                               d_type = IFTODT(s.st_mode);
+                       }
+               }
+
+               switch (d_type)
                {
                case DT_DIR:
                        /* Skip directories we are not interested in... */
                        if ((strcmp(filedir_entry->d_name, ".svn") == 0) ||
                            (strcmp(filedir_entry->d_name, "t") == 0))
                                break;
-                       snprintf(dirname, PATH_MAX, "%s/%s", 
+                       snprintf(dirname, PATH_MAX, "%s/%s/", 
                                 DirName, filedir_entry->d_name);
-                       snprintf(reldir, PATH_MAX, "%s/%s", 
-                                RelDir, filedir_entry->d_name);
+                       if (istoplevel)
+                               snprintf(reldir, PATH_MAX, "%s/", 
+                                        filedir_entry->d_name);
+                       else
+                               snprintf(reldir, PATH_MAX, "%s/%s/", 
+                                        RelDir, filedir_entry->d_name);
+                       StripSlashes(dirname, 1);
+                       StripSlashes(reldir, 1);
                        LoadStaticDir(dirname, DirList, reldir);                                 
                        break;
                case DT_LNK: /* TODO: check whether its a file or a directory */
@@ -241,12 +197,12 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
                        FlushStrBuf(OneWebName);
                        StrBufAppendBuf(OneWebName, WebDir, 0);
                        if ((StrLength(OneWebName) != 0) && 
-                           (ChrPtr(OneWebName) [ StrLength(OneWebName)] != '/'))
+                           (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
                                StrBufAppendBufPlain(OneWebName, "/", 1, 0);
                        StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);
 
                        Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
-/*                     printf("[%s | %s]  \n", ChrPtr(OneWebName), ChrPtr(FileName));*/
+                       /* lprintf(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
                        break;
                default:
                        break;
@@ -254,6 +210,7 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
 
 
        }
+       free(d);
        closedir(filedir);
        FreeStrBuf(&Dir);
        FreeStrBuf(&WebDir);
@@ -262,20 +219,39 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
 }
 
 
+void output_flat_static(void)
+{
+       wcsession *WCC = WC;
+       void *vFile;
+       StrBuf *File;
+
+       if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
+           (vFile != NULL))
+       {
+               File = (StrBuf*) vFile;
+               output_static(ChrPtr(vFile));
+       }
+}
+
+extern void do_404(void);
+
 void output_static_safe(HashList *DirList)
 {
        wcsession *WCC = WC;
        void *vFile;
        StrBuf *File;
 
-       if (GetHash(DirList, SKEY(WCC->Hdr->ReqLine), &vFile) &&
+       if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
            (vFile != NULL))
        {
                File = (StrBuf*) vFile;
                output_static(ChrPtr(vFile));
        }
        else {
+               lprintf(1, "output_static_safe() file %s not found. \n", 
+                       ChrPtr(WCC->Hdr->HR.ReqLine));
 ///TODO: detect image & output blank image
+               do_404();
        }
 }
 void output_static_0(void)
@@ -324,11 +300,10 @@ InitModule_STATIC
        LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
        LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
 
-       WebcitAddUrlHandler(HKEY("robots.txt"), output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
-       WebcitAddUrlHandler(HKEY("favicon.ico"), output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
-       WebcitAddUrlHandler(HKEY("static"), output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
-       WebcitAddUrlHandler(HKEY("static.local"), output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
-       WebcitAddUrlHandler(HKEY("tinymce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
-
-
+       WebcitAddUrlHandler(HKEY("robots.txt"), "", 0, output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
+       WebcitAddUrlHandler(HKEY("favicon.ico"), "", 0, output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
+       WebcitAddUrlHandler(HKEY("static"), "", 0, output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
+       WebcitAddUrlHandler(HKEY("static.local"), "", 0, output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
+       WebcitAddUrlHandler(HKEY("tinymce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
+       WebcitAddUrlHandler(HKEY("tiny_mce"), "", 0, output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
 }