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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ repository = "https://github.com/algesten/ureq-proto"
rust-version = "1.67"

[features]
default = ["std"]
std = []

[dependencies]
http = { version = "1.1.0", default-features = false, features = ["std"] }
httparse = { version = "1.8.0", default-features = false }
log = "0.4.22"
url = "2.5.2"
url = "2.5.4"
5 changes: 3 additions & 2 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use std::io::Write;
use alloc::string::ToString;
use core::fmt;
use core::fmt::Write;

use http::{HeaderName, HeaderValue, Method};

Expand Down
2 changes: 2 additions & 0 deletions src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ impl Dechunker {

#[cfg(test)]
mod test {
use alloc::string::String;

use super::*;

#[test]
Expand Down
5 changes: 3 additions & 2 deletions src/client/amended.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::mem;
use alloc::string::ToString;
use core::mem;

use http::{HeaderMap, HeaderName, HeaderValue, Method, Request, Uri, Version};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this using the http crate from hyper? would you want to try from this branch? hyperium/http#732 otherwise i'm confused how no_std is pulling in a std crate?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be where it breaks down. The http crate working in webasm is central to this effort.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hyperium/http#749 seems pretty good... are we able to build off of that?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! We should be able to. Let's hope they merge it.

use url::Url;
Expand Down Expand Up @@ -143,7 +144,7 @@ impl<Body> AmendedRequest<Body> {
}

#[cfg(test)]
pub fn headers_vec(&self) -> Vec<(&str, &str)> {
pub fn headers_vec(&self) -> alloc::vec::Vec<(&str, &str)> {
self.headers()
// unwrap here is ok because the tests using this method should
// only use header values representable as utf-8.
Expand Down
10 changes: 6 additions & 4 deletions src/client/call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! A single request-response. No redirection or other logic.

use std::fmt;
use std::io::Write;
use std::marker::PhantomData;
use alloc::string::ToString;
use core::fmt;
use core::fmt::Write;
use core::marker::PhantomData;

use http::{HeaderName, HeaderValue, Method, Request, Response, StatusCode, Version};

Expand Down Expand Up @@ -643,7 +644,8 @@ impl fmt::Debug for Phase {
mod test {
use super::*;

use std::str;
use alloc::vec;
use core::str;

use http::{Method, Request};

Expand Down
11 changes: 6 additions & 5 deletions src/client/flow.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! A sequence of calls, such as following redirects.

use std::fmt;
use std::marker::PhantomData;
use alloc::string::String;
use alloc::string::ToString;
use core::fmt;
use core::marker::PhantomData;

use http::uri::Scheme;
use http::{
HeaderMap, HeaderName, HeaderValue, Method, Request, Response, StatusCode, Uri, Version,
};
use http::{HeaderMap, HeaderName, HeaderValue, Method, Request};
use http::{Response, StatusCode, Uri, Version};

use crate::body::calculate_max_input;
use crate::ext::{HeaderIterExt, MethodExt, StatusExt};
Expand Down
2 changes: 1 addition & 1 deletion src/client/holder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

use http::Request;

Expand Down
2 changes: 1 addition & 1 deletion src/client/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ trait TestSliceExt {

impl TestSliceExt for [u8] {
fn as_str(&self) -> &str {
std::str::from_utf8(self).unwrap()
core::str::from_utf8(self).unwrap()
}
}
28 changes: 22 additions & 6 deletions src/client/test/scenario.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use std::io::Write;
use std::marker::PhantomData;
use core::fmt::Write;
// use std::io::Write;
use core::marker::PhantomData;

// use std::prelude::rust_2021::*;
// use std::vec;

use alloc::string::String;
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;

use http::{Method, Request, Response, StatusCode};

Expand All @@ -8,6 +17,7 @@ use crate::client::flow::state::{
};
use crate::client::flow::{Await100Result, Flow, SendRequestResult};
use crate::client::flow::{RecvBodyResult, RecvResponseResult};
use crate::util::Writer;

pub struct Scenario {
request: Request<()>,
Expand Down Expand Up @@ -176,12 +186,13 @@ impl Scenario {
}

pub fn write_response(r: &Response<()>) -> Vec<u8> {
let mut input = Vec::<u8>::new();
let mut input = vec![0; 1024];
let mut w = Writer::new(&mut input);

let s = r.status();

write!(
&mut input,
&mut w,
"{:?} {} {}\r\n",
r.version(),
s.as_u16(),
Expand All @@ -190,10 +201,15 @@ pub fn write_response(r: &Response<()>) -> Vec<u8> {
.unwrap();

for (k, v) in r.headers().iter() {
write!(&mut input, "{}: {}\r\n", k.as_str(), v.to_str().unwrap()).unwrap();
write!(&mut w, "{}: {}\r\n", k.as_str(), v.to_str().unwrap()).unwrap();
}

write!(&mut input, "\r\n").unwrap();
write!(&mut w, "\r\n").unwrap();

let len = w.len();

drop(w);
input.truncate(len);

input
}
Expand Down
2 changes: 2 additions & 0 deletions src/client/test/state_recv_body.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec;

use http::Response;

use crate::client::flow::CloseReason;
Expand Down
3 changes: 3 additions & 0 deletions src/client/test/state_redirect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use alloc::string::ToString;
use alloc::vec;

use http::{Method, Response, StatusCode};

use crate::client::flow::RedirectAuthHeaders;
Expand Down
2 changes: 2 additions & 0 deletions src/client/test/state_send_body.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec;

use crate::client::flow::SendRequestResult;

use super::scenario::Scenario;
Expand Down
2 changes: 2 additions & 0 deletions src/client/test/state_send_request.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec;

use crate::client::flow::SendRequestResult;
use crate::Error;

Expand Down
9 changes: 8 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::fmt;
use alloc::string::String;
use alloc::string::ToString;
use core::fmt;

use http::{Method, Version};

Expand Down Expand Up @@ -42,8 +44,13 @@ impl From<httparse::Error> for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

// TODO(martin): uncomment this when we bump MSRV >= 1.81
// #[cfg(not(feature = "std"))]
// impl core::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
//!
//!

#![no_std]
#![forbid(unsafe_code)]
#![warn(clippy::all)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::needless_lifetimes)]
#![deny(missing_docs)]

extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

#[macro_use]
extern crate log;

Expand Down
57 changes: 37 additions & 20 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;
use std::io::{self, Cursor};
use std::ops::{Deref, DerefMut};
use core::fmt;
use core::ops::{Deref, DerefMut};

pub(crate) fn find_crlf(b: &[u8]) -> Option<usize> {
let cr = b.iter().position(|c| *c == b'\r')?;
Expand Down Expand Up @@ -30,38 +29,56 @@ pub(crate) fn compare_lowercase_ascii(a: &str, lowercased: &str) -> bool {
true
}

pub(crate) struct Writer<'a>(pub Cursor<&'a mut [u8]>);
pub(crate) struct Writer<'a>(&'a mut [u8], usize);

impl<'a> Writer<'a> {
pub(crate) fn new(output: &'a mut [u8]) -> Writer<'a> {
Self(Cursor::new(output))
Self(output, 0)
}

pub fn len(&self) -> usize {
self.0.position() as usize
#[inline(always)]
pub(crate) fn len(&self) -> usize {
self.1
}

pub fn available(&self) -> usize {
self.0.get_ref().len() - self.len()
#[inline(always)]
pub(crate) fn available(&self) -> usize {
self.0.len() - self.1
}

pub(crate) fn try_write(&mut self, block: impl Fn(&mut Self) -> io::Result<()>) -> bool {
let pos = self.0.position();
pub(crate) fn try_write(
&mut self,
block: impl Fn(&mut Self) -> Result<(), fmt::Error>,
) -> bool {
let pos = self.1;
let success = (block)(self).is_ok();
if !success {
self.0.set_position(pos);
self.1 = pos;
}
success
}
}

impl<'a> io::Write for Writer<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
#[inline(always)]
pub(crate) fn write_all(&mut self, to_write: &[u8]) -> Result<(), fmt::Error> {
let len = to_write.len();

if len > self.available() {
return Err(fmt::Error);
}

let buf = &mut self.0[self.1..];
buf[..len].copy_from_slice(to_write);

self.1 += len;

Ok(())
}
}

fn flush(&mut self) -> io::Result<()> {
self.0.flush()
impl<'a> fmt::Write for Writer<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();
self.write_all(bytes)
}
}

Expand All @@ -70,7 +87,7 @@ const CHARS_PER_ROW: usize = 16;
impl<'a> Drop for Writer<'a> {
fn drop(&mut self) {
let len = self.len();
log_data(&self.0.get_ref()[..len]);
log_data(&self.0[..len]);
}
}

Expand Down Expand Up @@ -156,7 +173,7 @@ impl<T, const N: usize> ArrayVec<T, N> {
pub fn from_fn(cb: impl FnMut(usize) -> T) -> Self {
Self {
len: 0,
arr: std::array::from_fn(cb),
arr: core::array::from_fn(cb),
}
}

Expand Down
Loading