Skip to content

Commit

Permalink
Add support the [http] table (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranger-ross authored Aug 19, 2024
1 parent 1e52f45 commit 2cf5221
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fxsr
unparse
USERPROFILE
cainfo
libcurl
69 changes: 68 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ pub struct Config {
#[serde(skip_serializing_if = "FutureIncompatReportConfig::is_none")]
pub future_incompat_report: FutureIncompatReportConfig,
// TODO: cargo-new
// TODO: http
/// The `[http]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#http)
#[serde(default)]
#[serde(skip_serializing_if = "HttpConfig::is_none")]
pub http: HttpConfig,
// TODO: install
/// The `[net]` table.
///
Expand Down Expand Up @@ -438,6 +443,68 @@ pub struct FutureIncompatReportConfig {
pub frequency: Option<Value<Frequency>>,
}

/// The `[http]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#http)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct HttpConfig {
/// If true, enables debugging of HTTP requests.
/// The debug information can be seen by setting the `CARGO_LOG=network=debug` environment variable
/// (or use `network=trace` for even more information).
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpdebug)
#[serde(skip_serializing_if = "Option::is_none")]
pub debug: Option<Value<bool>>,
/// Sets an HTTP and HTTPS proxy to use. The format is in libcurl format as in `[protocol://]host[:port]`.
/// If not set, Cargo will also check the http.proxy setting in your global git configuration.
/// If none of those are set, the HTTPS_PROXY or https_proxy environment variables set the proxy for HTTPS requests,
/// and http_proxy sets it for HTTP requests.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpproxy)
#[serde(skip_serializing_if = "Option::is_none")]
pub proxy: Option<Value<String>>,
/// Sets the timeout for each HTTP request, in seconds.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httptimeout)
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<Value<u32>>,
/// Path to a Certificate Authority (CA) bundle file, used to verify TLS certificates.
/// If not specified, Cargo attempts to use the system certificates.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpcainfo)
#[serde(skip_serializing_if = "Option::is_none")]
pub cainfo: Option<Value<String>>,
/// This determines whether or not TLS certificate revocation checks should be performed.
/// This only works on Windows.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpcheck-revoke)
#[serde(skip_serializing_if = "Option::is_none")]
pub check_revoke: Option<Value<bool>>,
// TODO: handle ssl-version
/// This setting controls timeout behavior for slow connections.
/// If the average transfer speed in bytes per second is below the given value
/// for `http.timeout` seconds (default 30 seconds), then the connection is considered too slow and Cargo will abort and retry.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httplow-speed-limit)
#[serde(skip_serializing_if = "Option::is_none")]
pub low_speed_limit: Option<Value<u32>>,
/// When true, Cargo will attempt to use the HTTP2 protocol with multiplexing.
/// This allows multiple requests to use the same connection, usually improving performance when fetching multiple files.
/// If false, Cargo will use HTTP 1.1 without pipelining.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpmultiplexing)
#[serde(skip_serializing_if = "Option::is_none")]
pub multiplexing: Option<Value<bool>>,
/// Specifies a custom user-agent header to use.
/// The default if not specified is a string that includes Cargo’s version.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpuser-agent)
#[serde(skip_serializing_if = "Option::is_none")]
pub user_agent: Option<Value<String>>,
}

