Add OkHttpClient parameter

Ref #297

This adds a parameter so we can pass in OkHttpClient, if someone wants to customize HTTP behavior.
This commit is contained in:
Eugene Yokota 2019-04-03 00:02:32 -04:00
parent 0d9ff2b63e
commit 2e971b9a85
7 changed files with 51 additions and 36 deletions

View File

@ -257,6 +257,11 @@ lazy val lmIvy = (project in file("ivy"))
exclude[DirectMissingMethodProblem]("sbt.librarymanagement.ivy.ExternalIvyConfiguration.copy*"),
exclude[DirectMissingMethodProblem]("sbt.librarymanagement.ivy.InlineIvyConfiguration.copy*"),
exclude[DirectMissingMethodProblem]("sbt.librarymanagement.ivy.IvyPaths.copy*"),
exclude[DirectMissingMethodProblem]("sbt.internal.librarymanagement.ivyint.GigahorseUrlHandler.urlFactory"),
exclude[DirectMissingMethodProblem]("sbt.internal.librarymanagement.ivyint.GigahorseUrlHandler.http"),
exclude[DirectMissingMethodProblem]("sbt.internal.librarymanagement.ivyint.GigahorseUrlHandler.open"),
exclude[DirectMissingMethodProblem]("sbt.internal.librarymanagement.ivyint.GigahorseUrlHandler.this"),
),
)

View File

@ -0,0 +1,21 @@
package sbt.internal.librarymanagement
import gigahorse.HttpClient
import okhttp3.{ JavaNetAuthenticator => _, _ }
import sbt.librarymanagement.Http
object CustomHttp {
private[this] def http0: HttpClient = Http.http
private[sbt] def defaultHttpClientBuilder: OkHttpClient.Builder = {
http0
.underlying[OkHttpClient]
.newBuilder()
.authenticator(new sbt.internal.librarymanagement.JavaNetAuthenticator)
.followRedirects(true)
.followSslRedirects(true)
}
private[sbt] lazy val defaultHttpClient: OkHttpClient =
defaultHttpClientBuilder.build
}

View File

