Header Content-Length/FixedLengthStream is always ignored

In dealing with an embedded fossil browser, it has a bug that it won’t execute Javascript code or browser cache any asset unless it has a content-length header. So lets try the official CF documented way to add a Content-Length header to a HTTP response.

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
/**
 * Respond with hello worker text
 * @param {Request} request
 */
async function handleRequest(request) {
  const { writable, readable } = new FixedLengthStream(body.length);

  const enc = new TextEncoder();
  const writer = writable.getWriter();
  writer.write(enc.encode(body));
  writer.close();
  return new Response(readable, {
    headers: { 'content-type': 'text/plain' },
  })
}

var body=`Colin Robert Chase (February 5, 1935 – October 13, 1984) was an American academic. An associate professor of English at the University of Toronto, he was known for his contributions to the studies of Old English and Anglo-Latin literature. His father was a newspaper executive and his mother, Mary Chase, was a playwright who won the Pulitzer Prize for Drama. His best-known work, The Dating of Beowulf, challenged the accepted consensus as to when the Anglo-Saxon poem Beowulf (page pictured) was created; it left behind what was described in A Beowulf Handbook as "a cautious and necessary incertitude". Chase was also known for writing Two Alcuin Letter-Books, a scholarly collection of 24 letters by the 8th-century scholar Alcuin. He also contributed to the Dictionary of the Middle Ages and wrote the Beowulf section of "This Year's Work in Old English Studies" for the Old English Newsletter for nearly a decade. (Full article...)`;

Running curl on plain HTTP to avoid any HTTP2 weirdness,

C:\dev\app>curl -v --compressed -o t.txt "http://muddy-snowflake-aba7.wv
oice.workers.dev/"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*
  Trying 104.21.24.183:80...
* Connected to muddy-snowflake-aba7.wvoice.workers.dev (104.21.24.183) port 80 (
#0)
> GET / HTTP/1.1
> Host: muddy-snowflake-aba7.wvoice.workers.dev
> User-Agent: curl/7.86.0
> Accept: */*
> Accept-Encoding: deflate, gzip, br, zstd
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sun, 05 Feb 2023 22:32:29 GMT
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Connection: keep-alive
< Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=
1juqoYNlQ8aJol22frRcq%2Fp6%2FzAKDSqpsJHRP9ceytnFZVT5jpUx5%2FN6pkcFBIcJkr7kVeEe9X
GdYluICIvDtnVQYkfJBoxKGUZVsk8Bc04M59inTthVnHRI00Z3XOEy62KShtGo8hvHo8FE9YUhFowCuq
G9aYbm7iw%3D"}],"group":"cf-nel","max_age":604800}
< NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
< Vary: Accept-Encoding
< Server: cloudflare
< CF-RAY: 794f1a320cd38c2a-EWR
< Content-Encoding: gzip
< alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
<
{ [20 bytes data]
* transfer closed with outstanding read data remaining
100    15    0    15    0     0     88      0 --:--:-- --:--:-- --:--:--    89
* Closing connection 0
curl: (18) transfer closed with outstanding read data remaining

C:\dev\app>

Where is the content-length header?

if I shorten the resp body to 19 chars, CF gzip smartly!!! TURNS OFF and I get a content-length header

C:\dev\app>curl -v --compressed -o t.txt "http://muddy-snowflake-aba7.wv
oice.workers.dev/"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*
  Trying 104.21.24.183:80...
* Connected to muddy-snowflake-aba7.wvoice.workers.dev (104.21.24.183) port 80 (
#0)
> GET / HTTP/1.1
> Host: muddy-snowflake-aba7.wvoice.workers.dev
> User-Agent: curl/7.86.0
> Accept: */*
> Accept-Encoding: deflate, gzip, br, zstd
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sun, 05 Feb 2023 22:35:02 GMT
< Content-Type: text/plain
< Content-Length: 19
< Connection: keep-alive
< Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=
bzgAow%2BzA0wRhY7L5nHLd8Z6HIAz5IkyDDsj3wtS3UM26hRUIwGN62FOmyryk6FpVGcGXE1SB%2B1p
B5sORI6ZuySXrbvS3hFAd8vV9ieNv9yQtAxEsn30zqGC4qDUoJOL87nnyzy%2FO3yc%2FZm7cEuSyhDU
3VZdAkmzm7U%3D"}],"group":"cf-nel","max_age":604800}
< NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
< Vary: Accept-Encoding
< Server: cloudflare
< CF-RAY: 794f1dec7cf68c87-EWR
< alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
<
{ [19 bytes data]
100    19  100    19    0     0     73      0 --:--:-- --:--:-- --:--:--    73
* Connection #0 to host muddy-snowflake-aba7.wvoice.workers.dev left intact

C:\dev\app>

How do I really get the Content-Length header? My data is already always in a JS string obj, not a variable length stream.

I suggest using Transform Rules to set the header.

Transform Rules

You won’t get a Content-Length header with a chunked Transfer-Encoding.

https://support.cloudflare.com/hc/en-us/articles/200168386-Why-is-my-dynamic-content-being-sent-with-chunked-encoding-

So, using Cloudflare Workers, I found a solution to deliver gz encoding+content length header, every time, every permutation of UA. Obviously my resp body is a string or arraybuffer var in JS, not a stream object with random writes and random backpressure. All the CFW runtime API class names I use are googleable. If Kenton can’t post the solution in here, I’ll just keep it to myself. Need something for my resume :innocent:

Im still deciding if to just upgrade to IE 6 from IE 5 on the problematic terminals to fix the problem.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.