Skip to content

Commit

Permalink
Bug 1721148 - [remote] Report WebSocket handshake failures to the cli…
Browse files Browse the repository at this point in the history
…ent. r=webdriver-reviewers,jgraham

When a WebSocket upgrade fails we currently do not
send any details of the error. As such it makes
it hard to figure out the real underlying issue.

Further some clients like curl are hanging and do
not immediately exit when the response has been
received. Specifying the Content-Length header
within the response fixes it.

Differential Revision: https://phabricator.services.mozilla.com/D120575
  • Loading branch information
whimboo committed Jul 23, 2021
1 parent 2cc0b8a commit 96a3543
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions remote/server/WebSocketHandshake.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ function writeString(output, data) {
});
}

/** Write HTTP response (array of strings) to async output stream. */
function writeHttpResponse(output, response) {
const s = response.join("\r\n") + "\r\n\r\n";
/**
* Write HTTP response with headers (array of strings) and body
* to async output stream.
*/
function writeHttpResponse(output, headers, body = "") {
headers.push(`Content-Length: ${body.length}`);

const s = headers.join("\r\n") + `\r\n\r\n${body}`;
return writeString(output, s);
}

Expand Down Expand Up @@ -135,13 +140,23 @@ async function serverHandshake(request, output) {
// Send response headers
await writeHttpResponse(output, [
"HTTP/1.1 101 Switching Protocols",
"Server: httpd.js",
"Upgrade: websocket",
"Connection: Upgrade",
`Sec-WebSocket-Accept: ${acceptKey}`,
]);
} catch (error) {
// Send error response in case of error
await writeHttpResponse(output, ["HTTP/1.1 400 Bad Request"]);
await writeHttpResponse(
output,
[
"HTTP/1.1 400 Bad Request",
"Server: httpd.js",
"Content-Type: text/plain",
],
error.message
);

throw error;
}
}
Expand Down

0 comments on commit 96a3543

Please sign in to comment.