From 5e39658f7c0bc91613468c9513ba22ede1739d7e Mon Sep 17 00:00:00 2001
From: "Alan M. Carroll" <amc@apache.org>
Date: Tue, 2 Nov 2021 11:47:09 -0500
Subject: [PATCH] Tweak MimeHdr::get_host_port_values to not run over the end
 of the TextView. (#8468)

Origin: https://github.com/apache/trafficserver/commit/5e39658f7c0bc91613468c9513ba22ede1739d7e

Fix for #8461

(cherry picked from commit 055ca11c2842a64bf7df8d547515670e1a04afc1)
---
 proxy/hdrs/MIME.cc                         | 11 +++--------
 src/tscpp/util/unit_tests/test_TextView.cc | 11 +++--------
 2 files changed, 6 insertions(+), 16 deletions(-)

diff --git a/proxy/hdrs/MIME.cc b/proxy/hdrs/MIME.cc
index 45c16c386dd..0a55dd06b4d 100644
--- a/proxy/hdrs/MIME.cc
+++ b/proxy/hdrs/MIME.cc
@@ -2284,20 +2284,15 @@ MIMEHdr::get_host_port_values(const char **host_ptr, ///< Pointer to host.
     if (b) {
       if ('[' == *b) {
         auto idx = b.find(']');
-        if (idx <= b.size() && b[idx + 1] == ':') {
+        if (idx < b.size() - 1 && b[idx + 1] == ':') {
           host = b.take_prefix_at(idx + 1);
           port = b;
         } else {
           host = b;
         }
       } else {
-        auto x = b.split_prefix_at(':');
-        if (x) {
-          host = x;
-          port = b;
-        } else {
-          host = b;
-        }
+        host = b.take_prefix_at(':');
+        port = b;
       }
 
       if (host) {
diff --git a/src/tscpp/util/unit_tests/test_TextView.cc b/src/tscpp/util/unit_tests/test_TextView.cc
index 8f71e0aa39d..7f365369082 100644
--- a/src/tscpp/util/unit_tests/test_TextView.cc
+++ b/src/tscpp/util/unit_tests/test_TextView.cc
@@ -275,20 +275,15 @@ TEST_CASE("TextView Affixes", "[libts][TextView]")
   auto f_host = [](TextView b, TextView &host, TextView &port) -> void {
     if ('[' == *b) {
       auto idx = b.find(']');
-      if (idx <= b.size() && b[idx + 1] == ':') {
+      if (idx < b.size() - 1 && b[idx + 1] == ':') {
         host = b.take_prefix_at(idx + 1);
         port = b;
       } else {
         host = b;
       }
     } else {
-      auto x = b.split_prefix_at(':');
-      if (x) {
-        host = x;
-        port = b;
-      } else {
-        host = b;
-      }
+      host = b.take_prefix_at(':');
+      port = b;
     }
   };
 
