@@ -1,6 +1,5 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
use futures_util::TryStreamExt;
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
use hyper::{Body, Method, Request, Response, Server, StatusCode};
|
||||
|
||||
@@ -16,16 +15,17 @@ async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
|
||||
// Simply echo the body back to the client.
|
||||
(&Method::POST, "/echo") => Ok(Response::new(req.into_body())),
|
||||
|
||||
// TODO: Fix this, broken in PR #2896
|
||||
// Convert to uppercase before sending back to client using a stream.
|
||||
(&Method::POST, "/echo/uppercase") => {
|
||||
let chunk_stream = req.into_body().map_ok(|chunk| {
|
||||
chunk
|
||||
.iter()
|
||||
.map(|byte| byte.to_ascii_uppercase())
|
||||
.collect::<Vec<u8>>()
|
||||
});
|
||||
Ok(Response::new(Body::wrap_stream(chunk_stream)))
|
||||
}
|
||||
// (&Method::POST, "/echo/uppercase") => {
|
||||
// let chunk_stream = req.into_body().map_ok(|chunk| {
|
||||
// chunk
|
||||
// .iter()
|
||||
// .map(|byte| byte.to_ascii_uppercase())
|
||||
// .collect::<Vec<u8>>()
|
||||
// });
|
||||
// Ok(Response::new(Body::wrap_stream(chunk_stream)))
|
||||
// }
|
||||
|
||||
// Reverse the entire body before sending back to the client.
|
||||
//
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
use tokio::fs::File;
|
||||
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
use hyper::{Body, Method, Request, Response, Result, Server, StatusCode};
|
||||
|
||||
@@ -48,11 +44,8 @@ fn not_found() -> Response<Body> {
|
||||
}
|
||||
|
||||
async fn simple_file_send(filename: &str) -> Result<Response<Body>> {
|
||||
// Serve a file by asynchronously reading it by chunks using tokio-util crate.
|
||||
|
||||
if let Ok(file) = File::open(filename).await {
|
||||
let stream = FramedRead::new(file, BytesCodec::new());
|
||||
let body = Body::wrap_stream(stream);
|
||||
if let Ok(contents) = tokio::fs::read(filename).await {
|
||||
let body = contents.into();
|
||||
return Ok(Response::new(body));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
use bytes::Buf;
|
||||
use futures_util::{stream, StreamExt};
|
||||
use hyper::client::HttpConnector;
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
use hyper::{header, Body, Client, Method, Request, Response, Server, StatusCode};
|
||||
@@ -24,18 +23,10 @@ async fn client_request_response(client: &Client<HttpConnector>) -> Result<Respo
|
||||
.unwrap();
|
||||
|
||||
let web_res = client.request(req).await?;
|
||||
// Compare the JSON we sent (before) with what we received (after):
|
||||
let before = stream::once(async {
|
||||
Ok(format!(
|
||||
"<b>POST request body</b>: {}<br><b>Response</b>: ",
|
||||
POST_DATA,
|
||||
)
|
||||
.into())
|
||||
});
|
||||
let after = web_res.into_body();
|
||||
let body = Body::wrap_stream(before.chain(after));
|
||||
|
||||
Ok(Response::new(body))
|
||||
let res_body = web_res.into_body();
|
||||
|
||||
Ok(Response::new(res_body))
|
||||
}
|
||||
|
||||
async fn api_post_response(req: Request<Body>) -> Result<Response<Body>> {
|
||||
|
||||
Reference in New Issue
Block a user