mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
Fix proxy to handle both chunked and non-chunked responses (#59)
This commit is contained in:
parent
8a81a3bb58
commit
5871328be2
@ -11,7 +11,7 @@ use actix_web::{
|
|||||||
HttpResponse,
|
HttpResponse,
|
||||||
};
|
};
|
||||||
use clap::{value_t, Arg};
|
use clap::{value_t, Arg};
|
||||||
use futures::Future;
|
use futures::{future, Future};
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
@ -55,16 +55,28 @@ fn forward(
|
|||||||
forwarded_req
|
forwarded_req
|
||||||
.send()
|
.send()
|
||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
.and_then(move |resp| {
|
.and_then(construct_response)
|
||||||
let mut client_resp = HttpResponse::build(resp.status());
|
.responder()
|
||||||
for (header_name, header_value) in
|
}
|
||||||
resp.headers().iter().filter(|(h, _)| *h != "connection")
|
|
||||||
{
|
|
||||||
client_resp.header(header_name.clone(), header_value.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(client_resp.streaming(resp.payload()))
|
fn construct_response(
|
||||||
}).responder()
|
resp: client::ClientResponse,
|
||||||
|
) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
||||||
|
let mut client_resp = HttpResponse::build(resp.status());
|
||||||
|
for (header_name, header_value) in
|
||||||
|
resp.headers().iter().filter(|(h, _)| *h != "connection")
|
||||||
|
{
|
||||||
|
client_resp.header(header_name.clone(), header_value.clone());
|
||||||
|
}
|
||||||
|
if resp.chunked().unwrap_or(false) {
|
||||||
|
Box::new(future::ok(client_resp.streaming(resp.payload())))
|
||||||
|
} else {
|
||||||
|
Box::new(
|
||||||
|
resp.body()
|
||||||
|
.from_err()
|
||||||
|
.and_then(move |body| Ok(client_resp.body(body))),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user