Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always send body whole and don't use transfer encoding #186

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions http-proxy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use actix_web::client::Client;
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use clap::{value_t, Arg};
use futures::stream::Stream;
use futures::Future;
use std::net::ToSocketAddrs;
use url::Url;

fn forward(
req: HttpRequest,
payload: web::Payload,
body: web::Bytes,
url: web::Data<Url>,
client: web::Data<Client>,
) -> impl Future<Item = HttpResponse, Error = Error> {
let mut new_url = url.get_ref().clone();
new_url.set_path(req.uri().path());
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
.request_from(new_url.as_str(), req.head())
.no_decompress();
Expand All @@ -25,19 +28,24 @@ fn forward(
};

forwarded_req
.send_stream(payload)
.send_body(body)
.map_err(Error::from)
.map(|res| {
.map(|mut res| {
let mut client_resp = HttpResponse::build(res.status());
for (header_name, header_value) in res
.headers()
.iter()
.filter(|(h, _)| *h != "connection" && *h != "content-length")
// Remove `Connection` as per
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#Directives
for (header_name, header_value) in
res.headers().iter().filter(|(h, _)| *h != "connection")
{
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<()> {
Expand Down