移除“Expires” HTTP头(用于StreamedFiles)

huangapple go评论85阅读模式
英文:

Remove "Expires" HTTP header (for StreamedFiles)

问题

在 micronaut 过滤器中,我可以指定自己的头部,例如,我使用 "Cache-Control" 头部并设置 "max-age" 指令。因此,我想要移除 "Expires" 头部,因为使用 "Cache-Control" 会忽略 "Expires" 头部1

当从过滤器返回 StreamedFile 时,"Expires" 和 "Date" 头部会被 FileTypeHandler 设置2,我不知道如何更改这些。

是否有更改这些的选项?

示例:

@Filter("/**")
public class MyFilter implements HttpServerFilter {

    @Inject
    ImageService imageService;

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
        File image = imageService.getImage(request);

        return Publishers.just(
                HttpResponse.ok(new StreamedFile(new FileInputStream(image), MediaType.IMAGE_JPEG_TYPE))
                        .header("Cache-Control", "max-age=31449600")
                        .header("Access-Control-Allow-Methods", "GET")
                        .header("Referrer-Policy", "same-origin")
        );
    }

}
英文:

In a micronaut filter I specify my own headers, e. g. I set the "Cache-Control" header with a "max-age" directive. Therefore I want to remove the "Expires" header because by using "Cache-Control" the "Expires" header is ignored 1.

When returning a StreamedFile from a filter the "Expires" and "Date" header is set by FileTypeHandler 2 and I do not know how to change this.

Are there options to change this?

Example:

@Filter(&quot;/**&quot;)
public class MyFilter implements HttpServerFilter {

    @Inject
    ImageService imageService;

    @Override
    public Publisher&lt;MutableHttpResponse&lt;?&gt;&gt; doFilter(HttpRequest&lt;?&gt; request, ServerFilterChain chain) {
        File image = imageService.getImage(request);

        return Publishers.just(
                HttpResponse.ok(new StreamedFile(new FileInputStream(image), MediaType.IMAGE_JPEG_TYPE))
                        .header(&quot;Cache-Control&quot;, &quot;max-age=31449600&quot;)
                        .header(&quot;Access-Control-Allow-Methods&quot;, &quot;GET&quot;)
                        .header(&quot;Referrer-Policy&quot;,  &quot;same-origin&quot;)
        );
    }

}

答案1

得分: 2

不确定您为什么要从过滤器中返回文件。

如果只是您识别出的方法困扰您生成这些标头,您可以直接覆盖它:

@Singleton
@Replaces(FileTypeHandler.class)
public class CustomFileTypeHandler extends FileTypeHandler {

    public CustomFileTypeHandler(FileTypeHandlerConfiguration configuration) {
        super(configuration);
    }

    @Override
    protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastModified) {
        // 什么也不做
    }
}
英文:

Not sure why exactly you want to return a file from a filter

If it is just the method you identified that bother you generating this headers, you can just override it :

@Singleton
@Replaces(FileTypeHandler.class)
public class CustomFileTypeHandler extends FileTypeHandler {

    public CustomFileTypeHandler(FileTypeHandlerConfiguration configuration) {
        super(configuration);
    }

    @Override
    protected void setDateAndCacheHeaders(MutableHttpResponse response, long lastModified) {
        //do nothing
    }
}

huangapple
  • 本文由 发表于 2020年9月17日 15:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定