Let get_destination_from_headers() handle the https case as well to reduce code dupli...
authorFabian Keil <fk@fabiankeil.de>
Thu, 11 Sep 2025 12:44:38 +0000 (14:44 +0200)
committerFabian Keil <fk@fabiankeil.de>
Thu, 9 Oct 2025 09:51:07 +0000 (11:51 +0200)
jcc.c
parsers.c
parsers.h

diff --git a/jcc.c b/jcc.c
index 3aecb08..a3ebcf4 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -589,7 +589,7 @@ static jb_err get_request_destination_elsewhere(struct client_state *csp, struct
 
       return JB_ERR_PARSE;
    }
-   else if (JB_ERR_OK == get_destination_from_headers(headers, csp->http))
+   else if (JB_ERR_OK == get_destination_from_headers(headers, csp->http, FALSE))
    {
       /* Split the domain we just got for pattern matching */
       init_domain_components(csp->http);
@@ -2760,7 +2760,7 @@ static jb_err process_encrypted_request_headers(struct client_state *csp)
       freez(p);
    }
 
-   if (JB_ERR_OK != get_destination_from_https_headers(headers, csp->http))
+   if (JB_ERR_OK != get_destination_from_headers(headers, csp->http, TRUE))
    {
       /*
        * Our attempts to get the request destination
index 2ab405f..e962c14 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -4889,13 +4889,14 @@ static jb_err parse_time_header(const char *header, time_t *result)
  *          1  :  headers = List of headers (one of them hopefully being
  *                the "Host:" header)
  *          2  :  http = storage for the result (host, port and hostport).
+ *          3  :  https_request = Whether or not the request was received through https
  *
  * Returns     :  JB_ERR_MEMORY (or terminates) in case of memory problems,
  *                JB_ERR_PARSE if the host header couldn't be found or parsed,
  *                JB_ERR_OK otherwise.
  *
  *********************************************************************/
-jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
+jb_err get_destination_from_headers(const struct list *headers, struct http_request *http, int https_request)
 {
    char *q;
    char *p;
@@ -4958,132 +4959,26 @@ jb_err get_destination_from_headers(const struct list *headers, struct http_requ
    }
    else
    {
-      http->port = 80;
+      http->port = https_request ? 443 : 80;
    }
 
    /* Rebuild request URL */
    freez(http->url);
-   http->url = strdup("http://");
-   string_append(&http->url, http->hostport);
-   string_append(&http->url, http->path);
-   if (http->url == NULL)
-   {
-      return JB_ERR_MEMORY;
-   }
-
-   log_error(LOG_LEVEL_HEADER,
-      "Destination extracted from \"Host\" header. New request URL: %s",
-      http->url);
-
-   /*
-    * Regenerate request line in "proxy format"
-    * to make rewrites more convenient.
-    */
-   assert(http->cmd != NULL);
-   freez(http->cmd);
-   http->cmd = strdup_or_die(http->gpc);
-   string_append(&http->cmd, " ");
-   string_append(&http->cmd, http->url);
-   string_append(&http->cmd, " ");
-   string_append(&http->cmd, http->version);
-   if (http->cmd == NULL)
-   {
-      return JB_ERR_MEMORY;
-   }
-
-   return JB_ERR_OK;
-
-}
-
-
-#ifdef FEATURE_HTTPS_INSPECTION
-/*********************************************************************
- *
- * Function    :  get_destination_from_https_headers
- *
- * Description :  Parse the previously encrypted "Host:" header to
- *                get the request's destination.
- *
- * Parameters  :
- *          1  :  headers = List of headers (one of them hopefully being
- *                the "Host:" header)
- *          2  :  http = storage for the result (host, port and hostport).
- *
- * Returns     :  JB_ERR_MEMORY (or terminates) in case of memory problems,
- *                JB_ERR_PARSE if the host header couldn't be found,
- *                JB_ERR_OK otherwise.
- *
- *********************************************************************/
-jb_err get_destination_from_https_headers(const struct list *headers, struct http_request *http)
-{
-   char *q;
-   char *p;
-   char *host;
-
-   host = get_header_value(headers, "Host:");
-
-   if (NULL == host)
-   {
-      log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
-      return JB_ERR_PARSE;
-   }
-
-   p = string_tolower(host);
-   if (p == NULL)
+   if (https_request)
    {
-      return JB_ERR_MEMORY;
-   }
-   chomp(p);
-   q = strdup_or_die(p);
-
-   freez(http->hostport);
-   http->hostport = p;
-   freez(http->host);
-   http->host = q;
-   if (*p == '[')
-   {
-      /* Numeric IPv6 address delimited by brackets */
-      p++;
-
-      q = strchr(p, ']');
-      if (q == NULL)
-      {
-         /* Missing closing bracket */
-         return JB_ERR_PARSE;
-      }
-
-      *q++ = '\0';
-
-      if (*q == '\0')
-      {
-         q = NULL;
-      }
-      else if (*q != ':')
-      {
-         /* Garbage after closing bracket */
-         return JB_ERR_PARSE;
-      }
+      http->url = strdup(http->path);
    }
    else
    {
-      /* Plain non-escaped hostname */
-      q = strchr(http->host, ':');
-   }
-   if (q != NULL)
-   {
-      /* Terminate hostname and evaluate port string */
-      *q++ = '\0';
-      http->port = atoi(q);
+      http->url = strdup("http://");
+      string_append(&http->url, http->hostport);
+      string_append(&http->url, http->path);
    }
-   else
+   if (http->url == NULL)
    {
-      http->port = 443;
+      return JB_ERR_MEMORY;
    }
 
-   /* Rebuild request URL */
-   freez(http->url);
-   http->url = strdup_or_die(http->path);
-
    log_error(LOG_LEVEL_HEADER,
       "Destination extracted from \"Host\" header. New request URL: %s",
       http->url);
@@ -5107,7 +5002,6 @@ jb_err get_destination_from_https_headers(const struct list *headers, struct htt
    return JB_ERR_OK;
 
 }
-#endif /* def FEATURE_HTTPS_INSPECTION */
 
 
 /*********************************************************************
index 802133f..e0e5541 100644 (file)
--- a/parsers.h
+++ b/parsers.h
@@ -63,10 +63,7 @@ extern jb_err sed_https(struct client_state *csp);
 extern jb_err header_adjust_content_length(char **header, size_t content_length);
 extern jb_err update_server_headers(struct client_state *csp);
 extern void get_http_time(int time_offset, char *buf, size_t buffer_size);
-extern jb_err get_destination_from_headers(const struct list *headers, struct http_request *http);
-#ifdef FEATURE_HTTPS_INSPECTION
-extern jb_err get_destination_from_https_headers(const struct list *headers, struct http_request *http);
-#endif
+extern jb_err get_destination_from_headers(const struct list *headers, struct http_request *http, int https_request);
 extern unsigned long long get_expected_content_length(struct list *headers);
 extern jb_err client_transfer_encoding(struct client_state *csp, char **header);