Remove $Id$ tags from most of webcit
[citadel.git] / webcit / static.c
index 139cd32b481790d5b44173e58c26652bfaf3aaef..1bac24a745886a1bd6255302613009a26a2cf254 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: webcit.c 7459 2009-05-17 08:34:33Z dothebart $
- *
  * This is the main transaction loop of the web service.  It maintains a
  * persistent session to the Citadel server, handling HTTP WebCit requests as
  * they arrive and presenting a user interface.
@@ -13,6 +11,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <stddef.h>
+
 
 #include "webcit.h"
 #include "webserver.h"
@@ -24,7 +24,7 @@ HashList *StaticFilemappings[4] = {NULL, NULL, NULL, NULL};
                        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();
                }
 */
@@ -47,7 +47,7 @@ void output_static(const char *what)
                hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
                hprintf("Content-Type: text/plain\r\n");
                begin_burst();
-               wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
+               wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
                end_burst();
        } else {
                len = strlen (what);
@@ -58,7 +58,7 @@ void output_static(const char *what)
                        hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
                        hprintf("Content-Type: text/plain\r\n");
                        begin_burst();
-                       wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
+                       wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
                        end_burst();
                        return;
                }
@@ -98,9 +98,10 @@ 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;
                
@@ -109,32 +110,39 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
                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;
 
-               if (filedir_entry->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) {
-                               filedir_entry->d_type = IFTODT(s.st_mode);
-                       }
-               }
-
                if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
                        continue; /* Ignore backup files... */
 
@@ -147,7 +155,17 @@ 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... */
@@ -190,6 +208,7 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
 
 
        }
+       free(d);
        closedir(filedir);
        FreeStrBuf(&Dir);
        FreeStrBuf(&WebDir);
@@ -279,10 +298,10 @@ InitModule_STATIC
        LoadStaticDir(static_dirs[2], StaticFilemappings[2], "");
        LoadStaticDir(static_dirs[3], StaticFilemappings[3], "");
 
-       WebcitAddUrlHandler(HKEY("robots.txt"), output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
-       WebcitAddUrlHandler(HKEY("favicon.ico"), output_flat_static, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
-       WebcitAddUrlHandler(HKEY("static"), output_static_0, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
-       WebcitAddUrlHandler(HKEY("static.local"), output_static_1, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
-       WebcitAddUrlHandler(HKEY("tinymce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
-       WebcitAddUrlHandler(HKEY("tiny_mce"), output_static_2, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC|LOGCHATTY);
+       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);
 }