Add client, client codegen, helloworld and routeguide client examples

This commit is contained in:
Lucio Franco
2019-08-17 23:31:32 -04:00
parent 3951bcf7fe
commit 2ebad2d778
26 changed files with 883 additions and 364 deletions
+48
View File
@@ -0,0 +1,48 @@
use http::{Request, Uri};
use std::task::{Context, Poll};
use tower_service::Service;
#[derive(Debug)]
pub struct AddOrigin<T> {
inner: T,
origin: Uri,
}
impl<T> AddOrigin<T> {
pub fn new(inner: T, origin: Uri) -> Self {
Self { inner, origin }
}
}
impl<T, ReqBody> Service<Request<ReqBody>> for AddOrigin<T>
where
T: Service<Request<ReqBody>>,
{
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
// Split the request into the head and the body.
let (mut head, body) = req.into_parts();
// Split the request URI into parts.
let mut uri: http::uri::Parts = head.uri.into();
let set_uri = self.origin.clone().into_parts();
// Update the URI parts, setting hte scheme and authority
uri.scheme = Some(set_uri.scheme.expect("expected scheme").clone());
uri.authority = Some(set_uri.authority.expect("expected authority").clone());
// Update the the request URI
head.uri = http::Uri::from_parts(uri).expect("valid uri");
let request = Request::from_parts(head, body);
self.inner.call(request)
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
use crate::{buf::SendBuf, flush::Flush, recv_body::RecvBody};
use futures_util::{future, FutureExt, TryFutureExt};
use h2::{client::SendRequest, RecvStream};
use h2::client::SendRequest;
use http::{Request, Response};
use http_body::Body;
use std::future::Future;
+1 -1
View File
@@ -96,7 +96,7 @@ where
match ready!(self.h2.poll_capacity(cx)) {
Some(Ok(0)) => {}
Some(Ok(_)) => break,
Some(Err(e)) => return panic!("error {:?}", e),
Some(Err(e)) => panic!("error {:?}", e),
None => {
debug!("connection closed early");
// The error shouldn't really matter at this
+2
View File
@@ -3,6 +3,8 @@
#[macro_use]
extern crate log;
pub mod add_origin;
mod buf;
mod client;
mod error;
-1
View File
@@ -1,5 +1,4 @@
use bytes::{Buf, Bytes, BytesMut};
use futures_core::Stream;
use futures_util::TryStreamExt;
use http_body::Body;
use std::task::{Context, Poll};