Improve HTTP headers logic (#1584)

* Relax forbidden headers constraints
  - remove restriction on browser-related headers
- allow any glob pattern (no need to end with `/` or `*`, because glob
patterns already require users to explicitly declare prefix matches if
that's the intention)
* Replace `List<Pair<, ...>>`; use `Map<String, ...>` instead
* Use glob pattern strings as an API throughout, instead of `Pattern`
(e.g. in `HttpClientBuilder`)
* Add HTTP headers to message passing API
* Add HTTP headers to executor API (introduces `ExecutorSpiOptions4`)
* Add tests for Gradle, CLI, and pkl-executor invocations
* Improve documentation
* Add `isGlobPattern` API to class `String` for in-language validation
of http headers
* Behavior change: make sure explicitly configured `User-Agent` in
`HttpClientBuilder` can be shadowed by headers (allows users to set
`--http-header "**=User-Agent: My User Agent"` and for this to be the
only user agent).

CC @kyokuping
This commit is contained in:
Daniel Chao
2026-05-21 20:07:06 -07:00
committed by GitHub
parent 87ea28260b
commit 8e2e5e4ba8
48 changed files with 1067 additions and 222 deletions
@@ -210,6 +210,11 @@ class Http {
///
/// Each rewrite must start with `http://` or `https://`, and must end with `/`.
rewrites: Mapping<String, String>?
/// HTTP headers.
/// Each entry key is a glob pattern that is matched against outbound request URLs.
/// Each value is a map of headers that is added to the matching request.
headers: Mapping<String, Mapping<String, Listing<String>>>?
}
/// Settings that control how Pkl talks to HTTP proxies.
@@ -5059,8 +5059,6 @@ in the context of that module.
[[glob-patterns]]
=== Glob Patterns
Resources and modules may be imported at the same time by globbing with the <<globbed-imports>> and <<globbed-reads>> features.
Pkl's glob patterns mostly follow the rules described by link:{uri-glob-7}[glob(7)], with the following differences:
* `*` includes names that start with a dot (`.`).
@@ -158,6 +158,23 @@ This option is commonly used to enable package mirroring.
The above example will rewrite URL `\https://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.0` to `\https://my.internal.mirror/pkl-k8s/k8s@1.0.0`.
====
.--http-header
[%collapsible]
====
Default: (none) +
Example: `**=User-Agent: My User Agent` +
Additional headers to add to outbound HTTP requests.
The syntax is `<glob pattern>=<header name>:<header value>`, and any whitespace after the leading colon is trimmed.
The glob pattern is used to match against the URLs of outbound HTTP calls, and the value is the header name and value to add.
In the case of multiple matches, every header is added.
To describe a prefix match, the `\\**` wildcard should be added to the pattern.
For example, `https?://example.com/**` matches against every request to host `example.com`.
====
.--trace-mode
[%collapsible]
====
@@ -119,6 +119,19 @@ This option is commonly used to enable package mirroring.
The above example will rewrite URL `\https://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.0` to `\https://my.internal.mirror/pkl-k8s/k8s@1.0.0`.
====
.httpHeaders: MapProperty<String, Map<String, List<String>>>
[%collapsible]
====
Default: `null` +
Example: `httpHeaders = ["**": ["User-Agent": ["My User Agent"]]]` +
Additional headers to add to outbound HTTP requests.
The key is a glob pattern that is used to match against the URLs of outbound HTTP calls, and the value is the header name and values to add.
Each header value becomes its own HTTP header.
To describe a prefix match, the `\\**` wildcard should be added to the pattern.
For example, `https?://example.com/**` matches against every request to host `example.com`.
====
.powerAssertions: Property<Boolean>
[%collapsible]
====