]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/http.c
HTTP headers are shown in purple in the syslog
[citadel.git] / webcit-ng / http.c
index 4c50306667c5bf87898987a3dacbea1ca09cd1bb..4a0211e63a855a0216aa647e4e733f2add45e2c0 100644 (file)
@@ -1,12 +1,12 @@
 //
 // 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
@@ -15,9 +15,7 @@
 
 #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);
@@ -28,10 +26,8 @@ int client_write(struct client_handle *ch, char *buf, int 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);
@@ -51,11 +47,9 @@ int client_read(struct client_handle *ch, char *buf, int 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;
@@ -83,9 +77,7 @@ int client_readline(struct client_handle *ch, char *buf, int maxbytes) {
 }
 
 
-/*
- * 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();
@@ -99,12 +91,10 @@ void client_printf(struct client_handle *ch, const char *format, ...) {
 }
 
 
-/*
- * 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;
@@ -112,10 +102,8 @@ void add_response_header(struct http_transaction *h, char *key, char *val) {
 }
 
 
-/*
- * 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;
@@ -123,18 +111,18 @@ void perform_one_http_transaction(struct client_handle *ch) {
        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;
@@ -146,13 +134,13 @@ void perform_one_http_transaction(struct client_handle *ch) {
                                        *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;
@@ -162,7 +150,7 @@ void perform_one_http_transaction(struct client_handle *ch) {
                                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
                        }
                }
@@ -189,7 +177,7 @@ void perform_one_http_transaction(struct client_handle *ch) {
                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");
@@ -227,7 +215,7 @@ void perform_one_http_transaction(struct client_handle *ch) {
                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);
                }
@@ -258,8 +246,8 @@ void perform_one_http_transaction(struct client_handle *ch) {
        }
        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)
@@ -269,14 +257,12 @@ void perform_one_http_transaction(struct client_handle *ch) {
 }
 
 
-/*
- * 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);