@@ -5,8 +5,10 @@ import io.ktor.client.call.*
5
5
import io.ktor.client.plugins.contentnegotiation.*
6
6
import io.ktor.client.plugins.websocket.*
7
7
import io.ktor.client.request.*
8
+ import io.ktor.client.request.put
8
9
import io.ktor.client.statement.*
9
10
import io.ktor.http.*
11
+ import io.ktor.http.takeFrom
10
12
import io.ktor.serialization.kotlinx.json.*
11
13
import kotlinx.serialization.SerialName
12
14
import kotlinx.serialization.Serializable
@@ -101,8 +103,23 @@ class ChromeDPClient(
101
103
],
102
104
),
103
105
)
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
+ }
106
123
107
124
/* *
108
125
* Brings the page identified by the given [targetId] into the foreground (activates a tab).
@@ -144,13 +161,9 @@ class ChromeDPClient(
144
161
return httpClient.chromeWebSocket(browserDebuggerUrl, overrideHostHeader = overrideHostHeader)
145
162
}
146
163
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
154
167
if (overrideHostHeader) {
155
168
headers[" Host" ] = " localhost"
156
169
}
0 commit comments