* more fixes, offset in URLS is different now, since we strip the command.
[citadel.git] / webcit / downloads.c
index ed78c74c6bdfc07a188b45bbcea8440230d5ac6f..c8970ef3a528b2651a1e956b13241809f54db907 100644 (file)
@@ -25,32 +25,32 @@ void FreeFiles(void *vFile)
 }
 
 /* -------------------------------------------------------------------------------- */
-void tmplput_FILE_NAME(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+void tmplput_FILE_NAME(StrBuf *Target, WCTemplputParams *TP)
 {
-       FileListStruct *F = (FileListStruct*) Context;
-       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, F->Filename, 0);
+       FileListStruct *F = (FileListStruct*) CTX;
+       StrBufAppendTemplate(Target, TP, F->Filename, 0);
 }
-void tmplput_FILE_SIZE(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+void tmplput_FILE_SIZE(StrBuf *Target, WCTemplputParams *TP)
 {
-       FileListStruct *F = (FileListStruct*) Context;
+       FileListStruct *F = (FileListStruct*) CTX;
        StrBufAppendPrintf(Target, "%ld", F->FileSize);
 }
-void tmplput_FILEMIMETYPE(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+void tmplput_FILEMIMETYPE(StrBuf *Target, WCTemplputParams *TP)
 {
-       FileListStruct *F = (FileListStruct*) Context;
-       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, F->MimeType, 0);
+       FileListStruct *F = (FileListStruct*) CTX;
+       StrBufAppendTemplate(Target, TP, F->MimeType, 0);
 }
-void tmplput_FILE_COMMENT(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+void tmplput_FILE_COMMENT(StrBuf *Target, WCTemplputParams *TP)
 {
-       FileListStruct *F = (FileListStruct*) Context;
-       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, F->Comment, 0);
+       FileListStruct *F = (FileListStruct*) CTX;
+       StrBufAppendTemplate(Target, TP, F->Comment, 0);
 }
 
 /* -------------------------------------------------------------------------------- */
 
-int Conditional_FILE_ISPIC(WCTemplateToken *Tokens, void *Context, int ContextType)
+int Conditional_FILE_ISPIC(StrBuf *Target, WCTemplputParams *TP)
 {
-       FileListStruct *F = (FileListStruct*) Context;
+       FileListStruct *F = (FileListStruct*) CTX;
        return F->IsPic;
 }
 
@@ -163,7 +163,7 @@ int GroupchangeFilelistBySequence(const void *vFile1, const void *vFile2)
 }
 
 /* -------------------------------------------------------------------------------- */
