From c13979ab0c11b92fdb9916ceb7905143f651af89 Mon Sep 17 00:00:00 2001 From: Robinson Date: Tue, 7 Jun 2022 22:51:45 +0200 Subject: [PATCH] Added cleanupAndRemovePath --- src/dorkbox/util/WebUtil.kt | 44 ++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/dorkbox/util/WebUtil.kt b/src/dorkbox/util/WebUtil.kt index 0c6fe64..301ef58 100644 --- a/src/dorkbox/util/WebUtil.kt +++ b/src/dorkbox/util/WebUtil.kt @@ -127,6 +127,47 @@ object WebUtil { } + /** + * Only removes the path and query parameters. Only the transport + domain remain. + * ie: + * http://foo.com/index.php --> http://foo.com + * https://www.aa.foo.com/index.php --> https://www.aa.foo.com + * https://www.aa.foo.com/index&foo%bar --> https://www.aa.foo.com + * https://www.aa.foo.com%foobar --> https://www.aa.foo.com + */ + fun cleanupAndRemovePath(fullDomainName: String): String { + val start = 0 + var end = fullDomainName.length + + val slash = fullDomainName.indexOf("/", start + 3) + if (slash > -1 && slash < end) { + end = slash + } + + val colon = fullDomainName.indexOf(":", start + 3) + if (colon > -1 && colon < end) { + end = colon + } + + val percent = fullDomainName.indexOf("%", start) + if (percent > -1 && percent < end) { + end = percent + } + + val ampersand = fullDomainName.indexOf("&", start) + if (ampersand > -1 && ampersand < end) { + end = ampersand + } + + val question = fullDomainName.indexOf("?", start) + if (question > -1 && question < end) { + end = question + } + + + return fullDomainName.substring(start, end) + } + /** @@ -578,7 +619,8 @@ object WebUtil { HttpURLConnection.HTTP_MOVED_PERM, HttpURLConnection.HTTP_MOVED_TEMP -> { location = getHeaderField("Location") // java.net.URLDecoder is only valid for query parameters/headers -- NOT FOR ACTUAL URLS! - location = URLDecoder.decode(location, Charsets.UTF_8) + location = URLDecoder.decode(location, "US-ASCII") + // logger.trace { "Response to '$url' redirected to '$location'" }