From cc0a00d113575193ab58fc212f0b7aa4be57cb47 Mon Sep 17 00:00:00 2001 From: Leonid Stashevsky Date: Fri, 20 Mar 2020 12:09:14 +0300 Subject: [PATCH] Use window.location.origin as default host in URLBuilder Closes #1695 --- ktor-http/common/src/io/ktor/http/URLBuilder.kt | 9 ++++++++- ktor-http/js/src/io/ktor/http/URLBuilderJs.kt | 16 ++++++++++++++++ ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt | 8 ++++++++ .../posix/src/io/ktor/http/URLBuilderPosix.kt | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ktor-http/js/src/io/ktor/http/URLBuilderJs.kt create mode 100644 ktor-http/posix/src/io/ktor/http/URLBuilderPosix.kt diff --git a/ktor-http/common/src/io/ktor/http/URLBuilder.kt b/ktor-http/common/src/io/ktor/http/URLBuilder.kt index 9c09c2d692..ef7f8fb36b 100644 --- a/ktor-http/common/src/io/ktor/http/URLBuilder.kt +++ b/ktor-http/common/src/io/ktor/http/URLBuilder.kt @@ -24,7 +24,7 @@ const val DEFAULT_PORT: Int = 0 */ class URLBuilder( var protocol: URLProtocol = URLProtocol.HTTP, - var host: String = "localhost", + var host: String = originHost, var port: Int = DEFAULT_PORT, var user: String? = null, var password: String? = null, @@ -84,6 +84,13 @@ class URLBuilder( companion object } +/** + * Hostname of current origin. + * + * It uses "localhost" for all platforms except js. + */ +expect val URLBuilder.Companion.originHost: String + /** * Create a copy of this builder. Modifications in a copy is not reflected in the original instance and vise-versa. */ diff --git a/ktor-http/js/src/io/ktor/http/URLBuilderJs.kt b/ktor-http/js/src/io/ktor/http/URLBuilderJs.kt new file mode 100644 index 0000000000..497f8c6c29 --- /dev/null +++ b/ktor-http/js/src/io/ktor/http/URLBuilderJs.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package io.ktor.http + +import io.ktor.util.* +import kotlin.browser.* + +/** + * Hostname of current origin. + * + * It uses "localhost" for all platforms except js. + */ +actual val URLBuilder.Companion.originHost: String + get() = if (PlatformUtils.IS_BROWSER) window.location.origin else "localhost" diff --git a/ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt b/ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt index cc91099dee..e0d5bf420d 100644 --- a/ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt +++ b/ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt @@ -12,3 +12,11 @@ import java.net.* operator fun Url.Companion.invoke(fullUrl: String): Url = URLBuilder().apply { takeFrom(URI(fullUrl)) }.build() + +/** + * Hostname of current origin. + * + * It uses "localhost" for all platforms except js. + */ +actual val URLBuilder.Companion.originHost: String + get() = "localhost" diff --git a/ktor-http/posix/src/io/ktor/http/URLBuilderPosix.kt b/ktor-http/posix/src/io/ktor/http/URLBuilderPosix.kt new file mode 100644 index 0000000000..49025fbc27 --- /dev/null +++ b/ktor-http/posix/src/io/ktor/http/URLBuilderPosix.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package io.ktor.http + + +/** + * Hostname of current origin. + * + * It uses "localhost" for all platforms except js. + */ +actual val URLBuilder.Companion.originHost: String + get() = "localhost"