Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion edge-captive/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@ pub const DEFAULT_SOCKET: SocketAddr = SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSP

const PORT: u16 = 53;

#[derive(Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum DnsIoError<E> {
DnsError(DnsError),
IoError(E),
}

pub type DnsIoErrorKind = DnsIoError<edge_nal::io::ErrorKind>;

impl<E> DnsIoError<E>
where
E: edge_nal::io::Error,
{
pub fn erase(&self) -> DnsIoError<edge_nal::io::ErrorKind> {
match self {
Self::DnsError(e) => DnsIoError::DnsError(*e),
Self::IoError(e) => DnsIoError::IoError(e.kind()),
}
}
}

impl<E> From<DnsError> for DnsIoError<E> {
fn from(err: DnsError) -> Self {
Self::DnsError(err)
Expand Down
2 changes: 1 addition & 1 deletion edge-captive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use domain::{
#[cfg(feature = "io")]
pub mod io;

#[derive(Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum DnsError {
ShortBuf,
InvalidMessage,
Expand Down
16 changes: 15 additions & 1 deletion edge-dhcp/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ pub mod server;
pub const DEFAULT_SERVER_PORT: u16 = 67;
pub const DEFAULT_CLIENT_PORT: u16 = 68;

#[derive(Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error<E> {
Io(E),
Format(dhcp::Error),
}

pub type ErrorKind = Error<edge_nal::io::ErrorKind>;

impl<E> Error<E>
where
E: edge_nal::io::Error,
{
pub fn erase(&self) -> Error<edge_nal::io::ErrorKind> {
match self {
Self::Io(e) => Error::Io(e.kind()),
Self::Format(e) => Error::Format(*e),
}
}
}

impl<E> From<dhcp::Error> for Error<E> {
fn from(value: dhcp::Error) -> Self {
Self::Format(value)
Expand Down
2 changes: 1 addition & 1 deletion edge-dhcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod server;
#[cfg(feature = "io")]
pub mod io;

#[derive(Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
DataUnderflow,
BufferOverflow,
Expand Down
27 changes: 15 additions & 12 deletions edge-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ where
Ok(())
}

async fn request<'b, const N: usize, T: TcpConnect>(
conn: &mut Connection<'b, T, N>,
async fn request<const N: usize, T: TcpConnect>(
conn: &mut Connection<'_, T, N>,
uri: &str,
) -> Result<(), Error<T::Error>> {
conn.initiate_request(true, Method::Get, uri, &[("Host", "httpbin.org")])
Expand Down Expand Up @@ -103,7 +103,7 @@ async fn request<'b, const N: usize, T: TcpConnect>(
### HTTP server

```rust
use core::fmt::Display;
use core::fmt::{Debug, Display};

use edge_http::io::server::{Connection, DefaultServer, Handler};
use edge_http::io::Error;
Expand Down Expand Up @@ -133,24 +133,27 @@ pub async fn run(server: &mut DefaultServer) -> Result<(), anyhow::Error> {
.bind(addr.parse().unwrap())
.await?;

server.run(acceptor, HttpHandler).await?;
server.run(None, acceptor, HttpHandler).await?;

Ok(())
}

struct HttpHandler;

impl<'b, T, const N: usize> Handler<'b, T, N> for HttpHandler
where
T: Read + Write,
{
type Error = Error<T::Error>;
impl Handler for HttpHandler {
type Error<E>
= Error<E>
where
E: Debug;

async fn handle(
async fn handle<T, const N: usize>(
&self,
_task_id: impl Display + Copy,
conn: &mut Connection<'b, T, N>,
) -> Result<(), Self::Error> {
conn: &mut Connection<'_, T, N>,
) -> Result<(), Self::Error<T::Error>>
where
T: Read + Write,
{
let headers = conn.headers()?;

if headers.method != Method::Get {
Expand Down
34 changes: 29 additions & 5 deletions edge-http/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub mod client;
pub mod server;

/// An error in parsing the headers or the body.
#[derive(Debug)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Error<E> {
InvalidHeaders,
InvalidBody,
Expand All @@ -34,6 +34,30 @@ pub enum Error<E> {
Io(E),
}

pub type ErrorKind = Error<edge_nal::io::ErrorKind>;

impl<E> Error<E>
where
E: edge_nal::io::Error,
{
pub fn erase(&self) -> Error<edge_nal::io::ErrorKind> {
match self {
Self::InvalidHeaders => Error::InvalidHeaders,
Self::InvalidBody => Error::InvalidBody,
Self::TooManyHeaders => Error::TooManyHeaders,
Self::TooLongHeaders => Error::TooLongHeaders,
Self::TooLongBody => Error::TooLongBody,
Self::IncompleteHeaders => Error::IncompleteHeaders,
Self::IncompleteBody => Error::IncompleteBody,
Self::InvalidState => Error::InvalidState,
Self::ConnectionClosed => Error::ConnectionClosed,
Self::HeadersMismatchError(e) => Error::HeadersMismatchError(*e),
Self::WsUpgradeError(e) => Error::WsUpgradeError(*e),
Self::Io(e) => Error::Io(e.kind()),
}
}
}

impl<E> From<httparse::Error> for Error<E> {
fn from(e: httparse::Error) -> Self {
match e {
Expand Down Expand Up @@ -373,7 +397,7 @@ where
Ok((connection_type, body_type))
}

impl<'b, const N: usize> Headers<'b, N> {
impl<const N: usize> Headers<'_, N> {
fn resolve<E>(
&self,
carry_over_connection_type: Option<ConnectionType>,
Expand Down Expand Up @@ -497,7 +521,7 @@ where
type Error = Error<R::Error>;
}

impl<'b, R> Read for Body<'b, R>
impl<R> Read for Body<'_, R>
where
R: Read,
{
Expand Down Expand Up @@ -545,7 +569,7 @@ where
type Error = R::Error;
}

impl<'b, R> Read for PartiallyRead<'b, R>
impl<R> Read for PartiallyRead<'_, R>
where
R: Read,
{
Expand Down Expand Up @@ -824,7 +848,7 @@ where
type Error = Error<R::Error>;
}

impl<'b, R> Read for ChunkedRead<'b, R>
impl<R> Read for ChunkedRead<'_, R>
where
R: Read,
{
Expand Down
29 changes: 21 additions & 8 deletions edge-http/src/io/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ where
let needs_close = if self.response_mut().is_ok() {
self.complete_response().await?
} else {
true
false
};

Result::<_, Error<T::Error>>::Ok(needs_close)
Expand All @@ -239,11 +239,13 @@ where

match result {
Ok(true) | Err(_) => {
let mut io = state.io.take().unwrap();
let io = state.io.take();
*self = Self::Unbound(state);

io.close(Close::Both).await.map_err(Error::Io)?;
let _ = io.abort().await;
if let Some(mut io) = io {
io.close(Close::Both).await.map_err(Error::Io)?;
let _ = io.abort().await;
}
}
_ => {
*self = Self::Unbound(state);
Expand All @@ -255,6 +257,17 @@ where
Ok(())
}

pub async fn close(mut self) -> Result<(), Error<T::Error>> {
let res = self.complete().await;

if let Some(mut io) = self.unbind().io.take() {
io.close(Close::Both).await.map_err(Error::Io)?;
let _ = io.abort().await;
}

res
}

async fn complete_request(&mut self) -> Result<(), Error<T::Error>> {
self.request_mut()?.io.finish().await?;

Expand Down Expand Up @@ -401,7 +414,7 @@ where
type Error = Error<T::Error>;
}

impl<'b, T, const N: usize> Read for Connection<'b, T, N>
impl<T, const N: usize> Read for Connection<'_, T, N>
where
T: TcpConnect,
{
Expand All @@ -410,7 +423,7 @@ where
}
}

impl<'b, T, const N: usize> Write for Connection<'b, T, N>
impl<T, const N: usize> Write for Connection<'_, T, N>
where
T: TcpConnect,
{
Expand Down Expand Up @@ -473,7 +486,7 @@ mod embedded_svc_compat {

use embedded_svc::http::client::asynch::{Connection, Headers, Method, Status};

impl<'b, T, const N: usize> Headers for super::Connection<'b, T, N>
impl<T, const N: usize> Headers for super::Connection<'_, T, N>
where
T: TcpConnect,
{
Expand All @@ -484,7 +497,7 @@ mod embedded_svc_compat {
}
}

impl<'b, T, const N: usize> Status for super::Connection<'b, T, N>
impl<T, const N: usize> Status for super::Connection<'_, T, N>
where
T: TcpConnect,
{
Expand Down
Loading