-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content-Length is always 0 for content obtained from a response to a HEAD request #4245
Comments
Previously, we would only keep the Content-Length header for HEAD requests on hit-for-miss objects, now we simply keep it always to enable "fallback" caching of HEAD requests. The added vtc implements the basics of the logic to enable the (reasonable) use case documented in varnishcache#2107 (comment) but using Vary instead of cache key modification plus restart. Fixes varnishcache#4245
#4247 contains the necessary change to make this work. The test case there contains a different approach to implementing the same functionality avoiding the restart and cache key modification, but using varnishtest "cache a HEAD as a fallback for a GET"
server s1 {
rxreq
expect req.method == "HEAD"
expect req.http.t == "headmiss"
txresp -nolen -hdr "Content-Length: 42"
rxreq
expect req.method == "GET"
expect req.http.t == "getmiss"
txresp -bodylen 42
} -start
varnish v1 -vcl+backend {
sub vcl_recv {
unset req.http.X-Fetch-Method;
if (req.restarts == 1) {
return (hash);
}
}
sub vcl_hash {
if (req.restarts == 1) {
hash_data(req.method);
}
}
sub vcl_miss {
if (req.method == "HEAD") {
if (req.restarts == 0) {
return (restart);
}
set req.http.X-Fetch-Method = "HEAD";
}
}
sub vcl_backend_fetch {
if (bereq.http.X-Fetch-Method) {
set bereq.method = bereq.http.X-Fetch-Method;
}
unset bereq.http.X-Fetch-Method;
}
} -start
client c1 {
# miss
txreq -method "HEAD" -hdr "t: headmiss"
rxresphdrs
# hit
txreq -method "HEAD" -hdr "t: headhit"
rxresphdrs
# miss
txreq -hdr "t: getmiss"
rxresp
# hits on full object
txreq -hdr "t: gethit"
rxresp
txreq -method "HEAD" -hdr "t: getheadhit"
rxresphdrs
} -run
server s1 -wait Again, this is only for completeness, I do NOT recommend using this approach. |
Thanks @nigoroll, I will test the code from #4247.
Does it mean you recommend using the snippet with |
@sbraz yes I would recommend the I would be hesitant to add this particular and quite special use case to the core documentation, but rather add it to https://varnish-cache.org/tips/index.html, which we intend to revive. |
Previously, we would only keep the Content-Length header for HEAD requests on hit-for-miss objects, now we simply keep it always to enable "fallback" caching of HEAD requests. The added vtc implements the basics of the logic to enable the (reasonable) use case documented in varnishcache#2107 (comment) but using Vary instead of cache key modification plus restart. Fixes varnishcache#4245
Expected Behavior
When the backend returns a
Content-Length
while replying to a HEAD request, it should be passed to the client.Current Behavior
When the content was fetched in response to a HEAD request, the client receives
Content-Length: 0
.Possible Solution
No response
Steps to Reproduce (for bugs)
tshark -f "host D.E.F.G" -t ad -V
curl http://localhost/pub/debian/README.html -I
and the reply:
Context
Sending a Content-Length of 0 looks incorrect:
according to https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
It also breaks some clients such as (Go-http-client used by Packer doesn't even attempts to send a GET after the HEAD as it assumes that the file is empty).
Varnish Cache version
varnishd (varnish-trunk revision 772d738)
Operating system
Debian 12
Source of binary packages used (if any)
N/A
The text was updated successfully, but these errors were encountered: