From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 67CA563A31 for ; Wed, 26 Jan 2022 19:11:04 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 5DBA71B04D for ; Wed, 26 Jan 2022 19:11:04 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 7CCCF1B036 for ; Wed, 26 Jan 2022 19:11:02 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4D7AD469A9 for ; Wed, 26 Jan 2022 19:11:02 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Date: Wed, 26 Jan 2022 19:10:55 +0100 Message-Id: <20220126181055.968385-2-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220126181055.968385-1-t.lamprecht@proxmox.com> References: <20220126181055.968385-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.093 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [main.rs] Subject: [pve-devel] [PATCH 2/2] switch from curl to ureq X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Jan 2022 18:11:04 -0000 Not only less code, we lose a whopping 29 libraries on linkage cruft: --- ldd.before 2022-01-26 19:03:25.759426164 +0100 +++ ldd.after 2022-01-26 19:03:31.919529632 +0100 @@ -5,36 +5,7 @@ librt.so.1 libzstd.so.1 libc.so.6 /lib64/ld-linux-x86-64.so.2 -libcurl-gnutls.so.4 libgcc_s.so.1 libpthread.so.0 libm.so.6 libdl.so.2 -libnghttp2.so.14 -libidn2.so.0 -librtmp.so.1 -libssh2.so.1 -libpsl.so.5 -libnettle.so.8 -libgnutls.so.30 -libgssapi_krb5.so.2 -libldap_r-2.4.so.2 -liblber-2.4.so.2 -libbrotlidec.so.1 -libz.so.1 -libunistring.so.2 -libhogweed.so.6 -libgmp.so.10 -libgcrypt.so.20 -libp11-kit.so.0 -libtasn1.so.6 -libkrb5.so.3 -libk5crypto.so.3 -libcom_err.so.2 -libkrb5support.so.0 -libresolv.so.2 -libsasl2.so.2 -libbrotlicommon.so.1 -libgpg-error.so.0 -libffi.so.7 -libkeyutils.so.1 IOW.: curl is really nice for a CLI tool, but way to much overkill for simple HTTP requests. Signed-off-by: Thomas Lamprecht --- IMO a no brainer, but I only did a quick "does it still work in general" testing, I mean that *is* covering already quite a bit here ;-) Cargo.toml | 2 +- src/main.rs | 41 ++++++++++++++--------------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 25c4569..7f55edc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ exclude = [ "build", "debian" ] [dependencies] anyhow = "1" mio = { version = "0.7", features = [ "net", "os-ext" ] } -curl = "0.4" +ureq = { version = "2.4", default-features = false, features = [ "gzip", ] } clap = "2.33" proxmox-io = "1" proxmox-sys = "0.2" diff --git a/src/main.rs b/src/main.rs index 0003834..29508e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ use std::time::{Duration, Instant}; use anyhow::{bail, format_err, Result}; use clap::{App, AppSettings, Arg}; -use curl::easy::Easy; use mio::net::{TcpListener, TcpStream}; use mio::unix::SourceFd; use mio::{Events, Interest, Poll, Token}; @@ -152,38 +151,26 @@ fn authenticate( authport: u16, port: Option, ) -> Result<()> { - let mut curl = Easy::new(); - curl.url(&format!( - "http://localhost:{}/api2/json/access/ticket", - authport - ))?; - - let username = curl.url_encode(username); - let ticket = curl.url_encode(ticket); - let path = curl.url_encode(path.as_bytes()); - - let mut post_fields = Vec::with_capacity(5); - post_fields.push(format!("username={}", username)); - post_fields.push(format!("password={}", ticket)); - post_fields.push(format!("path={}", path)); - + let mut post_fields: Vec<(&str, &str)> = Vec::with_capacity(5); + post_fields.append(& mut vec![ + ("username", std::str::from_utf8(username)?), + ("password", std::str::from_utf8(ticket)?), + ("path", path), + ]); if let Some(perm) = perm { - let perm = curl.url_encode(perm.as_bytes()); - post_fields.push(format!("privs={}", perm)); + post_fields.push(("privs", perm)); } - + let port_str; if let Some(port) = port { - post_fields.push(format!("port={}", port)); + port_str = port.to_string(); + post_fields.push(("port", &port_str)); } - curl.post_fields_copy(post_fields.join("&").as_bytes())?; - curl.post(true)?; - curl.perform()?; + let url = format!("http://localhost:{}/api2/json/access/ticket", authport); + let res = ureq::post(&url).send_form(&post_fields[..])?; - let response_code = curl.response_code()?; - - if response_code != 200 { - bail!("invalid authentication, code {}", response_code); + if res.status() != 200 { + bail!("invalid authentication, {} - {}", res.status(), res.status_text()); } Ok(()) -- 2.34.1