1
0
mirror of https://github.com/square/okhttp.git synced 2025-12-25 00:01:02 +03:00

Native Image Feature. (#7071)

This commit is contained in:
Yuri Schimke
2022-02-09 19:04:49 +00:00
committed by GitHub
parent f5758b7f26
commit 7620f3ddd6
7 changed files with 85 additions and 20 deletions

View File

@@ -28,7 +28,7 @@ object Versions {
const val moshi = "1.13.0"
const val okio = "3.0.0"
const val picocli = "4.6.2"
const val graal = "21.2.0"
const val graal = "22.0.0.2"
}
object Dependencies {

View File

@@ -2,6 +2,7 @@ import org.apache.tools.ant.taskdefs.condition.Os
plugins {
id("com.palantir.graal")
kotlin("jvm")
}
dependencies {
@@ -38,14 +39,12 @@ animalsniffer {
}
sourceSets {
// Not included in IDE as this confuses Intellij for obvious reasons.
main {
java.srcDirs(
"../okhttp/src/test/java",
"../okhttp-brotli/src/test/java",
"../okhttp-dnsoverhttps/src/test/java",
"../okhttp-logging-interceptor/src/test/java",
"../okhttp-sse/src/test/java"
"../okhttp-sse/src/test/java",
)
}
}
@@ -60,11 +59,4 @@ graal {
option("--allow-incomplete-classpath")
option("--report-unsupported-elements-at-runtime")
option("-H:+ReportExceptionStackTraces")
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// May be possible without, but autodetection is problematic on Windows 10
// see https://github.com/palantir/gradle-graal
// see https://www.graalvm.org/docs/reference-manual/native-image/#prerequisites
windowsVsVarsPath("C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build\\vcvars64.bat")
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (C) 2022 Square, Inc.
*
* Licensed 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 okhttp3
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isGreaterThan
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
import org.junit.jupiter.api.Test
class PublicSuffixDatabaseTest {
@Test
fun testResourcesLoaded() {
val url = "https://api.twitter.com".toHttpUrl()
assertThat(url.topPrivateDomain()).isEqualTo("twitter.com")
}
@Test
fun testPublicSuffixes() {
PublicSuffixDatabase::class.java.getResourceAsStream(PublicSuffixDatabase.PUBLIC_SUFFIX_RESOURCE).use {
assertThat(it.available()).isGreaterThan(30000)
}
}
}

View File

@@ -17,7 +17,6 @@ package okhttp3
import mockwebserver3.MockResponse
import mockwebserver3.MockWebServer
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
import org.assertj.core.api.AssertionsForClassTypes.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
@@ -52,13 +51,6 @@ class SampleTest {
}
}
@Test
fun testPublicSuffixes() {
PublicSuffixDatabase::class.java.getResourceAsStream(PublicSuffixDatabase.PUBLIC_SUFFIX_RESOURCE).use {
assertThat(it.available()).isGreaterThan(30000)
}
}
@ParameterizedTest
@ArgumentsSource(SampleTestProvider::class)
fun testParams(mode: String) {

View File

@@ -33,6 +33,7 @@ okhttp3.MultipartReaderTest
okhttp3.OkHttpClientTest
okhttp3.OkHttpTest
okhttp3.ProtocolTest
okhttp3.PublicSuffixDatabaseTest
okhttp3.PublicInternalApiTest
okhttp3.RequestTest
okhttp3.ResponseBodyTest
@@ -83,4 +84,4 @@ okhttp3.logging.HttpLoggingInterceptorTest
okhttp3.logging.IsProbablyUtf8Test
okhttp3.logging.LoggingEventListenerTest
okhttp3.sse.internal.EventSourceHttpTest
okhttp3.sse.internal.ServerSentEventIteratorTest
okhttp3.sse.internal.ServerSentEventIteratorTest

View File

@@ -128,10 +128,13 @@ project.applyOsgi(
"Import-Package: " +
"android.*;resolution:=optional," +
"com.oracle.svm.core.annotate;resolution:=optional," +
"com.oracle.svm.core.configure;resolution:=optional," +
"dalvik.system;resolution:=optional," +
"org.conscrypt;resolution:=optional," +
"org.bouncycastle.*;resolution:=optional," +
"org.openjsse.*;resolution:=optional," +
"org.graalvm.nativeimage;resolution:=optional," +
"org.graalvm.nativeimage.hosted;resolution:=optional," +
"sun.security.ssl;resolution:=optional,*",
"Automatic-Module-Name: okhttp3",
"Bundle-SymbolicName: com.squareup.okhttp3"

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2022 Square, Inc.
*
* Licensed 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 okhttp3.internal.graal
import com.oracle.svm.core.annotate.AutomaticFeature
import com.oracle.svm.core.configure.ResourcesRegistry
import org.graalvm.nativeimage.ImageSingletons
import org.graalvm.nativeimage.hosted.Feature
/**
* Automatic configuration of OkHttp for native images.
*
* Currently, includes all necessary resources.
*/
@AutomaticFeature
class OkHttpFeature : Feature {
override fun beforeAnalysis(access: Feature.BeforeAnalysisAccess?) {
val resourcesRegistry = ImageSingletons.lookup(ResourcesRegistry::class.java)
resourcesRegistry.addResources(
"\\Qokhttp3/internal/publicsuffix/PublicSuffixDatabase.gz\\E"
)
}
}