-HashList* LoadFileList(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+HashList* LoadFileList(StrBuf *Target, WCTemplputParams *TP)
 {
        FileListStruct *Entry;
        StrBuf *Buf;
@@ -173,7 +173,9 @@ HashList* LoadFileList(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void
        char buf[1024];
        CompareFunc SortIt;
        int HavePic;
+       WCTemplputParams SubTP;
 
+       memset(&TP, 0, sizeof(WCTemplputParams));
        serv_puts("RDIR");
        serv_getln(buf, sizeof buf);
        if (buf[0] != '1') return NULL;
@@ -208,7 +210,8 @@ HashList* LoadFileList(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void
                }
                Put(Files, SKEY(Entry->Filename), Entry, FreeFiles);
        }
-       SortIt = RetrieveSort(CTX_FILELIST, NULL, HKEY("fileunsorted"), 0);
+       SubTP.Filter.ContextType = CTX_FILELIST;
+       SortIt = RetrieveSort(&SubTP, NULL, 0, HKEY("fileunsorted"), 0);
        if (SortIt != NULL)
                SortByPayload(Files, SortIt);
        else 
@@ -237,40 +240,61 @@ void display_mime_icon(void)
 
 void download_file(void)
 {
-       char buf[256];
+       wcsession *WCC = WC;
+       StrBuf *Buf;
        off_t bytes;
-       char content_type[256];
-       char *content = NULL;
+       StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
 
        /* Setting to nonzero forces a MIME type of application/octet-stream */
        int force_download = 1;
        
-       safestrncpy(buf, ChrPtr(WC->UrlFragment2), sizeof buf);
-       unescape_input(buf);
-       serv_printf("OPEN %s", buf);
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '2') {
-               bytes = extract_long(&buf[4], 0);
-               content = malloc(bytes + 2);
-               if (force_download) {
-                       strcpy(content_type, "application/octet-stream");
-               }
-               else {
-                       extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
+       Buf = NewStrBuf();
+       StrBufExtract_token(Buf, WCC->Hdr->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, '|');
                }
-               output_headers(0, 0, 0, 0, 0, 0);
-               read_server_binary(WC->WBuf, bytes);
+               read_server_binary(WCC->WBuf, bytes, Buf);
                serv_puts("CLOS");
-               serv_getln(buf, sizeof buf);
-               http_transmit_thing(content_type, 0);
-               free(content);
+               StrBuf_ServGetln(Buf);
+               http_transmit_thing(ChrPtr(ContentType), 0);
        } else {
-               hprintf("HTTP/1.1 404 %s\n", &buf[4]);
+               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");
-               wprintf(_("An error occurred while retrieving this file: %s\n"), &buf[4]);
+               wprintf(_("An error occurred while retrieving this file: %s\n"), 
+                       ChrPtr(Buf));
                end_burst();
        }
+       FreeStrBuf(&ContentType);
+       FreeStrBuf(&Buf);
+}
+
+
+
+void delete_file(void)
+{
+       StrBuf *Buf;
+       char buf[256];
+       
+       safestrncpy(buf, bstr("file"), sizeof buf);
+       unescape_input(buf);
+       serv_printf("DELF %s", buf);
+       Buf = NewStrBuf();
+       StrBuf_ServGetln(Buf);
+       GetServerStatus(Buf, NULL);
+       StrBufCutLeft(Buf, 4);
+       strcpy(WC->ImportantMessage, ChrPtr(Buf));
+       do_template("files", CTX_NONE);
+       output_headers(0, 0, 0, 0, 0, 0);
+       end_burst();
+       FreeStrBuf(&Buf);
 }
 
 
@@ -290,6 +314,8 @@ void upload_file(void)
        {
                strcpy(WCC->ImportantMessage, &buf[4]);
                do_template("files", NULL);
+               output_headers(0, 0, 0, 0, 0, 0);
+               end_burst();
                return;
        }
 
@@ -314,6 +340,8 @@ void upload_file(void)
        serv_getln(buf, sizeof buf);
        strcpy(WCC->ImportantMessage, &buf[4]);
        do_template("files", CTX_NONE);
+       output_headers(0, 0, 0, 0, 0, 0);
+       end_burst();
 }
 
 
@@ -324,22 +352,23 @@ void upload_file(void)
  */
 void output_image(void)
 {
-       char blank_gif[SIZ];
+       StrBuf *Buf;
        wcsession *WCC = WC;
-       char buf[SIZ];
        off_t bytes;
        const char *MimeType;
        
+       Buf = NewStrBuf();
        serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '2') {
-               bytes = extract_long(&buf[4], 0);
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL) == 2) {
+               StrBufCutLeft(Buf, 4);
+               bytes = StrBufExtract_long(Buf, 0, '|');
 
                /** Read it from the server */
                
-               if (read_server_binary(WCC->WBuf, bytes) > 0) {
+               if (read_server_binary(WCC->WBuf, bytes, Buf) > 0) {
                        serv_puts("CLOS");
-                       serv_getln(buf, sizeof buf);
+                       StrBuf_ServGetln(Buf);
                
                        MimeType = GuessMimeType (ChrPtr(WCC->WBuf), StrLength(WCC->WBuf));
                        /** Write it to the browser */
@@ -357,8 +386,9 @@ void output_image(void)
         * Instead of an ugly 404, send a 1x1 transparent GIF
         * when there's no such image on the server.
         */
-       snprintf (blank_gif, SIZ, "%s%s", static_dirs[0], "/blank.gif");
-       output_static(blank_gif);
+       StrBufPrintf (Buf, "%s%s", static_dirs[0], "/blank.gif");
+       //// TDODO output_static(ChrPtr(Buf));
+       FreeStrBuf(&Buf);
 }
 
 void 
@@ -408,8 +438,9 @@ InitModule_DOWNLOAD
 
        RegisterConditional(HKEY("COND:FILE:ISPIC"), 0, Conditional_FILE_ISPIC, CTX_FILELIST);
 
-       WebcitAddUrlHandler(HKEY("image"), output_image, 0);
-       WebcitAddUrlHandler(HKEY("display_mime_icon"), display_mime_icon , 0);
+       WebcitAddUrlHandler(HKEY("image"), output_image, ANONYMOUS);
+       WebcitAddUrlHandler(HKEY("display_mime_icon"), display_mime_icon , ANONYMOUS);
        WebcitAddUrlHandler(HKEY("download_file"), download_file, NEED_URL);
+       WebcitAddUrlHandler(HKEY("delete_file"), delete_file, NEED_URL);
        WebcitAddUrlHandler(HKEY("upload_file"), upload_file, 0);
 }