http-client: add verbose option to requests

Summary: Add a new `http.verbose` config option that turns on verbose output for libcurl (similar to the output printed by `curl -v`). This can be very useful for debugging HTTP issues.

Reviewed By: DurhamG

Differential Revision: D27693304

fbshipit-source-id: 2ad7a08889f40ffbcd2f14ac9c21d70433629da4
This commit is contained in:
Arun Kulshreshtha 2021-04-12 12:16:20 -07:00 committed by Facebook GitHub Bot
parent 11697cde4c
commit f738f65d28
3 changed files with 27 additions and 5 deletions

View File

@ -49,6 +49,7 @@ pub fn http_client(client_id: impl ToString) -> HttpClient {
/// Global configuration settings for Mercurial's HTTP client.
#[derive(Debug)]
pub struct HgHttpConfig {
pub verbose: bool,
pub disable_tls_verification: bool,
}
@ -60,10 +61,9 @@ pub fn set_global_config(config: HgHttpConfig) {
}
Request::on_new_request(move |req| {
// Disable TLS verification if --insecure is specified.
if config.disable_tls_verification {
req.set_verify_tls_cert(false).set_verify_tls_host(false);
}
req.set_verify_tls_cert(!config.disable_tls_verification)
.set_verify_tls_host(!config.disable_tls_verification)
.set_verbose(config.verbose);
});
}

View File

@ -522,8 +522,9 @@ mod exitcode {
pub const IOERR: i32 = 74;
}
fn setup_http(_config: &ConfigSet, global_opts: &HgGlobalOpts) {
fn setup_http(config: &ConfigSet, global_opts: &HgGlobalOpts) {
let http_config = HgHttpConfig {
verbose: config.get_or_default("http", "verbose").unwrap_or_default(),
disable_tls_verification: global_opts.insecure,
};
hg_http::set_global_config(http_config);

View File

@ -90,6 +90,7 @@ pub struct Request {
min_transfer_speed: Option<MinTransferSpeed>,
verify_tls_host: bool,
verify_tls_cert: bool,
verbose: bool,
}
static REQUEST_CREATION_LISTENERS: Lazy<RwLock<RequestCreationEventListeners>> =
@ -162,6 +163,7 @@ impl Request {
min_transfer_speed: None,
verify_tls_host: true,
verify_tls_cert: true,
verbose: false,
}
}
@ -397,6 +399,24 @@ impl Request {
self
}
/// Turn on libcurl's verbose output. This will cause libcurl to print lots
/// of verbose debug messages to stderr. This can be useful when trying to
/// understand exactly what libcurl is doing under the hood, which can help
/// to debug low-level protocol issues.
pub fn verbose(mut self, verbose: bool) -> Self {
self.set_verbose(verbose);
self
}
/// Turn on libcurl's verbose output. This will cause libcurl to print lots
/// of verbose debug messages to stderr. This can be useful when trying to
/// understand exactly what libcurl is doing under the hood, which can help
/// to debug low-level protocol issues.
pub fn set_verbose(&mut self, verbose: bool) -> &mut Self {
self.verbose = verbose;
self
}
/// Execute the request, blocking until completion.
///
/// This method is intended as a simple way to perform
@ -459,6 +479,7 @@ impl Request {
let mut easy = Easy2::new(handler);
easy.url(url.as_str())?;
easy.verbose(self.verbose)?;
// Configure the handle for the desired HTTP method.
match easy.get_ref().request_context().method {