feat: switched response type from Vec<u8> to Option<Vec<u8>> to allow returning of differentiation of no body from empty body.

This commit is contained in:
Josiah Bull 2022-10-19 12:28:44 +13:00
parent 36ab770059
commit 1c657d4c6a
No known key found for this signature in database
GPG Key ID: 10FCCC1BF551A8A8
4 changed files with 13 additions and 8 deletions

View File

@ -13,7 +13,7 @@ fn main() {
let callback = |_req: &Request, _resp| {
let resp = Response::builder()
.status(StatusCode::FORBIDDEN)
.body("Access denied".into())
.body(Some("Access denied".into()))
.unwrap();
Err(resp)
};

View File

@ -65,7 +65,7 @@ pub enum Error {
/// HTTP error.
#[error("HTTP error: {}", .0.status())]
#[cfg(feature = "handshake")]
Http(Response<Vec<u8>>),
Http(Response<Option<Vec<u8>>>),
/// HTTP format error.
#[error("HTTP format error: {0}")]
#[cfg(feature = "handshake")]

View File

@ -26,7 +26,7 @@ use crate::{
pub type Request = HttpRequest<()>;
/// Client response type.
pub type Response = HttpResponse<Vec<u8>>;
pub type Response = HttpResponse<Option<Vec<u8>>>;
/// Client handshake role.
#[derive(Debug)]
@ -86,7 +86,7 @@ impl<S: Read + Write> HandshakeRole for ClientHandshake<S> {
let result = match self.verify_data.verify_response(result) {
Ok(r) => r,
Err(Error::Http(mut e)) => {
*e.body_mut() = tail;
*e.body_mut() = Some(tail);
return Err(Error::Http(e))
},
Err(e) => return Err(e),
@ -263,7 +263,7 @@ impl<'h, 'b: 'h> FromHttparse<httparse::Response<'h, 'b>> for Response {
let headers = HeaderMap::from_httparse(raw.headers)?;
let mut response = Response::new(vec![]);
let mut response = Response::new(None);
*response.status_mut() = StatusCode::from_u16(raw.code.expect("Bug: no HTTP status code"))?;
*response.headers_mut() = headers;
// TODO: httparse only supports HTTP 0.9/1.0/1.1 but not HTTP 2.0

View File

@ -30,7 +30,7 @@ pub type Request = HttpRequest<()>;
pub type Response = HttpResponse<()>;
/// Server error response type.
pub type ErrorResponse = HttpResponse<Vec<u8>>;
pub type ErrorResponse = HttpResponse<Option<String>>;
fn create_parts<T>(request: &HttpRequest<T>) -> Result<Builder> {
if request.method() != http::Method::GET {
@ -265,7 +265,9 @@ impl<S: Read + Write, C: Callback> HandshakeRole for ServerHandshake<S, C> {
let mut output = vec![];
write_response(&mut output, resp)?;
output.extend_from_slice(resp.body());
if let Some(body) = resp.body() {
output.extend_from_slice(body.as_bytes());
}
ProcessingResult::Continue(HandshakeMachine::start_write(stream, output))
}
@ -275,7 +277,10 @@ impl<S: Read + Write, C: Callback> HandshakeRole for ServerHandshake<S, C> {
StageResult::DoneWriting(stream) => {
if let Some(err) = self.error_response.take() {
debug!("Server handshake failed.");
return Err(Error::Http(err));
let (parts, body) = err.into_parts();
let body = body.map(|b| b.as_bytes().to_vec());
return Err(Error::Http(http::Response::from_parts(parts, body)));
} else {
debug!("Server handshake done.");
let websocket = WebSocket::from_raw_socket(stream, Role::Server, self.config);