upload: change REST path from /ctdl/a/upload/ to /ctdl/p/
[citadel.git] / webcit-ng / server / request.c
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
5 // the client.
6 //
7 // Copyright (c) 1996-2023 by the citadel.org team
8 //
9 // This program is open source software.  Use, duplication, or
10 // disclosure are subject to the GNU General Public License v3.
11
12 #include "webcit.h"
13
14
15 // Not found!  Wowzers.
16 void do_404(struct http_transaction *h) {
17         h->response_code = 404;
18         h->response_string = strdup("NOT FOUND");
19         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
20         h->response_body = strdup("404 NOT FOUND\r\n");
21         h->response_body_length = strlen(h->response_body);
22 }
23
24
25 // Precondition failed (such as if-match)
26 void do_412(struct http_transaction *h) {
27         h->response_code = 412;
28         h->response_string = strdup("PRECONDITION FAILED");
29 }
30
31
32 // Succeed with no output
33 void do_204(struct http_transaction *h) {
34         h->response_code = 204;
35         h->response_string = strdup("No content");
36 }
37
38
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"));
44         h->response_body = strdup(_("This program was unable to connect or stay connected to the Citadel server.  Please report this problem to your system administrator."));
45         h->response_body_length = strlen(h->response_body);
46 }
47
48
49 // Tell the client to authenticate using HTTP-AUTH (RFC 2617)
50 void request_http_authenticate(struct http_transaction *h) {
51         h->response_code = 401;
52         h->response_string = strdup("Unauthorized");
53         add_response_header(h, strdup("WWW-Authenticate"), strdup("Basic realm=\"Citadel Server\""));
54 }
55
56
57 // Easy and fun utility function to throw a redirect.
58 void http_redirect(struct http_transaction *h, char *to_where) {
59         syslog(LOG_DEBUG, "Redirecting to: %s", to_where);
60         h->response_code = 302;
61         h->response_string = strdup("Moved Temporarily");
62         add_response_header(h, strdup("Location"), strdup(to_where));
63         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
64         h->response_body = strdup(to_where);
65         h->response_body_length = strlen(h->response_body);
66 }
67
68
69 // perform_request() is the entry point for *every* HTTP transaction.
70 void perform_request(struct http_transaction *h) {
71         struct ctdlsession *c;
72
73         // Determine which code path to take based on the beginning of the URL.
74         // This is implemented as a series of strncasecmp() calls rather than a
75         // lookup table in order to make the code more readable.
76
77         if (IsEmptyStr(h->url)) {       // Sanity check
78                 do_404(h);
79                 return;
80         }
81
82         // Right about here is where we should try to handle anything that doesn't start
83         // with the /ctdl/ prefix.
84         // Root (/) ...
85
86         if ((!strcmp(h->url, "/")) && (!strcasecmp(h->method, "GET"))) {
87                 http_redirect(h, "/ctdl/s/index.html");
88                 return;
89         }
90
91         // Legacy URL patterns (/readnew?gotoroom=xxx&start_reading_at=yyy) ...
92         // Direct room name (/my%20blog) ...
93
94         // HTTP-01 challenge [RFC5785 section 3, RFC8555 section 9.2]
95         if (!strncasecmp(h->url, HKEY("/.well-known"))) {       // Static content
96                 output_static(h);
97                 return;
98         }
99
100         if (!strcasecmp(h->url, "/favicon.ico")) {
101                 output_static(h);
102                 return;
103         }
104
105         // Everything below this line is strictly REST URL patterns.
106
107         if (strncasecmp(h->url, HKEY("/ctdl/"))) {              // Reject non-REST
108                 do_404(h);
109                 return;
110         }
111
112         if (!strncasecmp(h->url, HKEY("/ctdl/s/"))) {           // Static content
113                 output_static(h);
114                 return;
115         }
116
117         if (h->url[7] != '/') {
118                 do_404(h);
119                 return;
120         }
121
122         // Anything below this line:
123         //      1. Is in the format of /ctdl/?/*
124         //      2. Requires a connection to a Citadel server.
125
126         c = connect_to_citadel(h);
127         if (c == NULL) {
128                 do_502(h);
129                 return;
130         }
131
132         // WebDAV methods like OPTIONS and PROPFIND *require* a logged-in session,
133         // even if the Citadel server allows anonymous access.
134         if (IsEmptyStr(c->auth)) {
135                 if (       (!strcasecmp(h->method, "OPTIONS"))
136                         || (!strcasecmp(h->method, "PROPFIND"))
137                         || (!strcasecmp(h->method, "REPORT"))
138                         || (!strcasecmp(h->method, "DELETE"))
139                         || (!strcasecmp(h->method, "MOVE"))
140                         || (!strcasecmp(h->method, "COPY"))
141                 ) {
142                         request_http_authenticate(h);
143                         disconnect_from_citadel(c);
144                         return;
145                 }
146         }
147
148         // Break down the URL by path and send the request to the appropriate part of the program.
149         switch (h->url[6]) {
150         case 'a':               // /ctdl/a/ == RESTful path to admin functions
151                 ctdl_a(h, c);
152                 break;
153         case 'c':               // /ctdl/c/ == misc Citadel server commands
154                 ctdl_c(h, c);
155                 break;
156         case 'f':               // /ctdl/f/ == RESTful path to floors
157                 ctdl_f(h, c);
158                 break;
159         case 'r':               // /ctdl/r/ == RESTful path to rooms
160                 ctdl_r(h, c);
161                 break;
162         case 'u':               // /ctdl/u/ == RESTful path to users
163                 ctdl_u(h, c);
164                 break;
165         case 'p':               // /ctdl/p/ == RESTful path to upload functions
166                 ctdl_p(h, c);
167                 break;
168         default:
169                 do_404(h);
170         }
171
172         // Are we in an authenticated session?  If so, set a cookie so we stay logged in.
173         if (!IsEmptyStr(c->auth)) {
174                 char koekje[AUTH_MAX];
175                 char *exp = http_datestring(time(NULL) + (86400 * 30));
176                 snprintf(koekje, AUTH_MAX, "wcauth=%s; path=/ctdl/; Expires=%s", c->auth, exp); // warn
177                 free(exp);
178                 add_response_header(h, strdup("Set-Cookie"), strdup(koekje));
179         }
180
181         // Durlng development we are foiling the browser cache completely.  In production we'll be more selective.
182         add_response_header(h, strdup("Cache-Control"), strdup("no-store, must-revalidate"));
183         add_response_header(h, strdup("Pragma"), strdup("no-cache"));
184         add_response_header(h, strdup("Expires"), strdup("0"));
185
186         // Unbind from our Citadel server connection.
187         disconnect_from_citadel(c);
188 }