1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

Don't stream body but wait for it to complete and only send it then

This commit is contained in:
Sven-Hendrik Haase 2019-11-15 19:47:18 +01:00
parent fb5779fe8f
commit 7ff7accd79

View File

@ -1,6 +1,7 @@
use actix_web::client::Client; use actix_web::client::Client;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use clap::{value_t, Arg}; use clap::{value_t, Arg};
use futures::stream::Stream;
use futures::Future; use futures::Future;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use url::Url; use url::Url;
@ -15,6 +16,8 @@ fn forward(
new_url.set_path(req.uri().path()); new_url.set_path(req.uri().path());
new_url.set_query(req.uri().query()); new_url.set_query(req.uri().query());
// TODO: This forwarded implementation is incomplete as it only handles the inofficial
// X-Forwarded-For header but not the official Forwarded one.
let forwarded_req = client let forwarded_req = client
.request_from(new_url.as_str(), req.head()) .request_from(new_url.as_str(), req.head())
.no_decompress(); .no_decompress();
@ -27,19 +30,22 @@ fn forward(
forwarded_req forwarded_req
.send_body(body) .send_body(body)
.map_err(Error::from) .map_err(Error::from)
.map(|res| { .map(|mut res| {
let mut client_resp = HttpResponse::build(res.status()); let mut client_resp = HttpResponse::build(res.status());
// Remove `Connection` as per // Remove `Connection` as per
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#Directives // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#Directives
for (header_name, header_value) in res for (header_name, header_value) in
.headers() res.headers().iter().filter(|(h, _)| *h != "connection")
.iter()
.filter(|(h, _)| *h != "connection")
{ {
client_resp.header(header_name.clone(), header_value.clone()); client_resp.header(header_name.clone(), header_value.clone());
} }
client_resp.streaming(res) res.body()
.into_stream()
.concat2()
.map(move |b| client_resp.body(b))
.map_err(|e| e.into())
}) })
.flatten()
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {