Skip to content

fix: convert bytes to utf8 safely #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
alias b := bench
alias e := echo
alias t := test

build:
@cargo build
Expand Down
2 changes: 1 addition & 1 deletion rsocket-test/tests/test_composite_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_encode_and_decode() {
extension::MimeType::TEXT_PLAIN,
*metadatas[0].get_mime_type()
);
assert_eq!("Hello World!", metadatas[0].get_metadata_utf8());
assert_eq!("Hello World!", metadatas[0].get_metadata_utf8().unwrap());
assert_eq!(
MimeType::from("application/not_well"),
*metadatas[1].get_mime_type()
Expand Down
6 changes: 3 additions & 3 deletions rsocket/src/extension/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl CompositeMetadata {
.into());
}
let front = bs.split_to(mime_len);
MimeType::Normal(String::from_utf8(front.to_vec()).unwrap())
MimeType::Normal(String::from_utf8(front.to_vec())?)
};

if bs.len() < 3 {
Expand Down Expand Up @@ -174,8 +174,8 @@ impl CompositeMetadataEntry {
&self.metadata
}

pub fn get_metadata_utf8(&self) -> &str {
std::str::from_utf8(self.metadata.as_ref()).expect("Invalid UTF-8 bytes.")
pub fn get_metadata_utf8(&self) -> Option<&str> {
std::str::from_utf8(&self.metadata).ok()
}
}

Expand Down
2 changes: 1 addition & 1 deletion rsocket/src/extension/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl RoutingMetadata {
if bf.len() < size {
return Err(RSocketError::WithDescription("require more bytes!".into()).into());
}
let tag = String::from_utf8(bf.split_to(size).to_vec()).unwrap();
let tag = String::from_utf8(bf.split_to(size).to_vec())?;
Ok(Some(tag))
}
}
Expand Down
2 changes: 1 addition & 1 deletion rsocket/src/frame/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Error {

pub fn get_data_utf8(&self) -> Option<&str> {
match &self.data {
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
Some(b) => std::str::from_utf8(b).ok(),
None => None,
}
}
Expand Down
8 changes: 4 additions & 4 deletions rsocket/src/frame/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ impl Setup {
}
}

pub fn get_mime_metadata(&self) -> &str {
std::str::from_utf8(self.mime_metadata.as_ref()).expect("Invalid UTF-8 bytes.")
pub fn get_mime_metadata(&self) -> Option<&str> {
std::str::from_utf8(&self.mime_metadata).ok()
}

pub fn get_mime_data(&self) -> &str {
std::str::from_utf8(self.mime_data.as_ref()).expect("Invalid UTF-8 bytes.")
pub fn get_mime_data(&self) -> Option<&str> {
std::str::from_utf8(&self.mime_data).ok()
}

pub fn get_metadata(&self) -> Option<&Bytes> {
Expand Down
9 changes: 9 additions & 0 deletions rsocket/src/payload/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use bytes::Bytes;

#[inline]
pub(crate) fn bytes_to_utf8(input: &Option<Bytes>) -> Option<&str> {
match input {
Some(b) => std::str::from_utf8(b).ok(),
None => None,
}
}
1 change: 1 addition & 0 deletions rsocket/src/payload/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod misc;
mod normal;
mod setup;

Expand Down
11 changes: 3 additions & 8 deletions rsocket/src/payload/normal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::misc::bytes_to_utf8;
use crate::frame;
use bytes::Bytes;

Expand Down Expand Up @@ -77,17 +78,11 @@ impl Payload {
}

pub fn data_utf8(&self) -> Option<&str> {
match &self.d {
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
None => None,
}
bytes_to_utf8(&self.d)
}

pub fn metadata_utf8(&self) -> Option<&str> {
match &self.m {
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
None => None,
}
bytes_to_utf8(&self.m)
}

pub fn is_empty(&self) -> bool {
Expand Down
23 changes: 11 additions & 12 deletions rsocket/src/payload/setup.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::misc::bytes_to_utf8;
use crate::frame::Setup;
use crate::utils::DEFAULT_MIME_TYPE;
use bytes::Bytes;
Expand Down Expand Up @@ -125,32 +126,30 @@ impl SetupPayload {
}

pub fn metadata_mime_type(&self) -> Option<&str> {
match &self.mime_m {
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
None => None,
}
bytes_to_utf8(&self.mime_m)
}

pub fn data_mime_type(&self) -> Option<&str> {
match &self.mime_d {
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
None => None,
}
bytes_to_utf8(&self.mime_d)
}
}

impl From<Setup> for SetupPayload {
fn from(input: Setup) -> SetupPayload {
let mut bu = SetupPayload::builder();
// TODO: fill other properties.
bu = bu.set_data_mime_type(input.get_mime_data());
bu = bu.set_metadata_mime_type(input.get_mime_metadata());
let ka = (input.get_keepalive(), input.get_lifetime());
if let Some(m) = input.get_mime_data() {
bu = bu.set_data_mime_type(m);
}
if let Some(m) = input.get_mime_metadata() {
bu = bu.set_metadata_mime_type(m);
}
let keepalive = (input.get_keepalive(), input.get_lifetime());
let (d, m) = input.split();
bu.inner.d = d;
bu.inner.m = m;
let mut pa = bu.build();
pa.keepalive = ka;
pa.keepalive = keepalive;
pa
}
}