]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/http.c
response headers now use an array instead of a linked list
[citadel.git] / webcit-ng / http.c
index 4c50306667c5bf87898987a3dacbea1ca09cd1bb..1df8b8efc035f84a97ad9be13477df58b6951a76 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,23 +91,18 @@ 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));
-       new_response_header->key = key;
-       new_response_header->val = val;
-       new_response_header->next = h->response_headers;
-       h->response_headers = new_response_header;
+       struct keyval new_response_header;
+       new_response_header.key = key;
+       new_response_header.val = val;
+       array_append(h->response_headers, &new_response_header);
 }
 
 
-/*
- * 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 +110,20 @@ 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
+       int i;                                  // general purpose iterator variable
 
        memset(&h, 0, sizeof h);
+       h.request_headers = array_new(sizeof(struct keyval));
+       h.response_headers = array_new(sizeof(struct keyval));
 
        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,23 +135,23 @@ 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 keyval new_request_header;
                                *c = 0;
-                               new_request_header->key = strdup(buf);
+                               new_request_header.key = strdup(buf);
                                ++c;
-                               new_request_header->val = strdup(c);
-                               striplt(new_request_header->key);
-                               striplt(new_request_header->val);
-                               new_request_header->next = h.request_headers;
-                               h.request_headers = new_request_header;
+                               new_request_header.val = strdup(c);
+                               striplt(new_request_header.key);
+                               striplt(new_request_header.val);
+                               array_append(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 +178,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");
@@ -225,11 +214,13 @@ 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) {
+               int number_of_response_headers = array_len(h.response_headers);
+               for (i=0; i<number_of_response_headers; ++i) {
+                       struct keyval *kv = array_get_element_at(h.response_headers, i);
 #ifdef DEBUG_HTTP
-                       syslog(LOG_DEBUG, "} %s: %s", clh->key, clh->val);
+                       syslog(LOG_DEBUG, "\033[1m\033[35m} %s: %s\033[0m", kv->key, kv->val);
 #endif
-                       client_printf(ch, "%s: %s\r\n", clh->key, clh->val);
+                       client_printf(ch, "%s: %s\r\n", kv->key, kv->val);
                }
                client_printf(ch, "\r\n");
                if ((h.response_body_length > 0) && (h.response_body != NULL)) {
@@ -238,48 +229,49 @@ void perform_one_http_transaction(struct client_handle *ch) {
        }
 
        // free the transaction memory
-       while (h.request_headers) {
-               if (h.request_headers->key)
-                       free(h.request_headers->key);
-               if (h.request_headers->val)
-                       free(h.request_headers->val);
-               clh = h.request_headers->next;
-               free(h.request_headers);
-               h.request_headers = clh;
+       while (array_len(h.request_headers) > 0) {
+               struct keyval *kv = array_get_element_at(h.request_headers, 0);
+               if (kv->key) free(kv->key);
+               if (kv->val) free(kv->val);
+               array_delete_element_at(h.request_headers, 0);
        }
-       while (h.response_headers) {
-               if (h.response_headers->key)
-                       free(h.response_headers->key);
-               if (h.response_headers->val)
-                       free(h.response_headers->val);
-               clh = h.response_headers->next;
-               free(h.response_headers);
-               h.response_headers = clh;
+       array_free(h.request_headers);
+       while (array_len(h.response_headers) > 0) {
+               struct keyval *kv = array_get_element_at(h.response_headers, 0);
+               if (kv->key) free(kv->key);
+               if (kv->val) free(kv->val);
+               array_delete_element_at(h.response_headers, 0);
        }
-       if (h.method)
+       array_free(h.response_headers);
+       if (h.method) {
                free(h.method);
-       if (h.uri)
-               free(h.uri);
-       if (h.request_body)
+       }
+       if (h.url) {
+               free(h.url);
+       }
+       if (h.request_body) {
                free(h.request_body);
-       if (h.response_string)
+       }
+       if (h.response_string) {
                free(h.response_string);
-       if (h.site_prefix)
+       }
+       if (h.site_prefix) {
                free(h.site_prefix);
+       }
 }
 
 
-/*
- * 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
-       for (clh = h->request_headers; clh != NULL; clh = clh->next) {
-               if (!strcasecmp(clh->key, requested_header)) {
-                       return (clh->val);
+       struct keyval *kv;
+       int i;
+       for (i=0; i<array_len(h->request_headers); ++i) {
+               kv = array_get_element_at(h->request_headers, i);
+               if (!strcasecmp(kv->key, requested_header)) {
+                       return (kv->val);
                }
        }
        return (NULL);