2 // Output static content
4 // Copyright (c) 1996-2018 by the citadel.org team
6 // This program is open source software. It runs great on the
7 // Linux operating system (and probably elsewhere). You can use,
8 // copy, and run it under the terms of the GNU General Public
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
20 * Called from perform_request() to handle the /ctdl/s/ prefix -- always static content.
22 void output_static(struct http_transaction *h)
24 char filename[PATH_MAX];
27 snprintf(filename, sizeof filename, "static/%s", &h->uri[8]);
29 if (strstr(filename, "../")) { // 100% guaranteed attacker.
30 do_404(h); // Die in a car fire.
34 FILE *fp = fopen(filename, "r"); // Try to open the requested file.
40 fstat(fileno(fp), &statbuf);
41 h->response_body_length = statbuf.st_size;
42 h->response_body = malloc(h->response_body_length);
43 if (h->response_body != NULL) {
44 fread(h->response_body, h->response_body_length, 1, fp);
46 h->response_body_length = 0;
48 fclose(fp); // Content is now in memory.
50 h->response_code = 200;
51 h->response_string = strdup("OK");
52 add_response_header(h, strdup("Content-type"), strdup(GuessMimeByFilename(filename, strlen(filename))));
54 char *datestring = http_datestring(statbuf.st_mtime);
56 add_response_header(h, strdup("Last-Modified"), datestring);