bmcweb: support next= url for forwarding login

Related to patchset here.  This patchset implements the bmc side of
redirecting the users URL after login correctly.

https://gerrit.openbmc-project.xyz/#/c/openbmc/phosphor-webui/+/15925

Change-Id: Idf42f4cecd29fb0c6088721eb5e10fb39e1168e7
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
diff --git a/include/http_utility.hpp b/include/http_utility.hpp
index e13dfc0..0f5bb8c 100644
--- a/include/http_utility.hpp
+++ b/include/http_utility.hpp
@@ -24,4 +24,29 @@
     }
     return false;
 }
+
+inline std::string urlEncode(const boost::string_view value)
+{
+    std::ostringstream escaped;
+    escaped.fill('0');
+    escaped << std::hex;
+
+    for (const char c : value)
+    {
+        // Keep alphanumeric and other accepted characters intact
+        if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
+        {
+            escaped << c;
+            continue;
+        }
+
+        // Any other characters are percent-encoded
+        escaped << std::uppercase;
+        escaped << '%' << std::setw(2)
+                << static_cast<int>(static_cast<unsigned char>(c));
+        escaped << std::nouppercase;
+    }
+
+    return escaped.str();
+}
 } // namespace http_helpers
\ No newline at end of file
diff --git a/include/token_authorization_middleware.hpp b/include/token_authorization_middleware.hpp
index c419c97..b186a5f 100644
--- a/include/token_authorization_middleware.hpp
+++ b/include/token_authorization_middleware.hpp
@@ -63,7 +63,8 @@
             if (http_helpers::requestPrefersHtml(req))
             {
                 res.result(boost::beast::http::status::temporary_redirect);
-                res.addHeader("Location", "/#/login");
+                res.addHeader("Location", "/#/login?next=" +
+                                              http_helpers::urlEncode(req.url));
             }
             else
             {