Add request headers to wasmtime serve - #13471
#13471Add request headers to wasmtime serve#13471alexcrichton merged 3 commits intobytecodealliance:mainbytecodealliance/wasmtime:mainfrom angelnereira:serve-cli-headersangelnereira/wasmtime:serve-cli-headersCopy head branch name to clipboard
Conversation
| 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}`"))?; |
There was a problem hiding this comment.
s/header/value/ in the error message perhaps?
| .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}`"))?; |
There was a problem hiding this comment.
s/header/name/ in the error message perhaps?
| fn parse(headers: &[String]) -> Result<Self> { | ||
| let mut entries = Vec::new(); | ||
| for header in headers { | ||
| if let Some(path) = header.strip_prefix('@') { |
There was a problem hiding this comment.
Could you add a test for the file-reading behavior as well?
| for name in self.entries.iter().map(|(name, _)| name) { | ||
| headers.remove(name); | ||
| } | ||
| for (name, value) in &self.entries { | ||
| headers.append(name, value.clone()); | ||
| } |
There was a problem hiding this comment.
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?
|
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. |
|
Update pushed. I kept the two-phase remove/append behavior intentionally: first remove all request-provided values for any CLI-specified header I added coverage for that repeated-header case, added coverage for |
Summary
Fixes #11925.
This adds
-H, --headertowasmtime serveso 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.--header @path/to/headers.txtreads one header per non-empty line.Testing
cargo fmt --all -- --checkgit diff --checkcargo check -p wasmtime-clicargo test -p wasmtime-cli --test all p2_cli_serve_header_replaces_request_header