chore: Update to base64 0.21 (#1228)

This commit is contained in:
tottoto
2023-01-11 11:25:38 -05:00
committed by GitHub
parent 54c37a7c51
commit ee6ccfbcc2
7 changed files with 61 additions and 12 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ channel = [
# harness = false
[dependencies]
base64 = "0.13"
base64 = "0.21"
bytes = "1.0"
futures-core = {version = "0.3", default-features = false}
futures-util = {version = "0.3", default-features = false}
+6 -4
View File
@@ -1,3 +1,4 @@
use base64::Engine as _;
use bytes::Bytes;
use http::header::HeaderValue;
use std::error::Error;
@@ -127,7 +128,7 @@ impl self::value_encoding::Sealed for Binary {
}
fn from_bytes(value: &[u8]) -> Result<HeaderValue, InvalidMetadataValueBytes> {
let encoded_value: String = base64::encode_config(value, base64::STANDARD_NO_PAD);
let encoded_value: String = crate::util::base64::STANDARD_NO_PAD.encode(value);
HeaderValue::from_maybe_shared(Bytes::from(encoded_value))
.map_err(|_| InvalidMetadataValueBytes::new())
}
@@ -137,7 +138,7 @@ impl self::value_encoding::Sealed for Binary {
}
fn from_static(value: &'static str) -> HeaderValue {
if base64::decode(value).is_err() {
if crate::util::base64::STANDARD.decode(value).is_err() {
panic!("Invalid base64 passed to from_static: {}", value);
}
unsafe {
@@ -148,13 +149,14 @@ impl self::value_encoding::Sealed for Binary {
}
fn decode(value: &[u8]) -> Result<Bytes, InvalidMetadataValueBytes> {
base64::decode(value)
crate::util::base64::STANDARD
.decode(value)
.map(|bytes_vec| bytes_vec.into())
.map_err(|_| InvalidMetadataValueBytes::new())
}
fn equals(a: &HeaderValue, b: &[u8]) -> bool {
if let Ok(decoded) = base64::decode(a.as_bytes()) {
if let Ok(decoded) = crate::util::base64::STANDARD.decode(a.as_bytes()) {
decoded == b
} else {
a.as_bytes() == b
+5 -3
View File
@@ -1,5 +1,6 @@
use crate::body::BoxBody;
use crate::metadata::MetadataMap;
use base64::Engine as _;
use bytes::Bytes;
use http::header::{HeaderMap, HeaderValue};
use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS};
@@ -435,7 +436,8 @@ impl Status {
let details = header_map
.get(GRPC_STATUS_DETAILS_HEADER)
.map(|h| {
base64::decode(h.as_bytes())
crate::util::base64::STANDARD
.decode(h.as_bytes())
.expect("Invalid status header, expected base64 encoded value")
})
.map(Bytes::from)
@@ -516,7 +518,7 @@ impl Status {
}
if !self.details.is_empty() {
let details = base64::encode_config(&self.details[..], base64::STANDARD_NO_PAD);
let details = crate::util::base64::STANDARD_NO_PAD.encode(&self.details[..]);
header_map.insert(
GRPC_STATUS_DETAILS_HEADER,
@@ -971,7 +973,7 @@ mod tests {
let header_map = status.to_header_map().unwrap();
let b64_details = base64::encode_config(DETAILS, base64::STANDARD_NO_PAD);
let b64_details = crate::util::base64::STANDARD_NO_PAD.encode(DETAILS);
assert_eq!(header_map[super::GRPC_STATUS_DETAILS_HEADER], b64_details);
+24
View File
@@ -11,3 +11,27 @@ pub(crate) enum OptionPin<T> {
Some(#[pin] T),
None,
}
pub(crate) mod base64 {
use base64::{
alphabet,
engine::{
general_purpose::{GeneralPurpose, GeneralPurposeConfig},
DecodePaddingMode,
},
};
pub(crate) const STANDARD: GeneralPurpose = GeneralPurpose::new(
&alphabet::STANDARD,
GeneralPurposeConfig::new()
.with_encode_padding(true)
.with_decode_padding_mode(DecodePaddingMode::Indifferent),
);
pub(crate) const STANDARD_NO_PAD: GeneralPurpose = GeneralPurpose::new(
&alphabet::STANDARD,
GeneralPurposeConfig::new()
.with_encode_padding(false)
.with_decode_padding_mode(DecodePaddingMode::Indifferent),
);
}