Skip to content

Commit 2c48599

Browse files
committed
docs: doc comments for http registry
1 parent eb6c745 commit 2c48599

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/cargo/sources/registry/http_remote.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
//! Access to a HTTP-based crate registry.
2-
//!
3-
//! See [`HttpRegistry`] for details.
1+
//! Access to a HTTP-based crate registry. See [`HttpRegistry`] for details.
42
53
use crate::core::{PackageId, SourceId};
64
use crate::ops::{self};
@@ -52,8 +50,15 @@ const UNKNOWN: &'static str = "Unknown";
5250
///
5351
/// [RFC 2789]: https://github.com/rust-lang/rfcs/pull/2789
5452
pub struct HttpRegistry<'cfg> {
53+
/// Path to the registry index (`$CARGO_HOME/registry/index/$REG-HASH`).
54+
///
55+
/// To be fair, `HttpRegistry` doesn't store the registry index it
56+
/// downloads on the file system, but other cached data like registry
57+
/// configuration could be stored here.
5558
index_path: Filesystem,
59+
/// Path to the cache of `.crate` files (`$CARGO_HOME/registry/cache/$REG-HASH`).
5660
cache_path: Filesystem,
61+
/// The unique identifier of this registry source.
5762
source_id: SourceId,
5863
config: &'cfg Config,
5964

@@ -95,20 +100,20 @@ pub struct HttpRegistry<'cfg> {
95100
quiet: bool,
96101
}
97102

98-
/// Helper for downloading crates.
103+
/// State for currently pending index file downloads.
99104
struct Downloads<'cfg> {
100105
/// When a download is started, it is added to this map. The key is a
101-
/// "token" (see `Download::token`). It is removed once the download is
106+
/// "token" (see [`Download::token`]). It is removed once the download is
102107
/// finished.
103108
pending: HashMap<usize, (Download<'cfg>, EasyHandle)>,
104109
/// Set of paths currently being downloaded.
105-
/// This should stay in sync with `pending`.
110+
/// This should stay in sync with the `pending` field.
106111
pending_paths: HashSet<PathBuf>,
107112
/// Downloads that have failed and are waiting to retry again later.
108113
sleeping: SleepTracker<(Download<'cfg>, Easy)>,
109114
/// The final result of each download.
110115
results: HashMap<PathBuf, CargoResult<CompletedDownload>>,
111-
/// The next ID to use for creating a token (see `Download::token`).
116+
/// The next ID to use for creating a token (see [`Download::token`]).
112117
next: usize,
113118
/// Progress bar.
114119
progress: RefCell<Option<Progress<'cfg>>>,
@@ -119,9 +124,10 @@ struct Downloads<'cfg> {
119124
blocking_calls: usize,
120125
}
121126

127+
/// Represents a single index file download, including its progress and retry.
122128
struct Download<'cfg> {
123-
/// The token for this download, used as the key of the `Downloads::pending` map
124-
/// and stored in `EasyHandle` as well.
129+
/// The token for this download, used as the key of the
130+
/// [`Downloads::pending`] map and stored in [`EasyHandle`] as well.
125131
token: usize,
126132

127133
/// The path of the package that we're downloading.
@@ -137,28 +143,39 @@ struct Download<'cfg> {
137143
retry: Retry<'cfg>,
138144
}
139145

146+
/// HTTPS headers [`HttpRegistry`] cares about.
140147
#[derive(Default)]
141148
struct Headers {
142149
last_modified: Option<String>,
143150
etag: Option<String>,
144151
www_authenticate: Vec<String>,
152+
/// We don't care about these headers. Put them here for debugging purpose.
145153
others: Vec<String>,
146154
}
147155

156+
/// HTTP status code [`HttpRegistry`] cares about.
148157
enum StatusCode {
149158
Success,
150159
NotModified,
151160
NotFound,
152161
Unauthorized,
153162
}
154163

