Skip to content

Commit 2932c03

Browse files
committed
Update base64 to 0.21
Also added once_cell for caching the new Engine based APIs of base64 Signed-off-by: Ayush Singh <[email protected]>
1 parent aad2004 commit 2932c03

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ members = [
1919
[dependencies]
2020
http = "0.2.0"
2121
headers-core = { version = "0.2", path = "./headers-core" }
22-
base64 = "0.13"
22+
base64 = "0.21"
2323
bitflags = "1.0"
2424
bytes = "1"
2525
mime = "0.3.14"
2626
sha1 = "0.10"
2727
httpdate = "1"
28+
once_cell = "1.17.0"
2829

2930
[features]
3031
nightly = []

src/common/authorization.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Authorization header and types.
22
3-
use base64;
3+
use super::engine;
4+
use base64::Engine;
45
use bytes::Bytes;
56

67
use util::HeaderValueString;
@@ -158,7 +159,8 @@ impl Credentials for Basic {
158159
let bytes = &value.as_bytes()["Basic ".len()..];
159160
let non_space_pos = bytes.iter().position(|b| *b != b' ')?;
160161
let bytes = &bytes[non_space_pos..];
161-
let bytes = base64::decode(bytes).ok()?;
162+
163+
let bytes = engine().decode(bytes).ok()?;
162164

163165
let decoded = String::from_utf8(bytes).ok()?;
164166

@@ -169,7 +171,7 @@ impl Credentials for Basic {
169171

170172
fn encode(&self) -> HeaderValue {
171173
let mut encoded = String::from("Basic ");
172-
base64::encode_config_buf(&self.decoded, base64::STANDARD, &mut encoded);
174+
engine().encode_string(&self.decoded, &mut encoded);
173175

174176
let bytes = Bytes::from(encoded);
175177
HeaderValue::from_maybe_shared(bytes)

src/common/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,10 @@ mod upgrade;
188188
mod user_agent;
189189
mod vary;
190190
//mod warning;
191+
192+
fn engine() -> &'static base64::engine::GeneralPurpose {
193+
static ENGINE: once_cell::sync::OnceCell<base64::engine::GeneralPurpose> =
194+
once_cell::sync::OnceCell::new();
195+
196+
ENGINE.get_or_init(|| base64::engine::general_purpose::STANDARD)
197+
}

src/common/sec_websocket_accept.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use base64;
1+
use base64::Engine;
22
use bytes::Bytes;
33
use sha1::{Digest, Sha1};
44

5-
use super::SecWebsocketKey;
5+
use super::{engine, SecWebsocketKey};
66

77
/// The `Sec-Websocket-Accept` header.
88
///
@@ -39,7 +39,7 @@ fn sign(key: &[u8]) -> SecWebsocketAccept {
3939
let mut sha1 = Sha1::default();
4040
sha1.update(key);
4141
sha1.update(&b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"[..]);
42-
let b64 = Bytes::from(base64::encode(&sha1.finalize()));
42+
let b64 = Bytes::from(engine().encode(&sha1.finalize()));
4343

4444
let val = ::HeaderValue::from_maybe_shared(b64).expect("base64 is a valid value");
4545

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern crate headers_core;
8080
extern crate http;
8181
extern crate httpdate;
8282
extern crate mime;
83+
extern crate once_cell;
8384
extern crate sha1;
8485
#[cfg(all(test, feature = "nightly"))]
8586
extern crate test;

0 commit comments

Comments
 (0)