Force downloads to use a MIME type of application/octet-stream
[citadel.git] / webcit / downloads.c
1 /*
2  * $Id: downloads.c 4849 2007-01-08 20:05:56Z ajc $
3  */
4 #include "webcit.h"
5
6 void display_room_directory(void)
7 {
8         char buf[1024];
9         char filename[256];
10         char filesize[256];
11         char comment[512];
12         int bg = 0;
13         char title[256];
14
15         output_headers(1, 1, 2, 0, 0, 0);
16         wprintf("<div id=\"banner\">\n"
17                 "<table class=\"downloads_banner\"><tr><td>"
18                 "<span class=\"titlebar\">");
19         snprintf(title, sizeof title, _("Files available for download in %s"), WC->wc_roomname);
20         escputs(title);
21         wprintf("</span>"
22                 "</td></tr></table>\n"
23                 "</div>\n<div id=\"content\">\n"
24         );
25
26         wprintf("<div class=\"fix_scrollbar_bug\">"
27                 "<table class=\"downloads_background\"><tr><td>\n");
28         wprintf("<tr><th>%s</th><th>%s</th><th>%s</th></tr>\n",
29                         _("Filename"),
30                         _("Size"),
31                         _("Description")
32         );
33
34         serv_puts("RDIR");
35         serv_getln(buf, sizeof buf);
36         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
37         {
38                 extract_token(filename, buf, 0, '|', sizeof filename);
39                 extract_token(filesize, buf, 1, '|', sizeof filesize);
40                 extract_token(comment, buf, 2, '|', sizeof comment);
41                 bg = 1 - bg;
42                 wprintf("<tr bgcolor=\"#%s\">", (bg ? "DDDDDD" : "FFFFFF"));
43                 wprintf("<td>"
44                         "<a href=\"download_file/");
45                 urlescputs(filename);
46                 wprintf("\"><img src=\"static/diskette_24x.gif\" border=0 align=middle>\n");
47                                         escputs(filename);      wprintf("</a></td>");
48                 wprintf("<td>");        escputs(filesize);      wprintf("</td>");
49                 wprintf("<td>");        escputs(comment);       wprintf("</td>");
50                 wprintf("</tr>\n");
51         }
52
53         wprintf("</table></div>\n");
54         wDumpContent(1);
55 }
56
57
58 void download_file(char *filename)
59 {
60         char buf[256];
61         off_t bytes;
62         char content_type[256];
63         char *content = NULL;
64
65         /* Setting to nonzero forces a MIME type of application/octet-stream */
66         int force_download = 1;
67         
68         serv_printf("OPEN %s", filename);
69         serv_getln(buf, sizeof buf);
70         if (buf[0] == '2') {
71                 bytes = extract_long(&buf[4], 0);
72                 content = malloc(bytes + 2);
73                 if (force_download) {
74                         strcpy(content_type, "application/octet-stream");
75                 }
76                 else {
77                         extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
78                 }
79                 output_headers(0, 0, 0, 0, 0, 0);
80                 read_server_binary(content, bytes);
81                 serv_puts("CLOS");
82                 serv_getln(buf, sizeof buf);
83                 http_transmit_thing(content, bytes, content_type, 0);
84                 free(content);
85         } else {
86                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
87                 output_headers(0, 0, 0, 0, 0, 0);
88                 wprintf("Content-Type: text/plain\r\n");
89                 wprintf("\r\n");
90                 wprintf(_("An error occurred while retrieving this file: %s\n"), &buf[4]);
91         }
92
93 }
94