router icon indicating copy to clipboard operation
router copied to clipboard

Unable to Set Vary Header in Plugins (rust or rhai)

Open lleadbet opened this issue 3 years ago • 2 comments

Describe the bug It is currently not possible to set the Vary header when using Rhai scripts, as it's overwritten by the router.

To Reproduce Using the following script and configuration, you are returned the following headers. Notably missing is the potato entry for the Vary header.

fn router_service(service) {
    const response_callback = Fn("process_response");
    service.map_response(response_callback);
}

fn process_response(response) {
    response.headers["vary"] = "potato";
    response.headers["vary2"] = "potato";
}
plugins:
  experimental.rhai:
    filename: "./main.rhai"

image

Expected behavior Headers set via Rhai are respected over default values.

Desktop (please complete the following information):

  • OS: OSX
  • Version: v0.9.5

Additional context Alternatively, document headers that are unable to be overridden within documentation.

lleadbet avatar Jun 22 '22 17:06 lleadbet

This may not be a Rhai issue, but a plugin issue: i.e. you would have the same problem if you did this from a rust plugin. I'll confirm that as part of investigating this.

garypen avatar Jun 22 '22 18:06 garypen

I've verified that this is a "plugin" problem by trying to do the same thing from a rust plugin and getting the same result. I think the solution will be related to the fix for #1284, so we should make sure this use case is also addressed.

garypen avatar Jun 23 '22 09:06 garypen

After a little digging, I've confirmed that the root cause of the problem is the CORSLayer which is operating in the router.

By default, the layer sets the vary headers as follows: https://docs.rs/tower-http/latest/tower_http/cors/struct.CorsLayer.html#method.vary

Since we are not doing anything to change this behaviour, any vary header set in a response will be clobbered by this.

garypen avatar Aug 30 '22 17:08 garypen

More digging shows that setting an empty .vary([]) list when creating the CorsLayer means no default value is set and any value set by users is preserved. I'll proceed with a fix based on this.

garypen avatar Aug 31 '22 08:08 garypen