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

pull/298/head
Josiah Bull 2 years ago
parent 36ab770059
commit 1c657d4c6a
No known key found for this signature in database
GPG Key ID: 10FCCC1BF551A8A8
  1. 2
      examples/callback-error.rs
  2. 2
      src/error.rs
  3. 6
      src/handshake/client.rs
  4. 11
      src/handshake/server.rs

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

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

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

@ -30,7 +30,7 @@ pub type Request = HttpRequest<()>;
pub type Response = HttpResponse<()>; pub type Response = HttpResponse<()>;
/// Server error response type. /// 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> { fn create_parts<T>(request: &HttpRequest<T>) -> Result<Builder> {
if request.method() != http::Method::GET { 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![]; let mut output = vec![];
write_response(&mut output, resp)?; 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)) 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) => { StageResult::DoneWriting(stream) => {
if let Some(err) = self.error_response.take() { if let Some(err) = self.error_response.take() {
debug!("Server handshake failed."); 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 { } else {
debug!("Server handshake done."); debug!("Server handshake done.");
let websocket = WebSocket::from_raw_socket(stream, Role::Server, self.config); let websocket = WebSocket::from_raw_socket(stream, Role::Server, self.config);

Loading…
Cancel
Save