Skip to content

Commit

Permalink
Merge pull request #198 from c410-f3r/misc
Browse files Browse the repository at this point in the history
Fix SimplePool
  • Loading branch information
c410-f3r authored Aug 1, 2024
2 parents 93e7b5c + 0b98a58 commit 7e86293
Show file tree
Hide file tree
Showing 43 changed files with 598 additions and 306 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
- run: .scripts/integration-tests.sh


internal-tests0:
internal-tests-0:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -86,9 +86,9 @@ jobs:
profile: minimal
toolchain: nightly-2024-07-10
- uses: Swatinem/rust-cache@v2
- run: .scripts/internal-tests0.sh
- run: .scripts/internal-tests-0.sh

internal-tests1:
internal-tests-1:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -103,4 +103,4 @@ jobs:
profile: minimal
toolchain: nightly-2024-07-10
- uses: Swatinem/rust-cache@v2
- run: .scripts/internal-tests1.sh
- run: .scripts/internal-tests-1.sh
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion .scripts/internal-tests-all.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

$(dirname "$0")/internal-tests0.sh && $(dirname "$0")/internal-tests1.sh
$(dirname "$0")/internal-tests-0.sh && $(dirname "$0")/internal-tests-1.sh
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
[profile.deploy]
codegen-units = 1
debug = false
debug-assertions = false
incremental = false
inherits = "release"
lto = true
opt-level = 3
overflow-checks = false
panic = 'abort'
rpath = false
strip = "symbols"

[profile.profiling]
inherits = "release"
debug = true
Expand Down
22 changes: 6 additions & 16 deletions wtx-docs/src/http-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@ High-level pool of HTTP clients that currently only supports HTTP/2. Allows mult

Activation feature is called `http-client`.

