OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SERVER_WEB_SOCKET_ENCODER_H_ | 5 #ifndef NET_SERVER_WEB_SOCKET_ENCODER_H_ |
6 #define NET_SERVER_WEB_SOCKET_ENCODER_H_ | 6 #define NET_SERVER_WEB_SOCKET_ENCODER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "net/server/web_socket.h" | 13 #include "net/server/web_socket.h" |
14 #include "net/websockets/websocket_deflater.h" | 14 #include "net/websockets/websocket_deflater.h" |
15 #include "net/websockets/websocket_inflater.h" | 15 #include "net/websockets/websocket_inflater.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 class WebSocketEncoder { | 19 class WebSocketDeflateParameters; |
| 20 |
| 21 class WebSocketEncoder final { |
20 public: | 22 public: |
| 23 static const char kClientExtensions[]; |
21 ~WebSocketEncoder(); | 24 ~WebSocketEncoder(); |
22 | 25 |
23 static WebSocketEncoder* CreateServer(const std::string& request_extensions, | 26 // Creates and returns an encoder for a server without deflate extension. |
24 std::string* response_extensions); | 27 static scoped_ptr<WebSocketEncoder> CreateServer(); |
25 | 28 // Creates and returns an encoder for a server with deflate extension. |
26 static const char kClientExtensions[]; | 29 // Returns nullptr when there is an error. |
| 30 static scoped_ptr<WebSocketEncoder> CreateServer( |
| 31 const std::string& request_extensions, |
| 32 WebSocketDeflateParameters* response_params, |
| 33 std::string* failure_message); |
| 34 // TODO(yhirano): Return a scoped_ptr instead of a raw pointer. |
27 static WebSocketEncoder* CreateClient(const std::string& response_extensions); | 35 static WebSocketEncoder* CreateClient(const std::string& response_extensions); |
28 | 36 |
29 WebSocket::ParseResult DecodeFrame(const base::StringPiece& frame, | 37 WebSocket::ParseResult DecodeFrame(const base::StringPiece& frame, |
30 int* bytes_consumed, | 38 int* bytes_consumed, |
31 std::string* output); | 39 std::string* output); |
32 | |
33 void EncodeFrame(const std::string& frame, | 40 void EncodeFrame(const std::string& frame, |
34 int masking_key, | 41 int masking_key, |
35 std::string* output); | 42 std::string* output); |
36 | 43 |
| 44 bool deflate_enabled() const { return deflater_; } |
| 45 |
37 private: | 46 private: |
38 explicit WebSocketEncoder(bool is_server); | 47 class AsServer {}; |
39 WebSocketEncoder(bool is_server, | 48 class AsClient {}; |
40 int deflate_bits, | |
41 int inflate_bits, | |
42 bool no_context_takeover); | |
43 | 49 |
44 // Parses a value in the Sec-WebSocket-Extensions header. If it contains a | 50 explicit WebSocketEncoder(const AsServer&); |
45 // single element of the permessage-deflate extension, stores the result of | 51 explicit WebSocketEncoder(const AsClient&); |
46 // parsing the parameters of the extension into the given variables. | 52 WebSocketEncoder(const AsServer&, const WebSocketDeflateParameters& params); |
47 // Otherwise, returns with *deflate set to false. | 53 WebSocketEncoder(const AsClient&, const WebSocketDeflateParameters& params); |
48 // | |
49 // - If the client_max_window_bits parameter is missing, *client_window_bits | |
50 // defaults to 15. | |
51 // - If the client_max_window_bits parameter has an invalid value, | |
52 // *client_window_bits will be set to 0. | |
53 // - If the server_max_window_bits parameter is missing, *server_window_bits | |
54 // defaults to 15. | |
55 // - If the server_max_window_bits parameter has an invalid value, | |
56 // *client_window_bits will be set to 0. | |
57 // | |
58 // TODO(tyoshino): Consider using a struct than taking a lot of pointers for | |
59 // output. | |
60 static void ParseExtensions(const std::string& header_value, | |
61 bool* deflate, | |
62 bool* has_client_window_bits, | |
63 int* client_window_bits, | |
64 int* server_window_bits, | |
65 bool* client_no_context_takeover, | |
66 bool* server_no_context_takeover); | |
67 | 54 |
68 bool Inflate(std::string* message); | 55 bool Inflate(std::string* message); |
69 bool Deflate(const std::string& message, std::string* output); | 56 bool Deflate(const std::string& message, std::string* output); |
70 | 57 |
| 58 bool is_server_; |
| 59 bool has_error_; |
71 scoped_ptr<WebSocketDeflater> deflater_; | 60 scoped_ptr<WebSocketDeflater> deflater_; |
72 scoped_ptr<WebSocketInflater> inflater_; | 61 scoped_ptr<WebSocketInflater> inflater_; |
73 bool is_server_; | |
74 | 62 |
75 DISALLOW_COPY_AND_ASSIGN(WebSocketEncoder); | 63 DISALLOW_COPY_AND_ASSIGN(WebSocketEncoder); |
76 }; | 64 }; |
77 | 65 |
78 } // namespace net | 66 } // namespace net |
79 | 67 |
80 #endif // NET_SERVER_WEB_SOCKET_ENCODER_H_ | 68 #endif // NET_SERVER_WEB_SOCKET_ENCODER_H_ |
OLD | NEW |