From c5289eccb78d316fa9e5a9016d4c6a2dd73f5c9c Mon Sep 17 00:00:00 2001 From: Simon Dingley Date: Tue, 19 Dec 2017 10:33:32 +0000 Subject: [PATCH] Fixes Issue #177 and prevents double slashes at the start of a path when redirecting to an absolute url --- Modules/UrlTrackerModule.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Modules/UrlTrackerModule.cs b/Modules/UrlTrackerModule.cs index ca9f358..5eb5566 100644 --- a/Modules/UrlTrackerModule.cs +++ b/Modules/UrlTrackerModule.cs @@ -353,8 +353,15 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa NameValueCollection newQueryString = HttpUtility.ParseQueryString(request.Url.Query); if (redirectQueryString.HasKeys()) newQueryString = newQueryString.Merge(redirectQueryString); - string pathAndQuery = Uri.UnescapeDataString(redirectUri.PathAndQuery) + redirectUri.Fragment; - redirectUri = new Uri(string.Format("{0}{1}{2}{3}/{4}{5}", redirectUri.Scheme, Uri.SchemeDelimiter, redirectUri.Host, redirectUri.Port != 80 && UrlTrackerSettings.AppendPortNumber ? string.Concat(":", redirectUri.Port) : string.Empty, pathAndQuery.Contains('?') ? pathAndQuery.Substring(0, pathAndQuery.IndexOf('?')) : pathAndQuery.StartsWith("/") ? pathAndQuery.Substring(1) : pathAndQuery, newQueryString.HasKeys() ? string.Concat("?", newQueryString.ToQueryString()) : string.Empty)); + string pathAndQuery = Uri.UnescapeDataString(redirectUri.PathAndQuery.EnsureStartsWith("/")) + redirectUri.Fragment; + // Determine if we need to include the port number + string port = redirectUri.Port != 80 && UrlTrackerSettings.AppendPortNumber ? string.Concat(":", redirectUri.Port) : string.Empty; + // Extract the Uri path wihtout any querystring + string path = pathAndQuery.Contains('?') ? pathAndQuery.Substring(0, pathAndQuery.IndexOf('?')) : pathAndQuery.StartsWith("/") ? pathAndQuery.Substring(1) : pathAndQuery; + // Assemble the new querystring but only if there are key/values to pass through + string querystring = newQueryString.HasKeys() ? string.Concat("?", newQueryString.ToQueryString()) : string.Empty; + + redirectUri = new Uri(string.Format("{0}{1}{2}{3}{4}{5}", redirectUri.Scheme, Uri.SchemeDelimiter, redirectUri.Host, port, path, querystring)); } if (redirectUri == new Uri(string.Format("{0}{1}{2}{3}/{4}", request.Url.Scheme, Uri.SchemeDelimiter, request.Url.Host, request.Url.Port != 80 && UrlTrackerSettings.AppendPortNumber ? string.Concat(":", request.Url.Port) : string.Empty, request.RawUrl.StartsWith("/") ? request.RawUrl.Substring(1) : request.RawUrl)))