1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_TEST_EMBEDDED_TEST_SERVER_WEBSOCKET_HANDLER_H_ 6 #define NET_TEST_EMBEDDED_TEST_SERVER_WEBSOCKET_HANDLER_H_ 7 8 #include <stdint.h> 9 10 #include <optional> 11 #include <string_view> 12 13 #include "base/containers/span.h" 14 #include "base/memory/scoped_refptr.h" 15 16 namespace net::test_server { 17 18 class WebSocketConnection; 19 struct HttpRequest; 20 21 // Base class defining methods for handling WebSocket connections. 22 // Subclasses should implement the methods to define specific WebSocket server 23 // behavior. 24 class WebSocketHandler { 25 public: 26 WebSocketHandler(const WebSocketHandler&) = delete; 27 WebSocketHandler& operator=(const WebSocketHandler&) = delete; 28 29 // The handler will be automatically destroyed when the WebSocket 30 // disconnects. 31 virtual ~WebSocketHandler(); 32 33 // Called when a valid WebSocket handshake has been received, before the 34 // response headers are sent. OnHandshake(const HttpRequest & request)35 virtual void OnHandshake(const HttpRequest& request) {} 36 37 // Called after handshake response headers have been sent. OnHandshakeComplete()38 virtual void OnHandshakeComplete() {} 39 40 // Called when a text message has been received. `message` will only be valid 41 // until this call returns. OnTextMessage(std::string_view message)42 virtual void OnTextMessage(std::string_view message) {} 43 44 // Called when a binary message has been received. `message` will only be 45 // valid until this call returns. OnBinaryMessage(base::span<const uint8_t> message)46 virtual void OnBinaryMessage(base::span<const uint8_t> message) {} 47 48 // Called when a PING frame has been received. `payload` will only be valid 49 // until this call returns. By default, it responds with a PONG message. 50 virtual void OnPing(base::span<const uint8_t> payload); 51 52 // Called when a PONG frame has been received. `payload` will only be valid 53 // until this call returns. Default behavior is no-op. 54 virtual void OnPong(base::span<const uint8_t> payload); 55 56 // Called when a CLOSE frame is received from the remote server. `code` will 57 // be std::nullopt if the CLOSE frame contained no data. `message` will only 58 // be valid until this call returns. The default implementation responds with 59 // a close frame. 60 virtual void OnClosingHandshake(std::optional<uint16_t> code, 61 std::string_view message); 62 63 protected: 64 // Constructor that initializes the WebSocketHandler with a pointer to the 65 // WebSocketConnection it interacts with. 66 explicit WebSocketHandler(scoped_refptr<WebSocketConnection> connection); 67 68 // Provides access to the associated WebSocketConnection. connection()69 const scoped_refptr<WebSocketConnection>& connection() const { 70 return connection_; 71 } 72 73 private: 74 // Pointer to the WebSocketConnection associated with this handler. 75 const scoped_refptr<WebSocketConnection> connection_; 76 }; 77 78 } // namespace net::test_server 79 80 #endif // NET_TEST_EMBEDDED_TEST_SERVER_WEBSOCKET_HANDLER_H_ 81