Skip to content

Commit

Permalink
Use window.location.origin as default host in URLBuilder
Browse files Browse the repository at this point in the history
	Closes #1695
  • Loading branch information
e5l committed Jul 21, 2020
1 parent 7056d47 commit 348847f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ktor-http/common/src/io/ktor/http/URLBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand Down
16 changes: 16 additions & 0 deletions ktor-http/js/src/io/ktor/http/URLBuilderJs.kt
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions ktor-http/jvm/src/io/ktor/http/URLBuilderJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 14 additions & 0 deletions ktor-http/posix/src/io/ktor/http/URLBuilderPosix.kt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 348847f

Please sign in to comment.