```rust,edition2021
```rust,edition2021,no_run
extern crate wtx;
use wtx::{
http::{Client, Method, ReqResBuffer, ReqUri},
misc::{from_utf8_basic, TokioRustlsConnector},
};
use wtx::{http::{Client, ReqBuilder}, misc::{Uri, from_utf8_basic}};
pub(crate) async fn get_and_print() -> wtx::Result<()> {
let client = Client::tokio(1, |uri| async move {
Ok(
TokioRustlsConnector::from_webpki_roots()
.with_tcp_stream(uri.host(), uri.hostname())
.await?,
)
});
let mut rrb = ReqResBuffer::default();
rrb.set_uri_from_str("https:://www.bing.com:443").unwrap();
let res = client.send(Method::Get, rrb).await.unwrap();
async fn get_and_print() -> wtx::Result<()> {
let res = ReqBuilder::get()
.send(&Client::tokio_rustls(1).build(), &Uri::new("https:://www.dukcduckgo.com:443"))
.await?;
println!("{}", from_utf8_basic(res.rrd.body()).unwrap());
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions wtx-docs/src/http2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate tokio;
extern crate wtx;
use wtx::{
http::{Method, Request, ReqResBuffer, ReqUri},
http::{Method, Request, ReqResBuffer},
http2::{Http2Buffer, Http2ErrorCode, Http2Params, Http2Tokio},
misc::{from_utf8_basic, UriRef},
rng::StaticRng,
Expand All @@ -31,7 +31,7 @@ async fn client() {
let mut rrb = ReqResBuffer::default();
let mut stream = http2.stream().await.unwrap();
stream
.send_req(Request::http2(Method::Get, b"Hello!"), ReqUri::Param(&uri))
.send_req(Request::http2(Method::Get, b"Hello!"), &uri)
.await
.unwrap();
let res = stream.recv_res(rrb).await.unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl SirAuxItemValues {
/// Delegates to a new builder structure.
///
/// This function must be called in order to continue constructing a package.
pub fn #fn_name_ident<#(#fn_lfs,)* #(#fn_tys,)*>(
pub fn #fn_name_ident<#(#fn_lfs,)* #(#fn_tys,)*>(
#fn_this,
#fn_args
) -> #fn_ret_wrapper_segments_idents_first #(::#fn_ret_wrapper_segments_idents)* #fn_ret_angle_bracket_left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl SirAuxItemValues {
let fpiv_params_iter = fpiv_params.iter();
let method = quote::quote!(
/// Final building method that creates a package with all the necessary values.
pub fn #fn_ident(self) -> #pkg_ident<
pub fn #fn_ident(self) -> #pkg_ident<
#(#fpiv_params_iter,)*
wtx::client_api_framework::data_format::#wrapper_ident<#freqdiv_ident<#freqdiv_params>>
> {
Expand Down
13 changes: 6 additions & 7 deletions wtx-ui/src/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use wtx::{
http::{Client, Method, ReqResBuffer},
misc::{from_utf8_basic, TokioRustlsConnector},
misc::{from_utf8_basic, Uri},
};

pub(crate) async fn http_client(uri: String) -> wtx::Result<()> {
let client = Client::tokio(1, |uri| async move {
TokioRustlsConnector::from_webpki_roots().with_tcp_stream(uri.host(), uri.hostname()).await
});
let mut rrb = ReqResBuffer::default();
rrb.set_uri_from_str(&uri).unwrap();
let res = client.send(Method::Get, rrb).await.unwrap();
let res = Client::tokio_rustls(1)
.build()
.send(Method::Get, ReqResBuffer::default(), &Uri::new(uri).to_ref())
.await
.unwrap();
println!("{}", from_utf8_basic(res.rrd.body()).unwrap());
Ok(())
}
2 changes: 1 addition & 1 deletion wtx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ simdutf8 = ["dep:simdutf8"]
smol = ["dep:smol", "std"]
smoltcp = ["dep:smoltcp"]
std = ["ahash?/std", "atoi?/std", "cl-aux?/std", "embassy-sync?/std", "memchr?/std", "miniserde?/std", "serde?/std", "serde_json?/std", "simdutf8?/std"]
test-strategy = ["dep:test-strategy", "proptest"]
test-strategy = ["dep:test-strategy", "proptest", "std"]
tokio = ["std", "dep:tokio"]
tokio-rustls = ["ring", "rustls-pki-types", "tokio", "dep:tokio-rustls"]
tracing = ["dep:tracing"]
Expand Down
5 changes: 3 additions & 2 deletions wtx/examples/http2-client-tokio-rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod common;

use wtx::{
http::{Method, ReqResBuffer, ReqUri, Request},
http::{Method, ReqResBuffer, Request},
http2::{Http2Buffer, Http2ErrorCode, Http2Params, Http2Tokio},
misc::{from_utf8_basic, TokioRustlsConnector, UriString},
rng::StaticRng,
Expand All @@ -17,14 +17,15 @@ async fn main() {
Http2Buffer::new(StaticRng::default()),
Http2Params::default(),
TokioRustlsConnector::from_webpki_roots()
.http2()
.with_tcp_stream(uri.host(), uri.hostname())
.await
.unwrap(),
)
.await
.unwrap();
let mut stream = http2.stream().await.unwrap();
stream.send_req(Request::http2(Method::Get, &uri), ReqUri::Data).await.unwrap();
stream.send_req(Request::http2(Method::Get, ()), &uri.to_ref()).await.unwrap();
let rrb = ReqResBuffer::default();
let (res_rrb, _status_code) = stream.recv_res(rrb).await.unwrap();
println!("{}", from_utf8_basic(res_rrb.body()).unwrap());
Expand Down
110 changes: 87 additions & 23 deletions wtx/src/client_api_framework/network/transport/wtx_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@ use crate::{
misc::{manage_after_sending_related, manage_before_sending_related},
network::{
transport::{Transport, TransportParams},
HttpParams, TransportGroup,
HttpParams, HttpReqParams, TransportGroup,
},
pkg::{Package, PkgsAux},
Api,
},
http::{Client, Header, Headers, KnownHeaderName, ReqResBuffer},
http2::{Http2, Http2Buffer, Http2Data},
misc::{Lock, RefCounter, Stream, Vector},
pool::SimplePoolResource,
pool::{ResourceManager, SimplePoolResource},
};
use core::{mem, ops::Range};

impl<DRSR, HD, RL, S, SF> Transport<DRSR> for Client<HD, RL, SF>
impl<DRSR, HD, RL, RM, S> Transport<DRSR> for Client<RL, RM>
where
HD: RefCounter + 'static,
HD::Item: Lock<Resource = Http2Data<Http2Buffer<ReqResBuffer>, ReqResBuffer, S, true>>,
RL: Lock<Resource = SimplePoolResource<Http2<HD, true>>> + 'static,
RL: Lock<Resource = SimplePoolResource<RM::Resource>>,
RM: ResourceManager<
CreateAux = str,
Error = crate::Error,
RecycleAux = str,
Resource = Http2<HD, true>,
>,
S: Stream,
SF: Future<Output = crate::Result<S>> + 'static,
for<'any> RL: 'any,
for<'any> RM: 'any,
{
const GROUP: TransportGroup = TransportGroup::HTTP;
type Params = HttpParams;
Expand Down Expand Up @@ -55,8 +62,55 @@ where
}
}

async fn response<A, DRSR, HD, P, RL, S, SF>(
client: &mut Client<HD, RL, SF>,
impl<DRSR, HD, RL, RM, S> Transport<DRSR> for &Client<RL, RM>
where
HD: RefCounter + 'static,
HD::Item: Lock<Resource = Http2Data<Http2Buffer<ReqResBuffer>, ReqResBuffer, S, true>>,
RL: Lock<Resource = SimplePoolResource<RM::Resource>>,
RM: ResourceManager<
CreateAux = str,
Error = crate::Error,
RecycleAux = str,
Resource = Http2<HD, true>,
>,
S: Stream,
for<'any> RL: 'any,
for<'any> RM: 'any,
{
const GROUP: TransportGroup = TransportGroup::HTTP;
type Params = HttpParams;

#[inline]
async fn send<A, P>(
&mut self,
pkg: &mut P,
pkgs_aux: &mut PkgsAux<A, DRSR, HttpParams>,
) -> Result<(), A::Error>
where
A: Api,
P: Package<A, DRSR, HttpParams>,
{
response(self, pkg, pkgs_aux).await?;
Ok(())
}

#[inline]
async fn send_recv<A, P>(
&mut self,
pkg: &mut P,
pkgs_aux: &mut PkgsAux<A, DRSR, HttpParams>,
) -> Result<Range<usize>, A::Error>
where
A: Api,
P: Package<A, DRSR, HttpParams>,
{
response(self, pkg, pkgs_aux).await?;
Ok(0..pkgs_aux.byte_buffer.len())
}
}

async fn response<A, DRSR, HD, P, RL, RM, S>(
client: &Client<RL, RM>,
pkg: &mut P,
pkgs_aux: &mut PkgsAux<A, DRSR, HttpParams>,
) -> Result<(), A::Error>
Expand All @@ -65,24 +119,34 @@ where
P: Package<A, DRSR, HttpParams>,
HD: RefCounter + 'static,
HD::Item: Lock<Resource = Http2Data<Http2Buffer<ReqResBuffer>, ReqResBuffer, S, true>>,
RL: Lock<Resource = SimplePoolResource<Http2<HD, true>>> + 'static,
RL: Lock<Resource = SimplePoolResource<RM::Resource>>,
RM: ResourceManager<
CreateAux = str,
Error = crate::Error,
RecycleAux = str,
Resource = Http2<HD, true>,
>,
S: Stream,
SF: Future<Output = crate::Result<S>> + 'static,
for<'any> RL: 'any,
for<'any> RM: 'any,
{
manage_before_sending_related(pkg, pkgs_aux, &mut *client).await?;
if let Some(mime) = pkgs_aux.tp.ext_req_params().mime {
pkgs_aux.tp.ext_req_params_mut().headers.push_front(
pkgs_aux.byte_buffer.clear();
pkgs_aux.tp.ext_req_params_mut().headers.clear();
manage_before_sending_related(pkg, pkgs_aux, &*client).await?;
let HttpReqParams { headers, method, mime, uri, user_agent } = pkgs_aux.tp.ext_req_params_mut();
if let Some(elem) = mime {
headers.push_front(
Header {
is_sensitive: false,
is_trailer: false,
name: KnownHeaderName::ContentType.into(),
value: mime._as_str().as_bytes(),
value: elem._as_str().as_bytes(),
},
&[],
)?;
}
if let Some(elem) = pkgs_aux.tp.ext_req_params().user_agent {
pkgs_aux.tp.ext_req_params_mut().headers.push_front(
if let Some(elem) = user_agent {
headers.push_front(
Header {
is_sensitive: false,
is_trailer: false,
Expand All @@ -92,18 +156,18 @@ where
&[],
)?;
}
let (mut rrb_data, mut rrb_headers) = (Vector::new(), Headers::new(0));
{
let rrb = {
let (mut rrb_data, mut rrb_headers) = (Vector::new(), Headers::new(0));
mem::swap(&mut rrb_data, &mut pkgs_aux.byte_buffer);
mem::swap(&mut rrb_headers, &mut pkgs_aux.tp.ext_req_params_mut().headers);
}
let rrb = ReqResBuffer::new(rrb_data, rrb_headers);
let res = (*client).send(pkgs_aux.tp.ext_req_params().method, rrb).await?;
pkgs_aux.tp.ext_res_params_mut().status_code = res.status_code;
mem::swap(&mut rrb_headers, headers);
ReqResBuffer::new(rrb_data, rrb_headers)
};
{
let res = (*client).send(*method, rrb, &uri.to_ref()).await?;
let (mut rrb_data, mut rrb_headers) = res.rrd.into_parts();
mem::swap(&mut pkgs_aux.byte_buffer, &mut rrb_data);
mem::swap(&mut pkgs_aux.tp.ext_req_params_mut().headers, &mut rrb_headers);
mem::swap(headers, &mut rrb_headers);
pkgs_aux.tp.ext_res_params_mut().status_code = res.status_code;
}
manage_after_sending_related(pkg, pkgs_aux).await?;
pkgs_aux.tp.reset();
Expand Down
2 changes: 1 addition & 1 deletion wtx/src/client_api_framework/pkg/batch_pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ where
if pkgs.get(idx).map(|pkg| *pkg.ext_req_content().borrow() == eresc_id).unwrap_or_default() {
return Ok(idx);
}
pkgs.binary_search_by(|req| req.ext_req_content().borrow().cmp(&eresc_id)).ok().ok_or(
pkgs.binary_search_by(|req| req.ext_req_content().borrow().cmp(&&eresc_id)).ok().ok_or(
ClientApiFrameworkError::ResponseIdIsNotPresentInTheOfSentBatchPackages(eresc_id).into(),
)
}
Expand Down
2 changes: 1 addition & 1 deletion wtx/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) const _MAX_SCHEME_LEN: usize = 16;
/// Maximum number of bytes for the name of a header.
pub const MAX_HEADER_NAME_LEN: usize = 128;
/// Maximum number of bytes for the value of a header.
pub const MAX_HEADER_VALUE_LEN: usize = 1024 + 256;
pub const MAX_HEADER_VALUE_LEN: usize = 1024 + 1024;

pub(crate) type _HeaderNameBuffer = crate::misc::ArrayVector<u8, MAX_HEADER_NAME_LEN>;
pub(crate) type _HeaderValueBuffer = crate::misc::ArrayVector<u8, MAX_HEADER_VALUE_LEN>;
Loading

0 comments on commit 7e86293

Please sign in to comment.