//
// This module handles HTTP transactions.
//
-// Copyright (c) 1996-2018 by the citadel.org team
+// Copyright (c) 1996-2021 by the citadel.org team
//
// This program is open source software. It runs great on the
// Linux operating system (and probably elsewhere). You can use,
// copy, and run it under the terms of the GNU General Public
-// License version 3. Richard Stallman is an asshole communist.
+// License version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
#include "webcit.h"
-/*
- * Write data to the HTTP client. Encrypt if necessary.
- */
+// Write data to the HTTP client. Encrypt if necessary.
int client_write(struct client_handle *ch, char *buf, int nbytes) {
if (is_https) {
return client_write_ssl(ch, buf, nbytes);
}
-/*
- * Read data from the HTTP client. Decrypt if necessary.
- * Returns number of bytes read, or -1 to indicate an error.
- */
+// Read data from the HTTP client. Decrypt if necessary.
+// Returns number of bytes read, or -1 to indicate an error.
int client_read(struct client_handle *ch, char *buf, int nbytes) {
if (is_https) {
return client_read_ssl(ch, buf, nbytes);
}
-/*
- * Read a newline-terminated line of text from the client.
- * Implemented in terms of client_read() and is therefore transparent...
- * Returns the string length or -1 for error.
- */
+// Read a newline-terminated line of text from the client.
+// Implemented in terms of client_read() and is therefore transparent...
+// Returns the string length or -1 for error.
int client_readline(struct client_handle *ch, char *buf, int maxbytes) {
int len = 0;
int c = 0;
}
-/*
- * printf() type function to send data to the web client.
- */
+// printf() type function to send data to the web client.
void client_printf(struct client_handle *ch, const char *format, ...) {
va_list arg_ptr;
StrBuf *Buf = NewStrBuf();
}
-/*
- * Push one new header into the response of an HTTP request.
- * When completed, the HTTP transaction now owns the memory allocated for key and val.
- */
+// Push one new header into the response of an HTTP request.
+// When completed, the HTTP transaction now owns the memory allocated for key and val.
void add_response_header(struct http_transaction *h, char *key, char *val) {
- struct http_header *new_response_header = malloc(sizeof(struct http_header));
+ struct key_val_list *new_response_header = malloc(sizeof(struct key_val_list));
new_response_header->key = key;
new_response_header->val = val;
new_response_header->next = h->response_headers;
}
-/*
- * Entry point for this layer. Socket is connected. If running as an HTTPS
- * server, SSL has already been negotiated. Now perform one transaction.
- */
+// Entry point for this layer. Socket is connected. If running as an HTTPS
+// server, SSL has already been negotiated. Now perform one transaction.
void perform_one_http_transaction(struct client_handle *ch) {
char buf[1024];
int len;
struct http_transaction h;
char *c, *d;
struct sockaddr_in sin;
- struct http_header *clh; // general purpose iterator variable
+ struct key_val_list *clh; // general purpose iterator variable
memset(&h, 0, sizeof h);
while (len = client_readline(ch, buf, sizeof buf), (len > 0)) {
++lines_read;
- if (lines_read == 1) { // First line is method and URI.
+ if (lines_read == 1) { // First line is method and URL.
c = strchr(buf, ' '); // IGnore the HTTP-version.
if (c == NULL) {
h.method = strdup("NULL");
- h.uri = strdup("/");
+ h.url = strdup("/");
}
else {
*c = 0;
*c = 0;
}
++c;
- h.uri = strdup(d);
+ h.url = strdup(d);
}
}
else { // Subsequent lines are headers.
c = strchr(buf, ':'); // Header line folding is obsolete so we don't support it.
if (c != NULL) {
- struct http_header *new_request_header = malloc(sizeof(struct http_header));
+ struct key_val_list *new_request_header = malloc(sizeof(struct key_val_list));
*c = 0;
new_request_header->key = strdup(buf);
++c;
new_request_header->next = h.request_headers;
h.request_headers = new_request_header;
#ifdef DEBUG_HTTP
- syslog(LOG_DEBUG, "{ %s: %s", new_request_header->key, new_request_header->val);
+ syslog(LOG_DEBUG, "\033[1m\033[35m{ %s: %s\033[0m", new_request_header->key, new_request_header->val);
#endif
}
}
syslog(LOG_DEBUG, "Client disconnected");
}
else {
- syslog(LOG_DEBUG, "< %s %s", h.method, h.uri);
+ syslog(LOG_DEBUG, "\033[33m\033[1m< %s %s\033[0m", h.method, h.url);
// If there is a request body, read it now.
char *ccl = header_val(&h, "Content-Length");
client_printf(ch, "Content-encoding: identity\r\n"); // change if we gzip/deflate
for (clh = h.response_headers; clh != NULL; clh = clh->next) {
#ifdef DEBUG_HTTP
- syslog(LOG_DEBUG, "} %s: %s", clh->key, clh->val);
+ syslog(LOG_DEBUG, "\033[1m\033[35m} %s: %s\033[0m", clh->key, clh->val);
#endif
client_printf(ch, "%s: %s\r\n", clh->key, clh->val);
}
}
if (h.method)
free(h.method);
- if (h.uri)
- free(h.uri);
+ if (h.url)
+ free(h.url);
if (h.request_body)
free(h.request_body);
if (h.response_string)
}
-/*
- * Utility function to fetch a specific header from a completely read-in request.
- * Returns the value of the requested header, or NULL if the specified header was not sent.
- * The caller does NOT own the memory of the returned pointer, but can count on the pointer
- * to still be valid through the end of the transaction.
- */
+// Utility function to fetch a specific header from a completely read-in request.
+// Returns the value of the requested header, or NULL if the specified header was not sent.
+// The caller does NOT own the memory of the returned pointer, but can count on the pointer
+// to still be valid through the end of the transaction.
char *header_val(struct http_transaction *h, char *requested_header) {
- struct http_header *clh; // general purpose iterator variable
+ struct key_val_list *clh; // general purpose iterator variable
for (clh = h->request_headers; clh != NULL; clh = clh->next) {
if (!strcasecmp(clh->key, requested_header)) {
return (clh->val);