Add support for HTTP rewrites (#1062)

This adds a new configuration option for the HTTP client to replace URI prefixes when making outbound calls.

Follows the design of https://github.com/apple/pkl-evolution/pull/17
This commit is contained in:
Daniel Chao
2025-07-16 15:53:31 -07:00
committed by GitHub
parent fea031a138
commit 99020bb79d
27 changed files with 607 additions and 47 deletions

View File

@@ -95,7 +95,13 @@ class ServerMessagePackDecoder(unpacker: MessageUnpacker) : BaseMessagePackDecod
val httpMap = getNullable(this, "http")?.asMapValue()?.map() ?: return null
val proxy = httpMap.unpackProxy()
val caCertificates = getNullable(httpMap, "caCertificates")?.asBinaryValue()?.asByteArray()
return Http(caCertificates, proxy)
val rewrites =
getNullable(httpMap, "rewrites")
?.asMapValue()
?.map()
?.mapKeys { it.key.asStringValue().asString() }
?.mapValues { it.value.asStringValue().asString() }
return Http(caCertificates, proxy, rewrites)
}
private fun Map<Value, Value>.unpackProxy(): Proxy? {

View File

@@ -36,7 +36,7 @@ class ServerMessagePackEncoder(packer: MessagePacker) : BaseMessagePackEncoder(p
}
private fun MessagePacker.packHttp(http: Http) {
packMapHeader(0, http.caCertificates, http.proxy)
packMapHeader(0, http.caCertificates, http.proxy, http.rewrites)
http.caCertificates?.let { packKeyValue("caCertificates", it) }
http.proxy?.let { proxy ->
packString("proxy")
@@ -44,6 +44,14 @@ class ServerMessagePackEncoder(packer: MessagePacker) : BaseMessagePackEncoder(p
packKeyValue("address", proxy.address?.toString())
packKeyValue("noProxy", proxy.noProxy)
}
http.rewrites?.let { rewrites ->
packString("rewrites")
packMapHeader(rewrites.size)
for ((key, value) in rewrites) {
packString(key)
packString(value)
}
}
}
private fun MessagePacker.packDependencies(dependencies: Map<String, Dependency>) {

View File

@@ -56,6 +56,8 @@ data class Http(
val caCertificates: ByteArray?,
/** Proxy settings */
val proxy: Proxy?,
/** HTTP rewrites */
val rewrites: Map<String, String>?,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
@@ -65,12 +67,13 @@ data class Http(
if (other.caCertificates == null) return false
if (!caCertificates.contentEquals(other.caCertificates)) return false
} else if (other.caCertificates != null) return false
return Objects.equals(proxy, other.proxy)
return Objects.equals(rewrites, other.rewrites) && Objects.equals(proxy, other.proxy)
}
override fun hashCode(): Int {
var result = caCertificates?.contentHashCode() ?: 0
result = 31 * result + (proxy?.hashCode() ?: 0)
result = 31 * result + (rewrites?.hashCode() ?: 0)
return result
}
}

View File

@@ -96,6 +96,7 @@ class ServerMessagePackCodecTest {
Http(
proxy = Proxy(URI("http://foo.com:1234"), listOf("bar", "baz")),
caCertificates = byteArrayOf(1, 2, 3, 4),
rewrites = mapOf("https://foo.com" to "https://bar.com"),
),
externalModuleReaders = mapOf("external" to externalReader, "external2" to externalReader),
externalResourceReaders = mapOf("external" to externalReader),