Skip to content

Commit 70ec225

Browse files
committed
Use window.location.origin as default host in URLBuilder
Closes #1695
1 parent c68d929 commit 70ec225

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

ktor-http/common/src/io/ktor/http/URLBuilder.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const val DEFAULT_PORT: Int = 0
2424
*/
2525
class URLBuilder(
2626
var protocol: URLProtocol = URLProtocol.HTTP,
27-
var host: String = "localhost",
27+
var host: String = originHost,
2828
var port: Int = DEFAULT_PORT,
2929
var user: String? = null,
3030
var password: String? = null,
@@ -84,6 +84,13 @@ class URLBuilder(
8484
companion object
8585
}
8686

87+
/**
88+
* Hostname of current origin.
89+
*
90+
* It uses "localhost" for all platforms except js.
91+
*/
92+
expect val URLBuilder.Companion.originHost: String
93+
8794
/**
8895
* Create a copy of this builder. Modifications in a copy is not reflected in the original instance and vise-versa.
8996
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package io.ktor.http
6+
7+
import io.ktor.util.*
8+
import kotlin.browser.*
9+
10+
/**
11+
* Hostname of current origin.
12+
*
13+
* It uses "localhost" for all platforms except js.
14+
*/
15+
actual val URLBuilder.Companion.originHost: String
16+
get() = if (PlatformUtils.IS_BROWSER) window.location.origin else "localhost"

ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt

+8
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ import java.net.*
1212
operator fun Url.Companion.invoke(fullUrl: String): Url = URLBuilder().apply {
1313
takeFrom(URI(fullUrl))
1414
}.build()
15+
16+
/**
17+
* Hostname of current origin.
18+
*
19+
* It uses "localhost" for all platforms except js.
20+
*/
21+
actual val URLBuilder.Companion.originHost: String
22+
get() = "localhost"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package io.ktor.http
6+
7+
8+
/**
9+
* Hostname of current origin.
10+
*
11+
* It uses "localhost" for all platforms except js.
12+
*/
13+
actual val URLBuilder.Companion.originHost: String
14+
get() = "localhost"

0 commit comments

Comments
 (0)