Skip to content

fix(http): ask for identity encoding so compressed lengths never truncate files - #623

Merged
linroid merged 1 commit into
mainfrom
fix/http-identity-encoding
Oct 23, 2026
Merged

linroid merged 1 commit into
mainfrom
fix/http-identity-encoding

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Found while downloading real HTTPS resources with the CLI: files were silently truncated.

ketch 'https://api.github.com/repos/linroid/Ketch/releases?per_page=5'
Download completed: releases        # 20,142 bytes: the first 20 KB of a 194 KB JSON, invalid

Why

On the JVM and Android, HTTPS goes through OkHttp, which adds Accept-Encoding: gzip to every request without a Range. Ketch's HEAD is one of them:

  1. HEAD → the server compresses on the fly and reports the compressed Content-Length (20,142), plus Accept-Ranges: bytes.
  2. Ketch plans ranged GETs for bytes 0–20,141. OkHttp never asks for gzip on ranged requests, so the server sends plain bytes.
  3. Ketch completes at 20,142 bytes. No error is raised and the checksum check doesn't apply (no digest is published).

Any server that compresses on the fly (nginx gzip on, GitHub's API, many CDNs) causes this for text, JSON, CSV, HTML, XML or SVG files. iOS's NSURLSession adds gzip by default too.

What changes

KtorHttpEngine.requestHeaders adds Accept-Encoding: identity unless the caller names an encoding, as wget does. Lengths, ranges and bytes then all describe the same representation. Callers that pass their own Accept-Encoding keep it. Cross-origin redirects already keep Accept-Encoding.

Verification

  • ketch from this branch on the GitHub API URL saves the whole document, which parses as JSON with its 5 entries. Before: 20,142 bytes that failed to parse.
  • A Gutenberg text (streamed, no length) is still byte-identical to curl --compressed, and binary release assets are unchanged.
  • New KtorHttpEngineEncodingTest: HEAD and GET ask for identity; a caller's own accept-encoding: gzip is kept.
  • ./gradlew :library:ktor:jvmTest :library:core:jvmTest --tests '*Http*' --tests '*Headers*' passes.

…cate files

Downloading https://api.github.com/repos/linroid/Ketch/releases saved the
first 20 KB of a 194 KB JSON. OkHttp asks for gzip on requests without a
range, a HEAD included, so the server reported its compressed length; the
ranged GETs that followed, which OkHttp never compresses, got the plain
bytes, and Ketch stopped at the compressed size. Servers that compress on
the fly (nginx gzip, GitHub's API) did this for any text, JSON, CSV, HTML
or SVG file.

KtorHttpEngine now sends Accept-Encoding: identity unless the caller names
an encoding, as download managers such as wget do.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T16:08:38.847334Z ab3767f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 200 files  +1   2 228 suites  +1   58m 2s ⏱️ + 1m 11s
15 320 tests +1  15 052 ✅ +1  268 💤 ±0  0 ❌ ±0 
18 677 runs  +1  18 409 ✅ +1  268 💤 ±0  0 ❌ ±0 

Results for commit ab3767f. ± Comparison against base commit c7c6457.

@linroid
linroid merged commit 6616d52 into main Oct 23, 2026
15 checks passed
@linroid
linroid deleted the fix/http-identity-encoding branch October 23, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant