Skip to content

Commit 7dd2d31

Browse files
return error early for other httparse error rather than panicking
1 parent 67e484b commit 7dd2d31

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

examples/server/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ pub enum WebServerError {
3030
Io(std::io::Error),
3131
Framer(FramerError<std::io::Error>),
3232
WebSocket(ws::Error),
33-
HttpError(String),
33+
HttpError(httparse::Error),
3434
Utf8Error,
35+
Custom(String),
3536
}
3637

3738
impl From<std::io::Error> for WebServerError {
@@ -58,6 +59,12 @@ impl From<Utf8Error> for WebServerError {
5859
}
5960
}
6061

62+
impl From<httparse::Error> for WebServerError {
63+
fn from(err: httparse::Error) -> WebServerError {
64+
WebServerError::HttpError(err)
65+
}
66+
}
67+
6168
fn main() -> std::io::Result<()> {
6269
let addr = "127.0.0.1:1337";
6370
let listener = TcpListener::bind(addr)?;
@@ -150,7 +157,7 @@ fn handle_client(mut stream: TcpStream) -> Result<()> {
150157
Err(httparse::Error::TooManyHeaders) => {
151158
headers.resize(headers.len() * 2, httparse::EMPTY_HEADER)
152159
}
153-
_ => panic!("http parser error"),
160+
Err(e) => return Err(e.into()),
154161
}
155162
};
156163

@@ -164,7 +171,7 @@ fn handle_client(mut stream: TcpStream) -> Result<()> {
164171
msg = e
165172
);
166173
stream.write_all(&html.as_bytes())?;
167-
Err(WebServerError::HttpError(e))
174+
Err(WebServerError::Custom(e))
168175
}
169176
}
170177
}

examples/server_async/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ pub enum WebServerError {
5151
Io(std::io::Error),
5252
Framer(FramerError<std::io::Error>),
5353
WebSocket(ws::Error),
54-
HttpError(String),
54+
HttpError(httparse::Error),
5555
Utf8Error,
56+
Custom(String),
5657
}
5758

5859
impl From<std::io::Error> for WebServerError {
@@ -79,6 +80,12 @@ impl From<Utf8Error> for WebServerError {
7980
}
8081
}
8182

83+
impl From<httparse::Error> for WebServerError {
84+
fn from(err: httparse::Error) -> WebServerError {
85+
WebServerError::HttpError(err)
86+
}
87+
}
88+
8289
#[cfg_attr(feature = "example-async-std", async_std::main)]
8390
#[cfg_attr(feature = "example-tokio", tokio::main)]
8491
#[cfg_attr(feature = "example-smol", smol_potat::main)]
@@ -214,7 +221,7 @@ async fn handle_client(mut stream: TcpStream) -> Result<()> {
214221
Err(httparse::Error::TooManyHeaders) => {
215222
headers.resize(headers.len() * 2, httparse::EMPTY_HEADER)
216223
}
217-
_ => panic!("http parser error"),
224+
Err(e) => return Err(e.into()),
218225
}
219226
};
220227

@@ -228,7 +235,7 @@ async fn handle_client(mut stream: TcpStream) -> Result<()> {
228235
msg = e
229236
);
230237
stream.write_all(&html.as_bytes()).await?;
231-
Err(WebServerError::HttpError(e))
238+
Err(WebServerError::Custom(e))
232239
}
233240
}
234241
}

0 commit comments

Comments
 (0)