Remote downloads and bencode
Remote downloads and bencode support turn a link into torrent metadata that the client can send to a Transmission daemon. The flow accepts HTTP(S) torrent links and magnet links, keeps local files as a separate input, and uses bencode data to identify torrent hashes and trackers. This page is a child of Networking and data services; see Torrent intake for the wider add-torrent workflow and IP resolution and geolocation for the separate peer metadata path.
Intake boundary
The intake boundary is the point where user or operating-system input becomes one of three supported torrent sources: a local file, an HTTP(S) URL, or a magnet URI. It accepts only the schemes that the add-torrent flow can process, while a local path continues through the file path instead of the network path.
Supported source forms
The client recognizes http:, https:, and magnet: as remote or link-based inputs. A bare 32-character hash can also become a magnet URI, which gives the user a short way to request content by its BitTorrent info hash.
| Source | Meaning in the intake flow | Next stage |
|---|---|---|
| Local path | Existing torrent data on the client machine | Read and encode the file for Transmission |
| HTTP or HTTPS URL | Torrent metadata that must be fetched first | Download to a temporary file, then decode |
| Magnet URI | A content reference with a hash and optional tracker values | Send the URI to Transmission and use its metadata lookup |
| Bare torrent hash | A shorthand link value | Normalize to a magnet URI |
References: main.pas• L1037-1051 main.pas• L1174-1189 main.pas• L2422-2436 main.pas• L2738-2757
Link entry
The link dialog provides a small validation boundary before the main add flow starts. It trims the value and rejects an empty entry. Other checks then decide whether the value is a supported URI or a valid local file, so link entry stays focused on collecting a usable value rather than interpreting torrent metadata.
On macOS, the operating system can deliver a registered URL event to the application. The application passes that URL into the same torrent-file intake entry point, which keeps external links aligned with links entered in the client.
References: addlink.pas• L42-83 main.pas• L1230-1255 main.pas• L1575-1609 urllistenerosx.pas• L40-75
HTTP acquisition
The HTTP acquisition service retrieves remote content before the add flow reads it. It performs a GET request, writes the response to a UTF-8-aware destination file, reports progress to the user, and removes an incomplete file when the request fails or is canceled.
Download lifecycle
The download runs outside the main UI work so the progress window can show byte counts while the network request is active. A successful response requires HTTP status 200; network errors, other response codes, exceptions, and cancellation return failure to the caller.
The following flow shows how a supported torrent URL becomes a temporary local input.
flowchart LR
A[Supported HTTP or HTTPS URL] --> B[Start download]
B --> C[GET response]
C --> D{Status 200?}
D -->|Yes| E[Write temporary torrent file]
E --> F[Decode bencode]
D -->|No| G[Report error and remove partial file]
C -->|Network error or cancel| G
References: download.pas• L77-91 download.pas• L97-123 download.pas• L127-189 main.pas• L2504-2513
Network path
The downloader uses the HTTP service shared by the client’s network layer. HTTPS requires the OpenSSL libraries to be available. If the active connection profile defines an HTTP proxy, the downloader uses that proxy and its credentials for the request.
This makes remote torrent acquisition follow the same trust and routing assumptions as the daemon connection, while the downloaded torrent itself remains a local temporary artifact until the add operation finishes.
References: download.pas• L38-39 download.pas• L101-105 download.pas• L151-181 rpc.pas• L188-220 main.pas• L5364-5386 synapse/…/httpsend.pas• L127-138 synapse/…/httpsend.pas• L384-431
Progress and cleanup
The service streams received blocks into the destination file and updates the progress display from the response size when the server provides one. A successful call returns after the file is complete. A failed call deletes the destination so later intake cannot mistake partial data for a torrent file.
References: download.pas• L127-149 download.pas• L166-188 download.pas• L231-255
URI interpretation
URI interpretation separates a link’s transport from the torrent information carried by the link. HTTP(S) values identify content to fetch, while magnet values identify content directly and can carry tracker hints in query fields.
HTTP and HTTPS URLs
The HTTP library splits a URL into its scheme, credentials, host, port, path, and query data before it opens the connection. It uses port 80 for HTTP and port 443 for HTTPS when the URL does not supply another port. URL-escaped user information is decoded before authentication is applied.
References: synapse/…/synautil.pas• L1303-1374 synapse/…/httpsend.pas• L384-430
Magnet values
The add flow reads magnet query fields to obtain the BitTorrent info hash from xt=urn:btih: and to collect tracker values from tr. Tracker values are URL-decoded before they are used. A 32-character hash is treated as base32 and converted to the hexadecimal form expected by the later torrent lookup; a hexadecimal hash is kept as supplied after validation.
Magnet links do not provide a torrent file for the client to inspect. Transmission receives the magnet URI, resolves its metadata, and the add dialog can then show the metadata state that is available from the daemon.
References: main.pas• L1037-1051 main.pas• L2476-2503 main.pas• L2738-2757 main.pas• L2823-2863 utils.pas• L646-690 synapse/…/synacode.pas• L478-481
Bencode data model
Bencode is the compact data format used by torrent metadata. The decoder turns a stream into a tree of typed values so the client can inspect fields such as info, announce, and announce-list without treating the torrent as plain text.
Value types
The data model represents strings, integers, lists, and dictionaries. Dictionary entries keep a string key beside their child value, while list entries keep the child value without a key. The model owns its child values, so a parsed torrent can be released as one data tree.
References: bencode.pas• L38-80 bencode.pas• L84-104 bencode.pas• L269-274
Stream decoding
The decoder reads one value at a time from a stream. It uses the value’s leading marker to distinguish an integer, list, dictionary, or length-prefixed string. Missing markers, malformed lengths, invalid characters, and truncated input raise an invalid-torrent error instead of producing incomplete metadata.
Dictionary lookup is case-insensitive. This lets the intake flow ask for known torrent fields while keeping the parsed structure general enough for other bencoded data.
References: bencode.pas• L106-234 bencode.pas• L276-292
Re-encoding
The data model can encode a parsed value back into bencode. The add flow uses this capability on the info dictionary, then hashes the exact encoded form to derive the torrent’s identity when it needs to match the torrent against Transmission data.
References: bencode.pas• L236-267 main.pas• L2519-2526
Torrent metadata handoff
Torrent metadata handoff connects acquisition and parsing to the Transmission RPC add operation. A local or downloaded torrent file becomes base64 metainfo for the daemon, while a remote link remains a filename-like URI that Transmission can resolve.
File metadata path
When the input is a local or downloaded torrent file, the client reads the bytes, encodes them for the RPC request, and asks Transmission to add the torrent. Before and around this handoff, it can inspect the bencoded data to calculate the info hash and collect tracker URLs.
The add dialog then uses the daemon’s response to present the available files and let the user choose the destination, file selection, peer limit, name, and start state. These choices belong to the broader workflow described in Torrent intake.
References: main.pas• L2514-2558 main.pas• L2628-2662 main.pas• L2728-2757 main.pas• L2802-2859
Tracker metadata path
Tracker metadata can come from the multi-level announce-list field or from the single announce field. The client prefers valid entries from the list and falls back to the single tracker when the list is absent or has an unusable shape. For a duplicate torrent, it can use the discovered trackers to offer an update to the existing daemon record.
References: main.pas• L2444-2474 main.pas• L2519-2549 main.pas• L2560-2621 main.pas• L2641-2655
Shared download uses
The same HTTP download service also obtains optional support data used by the desktop client, such as the GeoIP database and flag archive. Those downloads are separate from torrent intake, but they share the same success, error, proxy, and HTTPS behavior.
References: main.pas• L3180-3204 main.pas• L3955-3980 download.pas• L97-123 download.pas• L151-188
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the supported URI schemes in
IsProtocolSupportedaligned with every path that decides between a local file, remote URL, and magnet input. - Keep magnet
xthash normalization aligned with the hash form used for Transmission torrent lookup and duplicate detection. - Preserve the temporary-file cleanup boundary when changing remote download success or cancellation behavior.
- Keep tracker extraction compatible with both
announce-listtiers and the singleannouncefallback. - Keep bencode field names and the
infore-encoding rule aligned with torrent identity and tracker-update behavior. - Keep HTTP proxy and HTTPS library checks aligned between the daemon connection profile and auxiliary downloads.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| HTTPS is selected but the required OpenSSL libraries are not available | The remote torrent is not downloaded | Check OpenSSL availability before starting an HTTPS download |
The HTTP request fails or returns a status other than 200 | Intake stops and the temporary destination is removed | Surface the network or HTTP error and treat the download as failed |
| A downloaded file is not valid bencode | Torrent parsing stops with an invalid-torrent error | Validate the stream through the bencode decoder before using metadata |
| A magnet hash has an invalid length or character set | The client cannot identify the requested torrent | Normalize base32 hashes and validate the resulting hash form |
announce-list has an invalid shape and no usable announce value exists | No tracker is added during duplicate handling | Fall back only to a valid single tracker and allow an empty result |
Key files
| File | Why to read it |
|---|---|
main.pas | Connects URI validation, magnet handling, remote-file acquisition, bencode inspection, and the Transmission add request. |
download.pas | Defines the user-visible HTTP download lifecycle and its temporary-file contract. |
bencode.pas | Defines the general bencode value tree, stream decoder, dictionary lookup, and encoder. |
addlink.pas | Shows the narrow user-input boundary for manually entered links. |
urllistenerosx.pas | Shows how macOS URL events enter the common torrent intake path. |
synapse/source/lib/httpsend.pas | Describes the HTTP request contract, HTTPS behavior, proxy routing, status codes, and progress data used by the client. |
synapse/source/lib/synautil.pas | Defines the URL parts used by HTTP requests. |
utils.pas | Provides base32 decoding and UTF-8-aware file support used by link and download handling. |