• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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_SERVER_WEB_SOCKET_H_
6 #define NET_SERVER_WEB_SOCKET_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/memory/raw_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "net/traffic_annotation/network_traffic_annotation.h"
14 #include "net/websockets/websocket_frame.h"
15 
16 namespace net {
17 
18 class HttpConnection;
19 class HttpServer;
20 class HttpServerRequestInfo;
21 class WebSocketEncoder;
22 
23 class WebSocket final {
24  public:
25   enum ParseResult {
26     // Final frame of a text message or compressed frame.
27     FRAME_OK_FINAL,
28     // Other frame of a text message.
29     FRAME_OK_MIDDLE,
30     FRAME_PING,
31     FRAME_PONG,
32     FRAME_INCOMPLETE,
33     FRAME_CLOSE,
34     FRAME_ERROR
35   };
36 
37   WebSocket(HttpServer* server, HttpConnection* connection);
38 
39   void Accept(const HttpServerRequestInfo& request,
40               const NetworkTrafficAnnotationTag traffic_annotation);
41   ParseResult Read(std::string* message);
42   void Send(base::StringPiece message,
43             WebSocketFrameHeader::OpCodeEnum op_code,
44             const NetworkTrafficAnnotationTag traffic_annotation);
45 
46   WebSocket(const WebSocket&) = delete;
47   WebSocket& operator=(const WebSocket&) = delete;
48 
49   ~WebSocket();
50 
51  private:
52   void Fail();
53   void SendErrorResponse(const std::string& message,
54                          const NetworkTrafficAnnotationTag traffic_annotation);
55 
56   const raw_ptr<HttpServer> server_;
57   const raw_ptr<HttpConnection> connection_;
58   std::unique_ptr<WebSocketEncoder> encoder_;
59   bool closed_ = false;
60   std::unique_ptr<NetworkTrafficAnnotationTag> traffic_annotation_ = nullptr;
61 };
62 
63 }  // namespace net
64 
65 #endif  // NET_SERVER_WEB_SOCKET_H_
66