Skip to content

Commit 7a26325

Browse files
committed
Use java.net.http.HttpClient instead of java.net.Http(s)URLConnection
Moving to java.net.http.HttpClient brings many benefits, including HTTP/2 support and the ability to make asynchronous requests. Major additions and changes: - Introduce a lightweight org.pkl.core.http.HttpClient API. This keeps some flexibility and allows to enforce behavior such as setting the User-Agent header. - Provide an implementation that delegates to java.net.http.HttpClient. - Use HttpClient for all HTTP(s) requests across the codebase. This required adding an HttpClient parameter to constructors and factory methods of multiple classes, some of which are public APIs. - Manage CA certificates per HTTP client instead of per JVM. This makes it unnecessary to set JVM-wide system/security properties and default SSLSocketFactory's. Each HTTP client maintains its own connection pool and SSLContext. For efficiency reasons, I've tried to reuse clients whenever feasible. To avoid memory leaks, clients are not stored in static fields. HTTP clients are expensive to create. For this reason, EvaluatorBuilder defaults to a "lazy" client that creates the underlying java.net.http.HttpClient on the first send (which may never happen).
1 parent 277f1e0 commit 7a26325

File tree

58 files changed

+4997
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4997
-199
lines changed

bench/src/jmh/java/org/pkl/core/ListSort.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openjdk.jmh.annotations.*;
2424
import org.openjdk.jmh.util.TempFile;
2525
import org.openjdk.jmh.util.TempFileManager;
26+
import org.pkl.core.http.HttpClient;
2627
import org.pkl.core.module.ModuleKeyFactories;
2728
import org.pkl.core.repl.ReplRequest;
2829
import org.pkl.core.repl.ReplResponse;
@@ -39,6 +40,7 @@ public class ListSort {
3940
private static final ReplServer repl =
4041
new ReplServer(
4142
SecurityManagers.defaultManager,
43+
HttpClient.dummyClient(),
4244
Loggers.stdErr(),
4345
List.of(ModuleKeyFactories.standardLibrary),
4446
List.of(ResourceReaders.file()),

docs/src/test/kotlin/DocSnippetTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.pkl.core.repl.ReplServer
2525
import org.pkl.core.resource.ResourceReaders
2626
import org.pkl.core.util.IoUtils
2727
import org.antlr.v4.runtime.ParserRuleContext
28+
import org.pkl.core.http.HttpClient
2829
import java.nio.file.Files
2930
import kotlin.io.path.isDirectory
3031
import kotlin.io.path.isRegularFile
@@ -78,6 +79,7 @@ class DocSnippetTestsEngine : HierarchicalTestEngine<DocSnippetTestsEngine.Execu
7879
override fun createExecutionContext(request: ExecutionRequest): ExecutionContext {
7980
val replServer = ReplServer(
8081
SecurityManagers.defaultManager,
82+
HttpClient.dummyClient(),
8183
Loggers.stdErr(),
8284
listOf(
8385
ModuleKeyFactories.standardLibrary,

pkl-cli/pkl-cli.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fun Exec.configureExecutable(isEnabled: Boolean, outputFile: File, extraArgs: Li
156156
,"--no-fallback"
157157
,"-H:IncludeResources=org/pkl/core/stdlib/.*\\.pkl"
158158
,"-H:IncludeResources=org/jline/utils/.*"
159-
,"-H:IncludeResources=org/pkl/commons/cli/commands/IncludedCARoots.pem"
159+
,"-H:IncludeResources=org/pkl/core/http/IncludedCARoots.pem"
160160
//,"-H:IncludeResources=org/pkl/core/Release.properties"
161161
,"-H:IncludeResourceBundles=org.pkl.core.errorMessages"
162162
,"--macro:truffle"

pkl-cli/src/main/kotlin/org/pkl/cli/CliDownloadPackageCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CliDownloadPackageCommand(
3131
if (moduleCacheDir == null) {
3232
throw CliException("Cannot download packages because no cache directory is specified.")
3333
}
34-
val packageResolver = PackageResolver.getInstance(securityManager, moduleCacheDir)
34+
val packageResolver = PackageResolver.getInstance(securityManager, httpClient, moduleCacheDir)
3535
val errors = mutableMapOf<PackageUri, Throwable>()
3636
for (pkg in packageUris) {
3737
try {

pkl-cli/src/main/kotlin/org/pkl/cli/CliProjectPackager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class CliProjectPackager(
8282
outputPath,
8383
stackFrameTransformer,
8484
securityManager,
85+
httpClient,
8586
skipPublishCheck,
8687
consoleWriter
8788
)

pkl-cli/src/main/kotlin/org/pkl/cli/CliProjectResolver.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CliProjectResolver(
4040
SecurityManagers.defaultTrustLevels,
4141
rootDir
4242
),
43+
httpClient,
4344
moduleCacheDir
4445
)
4546
val dependencies = ProjectDependenciesResolver(project, packageResolver, errWriter).resolve()

pkl-cli/src/main/kotlin/org/pkl/cli/CliRepl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal class CliRepl(private val options: CliEvaluatorOptions) : CliCommand(op
3636
SecurityManagers.defaultTrustLevels,
3737
rootDir
3838
),
39+
httpClient,
3940
Loggers.stdErr(),
4041
listOf(
4142
ModuleKeyFactories.standardLibrary,

pkl-cli/src/main/kotlin/org/pkl/cli/CliServer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.pkl.server.Server
2525
class CliServer(options: CliBaseOptions) : CliCommand(options) {
2626
override fun doRun() =
2727
try {
28-
val server = Server(MessageTransports.stream(System.`in`, System.out))
28+
val server = Server(MessageTransports.stream(System.`in`, System.out), httpClient)
2929
server.use { it.start() }
3030
} catch (e: ProtocolException) {
3131
throw CliException(e.message!!)

pkl-cli/src/test/kotlin/org/pkl/cli/CliDownloadPackageCommandTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class CliDownloadPackageCommandTest {
202202
203203
Failed to download package://bogus.domain/[email protected] because:
204204
Exception when making request `GET https://bogus.domain/[email protected]`:
205-
bogus.domain
205+
Error connecting to host `bogus.domain`.
206206
207207
"""
208208
.trimIndent()

pkl-cli/src/test/kotlin/org/pkl/cli/CliEvaluatorTest.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import org.junit.jupiter.params.provider.EnumSource
3333
import org.pkl.commons.*
3434
import org.pkl.commons.cli.CliBaseOptions
3535
import org.pkl.commons.cli.CliException
36-
import org.pkl.commons.cli.commands.BaseOptions
3736
import org.pkl.commons.test.FileTestUtils
3837
import org.pkl.commons.test.PackageServer
3938
import org.pkl.core.OutputFormat
@@ -1177,9 +1176,7 @@ result = someLib.x
11771176
sourceModules = listOf(moduleUri),
11781177
workingDir = tempDir,
11791178
moduleCacheDir = tempDir,
1180-
noCache = true,
1181-
// ensure we override any previously set root cert to the default buundle.
1182-
caCertificates = listOf(BaseOptions.Companion.includedCARootCerts())
1179+
noCache = true
11831180
),
11841181
)
11851182
val err = assertThrows<CliException> { CliEvaluator(options, consoleWriter = buffer).run() }

0 commit comments

Comments
 (0)