Fix ws handshake with correctly empty search string (#3587)

This commit is contained in:
Dreamacro 2020-01-04 17:31:12 +08:00 committed by Ry Dahl
parent cbc4bbc71f
commit 70b1be6ff4
2 changed files with 52 additions and 4 deletions

View file

@ -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`;
}

View file

@ -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);