1 // The functions in this file handle static pages and objects -- a basic web server.
3 // Copyright (c) 1996-2021 by the citadel.org team
5 // This program is open source software. You can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
17 #include "webserver.h"
19 unsigned char OnePixelGif[37] = {
20 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x01, 0x00,
21 0x01, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff,
22 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
23 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44,
28 void output_error_pic(const char *ErrMsg1, const char *ErrMsg2) {
29 hprintf("HTTP/1.1 200 %s\r\n", ErrMsg1);
30 hprintf("Content-Type: image/gif\r\n");
31 hprintf("x-webcit-errormessage: %s\r\n", ErrMsg2);
33 StrBufPlain(WC->WBuf, (const char *) OnePixelGif, sizeof(OnePixelGif));
38 * dump out static pages from disk
40 void output_static(char *prefix) {
44 const char *content_type;
48 snprintf(what, sizeof what, "./%s/%s", prefix, (char *) ChrPtr(WC->Hdr->HR.ReqLine));
50 syslog(LOG_DEBUG, "output_static(%s)", what);
52 content_type = GuessMimeByFilename(what, len);
53 fd = open(what, O_RDONLY);
55 syslog(LOG_INFO, "output_static('%s') [%s] : %s", what, ChrPtr(WC->Hdr->this_page), strerror(errno));
56 if (strstr(content_type, "image/") != NULL) {
57 output_error_pic("the file you requsted is gone.", strerror(errno));
60 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
61 hprintf("Content-Type: text/plain\r\n");
63 wc_printf("Cannot open %s: %s\r\n", what, strerror(errno));
68 if (fstat(fd, &statbuf) == -1) {
69 syslog(LOG_INFO, "output_static('%s') : %s", what, strerror(errno));
70 if (strstr(content_type, "image/") != NULL) {
71 output_error_pic("Stat failed!", strerror(errno));
74 hprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
75 hprintf("Content-Type: text/plain\r\n");
77 wc_printf("Cannot fstat %s: %s\n", what, strerror(errno));
85 bytes = statbuf.st_size;
87 if (StrBufReadBLOB(WC->WBuf, &fd, 1, bytes, &Err) < 0) {
90 syslog(LOG_INFO, "output_static('%s') -- FREAD FAILED (%s) --\n", what, strerror(errno));
91 hprintf("HTTP/1.1 500 internal server error \r\n");
92 hprintf("Content-Type: text/plain\r\n");
98 http_transmit_thing(content_type, 2);
100 if (yesbstr("force_close_session")) {
101 end_webcit_session();
109 void robots_txt(void) {
110 output_headers(0, 0, 0, 0, 0, 0);
112 hprintf("Content-type: text/plain\r\n" "Server: %s\r\n" "Connection: close\r\n", PACKAGE_STRING);
115 wc_printf("User-agent: *\r\n"
116 "Disallow: /printmsg\r\n"
117 "Disallow: /msgheaders\r\n"
118 "Disallow: /groupdav\r\n"
119 "Disallow: /do_template\r\n"
120 "Disallow: /static\r\n"
121 "Disallow: /display_page\r\n"
122 "Disallow: /readnew\r\n"
123 "Disallow: /display_enter\r\n"
124 "Disallow: /skip\r\n" "Disallow: /ungoto\r\n" "Sitemap: %s/sitemap.xml\r\n" "\r\n", ChrPtr(site_prefix)
131 // These are the various prefixes we can use to fetch static pages.
132 void output_static_root(void) {
135 void output_static_static(void) {
136 output_static("static");
138 void output_static_tinymce(void) {
139 output_static("tiny_mce");
141 void output_static_acme(void) {
142 output_static(".well-known");
146 void ServerStartModule_STATIC(void) {
150 void ServerShutdownModule_STATIC(void) {
153 void InitModule_STATIC(void) {
154 WebcitAddUrlHandler(HKEY("robots.txt"), "", 0, robots_txt, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC | LOGCHATTY);
155 WebcitAddUrlHandler(HKEY("favicon.ico"), "", 0, output_static_root, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC | LOGCHATTY);
156 WebcitAddUrlHandler(HKEY("static"), "", 0, output_static_static, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC | LOGCHATTY);
157 WebcitAddUrlHandler(HKEY("tinymce"), "", 0, output_static_tinymce, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC | LOGCHATTY);
158 WebcitAddUrlHandler(HKEY("tiny_mce"), "", 0, output_static_tinymce, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC | LOGCHATTY);
159 WebcitAddUrlHandler(HKEY(".well-known"), "", 0, output_static_acme, ANONYMOUS | COOKIEUNNEEDED | ISSTATIC | LOGCHATTY);