@ -7,6 +7,7 @@ import java.io.File
import java.net.URI
import java.util.concurrent.Callable
import okhttp3.OkHttpClient
import org.apache.ivy.Ivy
import org.apache.ivy.core.IvyPatternHelper
import org.apache.ivy.core.cache.{ CacheMetadataOptions, DefaultRepositoryCacheManager }
@ -54,7 +55,12 @@ import ivyint.{
import sjsonnew.JsonFormat
import sjsonnew.support.murmurhash.Hasher
final class IvySbt(val configuration: IvyConfiguration) { self =>
final class IvySbt(
val configuration: IvyConfiguration,
val http: OkHttpClient
) { self =>
def this(configuration: IvyConfiguration) = this(configuration, CustomHttp.defaultHttpClient)
/*
* ========== Configuration/Setup ============
* This part configures the Ivy instance by first creating the logger interface to ivy, then IvySettings, and then the Ivy instance.
@ -80,7 +86,7 @@ final class IvySbt(val configuration: IvyConfiguration) { self =>
}
private lazy val basicUrlHandler: URLHandler = new BasicURLHandler
private lazy val gigahorseUrlHandler: URLHandler = new GigahorseUrlHandler
private lazy val gigahorseUrlHandler: URLHandler = new GigahorseUrlHandler(http)
private lazy val settings: IvySettings = {
val dispatcher: URLHandlerDispatcher = URLHandlerRegistry.getDefault match {

View File

@ -114,7 +114,7 @@ class IvyCache(val ivyHome: Option[File]) {
.withResolvers(Vector(local))
.withLock(lock)
.withLog(log)
(new IvySbt(conf), local)
(new IvySbt(conf, CustomHttp.defaultHttpClient), local)
}
/** Creates a default jar artifact based on the given ID.*/

View File

@ -1,12 +1,12 @@
package sbt.internal.librarymanagement
package ivyint
import java.net.{ HttpURLConnection, URL, UnknownHostException }
import java.net.{ URL, UnknownHostException }
import java.io._
import scala.util.control.NonFatal
import okhttp3.{ MediaType, OkUrlFactory, Request, RequestBody }
import okhttp3.{ MediaType, Request, RequestBody }
import okhttp3.internal.http.HttpDate
import okhttp3.{ JavaNetAuthenticator => _, _ }
@ -18,7 +18,7 @@ import org.apache.ivy.util.url.URLHandler._
import sbt.io.IO
// Copied from Ivy's BasicURLHandler.
class GigahorseUrlHandler extends AbstractURLHandler {
class GigahorseUrlHandler(http: OkHttpClient) extends AbstractURLHandler {
import GigahorseUrlHandler._
@ -45,7 +45,7 @@ class GigahorseUrlHandler extends AbstractURLHandler {
if (getRequestMethod == URLHandler.REQUEST_METHOD_HEAD) request.head() else request.get()
val response = okHttpClient.newCall(request.build()).execute()
val response = http.newCall(request.build()).execute()
try {
val infoOption = try {
@ -101,7 +101,7 @@ class GigahorseUrlHandler extends AbstractURLHandler {
.get()
.build()
val response = okHttpClient.newCall(request).execute()
val response = http.newCall(request).execute()
try {
if (!checkStatusCode(url, response)) {
throw new IOException(
@ -183,7 +183,7 @@ class GigahorseUrlHandler extends AbstractURLHandler {
if (l != null) {
l.start(new CopyProgressEvent())
}
val response = okHttpClient.newCall(request).execute()
val response = http.newCall(request).execute()
try {
if (l != null) {
l.end(new CopyProgressEvent(EmptyBuffer, source.length()))
@ -197,10 +197,6 @@ class GigahorseUrlHandler extends AbstractURLHandler {
}
object GigahorseUrlHandler {
import gigahorse.HttpClient
import okhttp3.OkHttpClient
import sbt.librarymanagement.Http
// This is requires to access the constructor of URLInfo.
private[sbt] class SbtUrlInfo(available: Boolean,
contentLength: Long,
@ -214,27 +210,6 @@ object GigahorseUrlHandler {
private val EmptyBuffer: Array[Byte] = new Array[Byte](0)
lazy val http: HttpClient = Http.http
private lazy val okHttpClient: OkHttpClient = {
http
.underlying[OkHttpClient]
.newBuilder()
.authenticator(new sbt.internal.librarymanagement.JavaNetAuthenticator)
.followRedirects(true)
.followSslRedirects(true)
.build
}
@deprecated("Use the Gigahorse HttpClient directly instead.", "librarymanagement-ivy 1.0.1")
private[sbt] def urlFactory = {
new OkUrlFactory(okHttpClient)
}
@deprecated("Use the Gigahorse HttpClient directly instead.", "librarymanagement-ivy 1.0.1")
private[sbt] def open(url: URL): HttpURLConnection =
urlFactory.open(url)
private def checkStatusCode(url: URL, response: Response): Boolean =
response.code() match {
case 200 => true

View File

@ -2,6 +2,7 @@ package sbt
package librarymanagement
package ivy
import okhttp3.OkHttpClient
import sbt.internal.librarymanagement._
import sbt.util.Logger
@ -27,5 +28,8 @@ class IvyDependencyResolution private[sbt] (val ivySbt: IvySbt)
object IvyDependencyResolution {
def apply(ivyConfiguration: IvyConfiguration): DependencyResolution =
DependencyResolution(new IvyDependencyResolution(new IvySbt(ivyConfiguration)))
apply(ivyConfiguration, CustomHttp.defaultHttpClient)
def apply(ivyConfiguration: IvyConfiguration, http: OkHttpClient): DependencyResolution =
DependencyResolution(new IvyDependencyResolution(new IvySbt(ivyConfiguration, http)))
}

View File

@ -2,6 +2,7 @@ package sbt
package librarymanagement
package ivy
import okhttp3.OkHttpClient
import sbt.internal.librarymanagement._
import sbt.util.Logger
import java.io.File
@ -31,5 +32,8 @@ class IvyPublisher private[sbt] (val ivySbt: IvySbt) extends PublisherInterface
object IvyPublisher {
def apply(ivyConfiguration: IvyConfiguration): Publisher =
Publisher(new IvyPublisher(new IvySbt(ivyConfiguration)))
apply(ivyConfiguration, CustomHttp.defaultHttpClient)
def apply(ivyConfiguration: IvyConfiguration, http: OkHttpClient): Publisher =
Publisher(new IvyPublisher(new IvySbt(ivyConfiguration, http)))
}