Back to index

webcit  8.12-dfsg
Classes | Typedefs | Functions | Variables
downloads.c File Reference
#include "webcit.h"
#include "webserver.h"

Go to the source code of this file.

Classes

struct  _FileListStruct

Typedefs

typedef struct _FileListStruct FileListStruct

Functions

void output_static (const char *What)
void FreeFiles (void *vFile)
void tmplput_FILE_NAME (StrBuf *Target, WCTemplputParams *TP)
void tmplput_FILE_SIZE (StrBuf *Target, WCTemplputParams *TP)
void tmplput_FILEMIMETYPE (StrBuf *Target, WCTemplputParams *TP)
void tmplput_FILE_COMMENT (StrBuf *Target, WCTemplputParams *TP)
int Conditional_FILE_ISPIC (StrBuf *Target, WCTemplputParams *TP)
int CompareFilelistByMime (const void *vFile1, const void *vFile2)
int CompareFilelistByMimeRev (const void *vFile1, const void *vFile2)
int GroupchangeFilelistByMime (const void *vFile1, const void *vFile2)
int CompareFilelistByName (const void *vFile1, const void *vFile2)
int CompareFilelistByNameRev (const void *vFile1, const void *vFile2)
int GroupchangeFilelistByName (const void *vFile1, const void *vFile2)
int CompareFilelistBySize (const void *vFile1, const void *vFile2)
int CompareFilelistBySizeRev (const void *vFile1, const void *vFile2)
int GroupchangeFilelistBySize (const void *vFile1, const void *vFile2)
int CompareFilelistByComment (const void *vFile1, const void *vFile2)
int CompareFilelistByCommentRev (const void *vFile1, const void *vFile2)
int GroupchangeFilelistByComment (const void *vFile1, const void *vFile2)
int CompareFilelistBySequence (const void *vFile1, const void *vFile2)
int GroupchangeFilelistBySequence (const void *vFile1, const void *vFile2)
HashList * LoadFileList (StrBuf *Target, WCTemplputParams *TP)
void display_mime_icon (void)
void download_file (void)
void delete_file (void)
void upload_file (void)
void output_image (void)
void InitModule_DOWNLOAD (void)

Variables

char * static_dirs []

Class Documentation

struct _FileListStruct

Definition at line 20 of file downloads.c.

Class Members
StrBuf * Comment
StrBuf * Filename
long FileSize
int IsPic
StrBuf * MimeType
int Sequence

Typedef Documentation


Function Documentation

int CompareFilelistByComment ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 145 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       return strcasecmp(ChrPtr(File1->Comment), ChrPtr(File2->Comment));
}

Here is the caller graph for this function:

int CompareFilelistByCommentRev ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 151 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       return strcasecmp(ChrPtr(File2->Comment), ChrPtr(File1->Comment));
}

Here is the caller graph for this function:

int CompareFilelistByMime ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 69 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);

       if (File1->IsPic != File2->IsPic)
              return File1->IsPic > File2->IsPic;
       return strcasecmp(ChrPtr(File1->MimeType), ChrPtr(File2->MimeType));
}

Here is the caller graph for this function:

int CompareFilelistByMimeRev ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 78 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       if (File1->IsPic != File2->IsPic)
              return File1->IsPic < File2->IsPic;
       return strcasecmp(ChrPtr(File2->MimeType), ChrPtr(File1->MimeType));
}

Here is the caller graph for this function:

int CompareFilelistByName ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 97 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);

       if (File1->IsPic != File2->IsPic)
              return File1->IsPic > File2->IsPic;
       return strcasecmp(ChrPtr(File1->Filename), ChrPtr(File2->Filename));
}

Here is the caller graph for this function:

int CompareFilelistByNameRev ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 106 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       if (File1->IsPic != File2->IsPic)
              return File1->IsPic < File2->IsPic;
       return strcasecmp(ChrPtr(File2->Filename), ChrPtr(File1->Filename));
}

Here is the caller graph for this function:

int CompareFilelistBySequence ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 165 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       return (File2->Sequence >  File1->Sequence);
}

Here is the caller graph for this function:

int CompareFilelistBySize ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 123 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       if (File1->FileSize == File2->FileSize)
              return 0;
       return (File1->FileSize > File2->FileSize);
}

Here is the caller graph for this function:

int CompareFilelistBySizeRev ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 131 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) GetSearchPayload(vFile1);
       FileListStruct *File2 = (FileListStruct*) GetSearchPayload(vFile2);
       if (File1->FileSize == File2->FileSize)
              return 0;
       return (File1->FileSize < File2->FileSize);
}

