Content-Length in HEAD requests through workers


#1

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!


#2

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!


#3

Great news! Thank you for the fast response!


#4

This should now be fixed globally!


#5

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


#6

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.


#7

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


#8

@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 ?


#9

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.


#10

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


#11

My guess is this occurs for upstream 304 responses.