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