blob: af27eb66a3b81065d8281c5885259dabb986a4e9 [file] [log] [blame]
[email protected]0cfad2f32012-05-17 06:40:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]86c6a0b52011-08-02 19:49:252// 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_
[email protected]86c6a0b52011-08-02 19:49:257
danakja9850e12016-04-18 22:28:088#include <memory>
[email protected]86c6a0b52011-08-02 19:49:259#include <string>
10
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
byungchul38c3ae72014-08-25 23:27:4612#include "base/strings/string_piece.h"
Ramin Halavati90aa08ba2018-02-07 06:16:1613#include "net/traffic_annotation/network_traffic_annotation.h"
Shiho Noda79b790fc42021-09-16 06:16:3914#include "net/websockets/websocket_frame.h"
[email protected]86c6a0b52011-08-02 19:49:2515
16namespace net {
17
18class HttpConnection;
byungchul38c3ae72014-08-25 23:27:4619class HttpServer;
[email protected]86c6a0b52011-08-02 19:49:2520class HttpServerRequestInfo;
yhirano995e9642015-09-09 09:01:2021class WebSocketEncoder;
[email protected]86c6a0b52011-08-02 19:49:2522
yhirano995e9642015-09-09 09:01:2023class WebSocket final {
[email protected]86c6a0b52011-08-02 19:49:2524 public:
25 enum ParseResult {
Shiho Nodabc5f6fe02021-09-13 10:01:3526 // Final frame of a text message or compressed frame.
27 FRAME_OK_FINAL,
28 // Other frame of a text message.
29 FRAME_OK_MIDDLE,
Shiho Noda2e39de62021-09-17 04:39:0130 FRAME_PING,
31 FRAME_PONG,
[email protected]86c6a0b52011-08-02 19:49:2532 FRAME_INCOMPLETE,
[email protected]c51ab122011-09-26 16:14:4933 FRAME_CLOSE,
[email protected]86c6a0b52011-08-02 19:49:2534 FRAME_ERROR
35 };
36
yhiranoa10dd4e2015-09-28 09:06:3437 WebSocket(HttpServer* server, HttpConnection* connection);
[email protected]86c6a0b52011-08-02 19:49:2538
Ramin Halavati90aa08ba2018-02-07 06:16:1639 void Accept(const HttpServerRequestInfo& request,
40 const NetworkTrafficAnnotationTag traffic_annotation);
yhirano995e9642015-09-09 09:01:2041 ParseResult Read(std::string* message);
Johannes Henkelda8b9d32019-03-15 16:15:3342 void Send(base::StringPiece message,
Shiho Noda79b790fc42021-09-16 06:16:3943 WebSocketFrameHeader::OpCodeEnum op_code,
Ramin Halavati90aa08ba2018-02-07 06:16:1644 const NetworkTrafficAnnotationTag traffic_annotation);
Peter Boström293b1342021-09-22 17:31:4345
46 WebSocket(const WebSocket&) = delete;
47 WebSocket& operator=(const WebSocket&) = delete;
48
yhirano995e9642015-09-09 09:01:2049 ~WebSocket();
[email protected]86c6a0b52011-08-02 19:49:2550
yhirano995e9642015-09-09 09:01:2051 private:
yhiranoa10dd4e2015-09-28 09:06:3452 void Fail();
Ramin Halavati90aa08ba2018-02-07 06:16:1653 void SendErrorResponse(const std::string& message,
54 const NetworkTrafficAnnotationTag traffic_annotation);
byungchul38c3ae72014-08-25 23:27:4655
Keishi Hattori0e45c022021-11-27 09:25:5256 const raw_ptr<HttpServer> server_;
57 const raw_ptr<HttpConnection> connection_;
danakja9850e12016-04-18 22:28:0858 std::unique_ptr<WebSocketEncoder> encoder_;
yhirano995e9642015-09-09 09:01:2059 bool closed_;
Shiho Noda79b790fc42021-09-16 06:16:3960 std::unique_ptr<NetworkTrafficAnnotationTag> traffic_annotation_ = nullptr;
[email protected]86c6a0b52011-08-02 19:49:2561};
62
63} // namespace net
64
[email protected]0cfad2f32012-05-17 06:40:3965#endif // NET_SERVER_WEB_SOCKET_H_