Skip to content

Commit a57d1a1

Browse files
committed
Migrate to new error types
work in progress
1 parent 7d64ffc commit a57d1a1

File tree

5 files changed

+14
-47
lines changed

5 files changed

+14
-47
lines changed

src/client.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use http_types::{
88
headers::{HeaderName, HeaderValue, CONTENT_LENGTH, DATE, TRANSFER_ENCODING},
99
Body, Request, Response, StatusCode,
1010
};
11+
use http_types::{Error, ErrorKind};
1112

1213
use std::pin::Pin;
1314
use std::str::FromStr;
1415

1516
use crate::chunked::ChunkedDecoder;
1617
use crate::date::fmt_http_date;
17-
use crate::error::HttpError;
18-
use crate::{Exception, MAX_HEADERS};
18+
use crate::{MAX_HEADERS};
1919

2020
/// An HTTP encoder.
2121
#[derive(Debug)]
@@ -98,7 +98,7 @@ pub async fn encode(req: Request) -> Result<Encoder, std::io::Error> {
9898
}
9999

100100
/// Decode an HTTP response on the client.
101-
pub async fn decode<R>(reader: R) -> Result<Response, Exception>
101+
pub async fn decode<R>(reader: R) -> Result<Response, Error>
102102
where
103103
R: Read + Unpin + Send + Sync + 'static,
104104
{
@@ -125,15 +125,14 @@ where
125125
// Convert our header buf into an httparse instance, and validate.
126126
let status = httparse_res.parse(&buf)?;
127127
if status.is_partial() {
128-
dbg!(String::from_utf8(buf).unwrap());
129-
return Err("Malformed HTTP head".into());
128+
return Err(Error::new(ErrorKind::InvalidData, "Malformed HTTP head", StatusCode::BadRequest));
130129
}
131-
let code = httparse_res.code.ok_or_else(|| "No status code found")?;
130+
let code = httparse_res.code.ok_or_else(|| Err(Error::new(ErrorKind::InvalidData, "No status code found", StatusCode::BadRequest)))?;
132131

133132
// Convert httparse headers + body into a `http::Response` type.
134-
let version = httparse_res.version.ok_or_else(|| "No version found")?;
133+
let version = httparse_res.version.ok_or_else(|| Err(Error::new(ErrorKind::InvalidData, "No version found", StatusCode::BadRequest)))?;
135134
if version != 1 {
136-
return Err("Unsupported HTTP version".into());
135+
return Err(Error::new(ErrorKind::InvalidData, "Unsupported HTTP version", StatusCode::BadRequest));
137136
}
138137
use std::convert::TryFrom;
139138
let mut res = Response::new(StatusCode::try_from(code)?);
@@ -153,7 +152,7 @@ where
153152

154153
if content_length.is_some() && transfer_encoding.is_some() {
155154
// This is always an error.
156-
return Err(HttpError::UnexpectedContentLengthHeader.into());
155+
return Err(Error::new(ErrorKind::InvalidData, "Unexpected Content-Length header", StatusCode::BadRequest));
157156
}
158157

159158
// Check for Transfer-Encoding

src/error.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ const MAX_HEADERS: usize = 128;
3939
pub use check::check;
4040

4141
mod check;
42-
mod error;
43-
44-
pub use crate::error::HttpError;
4542

4643
pub mod client;
4744
pub mod server;

src/server.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use async_std::prelude::*;
1111
use async_std::task::{Context, Poll};
1212
use futures_core::ready;
1313
use http_types::headers::{HeaderName, HeaderValue, CONTENT_LENGTH, TRANSFER_ENCODING};
14-
use http_types::{Body, Method, Request, Response};
14+
use http_types::{Body, Method, Request, Response, StatusCode, Error, ErrorKind};
1515

1616
use crate::chunked::ChunkedDecoder;
1717
use crate::date::fmt_http_date;
18-
use crate::error::HttpError;
1918
use crate::{Exception, MAX_HEADERS};
2019

2120
const CR: u8 = b'\r';
@@ -24,7 +23,7 @@ const LF: u8 = b'\n';
2423
/// Parse an incoming HTTP connection.
2524
///
2625
/// Supports `KeepAlive` requests by default.
27-
pub async fn accept<RW, F, Fut>(addr: &str, mut io: RW, endpoint: F) -> Result<(), Exception>
26+
pub async fn accept<RW, F, Fut>(addr: &str, mut io: RW, endpoint: F) -> http_types::Result<()>
2827
where
2928
RW: Read + Write + Clone + Send + Sync + Unpin + 'static,
3029
F: Fn(Request) -> Fut,
@@ -56,7 +55,7 @@ where
5655
req = match timeout(timeout_duration, decode(addr, io.clone())).await {
5756
Ok(Ok(Some(r))) => r,
5857
Ok(Ok(None)) | Err(TimeoutError { .. }) => break, /* EOF or timeout */
59-
Ok(Err(e)) => return Err(e),
58+
Ok(Err(e)) => return Err(e).into(),
6059
};
6160
// Loop back with the new request and stream and start again
6261
}
@@ -336,7 +335,7 @@ impl Read for Encoder {
336335
const HTTP_1_1_VERSION: u8 = 1;
337336

338337
/// Decode an HTTP request on the server.
339-
async fn decode<R>(addr: &str, reader: R) -> Result<Option<Request>, Exception>
338+
async fn decode<R>(addr: &str, reader: R) -> Result<Option<Request>, Error>
340339
where
341340
R: Read + Unpin + Send + Sync + 'static,
342341
{
@@ -388,7 +387,7 @@ where
388387

389388
if content_length.is_some() && transfer_encoding.is_some() {
390389
// This is always an error.
391-
return Err(HttpError::UnexpectedContentLengthHeader.into());
390+
return Err(Error::new(ErrorKind::InvalidData, "Unexpected Content-Length header", StatusCode::BadRequest));
392391
}
393392

394393
// Check for Transfer-Encoding

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl TestCase {
6363
expected
6464
}
6565

66-
pub async fn assert(self) {
66+
pub(crate) async fn assert(self) {
6767
let mut actual = self.read_result().await;
6868
let mut expected = self.read_expected().await;
6969
assert!(!actual.is_empty(), "Received empty reply");

0 commit comments

Comments
 (0)