summaryrefslogtreecommitdiff
path: root/std/ws
diff options
context:
space:
mode:
authorDreamacro <Dreamacro@vip.qq.com>2020-01-04 17:31:12 +0800
committerRy Dahl <ry@tinyclouds.org>2020-01-04 04:31:12 -0500
commit70b1be6ff459ebd2bf57b7788ab7d66c1f375b29 (patch)
tree0cfe35152745189383ec8acd26c908c1d775be98 /std/ws
parentcbc4bbc71f4e619747707b1b9649abedfa234145 (diff)
Fix ws handshake with correctly empty search string (#3587)
Diffstat (limited to 'std/ws')
-rw-r--r--std/ws/mod.ts6
-rw-r--r--std/ws/test.ts50
2 files changed, 52 insertions, 4 deletions
diff --git a/std/ws/mod.ts b/std/ws/mod.ts
index a32380efc..c256f58ad 100644
--- a/std/ws/mod.ts
+++ b/std/ws/mod.ts
@@ -411,13 +411,13 @@ export function createSecKey(): string {
return btoa(key);
}
-async function handshake(
+export async function handshake(
url: URL,
headers: Headers,
bufReader: BufReader,
bufWriter: BufWriter
): Promise<void> {
- const { hostname, pathname, searchParams } = url;
+ const { hostname, pathname, search } = url;
const key = createSecKey();
if (!headers.has("host")) {
@@ -428,7 +428,7 @@ async function handshake(
headers.set("sec-websocket-key", key);
headers.set("sec-websocket-version", "13");
- let headerStr = `GET ${pathname}?${searchParams || ""} HTTP/1.1\r\n`;
+ let headerStr = `GET ${pathname}${search} HTTP/1.1\r\n`;
for (const [key, value] of headers) {
headerStr += `${key}: ${value}\r\n`;
}
diff --git a/std/ws/test.ts b/std/ws/test.ts
index 8ccda0988..e9cdd1d40 100644
--- a/std/ws/test.ts
+++ b/std/ws/test.ts
@@ -1,11 +1,13 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-import { BufReader } from "../io/bufio.ts";
+import { BufReader, BufWriter } from "../io/bufio.ts";
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import { runIfMain, test } from "../testing/mod.ts";
+import { TextProtoReader } from "../textproto/mod.ts";
import {
acceptable,
connectWebSocket,
createSecAccept,
+ handshake,
OpCode,
readFrame,
unmask,
@@ -224,4 +226,50 @@ test(async function wsWriteReadMaskedFrame(): Promise<void> {
assertEquals(frame.payload, encode(msg));
});
+test("handshake should not send search when it's empty", async function wsHandshakeWithEmptySearch(): Promise<
+ void
+> {
+ const writer = new Buffer();
+ const reader = new Buffer(encode("HTTP/1.1 400\r\n"));
+
+ await assertThrowsAsync(
+ async (): Promise<void> => {
+ await handshake(
+ new URL("ws://example.com"),
+ new Headers(),
+ new BufReader(reader),
+ new BufWriter(writer)
+ );
+ }
+ );
+
+ const tpReader = new TextProtoReader(new BufReader(writer));
+ const statusLine = await tpReader.readLine();
+
+ assertEquals(statusLine, "GET / HTTP/1.1");
+});
+
+test("handshake should send search correctly", async function wsHandshakeWithSearch(): Promise<
+ void
+> {
+ const writer = new Buffer();
+ const reader = new Buffer(encode("HTTP/1.1 400\r\n"));
+
+ await assertThrowsAsync(
+ async (): Promise<void> => {
+ await handshake(
+ new URL("ws://example.com?a=1"),
+ new Headers(),
+ new BufReader(reader),
+ new BufWriter(writer)
+ );
+ }
+ );
+
+ const tpReader = new TextProtoReader(new BufReader(writer));
+ const statusLine = await tpReader.readLine();
+
+ assertEquals(statusLine, "GET /?a=1 HTTP/1.1");
+});
+
runIfMain(import.meta);