1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 10:32:55 +01:00

Update base64 dependency to 0.21 (#316)

This commit is contained in:
citreae535 2023-01-31 00:53:30 +08:00 committed by GitHub
parent 713b157fd4
commit 2bc16eee18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 14 deletions

View File

@ -21,7 +21,7 @@ path = "src/lib.rs"
actix-utils = "3" actix-utils = "3"
actix-web = { version = "4.1", default_features = false } actix-web = { version = "4.1", default_features = false }
base64 = "0.20" base64 = "0.21"
futures-core = "0.3.7" futures-core = "0.3.7"
futures-util = { version = "0.3.7", default-features = false, features = ["std"] } futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
log = "0.4" log = "0.4"

View File

@ -4,6 +4,7 @@ use actix_web::{
http::header::{HeaderValue, InvalidHeaderValue, TryIntoHeaderValue}, http::header::{HeaderValue, InvalidHeaderValue, TryIntoHeaderValue},
web::{BufMut, BytesMut}, web::{BufMut, BytesMut},
}; };
use base64::{prelude::BASE64_STANDARD, Engine};
use crate::headers::authorization::{errors::ParseError, Scheme}; use crate::headers::authorization::{errors::ParseError, Scheme};
@ -58,7 +59,7 @@ impl Scheme for Basic {
_ => return Err(ParseError::MissingScheme), _ => return Err(ParseError::MissingScheme),
} }
let decoded = base64::decode(parts.next().ok_or(ParseError::Invalid)?)?; let decoded = BASE64_STANDARD.decode(parts.next().ok_or(ParseError::Invalid)?)?;
let mut credentials = str::from_utf8(&decoded)?.splitn(2, ':'); let mut credentials = str::from_utf8(&decoded)?.splitn(2, ':');
let user_id = credentials let user_id = credentials
@ -97,11 +98,13 @@ impl TryIntoHeaderValue for Basic {
type Error = InvalidHeaderValue; type Error = InvalidHeaderValue;
fn try_into_value(self) -> Result<HeaderValue, Self::Error> { fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
let mut credentials = BytesMut::with_capacity( let credential_length =
self.user_id.len() self.user_id.len() + 1 + self.password.as_ref().map_or(0, |pwd| pwd.len());
+ 1 // ':' // The length of BASE64 encoded bytes is `4 * credential_length.div_ceil(3)`
+ self.password.as_ref().map_or(0, |pwd| pwd.len()), // TODO: Use credential_length.div_ceil(3) when `int_roundings` becomes stable
); // https://github.com/rust-lang/rust/issues/88581
let mut value = String::with_capacity(6 + 4 * (credential_length + 2) / 3);
let mut credentials = BytesMut::with_capacity(credential_length);
credentials.extend_from_slice(self.user_id.as_bytes()); credentials.extend_from_slice(self.user_id.as_bytes());
credentials.put_u8(b':'); credentials.put_u8(b':');
@ -109,14 +112,10 @@ impl TryIntoHeaderValue for Basic {
credentials.extend_from_slice(password.as_bytes()); credentials.extend_from_slice(password.as_bytes());
} }
// TODO: It would be nice not to allocate new `String` here but write value.push_str("Basic ");
// directly to `value` BASE64_STANDARD.encode_string(&credentials, &mut value);
let encoded = base64::encode(&credentials);
let mut value = BytesMut::with_capacity(6 + encoded.len());
value.put(&b"Basic "[..]);
value.put(encoded.as_bytes());
HeaderValue::from_maybe_shared(value.freeze()) HeaderValue::from_maybe_shared(value)
} }
} }