Back to index

webcit  8.12-dfsg
Defines | Functions | Variables
static.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include "webcit.h"
#include "webserver.h"

Go to the source code of this file.

Defines

#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)

Functions

void output_error_pic (const char *ErrMsg1, const char *ErrMsg2)
void output_static (const char *what)
int LoadStaticDir (const char *DirName, HashList *DirList, const char *RelDir)
void output_flat_static (void)
void output_static_safe (HashList *DirList)
void output_static_0 (void)
void output_static_1 (void)
void output_static_2 (void)
void output_static_3 (void)
void robots_txt (void)
void ServerStartModule_STATIC (void)
void ServerShutdownModule_STATIC (void)
void InitModule_STATIC (void)

Variables

unsigned char OnePixelGif [37]
HashList * StaticFilemappings [4] = {NULL, NULL, NULL, NULL}

Define Documentation

#define DT_DIR   4
#define DT_LNK   10
#define DT_REG   8
#define DT_UNKNOWN   0
#define DTTOIF (   dirtype)    ((dirtype) << 12)
#define IFTODT (   mode)    (((mode) & 0170000) >> 12)

Function Documentation

void InitModule_STATIC ( void  )
int LoadStaticDir ( const char *  DirName,
HashList *  DirList,
const char *  RelDir 
)

Definition at line 151 of file static.c.

{
       char dirname[PATH_MAX];
       char reldir[PATH_MAX];
       StrBuf *FileName = NULL;
       StrBuf *Dir = NULL;
       StrBuf *WebDir = NULL;
       StrBuf *OneWebName = NULL;
       DIR *filedir = NULL;
       struct dirent *d;
       struct dirent *filedir_entry;
       int d_type = 0;
        int d_namelen;
       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) &&
              (filedir_entry != NULL))
       {
#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
              if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
                     continue; /* Ignore backup files... */

              if ((d_namelen == 1) && 
                  (filedir_entry->d_name[0] == '.'))
                     continue;

              if ((d_namelen == 2) && 
                  (filedir_entry->d_name[0] == '.') &&
                  (filedir_entry->d_name[1] == '.'))
                     continue;

              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/", 
                             DirName, 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 */
              case DT_REG:
                     FileName = NewStrBufDup(Dir);
                     if (ChrPtr(FileName) [ StrLength(FileName) - 1] != '/')
                            StrBufAppendBufPlain(FileName, "/", 1, 0);
                     StrBufAppendBufPlain(FileName, filedir_entry->d_name, d_namelen, 0);

                     FlushStrBuf(OneWebName);
                     StrBufAppendBuf(OneWebName, WebDir, 0);
                     if ((StrLength(OneWebName) != 0) && 
                         (ChrPtr(OneWebName) [ StrLength(OneWebName) - 1] != '/'))
                            StrBufAppendBufPlain(OneWebName, "/", 1, 0);
                     StrBufAppendBufPlain(OneWebName, filedir_entry->d_name, d_namelen, 0);

                     Put(DirList, SKEY(OneWebName), FileName, HFreeStrBuf);
                     /* syslog(9, "[%s | %s]\n", ChrPtr(OneWebName), ChrPtr(FileName)); */
                     break;
              default:
                     break;
              }


       }
       free(d);
       closedir(filedir);
       FreeStrBuf(&Dir);
       FreeStrBuf(&WebDir);
       FreeStrBuf(&OneWebName);
       return 1;
}

Here is the caller graph for this function:

void output_error_pic ( const char *  ErrMsg1,
const char *  ErrMsg2 
)

Definition at line 71 of file static.c.

