2 // This module handles HTTP transactions.
4 // Copyright (c) 1996-2018 by the citadel.org team
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
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.
19 * Write data to the HTTP client. Encrypt if necessary.
21 int client_write(struct client_handle *ch, char *buf, int nbytes) {
23 return client_write_ssl(ch, buf, nbytes);
26 return write(ch->sock, buf, nbytes);
32 * Read data from the HTTP client. Decrypt if necessary.
33 * Returns number of bytes read, or -1 to indicate an error.
35 int client_read(struct client_handle *ch, char *buf, int nbytes) {
37 return client_read_ssl(ch, buf, nbytes);
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) {
47 bytes_received += bytes_this_block;
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.
59 int client_readline(struct client_handle *ch, char *buf, int maxbytes) {
67 while (len < maxbytes) {
68 c = client_read(ch, &buf[len], 1);
70 syslog(LOG_DEBUG, "Socket error or zero-length read");
73 if (buf[len] == '\n') {
74 if ((len > 0) && (buf[len - 1] == '\r')) {
87 * printf() type function to send data to the web client.
89 void client_printf(struct client_handle *ch, const char *format, ...) {
91 StrBuf *Buf = NewStrBuf();
93 va_start(arg_ptr, format);
94 StrBufVAppendPrintf(Buf, format, arg_ptr);
97 client_write(ch, (char *) ChrPtr(Buf), StrLength(Buf));
103 * Push one new header into the response of an HTTP request.
104 * When completed, the HTTP transaction now owns the memory allocated for key and val.
106 void add_response_header(struct http_transaction *h, char *key, char *val) {
107 struct http_header *new_response_header = malloc(sizeof(struct http_header));
108 new_response_header->key = key;
109 new_response_header->val = val;
110 new_response_header->next = h->response_headers;
111 h->response_headers = new_response_header;
116 * Entry point for this layer. Socket is connected. If running as an HTTPS
117 * server, SSL has already been negotiated. Now perform one transaction.
119 void perform_one_http_transaction(struct client_handle *ch) {
123 struct http_transaction h;
125 struct sockaddr_in sin;
126 struct http_header *clh; // general purpose iterator variable
128 memset(&h, 0, sizeof h);
130 while (len = client_readline(ch, buf, sizeof buf), (len > 0)) {
133 if (lines_read == 1) { // First line is method and URI.
134 c = strchr(buf, ' '); // IGnore the HTTP-version.
136 h.method = strdup("NULL");
141 h.method = strdup(buf);
152 else { // Subsequent lines are headers.
153 c = strchr(buf, ':'); // Header line folding is obsolete so we don't support it.
155 struct http_header *new_request_header = malloc(sizeof(struct http_header));
157 new_request_header->key = strdup(buf);
159 new_request_header->val = strdup(c);
160 striplt(new_request_header->key);
161 striplt(new_request_header->val);
162 new_request_header->next = h.request_headers;
163 h.request_headers = new_request_header;
165 syslog(LOG_DEBUG, "{ %s: %s", new_request_header->key, new_request_header->val);
172 // build up the site prefix, such as https://foo.bar.com:4343
173 h.site_prefix = malloc(256);
174 strcpy(h.site_prefix, (is_https ? "https://" : "http://"));
175 char *hostheader = header_val(&h, "Host");
177 strcat(h.site_prefix, hostheader);
180 strcat(h.site_prefix, "127.0.0.1");
182 socklen_t llen = sizeof(sin);
183 if (getsockname(ch->sock, (struct sockaddr *) &sin, &llen) >= 0) {
184 sprintf(&h.site_prefix[strlen(h.site_prefix)], ":%d", ntohs(sin.sin_port));
187 // Now try to read in the request body (if one is present)
189 syslog(LOG_DEBUG, "Client disconnected");
192 syslog(LOG_DEBUG, "< %s %s", h.method, h.uri);
194 // If there is a request body, read it now.
195 char *ccl = header_val(&h, "Content-Length");
197 h.request_body_length = atol(ccl);
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);
204 //write(2, HKEY("\033[31m"));
205 //write(2, h.request_body, h.request_body_length);
206 //write(2, HKEY("\033[0m\n"));
209 // Now pass control up to the next layer to perform the request.
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"));
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));
223 client_printf(ch, "Date: %s\r\n", datestring);
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) {
230 syslog(LOG_DEBUG, "} %s: %s", clh->key, clh->val);
232 client_printf(ch, "%s: %s\r\n", clh->key, clh->val);
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);
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;
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;
264 free(h.request_body);
265 if (h.response_string)
266 free(h.response_string);
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.
278 char *header_val(struct http_transaction *h, char *requested_header) {
279 struct http_header *clh; // general purpose iterator variable
280 for (clh = h->request_headers; clh != NULL; clh = clh->next) {
281 if (!strcasecmp(clh->key, requested_header)) {