Here is the caller graph for this function:

int Conditional_FILE_ISPIC ( StrBuf *  Target,
WCTemplputParams TP 
)

Definition at line 62 of file downloads.c.

{
       FileListStruct *F = (FileListStruct*) CTX;
       return F->IsPic;
}

Here is the caller graph for this function:

void delete_file ( void  )

Definition at line 293 of file downloads.c.

{
       const StrBuf *MimeType;
       StrBuf *Line;
       char buf[256];
       
       safestrncpy(buf, bstr("file"), sizeof buf);
       unescape_input(buf);
       serv_printf("DELF %s", buf);

       StrBuf_ServGetln(Line);
       GetServerStatusMsg(Line, NULL, 1, 0);

       MimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
       http_transmit_thing(ChrPtr(MimeType), 0);
       FreeStrBuf(&Line);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void display_mime_icon ( void  )

Definition at line 236 of file downloads.c.

{
       char FileBuf[SIZ];
       const char *FileName;
       char *MimeType;
       size_t tlen;

       MimeType = xbstr("type", &tlen);
       FileName = GetIconFilename(MimeType, tlen);

       if (FileName == NULL)
              snprintf (FileBuf, SIZ, "%s%s", static_dirs[0], "/webcit_icons/essen/16x16/file.png");
       else
              snprintf (FileBuf, SIZ, "%s%s", static_dirs[3], FileName);
       output_static(FileBuf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void download_file ( void  )

Definition at line 253 of file downloads.c.

{
       wcsession *WCC = WC;
       StrBuf *Buf;
       off_t bytes;
       StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));

       /* Setting to nonzero forces a MIME type of application/octet-stream */
       int force_download = 1;
       
       Buf = NewStrBuf();
       StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 0, '/');
       StrBufUnescape(Buf, 1);
       serv_printf("OPEN %s", ChrPtr(Buf));
       StrBuf_ServGetln(Buf);
       if (GetServerStatus(Buf, NULL) == 2) {
              StrBufCutLeft(Buf, 4);
              bytes = StrBufExtract_long(Buf, 0, '|');
              if (!force_download) {
                     StrBufExtract_token(ContentType, Buf, 3, '|');
              }
              serv_read_binary(WCC->WBuf, bytes, Buf);
              serv_puts("CLOS");
              StrBuf_ServGetln(Buf);
              http_transmit_thing(ChrPtr(ContentType), 0);
       } else {
              StrBufCutLeft(Buf, 4);
              hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
              output_headers(0, 0, 0, 0, 0, 0);
              hprintf("Content-Type: text/plain\r\n");
              wc_printf(_("An error occurred while retrieving this file: %s\n"), 
                     ChrPtr(Buf));
              end_burst();
       }
       FreeStrBuf(&ContentType);
       FreeStrBuf(&Buf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void FreeFiles ( void *  vFile)

Definition at line 29 of file downloads.c.

{
       FileListStruct *F = (FileListStruct*) vFile;
       FreeStrBuf(&F->Filename);
       FreeStrBuf(&F->MimeType);
       FreeStrBuf(&F->Comment);
       free(F);
}

Here is the caller graph for this function:

int GroupchangeFilelistByComment ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 157 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) vFile1;
       FileListStruct *File2 = (FileListStruct*) vFile2;
       return ChrPtr(File1->Comment)[9] != ChrPtr(File2->Comment)[0];
}

Here is the caller graph for this function:

int GroupchangeFilelistByMime ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 86 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) vFile1;
       FileListStruct *File2 = (FileListStruct*) vFile2;

       if (File1->IsPic != File2->IsPic)
              return File1->IsPic > File2->IsPic;
       return strcasecmp(ChrPtr(File1->MimeType), ChrPtr(File2->MimeType)) != 0;
}

Here is the caller graph for this function:

int GroupchangeFilelistByName ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 114 of file downloads.c.

{
       FileListStruct *File1 = (FileListStruct*) vFile1;
       FileListStruct *File2 = (FileListStruct*) vFile2;

       return ChrPtr(File1->Filename)[0] != ChrPtr(File2->Filename)[0];
}

Here is the caller graph for this function:

int GroupchangeFilelistBySequence ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 171 of file downloads.c.

{
       return 0;
}

Here is the caller graph for this function:

int GroupchangeFilelistBySize ( const void *  vFile1,
const void *  vFile2 
)

Definition at line 139 of file downloads.c.

{
       return 0;
}

Here is the caller graph for this function:

void InitModule_DOWNLOAD ( void  )

Definition at line 421 of file downloads.c.

