Skeleton code for filters.
[citadel.git] / webcit-ng / server / http.c
1 // This module handles HTTP transactions.
2 //
3 // Copyright (c) 1996-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
6
7 #include "webcit.h"
8
9 // Write data to the HTTP client.  Encrypt if necessary.
10 int client_write(struct client_handle *ch, char *buf, int nbytes) {
11         if (is_https) {
12                 return client_write_ssl(ch, buf, nbytes);
13         }
14         else {
15                 return write(ch->sock, buf, nbytes);
16         }
17 }
18
19
20 // Read data from the HTTP client.  Decrypt if necessary.
21 // Returns number of bytes read, or -1 to indicate an error.
22 int client_read(struct client_handle *ch, char *buf, int nbytes) {
23         if (is_https) {
24                 return client_read_ssl(ch, buf, nbytes);
25         }
26         else {
27                 int bytes_received = 0;
28                 int bytes_this_block = 0;
29                 while (bytes_received < nbytes) {
30                         bytes_this_block = read(ch->sock, &buf[bytes_received], nbytes - bytes_received);
31                         if (bytes_this_block < 1) {
32                                 return (-1);
33                         }
34                         bytes_received += bytes_this_block;
35                 }
36                 return (nbytes);
37         }
38 }
39
40
41 // Read a newline-terminated line of text from the client.
42 // Implemented in terms of client_read() and is therefore transparent...
43 // Returns the string length or -1 for error.
44 int client_readline(struct client_handle *ch, char *buf, int maxbytes) {
45         int len = 0;
46         int c = 0;
47
48         if (buf == NULL) {
49                 return (-1);
50         }
51
52         while (len < maxbytes) {
53                 c = client_read(ch, &buf[len], 1);
54                 if (c <= 0) {
55                         syslog(LOG_DEBUG, "Socket error or zero-length read");
56                         return (-1);
57                 }
58                 if (buf[len] == '\n') {
59                         if ((len > 0) && (buf[len - 1] == '\r')) {
60                                 --len;
61                         }
62                         buf[len] = 0;
63                         return (len);
64                 }
65                 ++len;
66         }
67         return (len);
68 }
69
70
71 // printf() type function to send data to the web client.
72 void client_printf(struct client_handle *ch, const char *format, ...) {
73         va_list arg_ptr;
74         StrBuf *Buf = NewStrBuf();
75
76         va_start(arg_ptr, format);
77         StrBufVAppendPrintf(Buf, format, arg_ptr);
78         va_end(arg_ptr);
79
80         client_write(ch, (char *) ChrPtr(Buf), StrLength(Buf));
81         FreeStrBuf(&Buf);
82 }
83
84
85 // Push one new header into the response of an HTTP request.
86 // When completed, the HTTP transaction now owns the memory allocated for key and val.
87 void add_response_header(struct http_transaction *h, char *key, char *val) {
88         struct keyval new_response_header;
89         new_response_header.key = key;
90         new_response_header.val = val;
91         array_append(h->response_headers, &new_response_header);
92 }
93
94
95 // Entry point for this layer.  Socket is connected.  If running as an HTTPS
96 // server, SSL has already been negotiated.  Now perform one transaction.
97 void perform_one_http_transaction(struct client_handle *ch) {
98         char buf[1024];
99         int len;
100         int lines_read = 0;
101         struct http_transaction h;
102         char *c, *d;
103         struct sockaddr_in sin;
104         int i;                                  // general purpose iterator variable
105
106         memset(&h, 0, sizeof h);
107         h.request_headers = array_new(sizeof(struct keyval));
108         h.request_parms = array_new(sizeof(struct keyval));
109         h.response_headers = array_new(sizeof(struct keyval));
110
111         while (len = client_readline(ch, buf, sizeof buf), (len > 0)) {
112                 ++lines_read;
113
114                 if (lines_read == 1) {          // First line is method and URL.
115                         c = strchr(buf, ' ');   // IGnore the HTTP-version.
116                         if (c == NULL) {
117                                 h.method = strdup("NULL");
118                                 h.url = strdup("/");
119                         }
120                         else {
121                                 *c = 0;
122                                 h.method = strdup(buf);
123                                 ++c;
124                                 d = c;
125                                 c = strchr(d, ' ');
126                                 if (c != NULL) {
127                                         *c = 0;
128                                 }
129                                 ++c;
130                                 h.url = strdup(d);
131                         }
132                 }
133                 else {                          // Subsequent lines are headers.
134                         c = strchr(buf, ':');   // Header line folding is obsolete so we don't support it.
135                         if (c != NULL) {
136
137                                 struct keyval new_request_header;
138                                 *c = 0;
139                                 new_request_header.key = strdup(buf);
140                                 ++c;
141                                 new_request_header.val = strdup(c);
142                                 string_trim(new_request_header.key);
143                                 string_trim(new_request_header.val);
144                                 array_append(h.request_headers, &new_request_header);
145 #ifdef DEBUG_HTTP
146                                 syslog(LOG_DEBUG, "{ %s: %s", new_request_header.key, new_request_header.val);
147 #endif
148                         }
149                 }
150
151         }
152
153         // If the URL had any query parameters in it, parse them out now.
154         char *p = (h.url ? strchr(h.url, '?') : NULL);
155         if (p) {
156                 *p++ = 0;                                               // insert a null to remove parameters from the URL
157                 char *tok, *saveptr = NULL;
158                 for (tok = strtok_r(p, "&", &saveptr); tok!=NULL; tok = strtok_r(NULL, "&", &saveptr)) {
159                         char *eq = strchr(tok, '=');
160                         if (eq) {
161                                 *eq++ = 0;
162                                 unescape_input(eq);
163                                 struct keyval kv;
164                                 kv.key = strdup(tok);
165                                 kv.val = strdup(eq);
166                                 array_append(h.request_parms, &kv);
167 #ifdef DEBUG_HTTP
168                                 syslog(LOG_DEBUG, "| %s = %s", kv.key, kv.val);
169 #endif
170                         }
171                 }
172         }
173
174         // build up the site prefix, such as https://foo.bar.com:4343
175         h.site_prefix = malloc(256);
176         strcpy(h.site_prefix, (is_https ? "https://" : "http://"));
177         char *hostheader = header_val(&h, "Host");
178         if (hostheader) {
179                 strcat(h.site_prefix, hostheader);
180         }
181         else {
182                 strcat(h.site_prefix, "127.0.0.1");
183         }
184         socklen_t llen = sizeof(sin);
185         if (getsockname(ch->sock, (struct sockaddr *) &sin, &llen) >= 0) {
186                 sprintf(&h.site_prefix[strlen(h.site_prefix)], ":%d", ntohs(sin.sin_port));
187         }
188
189         // Now try to read in the request body (if one is present)
190         if (len < 0) {
191                 syslog(LOG_DEBUG, "Client disconnected");
192         }
193         else {
194 #ifdef DEBUG_HTTP
195                 syslog(LOG_DEBUG, "< %s %s", h.method, h.url);
196 #endif
197
198                 // If there is a request body, read it now.
199                 char *ccl = header_val(&h, "Content-Length");
200                 if (ccl) {
201                         h.request_body_length = atol(ccl);
202                 }
203                 if (h.request_body_length > 0) {
204                         syslog(LOG_DEBUG, "Reading request body (%ld bytes)", h.request_body_length);
205
206                         // Sometimes we need the request body by itself, sometimes we need it with headers.
207                         // So we save it with synthetic headers, and also provide a pointer into the place where the body begins.
208
209                         char *cct = header_val(&h, "Content-Type");
210                         if (cct) {
211                                 h.request_body_with_synth_headers = malloc(h.request_body_length + 1024);
212                                 memset(h.request_body_with_synth_headers, h.request_body_length + 1024, 0);
213                                 sprintf(h.request_body_with_synth_headers, "Content-Type: %s\r\n\r\n", cct);
214                                 h.request_body = h.request_body_with_synth_headers + strlen(h.request_body_with_synth_headers);
215                         }
216                         else {  // a request body absent a Content-Type: header is invalid, but handle it anyway.
217                                 h.request_body_with_synth_headers = malloc(h.request_body_length);
218                                 memset(h.request_body_with_synth_headers, h.request_body_length, 0);
219                                 h.request_body = h.request_body_with_synth_headers;
220                         }
221
222                         client_read(ch, h.request_body, h.request_body_length);
223
224                         // Write the entire request body to stderr -- not what you want during normal operation.
225                         #ifdef REQUEST_BODY_TO_STDERR
226                         write(2, HKEY("--- REQUEST BODY BEGIN ---\n"));
227                         write(2, h.request_body, h.request_body_length);
228                         write(2, HKEY("--- REQUEST BODY END ---\n"));
229                         #endif
230
231                 }
232
233                 // Now pass control up to the next layer to perform the request.
234                 perform_request(&h);
235
236                 // Write the entire response body to stderr -- not what you want during normal operation.
237                 #ifdef RESPONSE_BODY_TO_STDERR
238                 write(2, HKEY("--- RESPONSE BODY BEGIN ---\n"));
239                 write(2, h.response_body, h.response_body_length);
240                 write(2, HKEY("--- RESPONSE BODY END ---\n"));
241                 #endif
242
243                 // Output the results back to the client.
244 #ifdef DEBUG_HTTP
245                 syslog(LOG_DEBUG, "> %03d %s", h.response_code, h.response_string);
246 #endif
247                 client_printf(ch, "HTTP/1.1 %03d %s\r\n", h.response_code, h.response_string);
248                 client_printf(ch, "Connection: close\r\n");
249                 client_printf(ch, "Content-Length: %ld\r\n", h.response_body_length);
250                 char *datestring = http_datestring(time(NULL));
251                 if (datestring) {
252                         client_printf(ch, "Date: %s\r\n", datestring);
253                         free(datestring);
254                 }
255
256                 client_printf(ch, "Content-encoding: identity\r\n");    // change if we gzip/deflate
257                 int number_of_response_headers = array_len(h.response_headers);
258                 for (i=0; i<number_of_response_headers; ++i) {
259                         struct keyval *kv = array_get_element_at(h.response_headers, i);
260 #ifdef DEBUG_HTTP
261                         syslog(LOG_DEBUG, "} %s: %s", kv->key, kv->val);
262 #endif
263                         client_printf(ch, "%s: %s\r\n", kv->key, kv->val);
264                 }
265                 client_printf(ch, "\r\n");
266                 if ((h.response_body_length > 0) && (h.response_body != NULL)) {
267                         client_write(ch, h.response_body, h.response_body_length);
268                 }
269         }
270
271         // free the transaction memory
272         while (array_len(h.request_headers) > 0) {
273                 struct keyval *kv = array_get_element_at(h.request_headers, 0);
274                 if (kv->key) free(kv->key);
275                 if (kv->val) free(kv->val);
276                 array_delete_element_at(h.request_headers, 0);
277         }
278         array_free(h.request_headers);
279         while (array_len(h.request_parms) > 0) {
280                 struct keyval *kv = array_get_element_at(h.request_parms, 0);
281                 if (kv->key) free(kv->key);
282                 if (kv->val) free(kv->val);
283                 array_delete_element_at(h.request_parms, 0);
284         }
285         array_free(h.request_parms);
286         while (array_len(h.response_headers) > 0) {
287                 struct keyval *kv = array_get_element_at(h.response_headers, 0);
288                 if (kv->key) free(kv->key);
289                 if (kv->val) free(kv->val);
290                 array_delete_element_at(h.response_headers, 0);
291         }
292         array_free(h.response_headers);
293         if (h.method) {
294                 free(h.method);
295         }
296         if (h.url) {
297                 free(h.url);
298         }
299         if (h.request_body_with_synth_headers) {
300                 free(h.request_body_with_synth_headers);
301         }
302         if (h.response_string) {
303                 free(h.response_string);
304         }
305         if (h.site_prefix) {
306                 free(h.site_prefix);
307         }
308 }
309
310
311 // Utility function to fetch a specific header from a completely read-in request.
312 // Returns the value of the requested header, or NULL if the specified header was not sent.
313 // The caller does NOT own the memory of the returned pointer, but can count on the pointer
314 // to still be valid through the end of the transaction.
315 char *header_val(struct http_transaction *h, char *requested_header) {
316         struct keyval *kv;
317         int i;
318         for (i=0; i<array_len(h->request_headers); ++i) {
319                 kv = array_get_element_at(h->request_headers, i);
320                 if (!strcasecmp(kv->key, requested_header)) {
321                         return (kv->val);
322                 }
323         }
324         return (NULL);
325 }
326
327
328 // Utility function to fetch a specific URL parameter from a completely read-in request.
329 // Returns the value of the requested parameter, or NULL if the specified parameter was not sent.
330 // The caller does NOT own the memory of the returned pointer, but can count on the pointer
331 // to still be valid through the end of the transaction.
332 char *get_url_param(struct http_transaction *h, char *requested_param) {
333         struct keyval *kv;
334         int i;
335         for (i=0; i<array_len(h->request_parms); ++i) {
336                 kv = array_get_element_at(h->request_parms, i);
337                 if (!strcasecmp(kv->key, requested_param)) {
338                         return (kv->val);
339                 }
340         }
341         return (NULL);
342 }