| 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[]; |
| 24 |
| 21 ~WebSocketEncoder(); | 25 ~WebSocketEncoder(); |
| 22 | 26 |
| 23 static WebSocketEncoder* CreateServer(const std::string& request_extensions, | 27 // Creates and returns an encoder for a server without extensions. |
| 24 std::string* response_extensions); | 28 static scoped_ptr<WebSocketEncoder> CreateServer(); |
| 25 | 29 // Creates and returns an encoder. |
| 26 static const char kClientExtensions[]; | 30 // |extensions| is the value of a Sec-WebSocket-Extensions header. |
| 31 // Returns nullptr when there is an error. |
| 32 static scoped_ptr<WebSocketEncoder> CreateServer( |
| 33 const std::string& extensions, |
| 34 WebSocketDeflateParameters* params); |
| 35 // TODO(yhirano): Return a scoped_ptr instead of a raw pointer. |
| 27 static WebSocketEncoder* CreateClient(const std::string& response_extensions); | 36 static WebSocketEncoder* CreateClient(const std::string& response_extensions); |
| 28 | 37 |
| 29 WebSocket::ParseResult DecodeFrame(const base::StringPiece& frame, | 38 WebSocket::ParseResult DecodeFrame(const base::StringPiece& frame, |
| 30 int* bytes_consumed, | 39 int* bytes_consumed, |
| 31 std::string* output); | 40 std::string* output); |
| 32 | |
| 33 void EncodeFrame(const std::string& frame, | 41 void EncodeFrame(const std::string& frame, |
| 34 int masking_key, | 42 int masking_key, |
| 35 std::string* output); | 43 std::string* output); |
| 36 | 44 |
| 45 bool deflate_enabled() const { return deflater_; } |
| 46 |
| 37 private: | 47 private: |
| 38 explicit WebSocketEncoder(bool is_server); | 48 enum Type { |
| 39 WebSocketEncoder(bool is_server, | 49 FOR_SERVER, |
| 40 int deflate_bits, | 50 FOR_CLIENT, |
| 41 int inflate_bits, | 51 }; |
| 42 bool no_context_takeover); | |
| 43 | 52 |
| 44 // Parses a value in the Sec-WebSocket-Extensions header. If it contains a | 53 WebSocketEncoder(Type type, |
| 45 // single element of the permessage-deflate extension, stores the result of | 54 scoped_ptr<WebSocketDeflater> deflater, |
| 46 // parsing the parameters of the extension into the given variables. | 55 scoped_ptr<WebSocketInflater> inflater); |
| 47 // Otherwise, returns with *deflate set to false. | |
| 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 | 56 |
| 68 bool Inflate(std::string* message); | 57 bool Inflate(std::string* message); |
| 69 bool Deflate(const std::string& message, std::string* output); | 58 bool Deflate(const std::string& message, std::string* output); |
| 70 | 59 |
| 60 Type type_; |
| 71 scoped_ptr<WebSocketDeflater> deflater_; | 61 scoped_ptr<WebSocketDeflater> deflater_; |
| 72 scoped_ptr<WebSocketInflater> inflater_; | 62 scoped_ptr<WebSocketInflater> inflater_; |
| 73 bool is_server_; | |
| 74 | 63 |
| 75 DISALLOW_COPY_AND_ASSIGN(WebSocketEncoder); | 64 DISALLOW_COPY_AND_ASSIGN(WebSocketEncoder); |
| 76 }; | 65 }; |
| 77 | 66 |
| 78 } // namespace net | 67 } // namespace net |
| 79 | 68 |
| 80 #endif // NET_SERVER_WEB_SOCKET_ENCODER_H_ | 69 #endif // NET_SERVER_WEB_SOCKET_ENCODER_H_ |
| OLD | NEW |