Skip to content

Commit 6b751ce

Browse files
authored
fix: convert bytes to utf8 safely (#27)
Fix #26
1 parent c67e96d commit 6b751ce

File tree

10 files changed

+35
-30
lines changed

10 files changed

+35
-30
lines changed

justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
alias b := bench
22
alias e := echo
3+
alias t := test
34

45
build:
56
@cargo build

rsocket-test/tests/test_composite_metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn test_encode_and_decode() {
1010
extension::MimeType::TEXT_PLAIN,
1111
*metadatas[0].get_mime_type()
1212
);
13-
assert_eq!("Hello World!", metadatas[0].get_metadata_utf8());
13+
assert_eq!("Hello World!", metadatas[0].get_metadata_utf8().unwrap());
1414
assert_eq!(
1515
MimeType::from("application/not_well"),
1616
*metadatas[1].get_mime_type()

rsocket/src/extension/composite.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl CompositeMetadata {
134134
.into());
135135
}
136136
let front = bs.split_to(mime_len);
137-
MimeType::Normal(String::from_utf8(front.to_vec()).unwrap())
137+
MimeType::Normal(String::from_utf8(front.to_vec())?)
138138
};
139139

140140
if bs.len() < 3 {
@@ -174,8 +174,8 @@ impl CompositeMetadataEntry {
174174
&self.metadata
175175
}
176176

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

rsocket/src/extension/routing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl RoutingMetadata {
6363
if bf.len() < size {
6464
return Err(RSocketError::WithDescription("require more bytes!".into()).into());
6565
}
66-
let tag = String::from_utf8(bf.split_to(size).to_vec()).unwrap();
66+
let tag = String::from_utf8(bf.split_to(size).to_vec())?;
6767
Ok(Some(tag))
6868
}
6969
}

rsocket/src/frame/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Error {
6464

6565
pub fn get_data_utf8(&self) -> Option<&str> {
6666
match &self.data {
67-
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
67+
Some(b) => std::str::from_utf8(b).ok(),
6868
None => None,
6969
}
7070
}

rsocket/src/frame/setup.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ impl Setup {
9999
}
100100
}
101101

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

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

110110
pub fn get_metadata(&self) -> Option<&Bytes> {

rsocket/src/payload/misc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bytes::Bytes;
2+
3+
#[inline]
4+
pub(crate) fn bytes_to_utf8(input: &Option<Bytes>) -> Option<&str> {
5+
match input {
6+
Some(b) => std::str::from_utf8(b).ok(),
7+
None => None,
8+
}
9+
}

rsocket/src/payload/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod misc;
12
mod normal;
23
mod setup;
34

rsocket/src/payload/normal.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::misc::bytes_to_utf8;
12
use crate::frame;
23
use bytes::Bytes;
34

@@ -77,17 +78,11 @@ impl Payload {
7778
}
7879

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

8684
pub fn metadata_utf8(&self) -> Option<&str> {
87-
match &self.m {
88-
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
89-
None => None,
90-
}
85+
bytes_to_utf8(&self.m)
9186
}
9287

9388
pub fn is_empty(&self) -> bool {

rsocket/src/payload/setup.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::misc::bytes_to_utf8;
12
use crate::frame::Setup;
23
use crate::utils::DEFAULT_MIME_TYPE;
34
use bytes::Bytes;
@@ -125,32 +126,30 @@ impl SetupPayload {
125126
}
126127

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

134132
pub fn data_mime_type(&self) -> Option<&str> {
135-
match &self.mime_d {
136-
Some(b) => Some(std::str::from_utf8(b.as_ref()).expect("Invalid UTF-8 bytes.")),
137-
None => None,
138-
}
133+
bytes_to_utf8(&self.mime_d)
139134
}
140135
}
141136

142137
impl From<Setup> for SetupPayload {
143138
fn from(input: Setup) -> SetupPayload {
144139
let mut bu = SetupPayload::builder();
145140
// TODO: fill other properties.
146-
bu = bu.set_data_mime_type(input.get_mime_data());
147-
bu = bu.set_metadata_mime_type(input.get_mime_metadata());
148-
let ka = (input.get_keepalive(), input.get_lifetime());
141+
if let Some(m) = input.get_mime_data() {
142+
bu = bu.set_data_mime_type(m);
143+
}
144+
if let Some(m) = input.get_mime_metadata() {
145+
bu = bu.set_metadata_mime_type(m);
146+
}
147+
let keepalive = (input.get_keepalive(), input.get_lifetime());
149148
let (d, m) = input.split();
150149
bu.inner.d = d;
151150
bu.inner.m = m;
152151
let mut pa = bu.build();
153-
pa.keepalive = ka;
152+
pa.keepalive = keepalive;
154153
pa
155154
}
156155
}

0 commit comments

Comments
 (0)