Skip to content

Commit

Permalink
Upgrade to e-io 0.6 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov authored Oct 4, 2023
1 parent e9f5e2f commit 27d428c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 99 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ nightly = ["embedded-io-async", "embassy-sync/nightly", "embedded-svc?/nightly"]
embassy-util = [] # Just for backward compat. To be removed in future

[dependencies]
embedded-io = { version = "0.5", default-features = false }
embedded-io-async = { version = "0.5", default-features = false, optional = true }
embedded-io = { version = "0.6", default-features = false }
embedded-io-async = { version = "0.6", default-features = false, optional = true }
heapless = { version = "0.7", default-features = false }
httparse = { version = "1.7", default-features = false }
embassy-futures = "0.1"
Expand All @@ -34,7 +34,7 @@ no-std-net = { version = "0.6", default-features = false }
log = { version = "0.4", default-features = false }
base64 = { version = "0.13", default-features = false }
sha1_smol = { version = "1", default-features = false }
embedded-nal-async = "0.5"
embedded-nal-async = "0.6"
domain = { version = "0.7", default-features = false, optional = true }
futures-lite = { version = "1", default-features = false, optional = true }
async-io = { version = "1", default-features = false, optional = true }
Expand Down
83 changes: 13 additions & 70 deletions src/asynch/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use log::trace;
#[cfg(feature = "embedded-svc")]
pub use embedded_svc_compat::*;

use super::io::map_write_err;
use super::ws::http::UpgradeError;

pub mod client;
Expand Down Expand Up @@ -295,26 +294,10 @@ where
body = BodyType::from_header(name, unsafe { str::from_utf8_unchecked(value) });
}

output
.write_all(name.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output
.write_all(b": ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output
.write_all(value)
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(name.as_bytes()).await.map_err(Error::Io)?;
output.write_all(b": ").await.map_err(Error::Io)?;
output.write_all(value).await.map_err(Error::Io)?;
output.write_all(b"\r\n").await.map_err(Error::Io)?;
}

Ok(body)
Expand All @@ -324,11 +307,7 @@ pub async fn send_headers_end<W>(mut output: W) -> Result<(), Error<W::Error>>
where
W: Write,
{
output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)
output.write_all(b"\r\n").await.map_err(Error::Io)
}

#[derive(Debug)]
Expand Down Expand Up @@ -1150,11 +1129,7 @@ impl<W> ChunkedWrite<W> {
where
W: Write,
{
self.output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)
self.output.write_all(b"\r\n").await.map_err(Error::Io)
}

pub fn release(self) -> W {
Expand All @@ -1180,18 +1155,12 @@ where
self.output
.write_all(len_str.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

self.output
.write_all(buf)
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
self.output.write_all(buf).await.map_err(Error::Io)?;
self.output
.write_all("\r\n".as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

Ok(buf.len())
Expand Down Expand Up @@ -1420,71 +1389,45 @@ where
let mut written = false;

if !request {
output
.write_all(b"HTTP/1.1")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(b"HTTP/1.1").await.map_err(Error::Io)?;
written = true;
}

if let Some(token) = token {
if written {
output
.write_all(b" ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(b" ").await.map_err(Error::Io)?;
}

output
.write_all(token.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

written = true;
}

if let Some(extra) = extra {
if written {
output
.write_all(b" ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(b" ").await.map_err(Error::Io)?;
}

output
.write_all(extra.as_bytes())
.await
.map_err(map_write_err)
.map_err(Error::Io)?;

written = true;
}

if request {
if written {
output
.write_all(b" ")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(b" ").await.map_err(Error::Io)?;
}

output
.write_all(b"HTTP/1.1")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(b"HTTP/1.1").await.map_err(Error::Io)?;
}

output
.write_all(b"\r\n")
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
output.write_all(b"\r\n").await.map_err(Error::Io)?;

Ok(())
}
Expand Down
6 changes: 1 addition & 5 deletions src/asynch/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::asynch::http::{
send_headers, send_headers_end, send_status, Body, BodyType, Error, Method, RequestHeaders,
SendBody,
};
use crate::asynch::io::map_write_err;

#[cfg(feature = "embedded-svc")]
pub use embedded_svc_compat::*;
Expand Down Expand Up @@ -171,10 +170,7 @@ where

self.complete_request(Some(500), Some("Internal Error"), &headers)
.await?;
self.response_mut()?
.write_all(err_str.as_bytes())
.await
.map_err(map_write_err)?;
self.response_mut()?.write_all(err_str.as_bytes()).await?;

Ok(())
} else {
Expand Down
8 changes: 0 additions & 8 deletions src/asynch/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ where
write
.write_all(&buf[0..size_read])
.await
.map_err(map_write_err)
.map_err(CopyError::Write)?;

copied += size_read as u64;
Expand All @@ -101,10 +100,3 @@ where

Ok(copied)
}

pub(crate) fn map_write_err<W>(e: embedded_io::WriteAllError<W>) -> W {
match e {
embedded_io::WriteAllError::WriteZero => panic!("write() returned Ok(0)"),
embedded_io::WriteAllError::Other(e) => e,
}
}
15 changes: 2 additions & 13 deletions src/asynch/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ impl FrameHeader {
write
.write_all(&header_buf[..header_len])
.await
.map_err(map_write_err)
.map_err(Error::Io)
}

Expand Down Expand Up @@ -324,11 +323,7 @@ impl FrameHeader {
} else if payload.is_empty() {
Ok(())
} else if self.mask_key.is_none() {
write
.write_all(payload)
.await
.map_err(map_write_err)
.map_err(Error::Io)
write.write_all(payload).await.map_err(Error::Io)
} else {
let mut buf = [0_u8; 64];

Expand All @@ -341,11 +336,7 @@ impl FrameHeader {

self.mask(&mut buf, offset);

write
.write_all(&buf)
.await
.map_err(map_write_err)
.map_err(Error::Io)?;
write.write_all(&buf).await.map_err(Error::Io)?;

offset += len;
}
Expand Down Expand Up @@ -569,8 +560,6 @@ pub mod http {
#[cfg(feature = "embedded-svc")]
pub use embedded_svc_compat::*;

use super::io::map_write_err;

#[cfg(feature = "embedded-svc")]
mod embedded_svc_compat {
use core::convert::{TryFrom, TryInto};
Expand Down

0 comments on commit 27d428c

Please sign in to comment.