Some CDNs (in my case keyCDN) need the content length header to be able to cache the response.

The first question is why this is missing as a normal Drupal HTML response has this header.

CommentFileSizeAuthor
#5 jsonapi-3040738-5.patch694 bytesikphilip

Comments

yobottehg created an issue. See original summary.

wim leers’s picture

Title: Add the content length header to the response » Add the `Content-Length` header to the response
Status: Active » Postponed (maintainer needs more info)
Issue tags: +API-First Initiative

Interesting!

This is the first time I hear that Content-Length is required for a CDN to be able to cache a response — this does not make sense I think, but 🤷‍♂️ Are you absolutely certain?

I would think that Symfony should be doing this for us automatically, but apparently it is not. Could you investigate why Symfony doesn't do this?

yobottehg’s picture

Assigned: Unassigned » yobottehg
Status: Postponed (maintainer needs more info) » Needs work

this does not make sense I think

Yes thats true but i got it from their support because i ask why my server response is not cacheable for the their CDN.

Are you absolutely certain?

So yes i am :)

I would think that Symfony should be doing this for us automatically

I'll try to investigate in the next days. I'll assign this to me.

wim leers’s picture

Yay, thanks! It shouldn’t be too difficult to add this :)

ikphilip’s picture

StatusFileSize
new694 bytes

@yobottehg I looked into this. There's a few things.

You will not have a content-length header if transfer-encoding header is set.

Symfony\Component\HttpFoundation\Response

// Fix Content-Length
  if ($headers->has('Transfer-Encoding')) {
    $headers->remove('Content-Length');
  }

But I could not track where transfer-encoding is set in Drupal so this code is not executed

I looked at my Apache config and realized it will set Transfer-Encoding header before delivering a response depending on if the response body is compressed or not(?). I wonder if this is can be solved in http server settings instead of Drupal.

If you desire a patch for your JSONAPI responses then it would be attached. However the value not be accurate if you're transfer-encoding is chunked or otherwise when it's dispatched from the http server. So I would not recommend adding a content-length based on what I've read.

wim leers’s picture

Drupal/Symfony also don't do this for HTML responses. Caching HTML responses on a CDN is far more common. Yet I haven't seen complaints about that. I don't think this problem is JSON:API-specific.

So I searched some more. And there is an issue for the general problem, and apparently I reviewed the patch over there too. It's been blocked on tests for over 2.5 years now: #2579775: Missing Content-Length header blocks response until all registered shutdown functions execute and the Kernel terminate events run. Let's fix that issue instead!

wim leers’s picture

Project: JSON:API » Drupal core
Version: 8.x-2.x-dev » 8.8.x-dev
Component: Code » jsonapi.module
e0ipso’s picture

Some related FUD: I've had some very tricky and obscure issues in RESTful with this header when there's utf content and responses are compressed.

vacilando’s picture

Recently needed the same so as to be able to use KeyCDN.

Successfully solved using module https://www.drupal.org/project/content_length_header (full blog post at https://www.vacilando.org/article/using-cdn-accelerate-drupal-api-sourci...).