-
Hi! |
Beta Was this translation helpful? Give feedback.
Answered by
fafhrd91
Aug 2, 2022
Replies: 2 comments 3 replies
-
openssl needs configuration, here is example |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
kizeevov
-
@fafhrd91 example is working. use ntex::{
connect::{Address, Connect, ConnectError, Connector as BaseConnector},
io::{types, Base, Filter, FilterFactory, Io, IoRef, ReadStatus, WriteStatus},
util::{BytesVec, Ready},
Service, ServiceFactory,
};
use std::{
any::{Any, TypeId},
future::Future,
pin::Pin,
task::{Context, Poll},
};
pub struct Http2Filter<F = Base> {
inner: F,
}
impl<F: Filter> Filter for Http2Filter<F> {
fn query(&self, id: TypeId) -> Option<Box<dyn Any>> {
if id == TypeId::of::<types::HttpProtocol>() {
Some(Box::new(types::HttpProtocol::Http2))
} else {
self.inner.query(id)
}
}
fn get_read_buf(&self) -> Option<BytesVec> {
self.inner.get_read_buf()
}
fn release_read_buf(&self, buf: BytesVec) {
self.inner.release_read_buf(buf)
}
fn process_read_buf(&self, io: &IoRef, n: usize) -> std::io::Result<(usize, usize)> {
self.inner.process_read_buf(io, n)
}
fn get_write_buf(&self) -> Option<BytesVec> {
self.inner.get_write_buf()
}
fn release_write_buf(&self, buf: BytesVec) -> std::io::Result<()> {
self.inner.release_write_buf(buf)
}
fn poll_read_ready(&self, cx: &mut Context<'_>) -> Poll<ReadStatus> {
self.inner.poll_read_ready(cx)
}
fn poll_write_ready(&self, cx: &mut Context<'_>) -> Poll<WriteStatus> {
self.inner.poll_write_ready(cx)
}
fn poll_shutdown(&self) -> Poll<std::io::Result<()>> {
self.inner.poll_shutdown()
}
}
pub struct Http2FilterFactory;
impl Http2FilterFactory {
pub fn new() -> Self {
Http2FilterFactory {}
}
}
impl<F: Filter> FilterFactory<F> for Http2FilterFactory {
type Filter = Http2Filter<F>;
type Error = std::io::Error;
type Future = Ready<Io<Self::Filter>, Self::Error>;
fn create(self, st: Io<F>) -> Self::Future {
Ready::from(st.map_filter(|inner: F| Ok(Http2Filter { inner })))
}
}
pub struct Http2Connector<T> {
connector: BaseConnector<T>,
}
impl<T> Http2Connector<T> {
pub fn new() -> Self {
Http2Connector {
connector: BaseConnector::default(),
}
}
}
impl<T> Clone for Http2Connector<T> {
fn clone(&self) -> Self {
Http2Connector {
connector: self.connector.clone(),
}
}
}
impl<T: Address + 'static> Http2Connector<T> {
pub fn connect<U>(
&self,
message: U,
) -> impl Future<Output = Result<Io<Http2Filter<Base>>, ConnectError>>
where
Connect<T>: From<U>,
{
let message = Connect::from(message);
let conn = self.connector.call(message);
async move {
let io = conn.await?;
io.add_filter(Http2FilterFactory::new())
.await
.map_err(Into::into)
}
}
}
impl<T: Address, C> ServiceFactory<Connect<T>, C> for Http2Connector<T> {
type Response = Io<Http2Filter<Base>>;
type Error = ConnectError;
type Service = Http2Connector<T>;
type InitError = ();
type Future = Ready<Self::Service, Self::InitError>;
#[inline]
fn new_service(&self, _: C) -> Self::Future {
Ready::Ok(self.clone())
}
}
impl<T: Address> Service<Connect<T>> for Http2Connector<T> {
type Response = Io<Http2Filter<Base>>;
type Error = ConnectError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
#[inline]
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
#[inline]
fn call(&self, req: Connect<T>) -> Self::Future {
Box::pin(self.connect(req))
}
} Use: let connector = ntex::http::client::Connector::new()
.connector(Http2Connector::new())
.finish();
let client = ntex::http::client::Client::build()
.connector(connector)
.finish(); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
openssl needs configuration, here is example