|
17 | 17 |
|
18 | 18 | package org.openqa.selenium.remote.internal;
|
19 | 19 |
|
| 20 | +import com.google.common.base.Strings; |
| 21 | + |
20 | 22 | import org.openqa.selenium.remote.http.HttpClient;
|
21 | 23 | import org.openqa.selenium.remote.http.HttpRequest;
|
22 | 24 | import org.openqa.selenium.remote.http.HttpResponse;
|
23 | 25 |
|
24 | 26 | import okhttp3.ConnectionPool;
|
| 27 | +import okhttp3.Credentials; |
25 | 28 | import okhttp3.HttpUrl;
|
26 | 29 | import okhttp3.MediaType;
|
27 | 30 | import okhttp3.Request;
|
@@ -116,12 +119,31 @@ public static class Factory implements HttpClient.Factory {
|
116 | 119 |
|
117 | 120 | @Override
|
118 | 121 | public HttpClient createClient(URL url) {
|
119 |
| - okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder() |
| 122 | + okhttp3.OkHttpClient.Builder client = new okhttp3.OkHttpClient.Builder() |
120 | 123 | .connectionPool(pool)
|
121 | 124 | .followRedirects(true)
|
122 |
| - .followSslRedirects(true) |
123 |
| - .build(); |
124 |
| - return new OkHttpClient(client, url); |
| 125 | + .followSslRedirects(true); |
| 126 | + |
| 127 | + String info = url.getUserInfo(); |
| 128 | + if (!Strings.isNullOrEmpty(info)) { |
| 129 | + String[] parts = info.split(":", 2); |
| 130 | + String user = parts[0]; |
| 131 | + String pass = parts.length > 1 ? parts[1] : null; |
| 132 | + |
| 133 | + String credentials = Credentials.basic(user, pass); |
| 134 | + |
| 135 | + client.authenticator((route, response) -> { |
| 136 | + if (response.request().header("Authorization") != null) { |
| 137 | + return null; // Give up, we've already attempted to authenticate. |
| 138 | + } |
| 139 | + |
| 140 | + return response.request().newBuilder() |
| 141 | + .header("Authorization", credentials) |
| 142 | + .build(); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + return new OkHttpClient(client.build(), url); |
125 | 147 | }
|
126 | 148 |
|
127 | 149 | @Override
|
|
0 commit comments