// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote.internal;

import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import okhttp3.ConnectionPool;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;

public class OkHttpClient implements HttpClient {

  private final okhttp3.OkHttpClient client;
  private final URL baseUrl;

  public OkHttpClient(okhttp3.OkHttpClient client, URL url) {
    this.client = client;
    this.baseUrl = url;
  }

  @Override
  public HttpResponse execute(HttpRequest request) throws IOException {
    return execute(request, true);
  }

  @Override
  public HttpResponse execute(HttpRequest request, boolean followRedirects) throws IOException {
    if (followRedirects != client.followRedirects()) {
      throw new IllegalArgumentException("Unable to change how the http client follows redirets");
    }

    Request.Builder builder = new Request.Builder();

    HttpUrl.Builder url;
    try {
      String rawUrl = baseUrl.toExternalForm().replaceAll("/$", "") + request.getUri();
      url = HttpUrl.parse(rawUrl).newBuilder();
    } catch (NullPointerException e) {
      throw new IOException("Unable to parse URL: " + baseUrl.toString() + request.getUri());
    }

    for (String name : request.getQueryParameterNames()) {
      for (String value : request.getQueryParameters(name)) {
        url.addQueryParameter(name, value);
      }
    }

    builder.url(url.build());

    for (String name : request.getHeaderNames()) {
      for (String value : request.getHeaders(name)) {
        builder.addHeader(name, value);
      }
    }

    switch (request.getMethod()) {
      case GET:
        builder.get();
        break;

      case POST:
        String rawType = Optional.of(request.getHeader("Content-Type"))
            .orElse("application/json; charset=utf-8");
        MediaType type = MediaType.parse(rawType);
        RequestBody body = RequestBody.create(type, request.getContent());
        builder.post(body);
        break;

      case DELETE:
        builder.delete();
    }

    Response response = client.newCall(builder.build()).execute();

    HttpResponse toReturn = new HttpResponse();
    toReturn.setContent(response.body().bytes());
    toReturn.setStatus(response.code());
    response.headers().names().forEach(
        name -> response.headers(name).forEach(value -> toReturn.addHeader(name, value)));

    return toReturn;
  }

  @Override
  public void close() {
    // No-op
  }

  public static class Factory implements HttpClient.Factory {

    private ConnectionPool pool = new ConnectionPool();

    @Override
    public HttpClient createClient(URL url) {
      okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder()
          .connectionPool(pool)
          .followRedirects(true)
          .followSslRedirects(true)
          .build();
      return new OkHttpClient(client, url);
    }

    @Override
    public void cleanupIdleClients() {
      pool.evictAll();
    }
  }
}

@@ -843,16 +843,56 @@
 ernal;%0A%0A
+import com.google.common.base.Strings;%0A%0A
 import o
@@ -1071,16 +1071,44 @@
 onPool;%0A
+import okhttp3.Credentials;%0A
 import o
@@ -3755,24 +3755,32 @@
 OkHttpClient
+.Builder
  client = ne
@@ -3812,16 +3812,16 @@
 ilder()%0A
-
         
@@ -3916,28 +3916,645 @@
 rue)
+;%0A
 %0A      
-    .build();
+String info = url.getUserInfo();%0A      if (!Strings.isNullOrEmpty(info)) %7B%0A        String%5B%5D parts = info.split(%22:%22, 2);%0A        String user = parts%5B0%5D;%0A        String pass = parts.length %3E 1 ? parts%5B1%5D : null;%0A%0A        String credentials = Credentials.basic(user, pass);%0A%0A        client.authenticator((route, response) -%3E %7B%0A          if (response.request().header(%22Authorization%22) != null) %7B%0A            return null; // Give up, we've already attempted to authenticate.%0A          %7D%0A%0A          return response.request().newBuilder()%0A              .header(%22Authorization%22, credentials)%0A              .build();%0A        %7D);%0A      %7D%0A
 %0A   
@@ -4586,16 +4586,24 @@
 t(client
+.build()
 , url);%0A

Handle Authorization challenges when using okhttp