Skip to content

feat(metalink): download Metalinks from all their mirrors at once - #585

Merged
linroid merged 7 commits into
mainfrom
feat/metalink-mirrors
Oct 18, 2026
Merged

linroid merged 7 commits into
mainfrom
feat/metalink-mirrors

Conversation

@linroid

@linroid linroid commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Summary

Implements the Metalink roadmap item: downloads from several mirrors at once, with checksums and piece verification.

  • library:metalink (new published module, same targets as core): MetalinkDownloadSource reads Metalink 4 (.meta4, RFC 5854) and Metalink 3 (.metalink) from links and dropped/opened files. The parsed document is kept with the task (METAINFO_KEY), so resumes never fetch it again. It comes with a small XML reader that refuses DOCTYPE declarations and limits nesting, element count and document size (8 MiB).
  • Core MirrorDownloadHelper (com.linroid.ketch.core.mirror):
    • Segments work as in HTTP downloads, including live resegmenting, and each one is fetched from the least busy mirror, best priority first.
    • A failed segment continues from the byte it reached on another mirror.
    • Mirrors are dropped for errors retrying can't fix (4xx, wrong range) and otherwise back off, doubling up to 60 s.
    • The last mirror left goes through Ketch's normal RetryPolicy, so a one-mirror Metalink behaves like plain HTTP.
  • Checks:
    • Piece hashes are checked once the download finishes. Mirrors that sent bad pieces are dropped (while another remains), and those pieces are fetched again, up to 3 times.
    • Ketch then verifies the whole file against the Metalink's strongest hash (publishedChecksum); a mismatch is ChecksumMismatch.
  • Security:
    • Request headers (cookies) only go to mirrors on the Metalink's own origin.
    • A Metalink fetched over HTTPS keeps plain-HTTP mirrors only when the file has a SHA-1 or stronger hash.
    • Mirrors with user info, and non-HTTP mirrors, are skipped.
  • Several files: offered as a FileSelectionMode.SINGLE choice, the first built-in source to use it. New SourceFile.FILE_NAME_KEY makes the chosen file save under its own name. Choosing another file is refused once the task has an output path.
  • API: KetchFeatures.METALINK (download.metalink) and SourceFile.FILE_NAME_KEY (API dump updated).
  • Apps/CLI: the source is registered in Android, desktop, desktop-native, iOS and all four CLI engines. Dropped .meta4/.metalink files open the add sheet like .torrent files. The add sheet keeps exactly one file checked for single-choice rows and hides "Download all".
  • Docs: new docs/metalink.md; README (roadmap entry moved to features), AGENTS.md, architecture, API, developer, CLI and testing docs updated. CI runs :library:metalink:testAndroidHostTest.

Limits

  • A task downloads one file of a Metalink, not all of them.
  • The apps don't register as the system handler for .meta4 files.
  • Metalinks announced only through HTTP Link headers (RFC 6249) download as plain HTTP.

Snapshots

The add sheet for a .meta4 link listing three files. Before, every file was checked and "Download all" was offered, and Ketch refuses that selection for a single-choice source. After, one file is chosen and saved under its own name.

Before After
Desktop before after
Phone

Testing

  • MirrorDownloadHelperTest (core): spreading across mirrors and priorities, header scoping, refusing and mid-transfer failover, corrupt-piece repair, failing after repairs, last-mirror rethrow, resuming saved segments.
  • MetalinkTest and MetalinkDownloadSourceTest: v4/v3 parsing, HTTPS downgrade rule, invalid pieces, malformed XML and DOCTYPE, canHandle, URL/content resolve, size probing, downloading the chosen file, selection rules.
  • MetalinkKetchTest (jvmTest): a whole Ketch downloading to disk from three mirrors with checksum verification, the chosen file saved under its name, and a wrong whole-file hash failing as ChecksumMismatch.
  • IntakeSingleChoiceTest (app).
  • Ran: :library:metalink:allTests, testAndroidHostTest, :library:core:jvmTest, iOS test compilation, :app:shared:jvmTest (intake), :app:android:compileDirectDebugKotlin, :app:shared:compileKotlinIosSimulatorArm64, :cli:compileKotlin, :app:desktop:compileKotlin.

Adds library:metalink with MetalinkDownloadSource, which reads Metalink 4 (.meta4) and
Metalink 3 (.metalink) links and dropped files and downloads the file from every HTTP(S)
mirror they list at once. Core gains MirrorDownloadHelper: segments are fetched from the
least busy mirror, a failed segment continues on another mirror from the byte it reached,
pieces that fail their hashes are refetched from other mirrors after the mirrors that sent
them are dropped, and Ketch verifies the whole file against the Metalink's strongest hash.

A Metalink listing several files offers them as a single choice, saved under the chosen
file's own name (SourceFile.FILE_NAME_KEY). The apps and the CLI register the source, the
add sheet keeps one file checked for single-choice rows, and dropped .meta4/.metalink files
open the add sheet like .torrent files.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 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-17T16:37:43.047877Z 69e3289 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 17, 2026

Copy link
Copy Markdown

Code review

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

@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: 69e328902c

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

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 063 files  + 15   2 091 suites  +15   31m 48s ⏱️ - 1m 48s
13 856 tests +140  13 591 ✅ +139  265 💤 +1  0 ❌ ±0 
16 847 runs  +140  16 582 ✅ +139  265 💤 +1  0 ❌ ±0 

Results for commit c130400. ± Comparison against base commit 2462373.

@linroid
linroid enabled auto-merge (squash) October 18, 2026 00:11
@linroid
linroid disabled auto-merge October 18, 2026 00:15
…alink-mirrors

# Conflicts:
#	AGENTS.md
#	README.de.md
#	README.es.md
#	README.fr.md
#	README.ja.md
#	README.md
#	README.zh-CN.md
#	app/android/build.gradle.kts
#	app/android/src/main/kotlin/com/linroid/ketch/app/android/KetchService.kt
#	app/desktop-native/build.gradle.kts
#	app/desktop-native/src/main/kotlin/com/linroid/ketch/app/nativeimage/Main.kt
#	app/desktop/build.gradle.kts
#	app/desktop/src/main/kotlin/com/linroid/ketch/app/desktop/main.kt
#	app/shared/build.gradle.kts
#	app/shared/src/iosMain/kotlin/com/linroid/ketch/app/MainViewController.kt
#	cli/build.gradle.kts
#	cli/src/main/kotlin/com/linroid/ketch/cli/Main.kt
#	docs/architecture.md
#	docs/developers.md
#	settings.gradle.kts
@linroid
linroid merged commit 33343cf into main Oct 18, 2026
@linroid
linroid deleted the feat/metalink-mirrors branch October 18, 2026 00:27
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