Skip to content

Commit

Permalink
Keep-Alive header is only works in hop
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Apr 10, 2020
1 parent 6741389 commit b6f744d
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/relay/tcprelay/http_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,28 +287,28 @@ fn clear_hop_headers(headers: &mut HeaderMap<HeaderValue>) {

for connection in headers.get_all("Connection") {
if let Ok(conn) = connection.to_str() {
if !conn.eq_ignore_ascii_case("close") {
for header in conn.split(',') {
let header = header.trim();
// close is a command instead of a header
if conn.eq_ignore_ascii_case("close") {
continue;
}

if !header.eq_ignore_ascii_case("keep-alive") {
extra_headers.push(header.to_owned());
}
}
for header in conn.split(',') {
let header = header.trim();
extra_headers.push(header.to_owned());
}
}
}

for connection in headers.get_all("Proxy-Connection") {
if let Ok(conn) = connection.to_str() {
if !conn.eq_ignore_ascii_case("close") {
for header in conn.split(',') {
let header = header.trim();
// close is a command instead of a header
if conn.eq_ignore_ascii_case("close") {
continue;
}

if !header.eq_ignore_ascii_case("keep-alive") {
extra_headers.push(header.to_owned());
}
}
for header in conn.split(',') {
let header = header.trim();
extra_headers.push(header.to_owned());
}
}
}
Expand Down Expand Up @@ -478,6 +478,8 @@ async fn server_dispatch(
client_addr: SocketAddr,
bypass_client: DirectHttpClient,
) -> io::Result<Response<Body>> {
trace!("Request {} {:?}", client_addr, req);

let context = svr_score.context();

// Parse URI
Expand Down Expand Up @@ -565,6 +567,8 @@ async fn server_dispatch(
set_conn_keep_alive(req.version(), req.headers_mut(), conn_keep_alive);

let mut res = if context.check_target_bypassed(&host).await {
trace!("Bypassed {} -> {} {:?}", client_addr, host, req);

// Keep connections in a global client instance
match bypass_client.request(req).await {
Ok(res) => res,
Expand All @@ -581,6 +585,8 @@ async fn server_dispatch(
}
}
} else {
trace!("Proxied {} -> {} {:?}", client_addr, host, req);

// Keep connections for clients in ServerScore::client
//
// client instance is kept for Keep-Alive connections
Expand All @@ -602,6 +608,8 @@ async fn server_dispatch(
}
};

trace!("Received {} <- {} {:?}", client_addr, host, res);

let res_keep_alive = conn_keep_alive && check_keep_alive(res.version(), res.headers(), false);

// Clear unforwardable headers
Expand All @@ -610,7 +618,9 @@ async fn server_dispatch(
// Set Connection header
set_conn_keep_alive(res.version(), res.headers_mut(), res_keep_alive);

debug!("HTTP {} relay {} <-> {} finished", method, client_addr, host,);
trace!("Response {} <- {} {:?}", client_addr, host, res);

debug!("HTTP {} relay {} <-> {} finished", method, client_addr, host);

Ok(res)
}
Expand Down

0 comments on commit b6f744d

Please sign in to comment.