1 // This module sits directly above the HTTP layer. By the time we get here,
2 // an HTTP request has been fully received and parsed. Control is passed up
3 // to this layer to actually perform the request. We then fill in the response
4 // and pass control back down to the HTTP layer to output the response back to
7 // Copyright (c) 1996-2021 by the citadel.org team
9 // This program is open source software. It runs great on the
10 // Linux operating system (and probably elsewhere). You can use,
11 // copy, and run it under the terms of the GNU General Public
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
22 // Not found! Wowzers.
23 void do_404(struct http_transaction *h) {
24 h->response_code = 404;
25 h->response_string = strdup("NOT FOUND");
26 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
27 h->response_body = strdup("404 NOT FOUND\r\n");
28 h->response_body_length = strlen(h->response_body);
32 // Precondition failed (such as if-match)
33 void do_412(struct http_transaction *h) {
34 h->response_code = 412;
35 h->response_string = strdup("PRECONDITION FAILED");
39 // We throw an HTTP error "502 bad gateway" when we need to connect to Citadel, but can't.
40 void do_502(struct http_transaction *h) {
41 h->response_code = 502;
42 h->response_string = strdup("bad gateway");
43 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
46 ("This program was unable to connect or stay connected to the Citadel server. Please report this problem to your system administrator."));
47 h->response_body_length = strlen(h->response_body);
51 // Tell the client to authenticate using HTTP-AUTH (RFC 2617)
52 void request_http_authenticate(struct http_transaction *h) {
53 h->response_code = 401;
54 h->response_string = strdup("Unauthorized");
55 add_response_header(h, strdup("WWW-Authenticate"), strdup("Basic realm=\"Citadel Server\""));
59 // Easy and fun utility function to throw a redirect.
60 void http_redirect(struct http_transaction *h, char *to_where) {
61 syslog(LOG_DEBUG, "Redirecting to: %s", to_where);
62 h->response_code = 302;
63 h->response_string = strdup("Moved Temporarily");
64 add_response_header(h, strdup("Location"), strdup(to_where));
65 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
66 h->response_body = strdup(to_where);
67 h->response_body_length = strlen(h->response_body);
71 // perform_request() is the entry point for *every* HTTP transaction.
72 void perform_request(struct http_transaction *h) {
73 struct ctdlsession *c;
75 // Determine which code path to take based on the beginning of the URL.
76 // This is implemented as a series of strncasecmp() calls rather than a
77 // lookup table in order to make the code more readable.
79 if (IsEmptyStr(h->url)) { // Sanity check
84 // Right about here is where we should try to handle anything that doesn't start
85 // with the /ctdl/ prefix.
88 if ((!strcmp(h->url, "/")) && (!strcasecmp(h->method, "GET"))) {
89 http_redirect(h, "/ctdl/s/index.html");
93 // Legacy URL patterns (/readnew?gotoroom=xxx&start_reading_at=yyy) ...
94 // Direct room name (/my%20blog) ...
96 // Everything below this line is strictly REST URL patterns.
98 if (strncasecmp(h->url, HKEY("/ctdl/"))) { // Reject non-REST
103 if (!strncasecmp(h->url, HKEY("/ctdl/s/"))) { // Static content
108 if (h->url[7] != '/') {
113 // Anything below this line:
114 // 1. Is in the format of /ctdl/?/*
115 // 2. Requires a connection to a Citadel server.
117 c = connect_to_citadel(h);
123 // WebDAV methods like OPTIONS and PROPFIND *require* a logged-in session,
124 // even if the Citadel server allows anonymous access.
125 if (IsEmptyStr(c->auth)) {
126 if ((!strcasecmp(h->method, "OPTIONS"))
127 || (!strcasecmp(h->method, "PROPFIND"))
128 || (!strcasecmp(h->method, "REPORT"))
129 || (!strcasecmp(h->method, "DELETE"))
131 request_http_authenticate(h);
132 disconnect_from_citadel(c);
137 // Break down the URL by path and send the request to the appropriate part of the program.
139 case 'a': // /ctdl/a/ == RESTful path to admin functions
142 case 'c': // /ctdl/c/ == misc Citadel server commands
145 case 'r': // /ctdl/r/ == RESTful path to rooms
148 case 'u': // /ctdl/u/ == RESTful path to users
155 // Are we in an authenticated session? If so, set a cookie so we stay logged in.
156 if (!IsEmptyStr(c->auth)) {
157 char koekje[AUTH_MAX];
158 char *exp = http_datestring(time(NULL) + (86400 * 30));
159 snprintf(koekje, AUTH_MAX, "wcauth=%s; path=/ctdl/; Expires=%s", c->auth, exp); // warn
161 add_response_header(h, strdup("Set-Cookie"), strdup(koekje));
164 // Durlng development we are foiling the browser cache completely. In production we'll be more selective.
165 add_response_header(h, strdup("Cache-Control"), strdup("no-store, must-revalidate"));
166 add_response_header(h, strdup("Pragma"), strdup("no-cache"));
167 add_response_header(h, strdup("Expires"), strdup("0"));
169 // Unbind from our Citadel server connection.
170 disconnect_from_citadel(c);