Skip to content

Commit

Permalink
fix(http1): remove panic for HTTP upgrades that have been ignored (#2115
Browse files Browse the repository at this point in the history
)

Closes #2114
  • Loading branch information
seanmonstar authored Jan 24, 2020
1 parent ba2a144 commit 1881db6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,6 @@ impl State {
}

fn prepare_upgrade(&mut self) -> crate::upgrade::OnUpgrade {
debug_assert!(self.upgrade.is_none());
let (tx, rx) = crate::upgrade::pending();
self.upgrade = Some(tx);
rx
Expand Down
40 changes: 40 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,46 @@ async fn upgrades_new() {
assert_eq!(s(&vec), "bar=foo");
}

#[tokio::test]
async fn upgrades_ignored() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();

tokio::spawn(async move {
let svc = service_fn(move |req: Request<Body>| {
assert_eq!(req.headers()["upgrade"], "yolo");
future::ok::<_, hyper::Error>(Response::new(hyper::Body::empty()))
});

let (socket, _) = listener.accept().await.unwrap();
Http::new()
.serve_connection(socket, svc)
.with_upgrades()
.await
.expect("server task");
});

let client = hyper::Client::new();
let url = format!("http://{}/", addr);

let make_req = || {
hyper::Request::builder()
.uri(&*url)
.header("upgrade", "yolo")
.header("connection", "upgrade")
.body(hyper::Body::empty())
.expect("make_req")
};

let res1 = client.request(make_req()).await.expect("req 1");
assert_eq!(res1.status(), 200);
drop(res1);

let res2 = client.request(make_req()).await.expect("req 2");
assert_eq!(res2.status(), 200);
}

#[tokio::test]
async fn http_connect_new() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
Expand Down

0 comments on commit 1881db6

Please sign in to comment.