Skip to content

Commit 255aaa2

Browse files
bnjjjdaniel-abramov
authored andcommitted
add more details for utf8 errors for debugging
Signed-off-by: Benjamin <5719034+bnjjj@users.noreply.github.com>
1 parent 75b59d9 commit 255aaa2

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

examples/autobahn-client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn main() {
3939
for case in 1..=total {
4040
if let Err(e) = run_test(case) {
4141
match e {
42-
Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => (),
42+
Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8(_) => (),
4343
err => error!("test: {}", err),
4444
}
4545
}

examples/autobahn-server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
Ok(stream) => {
3737
if let Err(err) = handle_client(stream) {
3838
match err {
39-
Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8 => (),
39+
Error::ConnectionClosed | Error::Protocol(_) | Error::Utf8(_) => (),
4040
e => error!("test: {}", e),
4141
}
4242
}

src/error.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub enum Error {
5757
#[error("Write buffer is full")]
5858
WriteBufferFull(Message),
5959
/// UTF coding error.
60-
#[error("UTF-8 encoding error")]
61-
Utf8,
60+
#[error("UTF-8 encoding error: {0}")]
61+
Utf8(String),
6262
/// Attack attempt detected.
6363
#[error("Attack attempt detected")]
6464
AttackAttempt,
@@ -76,14 +76,14 @@ pub enum Error {
7676
}
7777

7878
impl From<str::Utf8Error> for Error {
79-
fn from(_: str::Utf8Error) -> Self {
80-
Error::Utf8
79+
fn from(err: str::Utf8Error) -> Self {
80+
Error::Utf8(err.to_string())
8181
}
8282
}
8383

8484
impl From<string::FromUtf8Error> for Error {
85-
fn from(_: string::FromUtf8Error) -> Self {
86-
Error::Utf8
85+
fn from(err: string::FromUtf8Error) -> Self {
86+
Error::Utf8(err.to_string())
8787
}
8888
}
8989

@@ -103,8 +103,8 @@ impl From<http::header::InvalidHeaderName> for Error {
103103

104104
#[cfg(feature = "handshake")]
105105
impl From<http::header::ToStrError> for Error {
106-
fn from(_: http::header::ToStrError) -> Self {
107-
Error::Utf8
106+
fn from(err: http::header::ToStrError) -> Self {
107+
Error::Utf8(err.to_string())
108108
}
109109
}
110110

src/handshake/client.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ pub fn generate_request(mut request: Request) -> Result<(Vec<u8>, String)> {
149149
HeaderName::from_bytes(header.as_bytes()).unwrap(),
150150
))
151151
})?;
152-
write!(req, "{header}: {value}\r\n", header = header, value = value.to_str()?).unwrap();
152+
write!(
153+
req,
154+
"{header}: {value}\r\n",
155+
header = header,
156+
value = value.to_str().map_err(|err| {
157+
Error::Utf8(format!("{err} for header name '{header}' with value: {value:?}"))
158+
})?
159+
)
160+
.unwrap();
153161
}
154162

155163
// Now we must ensure that the headers that we've written once are not anymore present in the map.
@@ -176,7 +184,15 @@ pub fn generate_request(mut request: Request) -> Result<(Vec<u8>, String)> {
176184
name = "Origin";
177185
}
178186

179-
writeln!(req, "{}: {}\r", name, v.to_str()?).unwrap();
187+
writeln!(
188+
req,
189+
"{}: {}\r",
190+
name,
191+
v.to_str().map_err(|err| {
192+
Error::Utf8(format!("{err} for header name '{name}' with value: {v:?}"))
193+
})?
194+
)
195+
.unwrap();
180196
}
181197

182198
writeln!(req, "\r").unwrap();

src/protocol/message.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ mod string_collect {
3333
if let Some(mut incomplete) = self.incomplete.take() {
3434
if let Some((result, rest)) = incomplete.try_complete(input) {
3535
input = rest;
36-
if let Ok(text) = result {
37-
self.data.push_str(text);
38-
} else {
39-
return Err(Error::Utf8);
36+
match result {
37+
Ok(text) => self.data.push_str(text),
38+
Err(result_bytes) => {
39+
return Err(Error::Utf8(String::from_utf8_lossy(result_bytes).into()))
40+
}
4041
}
4142
} else {
4243
input = &[];
@@ -55,9 +56,9 @@ mod string_collect {
5556
self.incomplete = Some(incomplete_suffix);
5657
Ok(())
5758
}
58-
Err(DecodeError::Invalid { valid_prefix, .. }) => {
59+
Err(DecodeError::Invalid { valid_prefix, invalid_sequence, .. }) => {
5960
self.data.push_str(valid_prefix);
60-
Err(Error::Utf8)
61+
Err(Error::Utf8(String::from_utf8_lossy(invalid_sequence).into()))
6162
}
6263
}
6364
} else {
@@ -66,8 +67,8 @@ mod string_collect {
6667
}
6768

6869
pub fn into_string(self) -> Result<String> {
69-
if self.incomplete.is_some() {
70-
Err(Error::Utf8)
70+
if let Some(incomplete) = self.incomplete {
71+
Err(Error::Utf8(format!("incomplete string: {:?}", incomplete)))
7172
} else {
7273
Ok(self.data)
7374
}

0 commit comments

Comments
 (0)