{
       hprintf("HTTP/1.1 200 %s\r\n", ErrMsg1);
       hprintf("Content-Type: image/gif\r\n");
       hprintf("x-webcit-errormessage: %s\r\n", ErrMsg2);
       begin_burst();
       StrBufPlain(WC->WBuf, (const char *)OnePixelGif, sizeof(OnePixelGif));
       end_burst();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void output_flat_static ( void  )

Definition at line 274 of file static.c.

{
       wcsession *WCC = WC;
       void *vFile;
       StrBuf *File;

       if (WCC->Hdr->HR.Handler == NULL)
              return;
       if (GetHash(StaticFilemappings[0], SKEY(WCC->Hdr->HR.Handler->Name), &vFile) &&
           (vFile != NULL))
       {
              File = (StrBuf*) vFile;
              output_static(ChrPtr(File));
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void output_static ( const char *  what)

Definition at line 84 of file static.c.

{
       int fd;
       struct stat statbuf;
       off_t bytes;
       const char *content_type;
       int len;
       const char *Err;

       len = strlen (what);
       content_type = GuessMimeByFilename(what, len);
       fd = open(what, O_RDONLY);
       if (fd <= 0) {
              syslog(9, "output_static('%s') [%s]  -- NOT FOUND --\n", what, ChrPtr(WC->Hdr->this_page));
              if (strstr(content_type, "image/") != NULL)
              {
                     output_error_pic("the file you requsted is gone.", strerror(errno));
              }
              else
              {
                     hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
                     hprintf("Content-Type: text/plain\r\n");
                     begin_burst();
                     wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
                     end_burst();
              }
       } else {
              if (fstat(fd, &statbuf) == -1) {
                     syslog(9, "output_static('%s')  -- FSTAT FAILED --\n", what);
                     if (strstr(content_type, "image/") != NULL)
                     {
                            output_error_pic("Stat failed!", strerror(errno));
                     }
                     else
                     {
                            hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
                            hprintf("Content-Type: text/plain\r\n");
                            begin_burst();
                            wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
                            end_burst();
                     }
                     if (fd > 0) close(fd);
                     return;
              }

              bytes = statbuf.st_size;

              if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0)
              {
                     if (fd > 0) close(fd);
                     syslog(9, "output_static('%s')  -- FREAD FAILED (%s) --\n", what, strerror(errno));
                            hprintf("HTTP/1.1 500 internal server error \r\n");
                            hprintf("Content-Type: text/plain\r\n");
                            end_burst();
                            return;
              }


              close(fd);
              http_transmit_thing(content_type, 2);
       }
       if (yesbstr("force_close_session")) {
              end_webcit_session();
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void output_static_0 ( void  )

Definition at line 317 of file static.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void output_static_1 ( void  )

Definition at line 321 of file static.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void output_static_2 ( void  )

Definition at line 325 of file static.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void output_static_3 ( void  )

Definition at line 329 of file static.c.

Here is the call graph for this function:

void output_static_safe ( HashList *  DirList)

Definition at line 290 of file static.c.

{
       wcsession *WCC = WC;
       void *vFile;
       StrBuf *File;
       const char *MimeType;

       if (GetHash(DirList, SKEY(WCC->Hdr->HR.ReqLine), &vFile) &&
           (vFile != NULL))
       {
              File = (StrBuf*) vFile;
              output_static(ChrPtr(File));
       }
       else {
              syslog(1, "output_static_safe() file %s not found. \n", 
                     ChrPtr(WCC->Hdr->HR.ReqLine));
              MimeType =  GuessMimeByFilename(SKEY(WCC->Hdr->HR.ReqLine));
              if (strstr(MimeType, "image/") != NULL)
              {
                     output_error_pic("the file you requested isn't known to our cache", "maybe reload webcit?");
              }
              else
              {                 
                     do_404();
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void robots_txt ( void  )

Definition at line 338 of file static.c.

                      {
       output_headers(0, 0, 0, 0, 0, 0);

       hprintf("Content-type: text/plain\r\n"
              "Server: %s\r\n"
              "Connection: close\r\n",
              PACKAGE_STRING);
       begin_burst();

       wc_printf("User-agent: *\r\n"
              "Disallow: /printmsg\r\n"
              "Disallow: /msgheaders\r\n"
              "Disallow: /groupdav\r\n"
              "Disallow: /do_template\r\n"
              "Disallow: /static\r\n"
              "Disallow: /display_page\r\n"
              "Disallow: /readnew\r\n"
              "Disallow: /display_enter\r\n"
              "Disallow: /skip\r\n"
              "Disallow: /ungoto\r\n"
              "Sitemap: %s/sitemap.xml\r\n"
              "\r\n"
              ,
              ChrPtr(site_prefix)
       );

       wDumpContent(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ServerShutdownModule_STATIC ( void  )

Definition at line 379 of file static.c.

{
       DeleteHash(&StaticFilemappings[0]);
       DeleteHash(&StaticFilemappings[1]);
       DeleteHash(&StaticFilemappings[2]);
       DeleteHash(&StaticFilemappings[3]);
}

Here is the caller graph for this function:

void ServerStartModule_STATIC ( void  )

Definition at line 370 of file static.c.

{
       StaticFilemappings[0] = NewHash(1, NULL);
       StaticFilemappings[1] = NewHash(1, NULL);
       StaticFilemappings[2] = NewHash(1, NULL);
       StaticFilemappings[3] = NewHash(1, NULL);
}

Here is the caller graph for this function:


Variable Documentation

unsigned char OnePixelGif[37]

Definition at line 20 of file static.c.

HashList* StaticFilemappings[4] = {NULL, NULL, NULL, NULL}

Definition at line 59 of file static.c.