Repository navigation
Conversation
…t-Encoding When a response is sent with a `Content-Encoding` such as gzip, the `Content-Length` header holds the size of the compressed body, while the fetch stream yields the decompressed bytes. `FetchBackend` used `Content-Length` as the `total` of its download progress events, so `loaded` and `total` counted different bytes. For a 1.2 MB JSON body served with gzip (100 KB on the wire), the events reported `loaded: 1197781` with `total: 102460`. Omit `total` when `Content-Encoding` is present in the response headers, since the encoded size cannot be compared with the decoded bytes read from the stream. This matches `HttpXhrBackend` in Chrome, which reports no total for the same gzip response. A cross-origin response only exposes `Content-Encoding` when the server lists it in `Access-Control-Expose-Headers`.
JeanMeche
left a comment
There was a problem hiding this comment.
AGENT: Thanks for the PR! This addresses a real mismatch between compressed Content-Length and decompressed stream bytes in FetchBackend and brings it closer to HttpXhrBackend. I left two small suggestions inline.
| const contentLength = response.headers.get('content-length'); | ||
| const contentLengthValue = contentLength !== null ? Number(contentLength) : NaN; | ||
| const total = | ||
| Number.isFinite(contentLengthValue) && !response.headers.has('content-encoding') |
There was a problem hiding this comment.
AGENT: response.headers.has('content-encoding') will also match Content-Encoding: identity (or an empty header value), where no decompression is applied and Content-Length still matches the decoded byte count.
Consider checking the normalized header value instead (e.g., treating "" and "identity" as unencoded):
const contentEncoding = response.headers.get('content-encoding')?.trim().toLowerCase();
const isEncoded = Boolean(contentEncoding && contentEncoding !== 'identity');
const total = Number.isFinite(contentLengthValue) && !isEncoded ? contentLengthValue : undefined;It would also be great to add a companion test case in fetch_spec.ts verifying that Content-Encoding: identity still reports total.
There was a problem hiding this comment.
Done in 8a4eb51, using your suggestion. identity, an empty value and Identity with surrounding spaces now keep the total (checked in Chromium and Firefox), and fetch_spec.ts has a test for identity.
| switch (event.type) { | ||
| case HttpEventType.DownloadProgress: | ||
| console.log('Downloaded ' + event.loaded + ' out of ' + event.total + ' bytes'); | ||
| if (event.total !== undefined) { |
There was a problem hiding this comment.
AGENT: Consider adding a brief sentence in the surrounding prose explaining why event.total can be undefined (for example, compressed or chunked responses where the uncompressed size is not known), so readers understand why this guard is needed.
There was a problem hiding this comment.
Added a sentence after the example in 0baf4e8.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
FetchBackendusesContent-Lengthas thetotalof download progress events. When the response has aContent-Encodingsuch as gzip,Content-Lengthis the size of the compressed body, whileloadedcounts the decoded bytes read from the stream. A 1.2 MB JSON response served with gzip (100 KB on the wire) ends withloaded: 1197781andtotal: 102460, so the progress example from the HTTP guide prints more downloaded bytes than the total. Measured with 22.3.0 in Chromium 145 and Firefox 146. WithwithXhr(), Chromium reports nototalfor the same response.Issue Number: N/A
What is the new behavior?
FetchBackendleavestotalundefined when the response headers include aContent-Encodingother thanidentity. Responses without one, or withidentity, keep theirtotal. The progress example in the HTTP guide now prints only the downloaded bytes when there is nototal, and the guide explains when that happens.Does this PR introduce a breaking change?
Other information
For a cross-origin response, the response headers only include
Content-Encodingwhen the server lists it inAccess-Control-Expose-Headers, so without that the wrongtotalremains.