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