mirror of
https://github.com/apple/swift-nio
synced 2026-11-23 04:34:23 +00:00
Enhance `WebSocketProtocolErrorHandler` to correctly add masking key for client/server. ### Motivation: In `NIOWebSocket`, the automatic error handling provided by `WebSocketProtocolErrorHandler` offers a convenient way for both clients and servers to handle protocol-level errors. However, the `WebSocketFrame` used in `WebSocketProtocolErrorHandler` does not fully adhere to the RFC 6455 standard. Specifically, as per [RFC 6455 Section 5.1](https://datatracker.ietf.org/doc/html/rfc6455#section-5.1), a client *must* mask all frames it sends to the server, while a server *must not* mask any frames it sends to the client. This PR addressed this discrepancy to ensure compliance with the WebSocket protocol. ### Modifications: In the `WebSocketProtocolErrorHandler` initializer, the user can specify whether the handler is used for a WebSocket client or server. Within the `errorCaught` method, the `WebSocketFrame` will include a maskingKey if the handler is used by a client, or nil if it is used by a server. Additionally, the `@unchecked` annotation is removed from `NIOWebSocketServerUpgrader` since the project is now using swift 5.9 toolchain. ### Result: The `WebSocketProtocolErrorHandler` is more robust and can correctly close the connection is error occurs.