diff options
author | 2022-06-18 01:27:36 -0700 | |
---|---|---|
committer | 2022-06-22 06:56:47 -0700 | |
commit | ab888d2ebebea0d128f4151a4240180211d95f03 (patch) | |
tree | 7f0831ebc4a95f8abeda7191de6d0c9064346836 | |
parent | be9efacf1bf03fba016bb02f1dd25766fd874033 (diff) | |
download | bun-ab888d2ebebea0d128f4151a4240180211d95f03.tar.gz bun-ab888d2ebebea0d128f4151a4240180211d95f03.tar.zst bun-ab888d2ebebea0d128f4151a4240180211d95f03.zip |
It connects! but the frames are corrupt
-rw-r--r-- | src/deps/uws.zig | 8 | ||||
-rw-r--r-- | src/http/websocket_http_client.zig | 42 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/webcore/WebSocket.cpp | 15 |
3 files changed, 34 insertions, 31 deletions
diff --git a/src/deps/uws.zig b/src/deps/uws.zig index 3d3629bcb..08037a890 100644 --- a/src/deps/uws.zig +++ b/src/deps/uws.zig @@ -64,11 +64,11 @@ pub fn NewSocketHandler(comptime ssl: bool) type { this.socket, ); } - pub fn isShutdown(this: ThisSocket) c_int { + pub fn isShutdown(this: ThisSocket) bool { return us_socket_is_shut_down( comptime ssl_int, this.socket, - ); + ) > 0; } pub fn isClosed(this: ThisSocket) c_int { return us_socket_is_closed( @@ -136,12 +136,10 @@ pub fn NewSocketHandler(comptime ssl: bool) type { comptime onEnd: anytype, ) void { const SocketHandler = struct { - pub fn on_open(socket: *Socket, _: c_int, ip: [*c]u8, port: c_int) callconv(.C) ?*Socket { + pub fn on_open(socket: *Socket, _: c_int, _: [*c]u8, _: c_int) callconv(.C) ?*Socket { onOpen( @ptrCast(*ContextType, @alignCast(std.meta.alignment(ContextType), us_socket_ext(comptime ssl_int, socket).?)), ThisSocket{ .socket = socket }, - bun.span(ip), - port, ); return socket; } diff --git a/src/http/websocket_http_client.zig b/src/http/websocket_http_client.zig index d577fadc6..0a141c4c2 100644 --- a/src/http/websocket_http_client.zig +++ b/src/http/websocket_http_client.zig @@ -60,7 +60,7 @@ fn buildRequestBody(vm: *JSC.VirtualMachine, pathname: *const JSC.ZigString, hos "Connection: Upgrade\r\n" ++ "Upgrade: websocket\r\n" ++ "Sec-WebSocket-Version: 13\r\n" ++ - "{any}\n" ++ + "{any}" ++ "\r\n", .{ pathname_, @@ -199,7 +199,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { pub fn handleClose(this: *HTTPClient, _: Socket, _: c_int, _: ?*anyopaque) void { JSC.markBinding(); this.clearData(); - WebSocket__didFailToConnect(this.outgoing_websocket, ErrorCode.closed); + WebSocket__didFailToConnect(this.outgoing_websocket, ErrorCode.ended); } pub fn terminate(this: *HTTPClient, code: ErrorCode) void { @@ -208,13 +208,13 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { this.socket.close(0, null); } - pub fn handleOpen(this: *HTTPClient, socket: Socket, _: []const u8, _: c_int) void { + pub fn handleOpen(this: *HTTPClient, socket: Socket) void { std.debug.assert(socket.socket == this.socket.socket); std.debug.assert(this.input_body_buf.len > 0); std.debug.assert(this.to_send.len == 0); - const wrote = socket.write(this.input_body_buf, false); + const wrote = socket.write(this.input_body_buf, true); if (wrote < 0) { this.terminate(ErrorCode.failed_to_write); return; @@ -235,14 +235,14 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { std.debug.assert(socket.socket == this.socket.socket); if (comptime Environment.allow_assert) - std.debug.assert(socket.isShutdown() == 0); + std.debug.assert(!socket.isShutdown()); var body = this.getBody(); var remain = body[this.body_written..]; const is_first = this.body_written == 0; - if (is_first and data.len >= "101 ".len) { + if (is_first and data.len >= "HTTP/1.1 101 ".len) { // fail early if we receive a non-101 status code - if (!strings.eqlComptimeIgnoreLen(data[0.."101 ".len], "101 ")) { + if (!strings.eqlComptimeIgnoreLen(data[0.."HTTP/1.1 101 ".len], "HTTP/1.1 101 ")) { this.terminate(ErrorCode.expected_101_status_code); return; } @@ -291,7 +291,8 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { var connection_header = PicoHTTP.Header{ .name = "", .value = "" }; var websocket_accept_header = PicoHTTP.Header{ .name = "", .value = "" }; var visited_protocol = this.websocket_protocol == 0; - var visited_version = false; + // var visited_version = false; + std.debug.assert(response.status_code == 101); if (remain_buf.len > 0) { std.debug.assert(overflow_buf.len == 0); @@ -302,7 +303,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { "Connection".len => { if (connection_header.name.len == 0 and strings.eqlCaseInsensitiveASCII(header.name, "Connection", false)) { connection_header = header; - if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0 and visited_version) { + if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0) { break; } } @@ -310,14 +311,13 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { "Upgrade".len => { if (upgrade_header.name.len == 0 and strings.eqlCaseInsensitiveASCII(header.name, "Upgrade", false)) { upgrade_header = header; - if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0 and visited_version) { + if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0) { break; } } }, "Sec-WebSocket-Version".len => { - if (!visited_version and strings.eqlCaseInsensitiveASCII(header.name, "Sec-WebSocket-Version", false)) { - visited_version = true; + if (strings.eqlCaseInsensitiveASCII(header.name, "Sec-WebSocket-Version", false)) { if (!strings.eqlComptimeIgnoreLen(header.value, "13")) { this.terminate(ErrorCode.invalid_websocket_version); return; @@ -327,7 +327,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { "Sec-WebSocket-Accept".len => { if (websocket_accept_header.name.len == 0 and strings.eqlCaseInsensitiveASCII(header.name, "Sec-WebSocket-Accept", false)) { websocket_accept_header = header; - if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0 and visited_version) { + if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0) { break; } } @@ -340,7 +340,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { } visited_protocol = true; - if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0 and visited_version) { + if (visited_protocol and upgrade_header.name.len > 0 and connection_header.name.len > 0 and websocket_accept_header.name.len > 0) { break; } } @@ -349,10 +349,10 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { } } - if (!visited_version) { - this.terminate(ErrorCode.invalid_websocket_version); - return; - } + // if (!visited_version) { + // this.terminate(ErrorCode.invalid_websocket_version); + // return; + // } if (@minimum(upgrade_header.name.len, upgrade_header.value.len) == 0) { this.terminate(ErrorCode.missing_upgrade_header); @@ -374,7 +374,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { return; } - if (strings.eqlComptime(connection_header.value, "Upgrade")) { + if (!strings.eqlComptime(connection_header.value, "Upgrade")) { this.terminate(ErrorCode.invalid_connection_header); return; } @@ -394,7 +394,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { return; }; if (remain_buf.len > 0) @memcpy(overflow.ptr, remain_buf.ptr, remain_buf.len); - if (overflow_buf.len > 0) @memcpy(overflow.ptr + remain_buf.len, overflow_buf.ptr, remain_buf.len); + if (overflow_buf.len > 0) @memcpy(overflow.ptr + remain_buf.len, overflow_buf.ptr, overflow_buf.len); } this.clearData(); @@ -411,7 +411,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { if (this.to_send.len == 0) return; - const wrote = socket.write(this.to_send, false); + const wrote = socket.write(this.to_send, true); if (wrote < 0) { this.terminate(ErrorCode.failed_to_write); return; diff --git a/src/javascript/jsc/bindings/webcore/WebSocket.cpp b/src/javascript/jsc/bindings/webcore/WebSocket.cpp index 691da6aae..9b996565c 100644 --- a/src/javascript/jsc/bindings/webcore/WebSocket.cpp +++ b/src/javascript/jsc/bindings/webcore/WebSocket.cpp @@ -420,6 +420,8 @@ ExceptionOr<void> WebSocket::send(const String& message) if (utf8.length() > 0) this->sendWebSocketData<false>(utf8.data(), utf8.length()); + delete utf8; + return {}; } @@ -490,22 +492,22 @@ void WebSocket::sendWebSocketData(const char* baseAddress, size_t length) switch (m_connectedWebSocketKind) { case ConnectedWebSocketKind::Client: { - this->m_connectedWebSocket.client->send({ baseAddress, length }, opCode, false); + this->m_connectedWebSocket.client->send({ baseAddress, length }, opCode); this->m_bufferedAmount = this->m_connectedWebSocket.client->getBufferedAmount(); break; } case ConnectedWebSocketKind::ClientSSL: { - this->m_connectedWebSocket.clientSSL->send({ baseAddress, length }, opCode, false); + this->m_connectedWebSocket.clientSSL->send({ baseAddress, length }, opCode); this->m_bufferedAmount = this->m_connectedWebSocket.clientSSL->getBufferedAmount(); break; } case ConnectedWebSocketKind::Server: { - this->m_connectedWebSocket.server->send({ baseAddress, length }, opCode, false); + this->m_connectedWebSocket.server->send({ baseAddress, length }, opCode); this->m_bufferedAmount = this->m_connectedWebSocket.server->getBufferedAmount(); break; } case ConnectedWebSocketKind::ServerSSL: { - this->m_connectedWebSocket.serverSSL->send({ baseAddress, length }, opCode, false); + this->m_connectedWebSocket.serverSSL->send({ baseAddress, length }, opCode); this->m_bufferedAmount = this->m_connectedWebSocket.serverSSL->getBufferedAmount(); break; } @@ -827,13 +829,14 @@ void WebSocket::dispatchErrorEventIfNeeded() void WebSocket::didConnect(us_socket_t* socket, char* bufferedData, size_t bufferedDataSize) { - m_state = OPEN; this->m_upgradeClient = nullptr; if (m_isSecure) { /* Adopting a socket invalidates it, do not rely on it directly to carry any data */ uWS::WebSocket<true, false, WebSocket*>* webSocket = (uWS::WebSocket<true, false, WebSocket*>*)us_socket_context_adopt_socket(1, (us_socket_context_t*)this->scriptExecutionContext()->connnectedWebSocketContext<true, false>(), socket, sizeof(uWS::WebSocketData) + sizeof(WebSocket*)); + webSocket->AsyncSocket<true>::uncork(); + webSocket->init(0, uWS::CompressOptions::DISABLED, uWS::BackPressure()); *webSocket->getUserData() = this; this->m_connectedWebSocket.clientSSL = webSocket; @@ -843,6 +846,8 @@ void WebSocket::didConnect(us_socket_t* socket, char* bufferedData, size_t buffe uWS::WebSocket<false, false, WebSocket*>* webSocket = (uWS::WebSocket<false, false, WebSocket*>*)us_socket_context_adopt_socket(1, (us_socket_context_t*)this->scriptExecutionContext()->connnectedWebSocketContext<false, false>(), socket, sizeof(uWS::WebSocketData) + sizeof(WebSocket*)); + webSocket->AsyncSocket<false>::uncork(); + webSocket->init(0, uWS::CompressOptions::DISABLED, uWS::BackPressure()); *webSocket->getUserData() = this; this->m_connectedWebSocket.client = webSocket; |