Skip to content

Commit

Permalink
Merge pull request #523 from http4s/http2client-builder
Browse files Browse the repository at this point in the history
Add withHttp2 and withoutHttp2
  • Loading branch information
hamnis authored Aug 4, 2023
2 parents 275cb9d + e9a9acd commit e7ce145
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ lazy val client = project
ProblemFilters.exclude[Problem]("org.http4s.netty.client.Http4sChannelPoolMap.*"),
ProblemFilters.exclude[Problem]("org.http4s.netty.client.NettyClientBuilder.*"),
ProblemFilters.exclude[Problem]("org.http4s.netty.client.NettyWSClientBuilder.*"),
ProblemFilters.exclude[MissingTypesProblem](
"org.http4s.netty.client.Http4sChannelPoolMap$Config$"),
ProblemFilters.exclude[MissingFieldProblem](
"org.http4s.netty.client.NettyClientBuilder.SSLContextOption"),
ProblemFilters.exclude[MissingClassProblem]("org.http4s.netty.client.Http4sHandler$"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ private[client] class Http4sChannelPoolMap[F[_]](
)
}

def run(request: Request[F]): Resource[F, Response[F]] = {
def run(request0: Request[F]): Resource[F, Response[F]] = {
val request =
if (config.http2 && request0.uri.scheme.contains(Uri.Scheme.https))
request0.withHttpVersion(HttpVersion.`HTTP/2`)
else request0
val key = Key(RequestKey.fromRequest(request), request.httpVersion)

for {
Expand Down Expand Up @@ -235,7 +239,9 @@ private[client] object Http4sChannelPoolMap {
maxConnections: Int,
idleTimeout: Duration,
proxy: Option[Proxy],
sslConfig: SSLContextOption)
sslConfig: SSLContextOption,
http2: Boolean
)

private[client] def fromFuture[F[_]: Async, A](future: => Future[A]): F[A] =
Async[F].async { callback =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class NettyClientBuilder[F[_]](
transport: NettyTransport,
sslContext: SSLContextOption,
nettyChannelOptions: NettyChannelOptions,
proxy: Option[Proxy]
proxy: Option[Proxy],
http2: Boolean
)(implicit F: Async[F]) {
type Self = NettyClientBuilder[F]

Expand All @@ -49,7 +50,8 @@ class NettyClientBuilder[F[_]](
transport: NettyTransport = transport,
sslContext: SSLContextOption = sslContext,
nettyChannelOptions: NettyChannelOptions = nettyChannelOptions,
proxy: Option[Proxy] = proxy
proxy: Option[Proxy] = proxy,
http2: Boolean = http2
): NettyClientBuilder[F] =
new NettyClientBuilder[F](
idleTimeout,
Expand All @@ -61,7 +63,8 @@ class NettyClientBuilder[F[_]](
transport,
sslContext,
nettyChannelOptions,
proxy
proxy,
http2
)

def withNativeTransport: Self = copy(transport = NettyTransport.defaultFor(Os.get))
Expand Down Expand Up @@ -96,6 +99,8 @@ class NettyClientBuilder[F[_]](
def withProxy(proxy: Proxy): Self = copy(proxy = Some(proxy))
def withProxyFromSystemProperties: Self = copy(proxy = Proxy.fromSystemProperties)
def withoutProxy: Self = copy(proxy = None)
def withHttp2: Self = copy(http2 = true)
def withoutHttp2: Self = copy(http2 = false)

private def createBootstrap: Resource[F, Bootstrap] =
Resource.make(F.delay {
Expand All @@ -116,7 +121,8 @@ class NettyClientBuilder[F[_]](
maxConnectionsPerKey,
idleTimeout,
proxy,
sslContext
sslContext,
http2
)
Client[F](new Http4sChannelPoolMap[F](bs, config).run)
}
Expand All @@ -134,6 +140,7 @@ object NettyClientBuilder {
transport = NettyTransport.defaultFor(Os.get),
sslContext = SSLContextOption.TryDefaultSSLContext,
nettyChannelOptions = NettyChannelOptions.empty,
proxy = Proxy.fromSystemProperties
proxy = Proxy.fromSystemProperties,
http2 = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ class EmberHttp2Test extends IOSuite {
val client: IOFixture[Client[IO]] = resourceFixture(
NettyClientBuilder[IO].withNioTransport
.withSSLContext(EmberHttp2Test.sslContextForClient)
.resource
.map(client =>
Client((req: Request[IO]) => client.run(req.withHttpVersion(HttpVersion.`HTTP/2`)))),
.withHttp2
.resource,
"client"
)

Expand Down

0 comments on commit e7ce145

Please sign in to comment.