]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/http.c
It's 2022 ... updating all of the copyright notices in webcit-ng
[citadel.git] / webcit-ng / http.c
index 29a1fba6d5070e9ec4218d4fa5c6ae153c7e40c0..f39ebe2146a1e2dad4eaa5ea1ec28f2affe3236f 100644 (file)
@@ -1,7 +1,6 @@
-//
 // This module handles HTTP transactions.
 //
-// Copyright (c) 1996-2021 by the citadel.org team
+// Copyright (c) 1996-2022 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,
@@ -160,7 +159,7 @@ void perform_one_http_transaction(struct client_handle *ch) {
        }
 
        // If the URL had any query parameters in it, parse them out now.
-       char *p = strchr(h.url, '?');
+       char *p = (h.url ? strchr(h.url, '?') : NULL);
        if (p) {
                *p++ = 0;                                               // insert a null to remove parameters from the URL
                char *tok, *saveptr = NULL;
@@ -173,6 +172,7 @@ void perform_one_http_transaction(struct client_handle *ch) {
                                kv.key = strdup(tok);
                                kv.val = strdup(eq);
                                array_append(h.request_parms, &kv);
+                               syslog(LOG_DEBUG, "\033[1m\033[33m| %s = %s\033[0m", kv.key, kv.val);
                        }
                }
        }
@@ -209,19 +209,26 @@ void perform_one_http_transaction(struct client_handle *ch) {
                        h.request_body = malloc(h.request_body_length);
                        client_read(ch, h.request_body, h.request_body_length);
 
-                       //write(2, HKEY("\033[31m"));
-                       //write(2, h.request_body, h.request_body_length);
-                       //write(2, HKEY("\033[0m\n"));
+                       // Write the entire request body to stderr -- not what you want during normal operation.
+                       #ifdef BODY_TO_STDERR
+                       write(2, HKEY("\033[31m"));
+                       write(2, h.request_body, h.request_body_length);
+                       write(2, HKEY("\033[0m\n"));
+                       #endif
 
                }
+
                // Now pass control up to the next layer to perform the request.
                perform_request(&h);
 
-               // Output the results back to the client.
-               //write(2, HKEY("\033[32m"));
-               //write(2, h.response_body, h.response_body_length);
-               //write(2, HKEY("\033[0m\n"));
+               // Write the entire response body to stderr -- not what you want during normal operation.
+               #ifdef BODY_TO_STDERR
+               write(2, HKEY("\033[32m"));
+               write(2, h.response_body, h.response_body_length);
+               write(2, HKEY("\033[0m\n"));
+               #endif
 
+               // Output the results back to the client.
                syslog(LOG_DEBUG, "> %03d %s", h.response_code, h.response_string);
                client_printf(ch, "HTTP/1.1 %03d %s\r\n", h.response_code, h.response_string);
                client_printf(ch, "Connection: close\r\n");
@@ -302,3 +309,20 @@ char *header_val(struct http_transaction *h, char *requested_header) {
        }
        return (NULL);
 }
+
+
+// Utility function to fetch a specific URL parameter from a completely read-in request.
+// Returns the value of the requested parameter, or NULL if the specified parameter 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 *get_url_param(struct http_transaction *h, char *requested_param) {
+       struct keyval *kv;
+       int i;
+       for (i=0; i<array_len(h->request_parms); ++i) {
+               kv = array_get_element_at(h->request_parms, i);
+               if (!strcasecmp(kv->key, requested_param)) {
+                       return (kv->val);
+               }
+       }
+       return (NULL);
+}