Skip to content

fix: preserve download integrity and progress across transfers - #252

Open
linroid wants to merge 1 commit into
mainfrom
download-integrity-regressions
Open

linroid wants to merge 1 commit into
mainfrom
download-integrity-regressions

Conversation

@linroid

@linroid linroid commented Sep 21, 2026

Copy link
Copy Markdown
Owner

HTTP downloads could accept incorrect range responses, lose final segment progress, miss live connection changes, or report an empty download as complete without creating or truncating its destination. This change fixes those cases and adds regression coverage through real Ktor connections and SQLite task storage.

  • Validate range responses and body lengths before allowing out-of-range writes; preserve single-transfer downloads from servers without range support.
  • Preserve segment checkpoints on cancellation/failure, retain completed progress, and restart transfers when the connection count changes.
  • Apply retry handling to initial metadata resolution and keep paused progress consistent with saved segments.
  • Create/truncate zero-byte destinations and correctly decode UTF-8 filenames from URLs and Content-Disposition headers.
  • Add deterministic local HTTP integration tests for interruption, retries, cancellation, resume, SQLite restart, and file integrity. Public HTTPS tests verify GitHub content by SHA-256 and HTTPBingo range downloads byte-for-byte; they are excluded from ordinary test runs.

Validation passed on this branch: 605 tests reported with zero failures (Gradle reused matching cached results where applicable), including freshly executed public HTTPS tests. Desktop and Android compilation also passed.

./gradlew :library:api:jvmTest :library:core:jvmTest :library:ktor:jvmTest \
  :library:ftp:jvmTest :library:remote:jvmTest :library:server:test \
  :app:shared:jvmTest :app:desktop:compileKotlin :library:core:compileAndroidMain \
  -PpublicDownloadTests=true

Public tests require internet access and fail on service outages rather than silently skipping. Android validation is compilation only; the file-accessor change has not been exercised on an Android device.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 21, 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-09-21T14:42:30.355050Z 67fa565 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 67fa56568a

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +163 to +165
// A server may ignore Range. Only a complete response starting at zero is safe.
HttpStatusCode.OK -> range.first == 0L && response.contentLength() == expectedBytes
else -> false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept chunked full responses when Range is ignored

When the HEAD response supplies a size but the subsequent GET ignores Range: bytes=0-(N-1) and returns the full body using chunked transfer encoding, response.contentLength() is null, so this branch rejects the response before reading it. Ignoring a Range request and omitting Content-Length are both valid server behaviors, and the body loop can still validate the received byte count against the range-derived expectedBytes; as written, downloads from such no-range servers now fail with KetchError.Unsupported.

Useful? React with 👍 / 👎.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

  821 files  + 7    821 suites  +7   5m 11s ⏱️ -19s
3 542 tests +66  3 542 ✅ +66  0 💤 ±0  0 ❌ ±0 
4 820 runs  +66  4 820 ✅ +66  0 💤 ±0  0 ❌ ±0 

Results for commit 67fa565. ± Comparison against base commit 48ed5c6.

This pull request removes 1 and adds 67 tests. Note that renamed tests count towards both.
com.linroid.ketch.file.PathFileAccessorPreallocateTest ‑ preallocate_zero_isNoOp[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ cancel_activeTransfer_cleansUpPartialFile[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_edgeSizes_preservesEveryByte[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_emptyFile_replacesExistingExplicitDestination[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_extendedServerFilename_usesDecodedName[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_ignoredRange_failsInsteadOfCorruptingFile[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_interruptedBody_retriesFromWrittenOffset[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_missingResource_failsWithoutRetryOrOutputFile[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_noRangeSupport_usesSingleCompleteTransfer[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_transientHeadFailure_retriesProbe[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_transientServerError_retriesSuccessfully[jvm]
…

This branch has not been deployed

No deployments
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