|
17 | 17 | package org.scalasteward.core.client |
18 | 18 |
|
19 | 19 | import cats.effect._ |
| 20 | +import cats.syntax.all._ |
20 | 21 | import eu.timepit.refined.auto._ |
21 | 22 | import eu.timepit.refined.types.numeric.PosInt |
22 | 23 | import org.http4s.Response |
23 | 24 | import org.http4s.client._ |
24 | 25 | import org.http4s.headers.`User-Agent` |
25 | | -import org.http4s.okhttp.client.OkHttpBuilder |
| 26 | +import org.http4s.jdkhttpclient.JdkHttpClient |
| 27 | +import java.net.http.HttpClient |
| 28 | +import java.net.http.HttpClient.Builder |
26 | 29 | import org.typelevel.ci._ |
27 | 30 |
|
28 | 31 | import scala.concurrent.duration._ |
29 | 32 |
|
30 | 33 | object ClientConfiguration { |
31 | | - type BuilderMiddleware[F[_]] = OkHttpBuilder[F] => OkHttpBuilder[F] |
| 34 | + type BuilderMiddleware = Builder => Builder |
| 35 | + |
32 | 36 | object BuilderMiddleware { |
33 | | - def default[F[_]]: BuilderMiddleware[F] = bmw => bmw |
| 37 | + def default: BuilderMiddleware = _.followRedirects(HttpClient.Redirect.ALWAYS) |
34 | 38 | } |
35 | | - def build[F[_]: Async](bmw: BuilderMiddleware[F], cmw: Middleware[F]): Resource[F, Client[F]] = |
36 | | - OkHttpBuilder |
37 | | - .withDefaultClient[F] |
38 | | - .map(bmw) |
39 | | - .flatMap(_.resource) |
| 39 | + |
| 40 | + def build[F[_]: Async](bmw: BuilderMiddleware, cmw: Middleware[F]): Resource[F, Client[F]] = |
| 41 | + Resource |
| 42 | + .eval(defaultHttpClient[F](bmw).map(JdkHttpClient[F](_))) |
40 | 43 | .map(cmw) |
| 44 | + // Copied from https://github.com/http4s/http4s-jdk-http-client/blob/b9655b90549319fbe999069e7c95ab1752efecb9/core/src/main/scala/org/http4s/jdkhttpclient/JdkHttpClient.scala#L257-L275 to support BuilderMiddleware |
| 45 | + private def defaultHttpClient[F[_]: Async](bmw: BuilderMiddleware): F[HttpClient] = |
| 46 | + Async[F].executor.flatMap { exec => |
| 47 | + Async[F].delay { |
| 48 | + val builder = HttpClient.newBuilder() |
| 49 | + |
| 50 | + if (Runtime.version().feature() == 11) { |
| 51 | + val params = javax.net.ssl.SSLContext.getDefault().getDefaultSSLParameters() |
| 52 | + params.setProtocols(params.getProtocols().filter(_ != "TLSv1.3")) |
| 53 | + builder.sslParameters(params) |
| 54 | + } |
| 55 | + |
| 56 | + builder.executor(exec) |
| 57 | + |
| 58 | + bmw(builder) |
| 59 | + |
| 60 | + builder.build() |
| 61 | + } |
| 62 | + } |
41 | 63 |
|
42 | 64 | def setUserAgent[F[_]: MonadCancelThrow](userAgent: `User-Agent`): Middleware[F] = { client => |
43 | 65 | Client[F](req => client.run(req.putHeaders(userAgent))) |
@@ -69,8 +91,5 @@ object ClientConfiguration { |
69 | 91 | } |
70 | 92 | } |
71 | 93 |
|
72 | | - def disableFollowRedirect[F[_]](builder: OkHttpBuilder[F]): OkHttpBuilder[F] = |
73 | | - builder.withOkHttpClient( |
74 | | - builder.okHttpClient.newBuilder().followRedirects(false).build() |
75 | | - ) |
| 94 | + def disableFollowRedirect: BuilderMiddleware = _.followRedirects(HttpClient.Redirect.NEVER) |
76 | 95 | } |
0 commit comments