Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add request headers to wasmtime serve - #13471

#13471
Merged
alexcrichton merged 3 commits into
bytecodealliance:mainbytecodealliance/wasmtime:mainfrom
angelnereira:serve-cli-headersangelnereira/wasmtime:serve-cli-headersCopy head branch name to clipboard
May 25, 2026
Merged

Add request headers to wasmtime serve#13471
alexcrichton merged 3 commits into
bytecodealliance:mainbytecodealliance/wasmtime:mainfrom
angelnereira:serve-cli-headersangelnereira/wasmtime:serve-cli-headersCopy head branch name to clipboard

Conversation

@angelnereira

Copy link
Copy Markdown
Contributor

Summary

Fixes #11925.

This adds -H, --header to wasmtime serve so requests can be forwarded to the component with CLI-provided headers. This is useful when testing components that normally run behind a proxy or gateway that injects authentication, identity, routing, or other request metadata.

Behavior:

  • --header 'name: value' replaces incoming request headers with the same name before the request is passed to the component.
  • The option can be provided more than once; repeated names produce multiple forwarded values in CLI order.
  • --header @path/to/headers.txt reads one header per non-empty line.

Testing

  • cargo fmt --all -- --check
  • git diff --check
  • cargo check -p wasmtime-cli
  • cargo test -p wasmtime-cli --test all p2_cli_serve_header_replaces_request_header

@angelnereira
angelnereira requested a review from a team as a code owner May 24, 2026 11:41
@angelnereira
angelnereira requested review from pchickey and removed request for a team May 24, 2026 11:41

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread src/commands/serve.rs Outdated
let name = HeaderName::from_bytes(name.trim().as_bytes())
.with_context(|| format!("invalid header name in `{header}`"))?;
let value = HeaderValue::from_str(value.trim_start())
.with_context(|| format!("invalid header value in `{header}`"))?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/header/value/ in the error message perhaps?

Comment thread src/commands/serve.rs Outdated
.split_once(':')
.with_context(|| format!("header `{header}` is missing `:`"))?;
let name = HeaderName::from_bytes(name.trim().as_bytes())
.with_context(|| format!("invalid header name in `{header}`"))?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/header/name/ in the error message perhaps?

Comment thread src/commands/serve.rs
fn parse(headers: &[String]) -> Result<Self> {
let mut entries = Vec::new();
for header in headers {
if let Some(path) = header.strip_prefix('@') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for the file-reading behavior as well?

Comment thread src/commands/serve.rs
Comment on lines +1342 to +1347
for name in self.entries.iter().map(|(name, _)| name) {
headers.remove(name);
}
for (name, value) in &self.entries {
headers.append(name, value.clone());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, how come this removes everything, then appends everything? I'd naively expect that remove-then-append for each key individually would work?

If it's for some specific behavior, could a test be added for that?

@angelnereira

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I’ll update the two error messages, add coverage for @file header loading, and add a test/comment for the two-phase remove/append behavior to preserve repeated CLI-provided header values.

@angelnereira

Copy link
Copy Markdown
Contributor Author

Update pushed.

I kept the two-phase remove/append behavior intentionally: first remove all request-provided values for any CLI-specified header
name, then append all CLI-provided values in CLI order. This preserves repeated CLI-provided values for the same header name, which
would be lost if we removed and appended one entry at a time.

I added coverage for that repeated-header case, added coverage for --header=@file, and updated the error messages to distinguish
invalid header names from invalid header values.

@alexcrichton
alexcrichton added this pull request to the merge queue May 25, 2026
Merged via the queue into bytecodealliance:main with commit aa140a1 May 25, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add headers in wasmtime serve via CLI

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.