Content-Length in HEAD requests through workers

Hi!

I think I’m seeing an issue with workers when handling HEAD requests. According to RFC the Content-Length header should be the the same as when doing a GET request. My backend server sends the correct Content-Length but when the request goes through a worker then Content-Length is always 0.
This seems to happen with even a minimal handler like this:

addEventListener('fetch', event => {
  event.respondWith(handle(event))
})
async function handle(event) {
  return await fetch(event.request)
}

Worth noting that when the request bypasses workers but still goes via Cloudflare then Content-Length is correct.

Is this expected behaviour in workers?

Thanks!

Hi @dev8,

This is a bug! Here’s the fix:

If nothing goes wrong, it should roll out by the end of this week. Thanks for reporting this!

1 Like

Great news! Thank you for the fast response!

This should now be fixed globally!

Still seeing this occur? Is there a way to omit content-length on head requests?

Hi @bryand,

I’m not sure what you mean. HEAD requests are expected to return the same Content-Length that a GET request would. If you’d like to remove the Content-Length in your Worker, you can do that through the normal header modification APIs.

@KentonVarda Head requests are currently showing length of zero just like this initial thread described.

@bryand Hmm, not in my tests. Try opening Cloudflareworkers.com, go to the “Testing” tab, and do a HEAD request there (using the default script and tutorial.Cloudflare.com web site) – I see content-length: 8167.

Are you sure your origin isn’t actually returning Content-Length: 0 ?

Weird. Only routes with workers appear to have this issue. I’ve tested two different domains with workers too. Can you message me directly?

I assume the likely cause is a 304. That would make sense.

@bryand Can you e-mail workers-developer-help at Cloudflare.com? That goes to the Workers team (not public).

My guess is this occurs for upstream 304 responses.