{

       RegisterIterator("ROOM:FILES", 0, NULL, LoadFileList,
                      NULL, DeleteHash, CTX_FILELIST, CTX_NONE, 
                      IT_FLAG_DETECT_GROUPCHANGE);

       RegisterSortFunc(HKEY("filemime"),
                      NULL, 0,
                      CompareFilelistByMime,
                      CompareFilelistByMimeRev,
                      GroupchangeFilelistByMime,
                      CTX_FILELIST);
       RegisterSortFunc(HKEY("filename"),
                      NULL, 0,
                      CompareFilelistByName,
                      CompareFilelistByNameRev,
                      GroupchangeFilelistByName,
                      CTX_FILELIST);
       RegisterSortFunc(HKEY("filesize"),
                      NULL, 0,
                      CompareFilelistBySize,
                      CompareFilelistBySizeRev,
                      GroupchangeFilelistBySize,
                      CTX_FILELIST);
       RegisterSortFunc(HKEY("filesubject"),
                      NULL, 0,
                      CompareFilelistByComment,
                      CompareFilelistByCommentRev,
                      GroupchangeFilelistByComment,
                      CTX_FILELIST);
       RegisterSortFunc(HKEY("fileunsorted"),
                      NULL, 0,
                      CompareFilelistBySequence,
                      CompareFilelistBySequence,
                      GroupchangeFilelistBySequence,
                      CTX_FILELIST);

       RegisterNamespace("FILE:NAME", 0, 2, tmplput_FILE_NAME, NULL, CTX_FILELIST);
       RegisterNamespace("FILE:SIZE", 0, 1, tmplput_FILE_SIZE, NULL, CTX_FILELIST);
       RegisterNamespace("FILE:MIMETYPE", 0, 2, tmplput_FILEMIMETYPE, NULL, CTX_FILELIST);
       RegisterNamespace("FILE:COMMENT", 0, 2, tmplput_FILE_COMMENT, NULL, CTX_FILELIST);

       RegisterConditional(HKEY("COND:FILE:ISPIC"), 0, Conditional_FILE_ISPIC, CTX_FILELIST);

       WebcitAddUrlHandler(HKEY("image"), "", 0, output_image, ANONYMOUS);
       WebcitAddUrlHandler(HKEY("display_mime_icon"), "", 0, display_mime_icon , ANONYMOUS);
       WebcitAddUrlHandler(HKEY("download_file"), "", 0, download_file, NEED_URL);
       WebcitAddUrlHandler(HKEY("delete_file"), "", 0, delete_file, NEED_URL);
       WebcitAddUrlHandler(HKEY("upload_file"), "", 0, upload_file, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

HashList* LoadFileList ( StrBuf *  Target,
WCTemplputParams TP 
)

Definition at line 177 of file downloads.c.

{
       FileListStruct *Entry;
       StrBuf *Buf;
       HashList *Files;
       int Done = 0;
       int sequence = 0;
       char buf[1024];
       CompareFunc SortIt;
       int HavePic = 0;
       WCTemplputParams SubTP;

       memset(&SubTP, 0, sizeof(WCTemplputParams));
       serv_puts("RDIR");
       serv_getln(buf, sizeof buf);
       if (buf[0] != '1') return NULL;

       Buf = NewStrBuf();          
       Files = NewHash(1, NULL);
       while (!Done && (StrBuf_ServGetln(Buf)>=0)) {
              if ( (StrLength(Buf)==3) && 
                  !strcmp(ChrPtr(Buf), "000")) 
              {
                     Done = 1;
                     continue;
              }

              Entry = (FileListStruct*) malloc(sizeof (FileListStruct));
              Entry->Filename = NewStrBufPlain(NULL, StrLength(Buf));
              Entry->MimeType = NewStrBufPlain(NULL, StrLength(Buf));
              Entry->Comment = NewStrBufPlain(NULL, StrLength(Buf));

              Entry->Sequence = sequence++;

              StrBufExtract_token(Entry->Filename, Buf, 0, '|');
              Entry->FileSize = StrBufExtract_long(Buf, 1, '|');
              StrBufExtract_token(Entry->MimeType, Buf, 2, '|');
              StrBufExtract_token(Entry->Comment, Buf, 3, '|');



              Entry->IsPic = (strstr(ChrPtr(Entry->MimeType), "image") != NULL);
              if (Entry->IsPic) {
                     HavePic = 1;
              }
              Put(Files, SKEY(Entry->Filename), Entry, FreeFiles);
       }
       if (HavePic)
              putbstr("__HAVE_PIC", NewStrBufPlain(HKEY("1")));
       SubTP.Filter.ContextType = CTX_FILELIST;
       SortIt = RetrieveSort(&SubTP, NULL, 0, HKEY("fileunsorted"), 0);
       if (SortIt != NULL)
              SortByPayload(Files, SortIt);
       else 
              SortByPayload(Files, CompareFilelistBySequence);
       FreeStrBuf(&Buf);
       return Files;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void output_image ( void  )

Read it from the server

Write it to the browser

Definition at line 372 of file downloads.c.

{
       StrBuf *Buf;
       wcsession *WCC = WC;
       off_t bytes;
       const char *MimeType;
       
       Buf = NewStrBuf();
       serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
       StrBuf_ServGetln(Buf);
       if (GetServerStatus(Buf, NULL) == 2) {
              int rc;
              StrBufCutLeft(Buf, 4);
              bytes = StrBufExtract_long(Buf, 0, '|');

              rc = serv_read_binary(WCC->WBuf, bytes, Buf);
              serv_puts("CLOS");
              StrBuf_ServGetln(Buf);
              
              if (rc > 0) {
                     MimeType = GuessMimeType (ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
                     if (!IsEmptyStr(MimeType))
                     {
                            http_transmit_thing(MimeType, 0);
                            FreeStrBuf(&Buf);
                            return;
                     }
              }
              /* hm... unknown mimetype? fallback to blank gif */
       }
       else { 
              syslog(LOG_DEBUG, "OIMG failed: %s", ChrPtr(Buf));
       }

       
       /*
        * Instead of an ugly 404, send a 1x1 transparent GIF
        * when there's no such image on the server.
        */
       StrBufPrintf (Buf, "%s%s", static_dirs[0], "/webcit_icons/blank.gif");
       output_static(ChrPtr(Buf));
       FreeStrBuf(&Buf);
}

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 caller graph for this function:

void tmplput_FILE_COMMENT ( StrBuf *  Target,
WCTemplputParams TP 
)

Definition at line 54 of file downloads.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void tmplput_FILE_NAME ( StrBuf *  Target,
WCTemplputParams TP 
)

Definition at line 39 of file downloads.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void tmplput_FILE_SIZE ( StrBuf *  Target,
WCTemplputParams TP 
)

Definition at line 44 of file downloads.c.

{
       FileListStruct *F = (FileListStruct*) CTX;
       StrBufAppendPrintf(Target, "%ld", F->FileSize);
}

Here is the caller graph for this function:

void tmplput_FILEMIMETYPE ( StrBuf *  Target,
WCTemplputParams TP 
)

Definition at line 49 of file downloads.c.

Here is the call graph for this function:

Here is the caller graph for this function:

void upload_file ( void  )

Definition at line 313 of file downloads.c.

{
       const StrBuf *RetMimeType;
       const char *MimeType;
       StrBuf *Line;
       long bytes_transmitted = 0;
       long blocksize;
       const StrBuf *Desc;
       wcsession *WCC = WC;     /* stack this for faster access (WC is a function) */

       MimeType = GuessMimeType(ChrPtr(WCC->upload), WCC->upload_length); 

              Desc = sbstr("description");

       serv_printf("UOPN %s|%s|%s", 
                  ChrPtr(WCC->upload_filename), 
                  MimeType, 
                  ChrPtr(Desc));
       Line = NewStrBuf();
       StrBuf_ServGetln(Line);
       if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
              RetMimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
              http_transmit_thing(ChrPtr(RetMimeType), 0);
              FreeStrBuf(&Line);
              return;
       }

       while (bytes_transmitted < WCC->upload_length)
       {
              blocksize = 4096;
              if (blocksize > (WCC->upload_length - bytes_transmitted))
              {
                     blocksize = (WCC->upload_length - bytes_transmitted);
              }
              serv_printf("WRIT %ld", blocksize);
              StrBuf_ServGetln(Line);
              if (GetServerStatusMsg(Line, NULL, 0, 0) == 7) {
                     blocksize = atoi(ChrPtr(Line) + 4);
                     serv_write(&ChrPtr(WCC->upload)[bytes_transmitted], blocksize);
                     bytes_transmitted += blocksize;
              }
              else
                     break;
       }

       serv_puts("UCLS 1");
       StrBuf_ServGetln(Line);
       GetServerStatusMsg(Line, NULL, 1, 0);
       RetMimeType = DoTemplate(HKEY("files"), NULL, &NoCtx);
       http_transmit_thing(ChrPtr(RetMimeType), 0);
       FreeStrBuf(&Line);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char* static_dirs[]

Definition at line 90 of file sysdep.c.