164+
/// Represents a complete [`Download`] from an HTTP request.
165+
///
166+
/// Usually it is constructed in [`HttpRegistry::handle_completed_downloads`],
167+
/// and then returns to the caller of [`HttpRegistry::load()`].
155168
struct CompletedDownload {
156169
response_code: StatusCode,
157170
data: Vec<u8>,
158171
header_map: Headers,
159172
}
160173

161174
impl<'cfg> HttpRegistry<'cfg> {
175+
/// Creates a HTTP-rebased remote registry for `source_id`.
176+
///
177+
/// * `name` --- Name of a path segment where `.crate` tarballs and the
178+
/// registry index are stored. Expect to be unique.
162179
pub fn new(
163180
source_id: SourceId,
164181
config: &'cfg Config,
@@ -208,6 +225,7 @@ impl<'cfg> HttpRegistry<'cfg> {
208225
})
209226
}
210227

228+
/// Splits HTTP `HEADER: VALUE` to a tuple.
211229
fn handle_http_header(buf: &[u8]) -> Option<(&str, &str)> {
212230
if buf.is_empty() {
213231
return None;
@@ -222,6 +240,9 @@ impl<'cfg> HttpRegistry<'cfg> {
222240
Some((tag, value))
223241
}
224242

243+
/// Setup the necessary works before the first fetch gets started.
244+
///
245+
/// This is a no-op if called more than one time.
225246
fn start_fetch(&mut self) -> CargoResult<()> {
226247
if self.fetch_started {
227248
// We only need to run the setup code once.
@@ -249,6 +270,8 @@ impl<'cfg> HttpRegistry<'cfg> {
249270
Ok(())
250271
}
251272

273+
/// Checks the results inside the [`HttpRegistry::multi`] handle, and
274+
/// updates relevant state in [`HttpRegistry::downloads`] accordingly.
252275
fn handle_completed_downloads(&mut self) -> CargoResult<()> {
253276
assert_eq!(
254277
self.downloads.pending.len(),
@@ -322,11 +345,15 @@ impl<'cfg> HttpRegistry<'cfg> {
322345
Ok(())
323346
}
324347

348+
/// Constructs the full URL to download a index file.
325349
fn full_url(&self, path: &Path) -> String {
326350
// self.url always ends with a slash.
327351
format!("{}{}", self.url, path.display())
328352
}
329353

354+
/// Check if an index file of `path` is up-to-date.
355+
///
356+
/// The `path` argument is the same as in [`RegistryData::load`].
330357
fn is_fresh(&self, path: &Path) -> bool {
331358
if !self.requested_update {
332359
trace!(
@@ -373,7 +400,7 @@ impl<'cfg> HttpRegistry<'cfg> {
373400
Ok(self.registry_config.as_ref())
374401
}
375402

376-
/// Get the registry configuration.
403+
/// Get the registry configuration from either cache or remote.
377404
fn config(&mut self) -> Poll<CargoResult<&RegistryConfig>> {
378405
debug!("loading config");
379406
let index_path = self.assert_index_locked(&self.index_path);
@@ -405,6 +432,7 @@ impl<'cfg> HttpRegistry<'cfg> {
405432
}
406433
}
407434

435+
/// Moves failed [`Download`]s that are ready to retry to the pending queue.
408436
fn add_sleepers(&mut self) -> CargoResult<()> {
409437
for (dl, handle) in self.downloads.sleeping.to_retry() {
410438
let mut handle = self.multi.add(handle)?;
@@ -790,6 +818,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
790818
}
791819

792820
impl<'cfg> Downloads<'cfg> {
821+
/// Updates the state of the progress bar for downloads.
793822
fn tick(&self) -> CargoResult<()> {
794823
let mut progress = self.progress.borrow_mut();
795824
let Some(progress) = progress.as_mut() else { return Ok(()); };

0 commit comments

Comments
 (0)