summaryrefslogtreecommitdiffstats
path: root/vendor/reqwest/src/util.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /vendor/reqwest/src/util.rs
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/reqwest/src/util.rs')
-rw-r--r--vendor/reqwest/src/util.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/vendor/reqwest/src/util.rs b/vendor/reqwest/src/util.rs
new file mode 100644
index 000000000..018db307f
--- /dev/null
+++ b/vendor/reqwest/src/util.rs
@@ -0,0 +1,89 @@
+use crate::header::{Entry, HeaderMap, HeaderValue, OccupiedEntry};
+
+pub fn basic_auth<U, P>(username: U, password: Option<P>) -> HeaderValue
+where
+ U: std::fmt::Display,
+ P: std::fmt::Display,
+{
+ use base64::prelude::BASE64_STANDARD;
+ use base64::write::EncoderWriter;
+ use std::io::Write;
+
+ let mut buf = b"Basic ".to_vec();
+ {
+ let mut encoder = EncoderWriter::new(&mut buf, &BASE64_STANDARD);
+ let _ = write!(encoder, "{}:", username);
+ if let Some(password) = password {
+ let _ = write!(encoder, "{}", password);
+ }
+ }
+ let mut header = HeaderValue::from_bytes(&buf).expect("base64 is always valid HeaderValue");
+ header.set_sensitive(true);
+ header
+}
+
+// xor-shift
+#[cfg(not(target_arch = "wasm32"))]
+pub(crate) fn fast_random() -> u64 {
+ use std::cell::Cell;
+ use std::collections::hash_map::RandomState;
+ use std::hash::{BuildHasher, Hasher};
+ use std::num::Wrapping;
+
+ thread_local! {
+ static RNG: Cell<Wrapping<u64>> = Cell::new(Wrapping(seed()));
+ }
+
+ fn seed() -> u64 {
+ let seed = RandomState::new();
+
+ let mut out = 0;
+ let mut cnt = 0;
+ while out == 0 {
+ cnt += 1;
+ let mut hasher = seed.build_hasher();
+ hasher.write_usize(cnt);
+ out = hasher.finish();
+ }
+ out
+ }
+
+ RNG.with(|rng| {
+ let mut n = rng.get();
+ debug_assert_ne!(n.0, 0);
+ n ^= n >> 12;
+ n ^= n << 25;
+ n ^= n >> 27;
+ rng.set(n);
+ n.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
+ })
+}
+
+pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
+ // IntoIter of HeaderMap yields (Option<HeaderName>, HeaderValue).
+ // The first time a name is yielded, it will be Some(name), and if
+ // there are more values with the same name, the next yield will be
+ // None.
+
+ let mut prev_entry: Option<OccupiedEntry<_>> = None;
+ for (key, value) in src {
+ match key {
+ Some(key) => match dst.entry(key) {
+ Entry::Occupied(mut e) => {
+ e.insert(value);
+ prev_entry = Some(e);
+ }
+ Entry::Vacant(e) => {
+ let e = e.insert_entry(value);
+ prev_entry = Some(e);
+ }
+ },
+ None => match prev_entry {
+ Some(ref mut entry) => {
+ entry.append(value);
+ }
+ None => unreachable!("HeaderMap::into_iter yielded None first"),
+ },
+ }
+ }
+}