Skip to content

Commit

Permalink
Update base64 to 0.21
Browse files Browse the repository at this point in the history
Also added once_cell for caching the new Engine based APIs of base64

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
  • Loading branch information
Ayush1325 committed Jan 21, 2023
1 parent aad2004 commit 2932c03
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ members = [
[dependencies]
http = "0.2.0"
headers-core = { version = "0.2", path = "./headers-core" }
base64 = "0.13"
base64 = "0.21"
bitflags = "1.0"
bytes = "1"
mime = "0.3.14"
sha1 = "0.10"
httpdate = "1"
once_cell = "1.17.0"

[features]
nightly = []
8 changes: 5 additions & 3 deletions src/common/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Authorization header and types.

use base64;
use super::engine;
use base64::Engine;
use bytes::Bytes;

use util::HeaderValueString;
Expand Down Expand Up @@ -158,7 +159,8 @@ impl Credentials for Basic {
let bytes = &value.as_bytes()["Basic ".len()..];
let non_space_pos = bytes.iter().position(|b| *b != b' ')?;
let bytes = &bytes[non_space_pos..];
let bytes = base64::decode(bytes).ok()?;

let bytes = engine().decode(bytes).ok()?;

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

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

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

let bytes = Bytes::from(encoded);
HeaderValue::from_maybe_shared(bytes)
Expand Down
7 changes: 7 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,10 @@ mod upgrade;
mod user_agent;
mod vary;
//mod warning;

fn engine() -> &'static base64::engine::GeneralPurpose {
static ENGINE: once_cell::sync::OnceCell<base64::engine::GeneralPurpose> =
once_cell::sync::OnceCell::new();

ENGINE.get_or_init(|| base64::engine::general_purpose::STANDARD)
}
6 changes: 3 additions & 3 deletions src/common/sec_websocket_accept.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use base64;
use base64::Engine;
use bytes::Bytes;
use sha1::{Digest, Sha1};

use super::SecWebsocketKey;
use super::{engine, SecWebsocketKey};

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

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

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extern crate headers_core;
extern crate http;
extern crate httpdate;
extern crate mime;
extern crate once_cell;
extern crate sha1;
#[cfg(all(test, feature = "nightly"))]
extern crate test;
Expand Down

0 comments on commit 2932c03

Please sign in to comment.