/rust/registry/src/index.crates.io-1949cf8c6b5b557f/reqwest-0.12.24/src/util.rs
Line | Count | Source |
1 | | use crate::header::{Entry, HeaderMap, HeaderValue, OccupiedEntry}; |
2 | | use std::fmt; |
3 | | |
4 | 0 | pub fn basic_auth<U, P>(username: U, password: Option<P>) -> HeaderValue |
5 | 0 | where |
6 | 0 | U: std::fmt::Display, |
7 | 0 | P: std::fmt::Display, |
8 | | { |
9 | | use base64::prelude::BASE64_STANDARD; |
10 | | use base64::write::EncoderWriter; |
11 | | use std::io::Write; |
12 | | |
13 | 0 | let mut buf = b"Basic ".to_vec(); |
14 | | { |
15 | 0 | let mut encoder = EncoderWriter::new(&mut buf, &BASE64_STANDARD); |
16 | 0 | let _ = write!(encoder, "{username}:"); |
17 | 0 | if let Some(password) = password { |
18 | 0 | let _ = write!(encoder, "{password}"); |
19 | 0 | } |
20 | | } |
21 | 0 | let mut header = HeaderValue::from_maybe_shared(bytes::Bytes::from(buf)) |
22 | 0 | .expect("base64 is always valid HeaderValue"); |
23 | 0 | header.set_sensitive(true); |
24 | 0 | header |
25 | 0 | } Unexecuted instantiation: reqwest::util::basic_auth::<alloc::string::String, alloc::string::String> Unexecuted instantiation: reqwest::util::basic_auth::<&str, &str> |
26 | | |
27 | | #[cfg(not(target_arch = "wasm32"))] |
28 | 0 | pub(crate) fn fast_random() -> u64 { |
29 | | use std::cell::Cell; |
30 | | use std::collections::hash_map::RandomState; |
31 | | use std::hash::{BuildHasher, Hasher}; |
32 | | |
33 | | thread_local! { |
34 | | static KEY: RandomState = RandomState::new(); |
35 | | static COUNTER: Cell<u64> = Cell::new(0); |
36 | | } |
37 | | |
38 | 0 | KEY.with(|key| { |
39 | 0 | COUNTER.with(|ctr| { |
40 | 0 | let n = ctr.get().wrapping_add(1); |
41 | 0 | ctr.set(n); |
42 | | |
43 | 0 | let mut h = key.build_hasher(); |
44 | 0 | h.write_u64(n); |
45 | 0 | h.finish() |
46 | 0 | }) |
47 | 0 | }) |
48 | 0 | } |
49 | | |
50 | 0 | pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) { |
51 | | // IntoIter of HeaderMap yields (Option<HeaderName>, HeaderValue). |
52 | | // The first time a name is yielded, it will be Some(name), and if |
53 | | // there are more values with the same name, the next yield will be |
54 | | // None. |
55 | | |
56 | 0 | let mut prev_entry: Option<OccupiedEntry<_>> = None; |
57 | 0 | for (key, value) in src { |
58 | 0 | match key { |
59 | 0 | Some(key) => match dst.entry(key) { |
60 | 0 | Entry::Occupied(mut e) => { |
61 | 0 | e.insert(value); |
62 | 0 | prev_entry = Some(e); |
63 | 0 | } |
64 | 0 | Entry::Vacant(e) => { |
65 | 0 | let e = e.insert_entry(value); |
66 | 0 | prev_entry = Some(e); |
67 | 0 | } |
68 | | }, |
69 | 0 | None => match prev_entry { |
70 | 0 | Some(ref mut entry) => { |
71 | 0 | entry.append(value); |
72 | 0 | } |
73 | 0 | None => unreachable!("HeaderMap::into_iter yielded None first"), |
74 | | }, |
75 | | } |
76 | | } |
77 | 0 | } |
78 | | |
79 | | #[cfg(feature = "cookies")] |
80 | | #[cfg(not(target_arch = "wasm32"))] |
81 | | pub(crate) fn add_cookie_header( |
82 | | headers: &mut HeaderMap, |
83 | | cookie_store: &dyn crate::cookie::CookieStore, |
84 | | url: &url::Url, |
85 | | ) { |
86 | | if let Some(header) = cookie_store.cookies(url) { |
87 | | headers.insert(crate::header::COOKIE, header); |
88 | | } |
89 | | } |
90 | | |
91 | | pub(crate) struct Escape<'a>(&'a [u8]); |
92 | | |
93 | | #[cfg(not(target_arch = "wasm32"))] |
94 | | impl<'a> Escape<'a> { |
95 | 0 | pub(crate) fn new(bytes: &'a [u8]) -> Self { |
96 | 0 | Escape(bytes) |
97 | 0 | } |
98 | | } |
99 | | |
100 | | impl fmt::Debug for Escape<'_> { |
101 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
102 | 0 | write!(f, "b\"{}\"", self)?; |
103 | 0 | Ok(()) |
104 | 0 | } |
105 | | } |
106 | | |
107 | | impl fmt::Display for Escape<'_> { |
108 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
109 | 0 | for &c in self.0 { |
110 | | // https://doc.rust-lang.org/reference.html#byte-escapes |
111 | 0 | if c == b'\n' { |
112 | 0 | write!(f, "\\n")?; |
113 | 0 | } else if c == b'\r' { |
114 | 0 | write!(f, "\\r")?; |
115 | 0 | } else if c == b'\t' { |
116 | 0 | write!(f, "\\t")?; |
117 | 0 | } else if c == b'\\' || c == b'"' { |
118 | 0 | write!(f, "\\{}", c as char)?; |
119 | 0 | } else if c == b'\0' { |
120 | 0 | write!(f, "\\0")?; |
121 | | // ASCII printable |
122 | 0 | } else if c >= 0x20 && c < 0x7f { |
123 | 0 | write!(f, "{}", c as char)?; |
124 | | } else { |
125 | 0 | write!(f, "\\x{c:02x}")?; |
126 | | } |
127 | | } |
128 | 0 | Ok(()) |
129 | 0 | } |
130 | | } |