• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WEBSOCKETS_WEBSOCKET_CHUNK_ASSEMBLER_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_CHUNK_ASSEMBLER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/containers/span.h"
12 #include "base/types/expected.h"
13 #include "net/base/net_errors.h"
14 #include "net/websockets/websocket_frame.h"
15 
16 namespace net {
17 
18 class NET_EXPORT WebSocketChunkAssembler final {
19  public:
20   WebSocketChunkAssembler();
21   ~WebSocketChunkAssembler();
22 
23   // Resets the current state of the assembler.
24   void Reset();
25 
26   // Processes a WebSocket frame chunk and assembles it into a complete frame.
27   // Returns either the assembled frame or an error code.
28   base::expected<std::unique_ptr<WebSocketFrame>, net::Error> HandleChunk(
29       std::unique_ptr<WebSocketFrameChunk> chunk);
30 
31  private:
32   // Enum representing the current state of the assembler.
33   enum class AssemblyState {
34     kMessageFinished,    // Message finished, ready for next frame.
35     kInitialFrame,       // Processing the first frame.
36     kContinuationFrame,  // Processing continuation frame.
37     kControlFrame        // Processing control frame.
38   };
39 
40   // Current state of the assembler
41   AssemblyState state_ = AssemblyState::kMessageFinished;
42 
43   std::unique_ptr<WebSocketFrameHeader> current_frame_header_;
44   std::vector<char> chunk_buffer_;
45 };
46 
47 }  // namespace net
48 
49 #endif  // NET_WEBSOCKETS_WEBSOCKET_CHUNK_ASSEMBLER_H_
50