[email protected] | 0cfad2f3 | 2012-05-17 06:40:39 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 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_ |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 7 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 8 | #include <memory> |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 13 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Shiho Noda | 79b790fc4 | 2021-09-16 06:16:39 | [diff] [blame] | 14 | #include "net/websockets/websocket_frame.h" |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | class HttpConnection; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 19 | class HttpServer; |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 20 | class HttpServerRequestInfo; |
yhirano | 995e964 | 2015-09-09 09:01:20 | [diff] [blame] | 21 | class WebSocketEncoder; |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 22 | |
yhirano | 995e964 | 2015-09-09 09:01:20 | [diff] [blame] | 23 | class WebSocket final { |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 24 | public: |
| 25 | enum ParseResult { |
Shiho Noda | bc5f6fe0 | 2021-09-13 10:01:35 | [diff] [blame] | 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, |
Shiho Noda | 2e39de6 | 2021-09-17 04:39:01 | [diff] [blame] | 30 | FRAME_PING, |
| 31 | FRAME_PONG, |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 32 | FRAME_INCOMPLETE, |
[email protected] | c51ab12 | 2011-09-26 16:14:49 | [diff] [blame] | 33 | FRAME_CLOSE, |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 34 | FRAME_ERROR |
| 35 | }; |
| 36 | |
yhirano | a10dd4e | 2015-09-28 09:06:34 | [diff] [blame] | 37 | WebSocket(HttpServer* server, HttpConnection* connection); |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 38 | |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 39 | void Accept(const HttpServerRequestInfo& request, |
| 40 | const NetworkTrafficAnnotationTag traffic_annotation); |
yhirano | 995e964 | 2015-09-09 09:01:20 | [diff] [blame] | 41 | ParseResult Read(std::string* message); |
Johannes Henkel | da8b9d3 | 2019-03-15 16:15:33 | [diff] [blame] | 42 | void Send(base::StringPiece message, |
Shiho Noda | 79b790fc4 | 2021-09-16 06:16:39 | [diff] [blame] | 43 | WebSocketFrameHeader::OpCodeEnum op_code, |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 44 | const NetworkTrafficAnnotationTag traffic_annotation); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 45 | |
| 46 | WebSocket(const WebSocket&) = delete; |
| 47 | WebSocket& operator=(const WebSocket&) = delete; |
| 48 | |
yhirano | 995e964 | 2015-09-09 09:01:20 | [diff] [blame] | 49 | ~WebSocket(); |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 50 | |
yhirano | 995e964 | 2015-09-09 09:01:20 | [diff] [blame] | 51 | private: |
yhirano | a10dd4e | 2015-09-28 09:06:34 | [diff] [blame] | 52 | void Fail(); |
Ramin Halavati | 90aa08ba | 2018-02-07 06:16:16 | [diff] [blame] | 53 | void SendErrorResponse(const std::string& message, |
| 54 | const NetworkTrafficAnnotationTag traffic_annotation); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 55 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 56 | const raw_ptr<HttpServer> server_; |
| 57 | const raw_ptr<HttpConnection> connection_; |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 58 | std::unique_ptr<WebSocketEncoder> encoder_; |
yhirano | 995e964 | 2015-09-09 09:01:20 | [diff] [blame] | 59 | bool closed_; |
Shiho Noda | 79b790fc4 | 2021-09-16 06:16:39 | [diff] [blame] | 60 | std::unique_ptr<NetworkTrafficAnnotationTag> traffic_annotation_ = nullptr; |
[email protected] | 86c6a0b5 | 2011-08-02 19:49:25 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | } // namespace net |
| 64 | |
[email protected] | 0cfad2f3 | 2012-05-17 06:40:39 | [diff] [blame] | 65 | #endif // NET_SERVER_WEB_SOCKET_H_ |