Skip to content

Commit f7fb6ce

Browse files
committed
Respect query params if present in the remoteDebugUrl
Resolves: #496
1 parent 7d15678 commit f7fb6ce

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/commonMain/kotlin/org/hildan/chrome/devtools/protocol/ChromeDPClient.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import io.ktor.client.call.*
55
import io.ktor.client.plugins.contentnegotiation.*
66
import io.ktor.client.plugins.websocket.*
77
import io.ktor.client.request.*
8+
import io.ktor.client.request.put
89
import io.ktor.client.statement.*
910
import io.ktor.http.*
11+
import io.ktor.http.takeFrom
1012
import io.ktor.serialization.kotlinx.json.*
1113
import kotlinx.serialization.SerialName
1214
import kotlinx.serialization.Serializable
@@ -101,8 +103,23 @@ class ChromeDPClient(
101103
],
102104
),
103105
)
104-
suspend fun newTab(url: String = "about:blank"): ChromeDPTarget =
105-
httpPut("/json/new?$url").body<ChromeDPTarget>().fixHost()
106+
suspend fun newTab(url: String = "about:blank"): ChromeDPTarget {
107+
// The /json/new endpoint takes a target URL instead of the query, it's not a real query parameter.
108+
// The hack that follows allows to append this weird parameter after a potentially existing query.
109+
val encodedTargetUrl = url.encodeURLParameter(spaceToPlus = false)
110+
val urlWithEndpoint = URLBuilder(remoteDebugUrl).apply { encodedPath += "/json/new" }.build()
111+
val urlStr = urlWithEndpoint.toString()
112+
val jsonNewUrl = if (urlWithEndpoint.encodedQuery.isEmpty()) {
113+
"${urlStr}?${encodedTargetUrl}"
114+
} else {
115+
"${urlStr}&${encodedTargetUrl}"
116+
}
117+
return httpClient.put(jsonNewUrl) {
118+
if (overrideHostHeader) {
119+
headers["Host"] = "localhost"
120+
}
121+
}.body<ChromeDPTarget>().fixHost()
122+
}
106123

107124
/**
108125
* Brings the page identified by the given [targetId] into the foreground (activates a tab).
@@ -144,13 +161,9 @@ class ChromeDPClient(
144161
return httpClient.chromeWebSocket(browserDebuggerUrl, overrideHostHeader = overrideHostHeader)
145162
}
146163

147-
private suspend fun httpPut(endpoint: String): HttpResponse = httpClient.put(remoteDebugUrl + endpoint) {
148-
if (overrideHostHeader) {
149-
headers["Host"] = "localhost"
150-
}
151-
}
152-
153-
private suspend fun httpGet(endpoint: String): HttpResponse = httpClient.get(remoteDebugUrl + endpoint) {
164+
private suspend fun httpGet(endpoint: String): HttpResponse = httpClient.get {
165+
url.takeFrom(remoteDebugUrl)
166+
url.encodedPath += endpoint
154167
if (overrideHostHeader) {
155168
headers["Host"] = "localhost"
156169
}

0 commit comments

Comments
 (0)