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
9 changes: 7 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use embedded_io::ErrorType;
use embedded_io_async::{Read, Write};
use embedded_nal_async::{Dns, TcpConnect};
use nourl::{Url, UrlScheme};
use heapless::Vec;

/// An async HTTP client that can establish a TCP connection and perform
/// HTTP requests.
Expand Down Expand Up @@ -131,8 +132,12 @@ where
if url.scheme() == UrlScheme::HTTPS {
#[cfg(feature = "esp-mbedtls")]
if let Some(tls) = self.tls.as_mut() {
let mut servername = host.as_bytes().to_vec();
servername.push(0);
let servername = {
let mut vec = Vec::<u8, 256>::new(); // 256 bytes should be enough for most hostnames
vec.extend_from_slice(host.as_bytes()).map_err(|_| Error::InvalidUrl(nourl::Error::UnsupportedScheme))?;
vec.push(0).map_err(|_| Error::InvalidUrl(nourl::Error::UnsupportedScheme))?;
vec
};
let mut session = esp_mbedtls::asynch::Session::new(
conn,
esp_mbedtls::Mode::Client {
Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod response;

/// Errors that can be returned by this library.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", not(feature = "esp-mbedtls")), derive(defmt::Format))]
pub enum Error {
/// An error with DNS (it's always DNS)
Dns,
Expand All @@ -42,6 +42,23 @@ pub enum Error {
ConnectionAborted,
}

#[cfg(all(feature = "defmt", feature = "esp-mbedtls"))]
impl defmt::Format for Error {
fn format(&self, fmt: defmt::Formatter) {
match self {
Error::Dns => defmt::write!(fmt, "Dns"),
Error::Network(e) => defmt::write!(fmt, "Network({})", e),//, defmt::Debug2Format(e)),
Error::Codec => defmt::write!(fmt, "Codec"),
Error::InvalidUrl(e) => defmt::write!(fmt, "InvalidUrl({})", e),
Error::Tls(e) => defmt::write!(fmt, "Tls({:?})", defmt::Debug2Format(e)),
Error::BufferTooSmall => defmt::write!(fmt, "BufferTooSmall"),
Error::AlreadySent => defmt::write!(fmt, "AlreadySent"),
Error::IncorrectBodyWritten => defmt::write!(fmt, "IncorrectBodyWritten"),
Error::ConnectionAborted => defmt::write!(fmt, "ConnectionAborted"),
}
}
}

impl embedded_io::Error for Error {
fn kind(&self) -> embedded_io::ErrorKind {
match self {
Expand Down
Loading