Change all instances of "URI" to "URL" because that's more sensible
[citadel.git] / webcit-ng / static.c
1 //
2 // Output static content
3 //
4 // Copyright (c) 1996-2021 by the citadel.org team
5 //
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
9 // License version 3.
10 //
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.
15
16 #include "webcit.h"
17
18
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];
22         struct stat statbuf;
23
24         snprintf(filename, sizeof filename, "static/%s", &h->url[8]);
25
26         if (strstr(filename, "../")) {          // 100% guaranteed attacker.
27                 do_404(h);                      // Die in a car fire.
28                 return;
29         }
30
31         FILE *fp = fopen(filename, "r");        // Try to open the requested file.
32         if (fp == NULL) {
33                 do_404(h);
34                 return;
35         }
36
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);
42         }
43         else {
44                 h->response_body_length = 0;
45         }
46         fclose(fp);                             // Content is now in memory.
47
48         h->response_code = 200;
49         h->response_string = strdup("OK");
50         add_response_header(h, strdup("Content-type"), strdup(GuessMimeByFilename(filename, strlen(filename))));
51
52         char *datestring = http_datestring(statbuf.st_mtime);
53         if (datestring) {
54                 add_response_header(h, strdup("Last-Modified"), datestring);
55         }
56 }