/// The `[net]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#net)
Expand Down
87 changes: 86 additions & 1 deletion src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ pub struct Config {
#[serde(skip_serializing_if = "FutureIncompatReportConfig::is_none")]
pub future_incompat_report: FutureIncompatReportConfig,
// TODO: cargo-new
// TODO: http
/// The `[http]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#http)
#[serde(default)]
#[serde(skip_serializing_if = "HttpConfig::is_none")]
pub http: HttpConfig,

// TODO: install
/// The `[net]` table.
///
Expand Down Expand Up @@ -145,6 +151,7 @@ impl Config {
}
let future_incompat_report =
FutureIncompatReportConfig::from_unresolved(de.future_incompat_report);
let http = HttpConfig::from_unresolved(de.http);
let net = NetConfig::from_unresolved(de.net);
let mut registries = BTreeMap::new();
for (k, v) in de.registries {
Expand All @@ -159,6 +166,7 @@ impl Config {
doc,
env,
future_incompat_report,
http,
net,
registries,
registry,
Expand Down Expand Up @@ -694,6 +702,83 @@ impl FutureIncompatReportConfig {
}
}

/// The `[http]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#http)
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct HttpConfig {
/// If true, enables debugging of HTTP requests.
/// The debug information can be seen by setting the `CARGO_LOG=network=debug` environment variable
/// (or use `network=trace` for even more information).
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpdebug)
#[serde(skip_serializing_if = "Option::is_none")]
pub debug: Option<bool>,
/// Sets an HTTP and HTTPS proxy to use. The format is in libcurl format as in `[protocol://]host[:port]`.
/// If not set, Cargo will also check the http.proxy setting in your global git configuration.
/// If none of those are set, the HTTPS_PROXY or https_proxy environment variables set the proxy for HTTPS requests,
/// and http_proxy sets it for HTTP requests.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpproxy)
#[serde(skip_serializing_if = "Option::is_none")]
pub proxy: Option<String>,
/// Sets the timeout for each HTTP request, in seconds.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httptimeout)
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout: Option<u32>,
/// Path to a Certificate Authority (CA) bundle file, used to verify TLS certificates.
/// If not specified, Cargo attempts to use the system certificates.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpcainfo)
#[serde(skip_serializing_if = "Option::is_none")]
pub cainfo: Option<String>,
/// This determines whether or not TLS certificate revocation checks should be performed.
/// This only works on Windows.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpcheck-revoke)
#[serde(skip_serializing_if = "Option::is_none")]
pub check_revoke: Option<bool>,
// TODO: Add ssl-version
/// This setting controls timeout behavior for slow connections.
/// If the average transfer speed in bytes per second is below the given value
/// for `http.timeout` seconds (default 30 seconds), then the connection is considered too slow and Cargo will abort and retry.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httplow-speed-limit)
#[serde(skip_serializing_if = "Option::is_none")]
pub low_speed_limit: Option<u32>,
/// When true, Cargo will attempt to use the HTTP2 protocol with multiplexing.
/// This allows multiple requests to use the same connection, usually improving performance when fetching multiple files.
/// If false, Cargo will use HTTP 1.1 without pipelining.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpmultiplexing)
#[serde(skip_serializing_if = "Option::is_none")]
pub multiplexing: Option<bool>,
/// Specifies a custom user-agent header to use.
/// The default if not specified is a string that includes Cargo’s version.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#httpuser-agent)
#[serde(skip_serializing_if = "Option::is_none")]
pub user_agent: Option<String>,
}

impl HttpConfig {
fn from_unresolved(de: de::HttpConfig) -> Self {
Self {
debug: de.debug.map(|v| v.val),
proxy: de.proxy.map(|v| v.val),
timeout: de.timeout.map(|v| v.val),
cainfo: de.cainfo.map(|v| v.val),
check_revoke: de.check_revoke.map(|v| v.val),
low_speed_limit: de.low_speed_limit.map(|v| v.val),
multiplexing: de.multiplexing.map(|v| v.val),
user_agent: de.user_agent.map(|v| v.val),
}
}
}

/// The `[net]` table.
///
/// [reference](https://doc.rust-lang.org/nightly/cargo/reference/config.html#net)
Expand Down
10 changes: 10 additions & 0 deletions src/gen/assert_impl.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/gen/de.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/gen/is_none.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions tests/fixtures/reference/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ frequency = 'always' # when to display a notification about a future incompat re
# [cargo-new]
# vcs = "none" # VCS to use ('git', 'hg', 'pijul', 'fossil', 'none')

# [http]
# debug = false # HTTP debugging
# proxy = "host:port" # HTTP proxy in libcurl format
# # ssl-version = "tlsv1.3" # TLS version to use
[http]
debug = false # HTTP debugging
proxy = "host:port" # HTTP proxy in libcurl format
# ssl-version = "tlsv1.3" # TLS version to use
# ssl-version.max = "tlsv1.3" # maximum TLS version
# ssl-version.min = "tlsv1.1" # minimum TLS version
# timeout = 30 # timeout for each HTTP request, in seconds
# low-speed-limit = 10 # network timeout threshold (bytes/sec)
# cainfo = "cert.pem" # path to Certificate Authority (CA) bundle
# check-revoke = true # check for SSL certificate revocation
# multiplexing = true # HTTP/2 multiplexing
# user-agent = "…" # the user-agent header
timeout = 30 # timeout for each HTTP request, in seconds
low-speed-limit = 10 # network timeout threshold (bytes/sec)
cainfo = "cert.pem" # path to Certificate Authority (CA) bundle
check-revoke = true # check for SSL certificate revocation
multiplexing = true # HTTP/2 multiplexing
user-agent = "foo-usr-agt" # the user-agent header

# [install]
# root = "/some/path" # `cargo install` destination directory
Expand Down
9 changes: 8 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ fn assert_reference_example(de: fn(&Path, ResolveOptions) -> Result<Config>) ->
// TODO
// [cargo-new]

// TODO
// [http]
assert_eq!(config.http.debug, Some(false));
assert_eq!(config.http.proxy.as_deref(), Some("host:port"));
assert_eq!(config.http.timeout, Some(30));
assert_eq!(config.http.low_speed_limit, Some(10));
assert_eq!(config.http.cainfo.as_deref(), Some("cert.pem"));
assert_eq!(config.http.check_revoke, Some(true));
assert_eq!(config.http.multiplexing, Some(true));
assert_eq!(config.http.user_agent.as_deref(), Some("foo-usr-agt"));

// TODO
// [install]
Expand Down

0 comments on commit 2cf5221

Please sign in to comment.