mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-02-09 01:59:16 +08:00
continue http proxy only if keep-alive
This commit is contained in:
@@ -29,6 +29,7 @@ use std::fmt;
|
||||
|
||||
use hyper::uri::RequestUri;
|
||||
use hyper::header::{Header, HeaderFormat, Headers, ContentLength};
|
||||
use hyper::header::{Connection, ConnectionOption};
|
||||
use hyper::status::StatusCode;
|
||||
use hyper::version::HttpVersion;
|
||||
use hyper::method::Method;
|
||||
@@ -573,4 +574,20 @@ pub fn proxy_response<R, W>((r, w): (R, W),
|
||||
}
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
||||
pub fn should_keep_alive(req: &HttpRequest) -> bool {
|
||||
let default_keep_alive = req.version >= HttpVersion::Http11;
|
||||
match req.headers.get::<Connection>() {
|
||||
Some(conn) => {
|
||||
for opt in conn.iter() {
|
||||
if let &ConnectionOption::KeepAlive = opt {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
default_keep_alive
|
||||
}
|
||||
None => default_keep_alive,
|
||||
}
|
||||
}
|
||||
@@ -264,6 +264,8 @@ impl HttpRelayServer {
|
||||
HttpRequestFut::with_buf(r, req_remains)
|
||||
.and_then(move |(r, req, req_remains)| {
|
||||
let svr_addr = svr_cfg.addr.clone();
|
||||
let should_keep_alive = http::should_keep_alive(&req);
|
||||
|
||||
http::proxy_request((r, svr_w), client_addr, req, req_remains)
|
||||
.and_then(move |(r, svr_w, req_remains)| {
|
||||
HttpResponseFut::with_buf(svr_r, rsp_remains)
|
||||
@@ -273,11 +275,15 @@ impl HttpRelayServer {
|
||||
.map(move |(svr_r, w, rsp_remains)| (r, w, svr_r, svr_w, req_remains, rsp_remains))
|
||||
})
|
||||
.and_then(move |(r, w, svr_r, svr_w, req_remains, rsp_remains)| {
|
||||
HttpRelayServer::handle_http_again((r, w),
|
||||
(svr_r, svr_w),
|
||||
client_addr_cloned,
|
||||
(req_remains, rsp_remains),
|
||||
svr_cfg)
|
||||
if should_keep_alive {
|
||||
HttpRelayServer::handle_http_again((r, w),
|
||||
(svr_r, svr_w),
|
||||
client_addr_cloned,
|
||||
(req_remains, rsp_remains),
|
||||
svr_cfg)
|
||||
} else {
|
||||
futures::finished(()).boxed()
|
||||
}
|
||||
})
|
||||
})
|
||||
.boxed()
|
||||
@@ -292,6 +298,7 @@ impl HttpRelayServer {
|
||||
svr_cfg: Arc<ServerConfig>)
|
||||
-> BoxFuture<(), io::Error> {
|
||||
let client_addr_cloned = client_addr.clone();
|
||||
let should_keep_alive = http::should_keep_alive(&req);
|
||||
|
||||
super::connect_proxy_server(&handle, svr_cfg.clone(), addr)
|
||||
.and_then(move |(svr_r, svr_w)| {
|
||||
@@ -306,11 +313,15 @@ impl HttpRelayServer {
|
||||
.map(move |(svr_r, w, rsp_remains)| (r, w, svr_r, svr_w, req_remains, rsp_remains))
|
||||
})
|
||||
.and_then(move |(r, w, svr_r, svr_w, req_remains, rsp_remains)| {
|
||||
HttpRelayServer::handle_http_again((r, w),
|
||||
(svr_r, svr_w),
|
||||
client_addr_cloned,
|
||||
(req_remains, rsp_remains),
|
||||
svr_cfg)
|
||||
if should_keep_alive {
|
||||
HttpRelayServer::handle_http_again((r, w),
|
||||
(svr_r, svr_w),
|
||||
client_addr_cloned,
|
||||
(req_remains, rsp_remains),
|
||||
svr_cfg)
|
||||
} else {
|
||||
futures::finished(()).boxed()
|
||||
}
|
||||
})
|
||||
})
|
||||
.boxed()
|
||||
|
||||
Reference in New Issue
Block a user