2 // Output static content
4 // Copyright (c) 1996-2021 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.
19 // Called from perform_request() to handle the /ctdl/s/ prefix -- always static content.
20 void output_static(struct http_transaction *h) {
21 char filename[PATH_MAX];
24 snprintf(filename, sizeof filename, "static/%s", &h->uri[8]);
26 if (strstr(filename, "../")) { // 100% guaranteed attacker.
27 do_404(h); // Die in a car fire.
31 FILE *fp = fopen(filename, "r"); // Try to open the requested file.
37 fstat(fileno(fp), &statbuf);
38 h->response_body_length = statbuf.st_size;
39 h->response_body = malloc(h->response_body_length);
40 if (h->response_body != NULL) {
41 fread(h->response_body, h->response_body_length, 1, fp);
44 h->response_body_length = 0;
46 fclose(fp); // Content is now in memory.
48 h->response_code = 200;
49 h->response_string = strdup("OK");
50 add_response_header(h, strdup("Content-type"), strdup(GuessMimeByFilename(filename, strlen(filename))));
52 char *datestring = http_datestring(statbuf.st_mtime);
54 add_response_header(h, strdup("Last-Modified"), datestring);