Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
model_download.hpp
Go to the documentation of this file.
1#pragma once
2
3// Streaming HTTPS file downloader for the model manager. Unlike
4// runtime/http.hpp's http_download (whole-body, in-memory — fine for small JSON,
5// fatal for GB-scale weights), this streams the body incrementally to disk with a
6// progress callback, HTTP Range **resume**, request **headers** (e.g. HuggingFace
7// auth), **cancellation**, **sha256** verification, and a temp-stage → atomic-rename
8// so a partial/aborted transfer never appears as a finished file. HTTPS rides on
9// cpp-httplib's mbedTLS backend (mbedTLS is already linked by core/runtime).
10
11#include <cstdint>
12#include <filesystem>
13#include <functional>
14#include <string>
15#include <vector>
16
17namespace pulp::runtime {
18
19class CancelToken; // cancel.hpp
20
22 std::uint64_t downloaded = 0;
23 std::uint64_t total = 0;
24};
25
26struct HttpHeader {
27 std::string name;
28 std::string value;
29};
30
32 std::string url;
33 std::filesystem::path dest;
34 std::string expected_sha256;
35 std::vector<HttpHeader> headers;
36 bool resume = true;
41 bool allow_unverified = false;
42};
43
52inline int download_connect_timeout_seconds(int request_timeout_seconds) {
53 constexpr int kConnectTimeoutSeconds = 15;
54 return request_timeout_seconds > 0
55 ? (request_timeout_seconds < kConnectTimeoutSeconds
56 ? request_timeout_seconds
57 : kConnectTimeoutSeconds)
58 : kConnectTimeoutSeconds;
59}
60
62 bool ok = false;
63 std::string error;
64 std::filesystem::path path;
65 std::uint64_t bytes = 0;
66 std::string sha256;
67 bool resumed = false;
68 bool cancelled = false;
72 bool unverified = false;
73};
74
77 std::string scheme_host_port;
78 std::string path;
79 bool ok = false;
80 std::string error;
81};
82
87ResolvedRedirect resolve_redirect(const std::string& current_scheme_host_port,
88 const std::string& current_path, const std::string& location);
89
91using DownloadProgressFn = std::function<bool(const DownloadProgress&)>;
92
99 const DownloadProgressFn& on_progress = {},
100 const CancelToken* cancel = nullptr);
101
102} // namespace pulp::runtime
Definition cancel.hpp:77
Definition generated_event_source.hpp:33
int download_connect_timeout_seconds(int request_timeout_seconds)
Connect-phase timeout (seconds) the downloader applies for a given request timeout_seconds.
Definition model_download.hpp:52
ResolvedRedirect resolve_redirect(const std::string &current_scheme_host_port, const std::string &current_path, const std::string &location)
Resolve an absolute, host-absolute, or path-relative redirect location against the current origin + p...
std::function< bool(const DownloadProgress &)> DownloadProgressFn
Progress callback. Return false to cancel the in-flight download.
Definition model_download.hpp:91
DownloadResult download_file(const DownloadRequest &request, const DownloadProgressFn &on_progress={}, const CancelToken *cancel=nullptr)
Blocking streaming download.
Definition model_download.hpp:21
std::uint64_t total
Total expected bytes, or 0 if the server didn't say.
Definition model_download.hpp:23
std::uint64_t downloaded
Bytes written so far (includes resumed bytes).
Definition model_download.hpp:22
Definition model_download.hpp:31
std::vector< HttpHeader > headers
Extra request headers (e.g. Authorization).
Definition model_download.hpp:35
int timeout_seconds
0 ⇒ a sensible library default.
Definition model_download.hpp:37
std::filesystem::path dest
Final destination path.
Definition model_download.hpp:33
std::string expected_sha256
Hex digest the finished file must match.
Definition model_download.hpp:34
bool resume
Resume from a prior partial <dest>.part via Range.
Definition model_download.hpp:36
bool allow_unverified
An empty expected_sha256 means nothing can vouch for the bytes, so the download is refused (DownloadR...
Definition model_download.hpp:41
std::string url
http(s)://… (resolve hf:// first).
Definition model_download.hpp:32
Definition model_download.hpp:61
std::uint64_t bytes
Total bytes of the finished file.
Definition model_download.hpp:65
std::string error
Definition model_download.hpp:63
bool unverified
The request carried no expected digest.
Definition model_download.hpp:72
bool ok
Definition model_download.hpp:62
bool cancelled
Stopped via callback / token (partial kept for resume).
Definition model_download.hpp:68
bool resumed
A prior partial was continued.
Definition model_download.hpp:67
std::string sha256
Hex digest of the finished file.
Definition model_download.hpp:66
std::filesystem::path path
The finished file (== request.dest) on success.
Definition model_download.hpp:64
Definition model_download.hpp:26
std::string value
Definition model_download.hpp:28
std::string name
Definition model_download.hpp:27
A redirect Location resolved against the request it answered.
Definition model_download.hpp:76
std::string scheme_host_port
e.g. "https://cdn.example.com:8443"
Definition model_download.hpp:77
std::string path
Absolute path on that origin.
Definition model_download.hpp:78
std::string error
Definition model_download.hpp:80
bool ok
False when unresolvable or refused; see error.
Definition model_download.hpp:79