Skip to content

Commit

Permalink
fix(http/ws): support multiple options in connection header (denoland…
Browse files Browse the repository at this point in the history
…#11675)

Co-authored-by: Luca Casonato <[email protected]>
  • Loading branch information
crowlKats and lucacasonato committed Aug 13, 2021
1 parent c6e3f93 commit 0c9d6cb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
35 changes: 35 additions & 0 deletions cli/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,41 @@ fn websocketstream() {
assert!(status.success());
}

#[test]
fn websocket_server_multi_field_connection_header() {
let script = util::testdata_path()
.join("websocket_server_multi_field_connection_header_test.ts");
let root_ca = util::testdata_path().join("tls/RootCA.pem");
let mut child = util::deno_cmd()
.arg("run")
.arg("--unstable")
.arg("--allow-net")
.arg("--cert")
.arg(root_ca)
.arg(script)
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();

let stdout = child.stdout.as_mut().unwrap();
let mut buffer = [0; 5];
let read = stdout.read(&mut buffer).unwrap();
assert_eq!(read, 5);
let msg = std::str::from_utf8(&buffer).unwrap();
assert_eq!(msg, "READY");

let req = http::request::Builder::new()
.header(http::header::CONNECTION, "keep-alive, Upgrade")
.uri("ws:https://localhost:4319")
.body(())
.unwrap();
assert!(
deno_runtime::deno_websocket::tokio_tungstenite::tungstenite::connect(req)
.is_ok()
);
assert!(child.wait().unwrap().success());
}

#[cfg(not(windows))]
#[test]
fn set_raw_should_not_panic_on_no_tty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { deferred } from "../unit/test_util.ts";

const promise = deferred();
const listener = Deno.listen({ port: 4319 });
console.log("READY");
const conn = await listener.accept();
const httpConn = Deno.serveHttp(conn);
const { request, respondWith } = (await httpConn.nextRequest())!;
const {
response,
socket,
} = Deno.upgradeWebSocket(request);
socket.onerror = () => Deno.exit(1);
socket.onopen = () => socket.close();
socket.onclose = () => promise.resolve();
await respondWith(response);
await promise;
22 changes: 13 additions & 9 deletions ext/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,22 @@ async fn op_http_request_next(

let is_websocket_request = req
.headers()
.get(hyper::header::CONNECTION)
.and_then(|v| {
v.to_str().ok().map(|s| "Upgrade".eq_ignore_ascii_case(s))
.get_all(hyper::header::CONNECTION)
.iter()
.any(|v| {
v.to_str()
.map(|s| s.to_lowercase().contains("upgrade"))
.unwrap_or(false)
})
.unwrap_or(false)
&& req
.headers()
.get(hyper::header::UPGRADE)
.and_then(|v| {
v.to_str().ok().map(|s| "websocket".eq_ignore_ascii_case(s))
})
.unwrap_or(false);
.get_all(hyper::header::UPGRADE)
.iter()
.any(|v| {
v.to_str()
.map(|s| s.to_lowercase().contains("websocket"))
.unwrap_or(false)
});

let has_body = if let Some(exact_size) = req.size_hint().exact() {
exact_size > 0
Expand Down

0 comments on commit 0c9d6cb

Please sign in to comment.