websocket_deflate_parameters.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_
  5. #define NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/check.h"
  9. #include "net/base/net_export.h"
  10. #include "net/websockets/websocket_deflater.h"
  11. #include "net/websockets/websocket_extension.h"
  12. namespace net {
  13. // A WebSocketDeflateParameters represents permessage-deflate extension
  14. // parameters. This class is used either for request and response.
  15. class NET_EXPORT_PRIVATE WebSocketDeflateParameters {
  16. public:
  17. using ContextTakeOverMode = WebSocketDeflater::ContextTakeOverMode;
  18. // Returns a WebSocketExtension instance containing the parameters stored in
  19. // this object.
  20. WebSocketExtension AsExtension() const;
  21. // Returns true when succeeded.
  22. // Returns false and stores the failure message to |failure_message|
  23. // otherwise.
  24. // Note that even if this function succeeds it is not guaranteed that the
  25. // object is valid. To check it, call IsValidAsRequest or IsValidAsResponse.
  26. bool Initialize(const WebSocketExtension& input,
  27. std::string* failure_message);
  28. // Returns true when |*this| and |response| are compatible.
  29. // |*this| must be valid as a request and |response| must be valid as a
  30. // response.
  31. bool IsCompatibleWith(const WebSocketDeflateParameters& response) const;
  32. bool IsValidAsRequest(std::string* failure_message) const;
  33. bool IsValidAsResponse(std::string* failure_message) const;
  34. bool IsValidAsRequest() const {
  35. std::string message;
  36. return IsValidAsRequest(&message);
  37. }
  38. bool IsValidAsResponse() const {
  39. std::string message;
  40. return IsValidAsResponse(&message);
  41. }
  42. ContextTakeOverMode server_context_take_over_mode() const {
  43. return server_context_take_over_mode_;
  44. }
  45. ContextTakeOverMode client_context_take_over_mode() const {
  46. return client_context_take_over_mode_;
  47. }
  48. bool is_server_max_window_bits_specified() const {
  49. return server_max_window_bits_.is_specified;
  50. }
  51. int server_max_window_bits() const {
  52. DCHECK(is_server_max_window_bits_specified());
  53. return server_max_window_bits_.bits;
  54. }
  55. bool is_client_max_window_bits_specified() const {
  56. return client_max_window_bits_.is_specified;
  57. }
  58. bool has_client_max_window_bits_value() const {
  59. DCHECK(is_client_max_window_bits_specified());
  60. return client_max_window_bits_.has_value;
  61. }
  62. int client_max_window_bits() const {
  63. DCHECK(has_client_max_window_bits_value());
  64. return client_max_window_bits_.bits;
  65. }
  66. void SetServerNoContextTakeOver() {
  67. server_context_take_over_mode_ =
  68. WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT;
  69. }
  70. void SetClientNoContextTakeOver() {
  71. client_context_take_over_mode_ =
  72. WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT;
  73. }
  74. // |bits| must be valid as a max_window_bits value.
  75. void SetServerMaxWindowBits(int bits) {
  76. DCHECK(IsValidWindowBits(bits));
  77. server_max_window_bits_ = WindowBits(bits, true, true);
  78. }
  79. void SetClientMaxWindowBits() {
  80. client_max_window_bits_ = WindowBits(0, true, false);
  81. }
  82. // |bits| must be valid as a max_window_bits value.
  83. void SetClientMaxWindowBits(int bits) {
  84. DCHECK(IsValidWindowBits(bits));
  85. client_max_window_bits_ = WindowBits(bits, true, true);
  86. }
  87. int PermissiveServerMaxWindowBits() const {
  88. return server_max_window_bits_.PermissiveBits();
  89. }
  90. int PermissiveClientMaxWindowBits() const {
  91. return client_max_window_bits_.PermissiveBits();
  92. }
  93. // Return true if |bits| is valid as a max_window_bits value.
  94. static bool IsValidWindowBits(int bits) { return 8 <= bits && bits <= 15; }
  95. private:
  96. struct WindowBits {
  97. WindowBits() : WindowBits(0, false, false) {}
  98. WindowBits(int16_t bits, bool is_specified, bool has_value)
  99. : bits(bits), is_specified(is_specified), has_value(has_value) {}
  100. int PermissiveBits() const {
  101. return (is_specified && has_value) ? bits : 15;
  102. }
  103. int16_t bits;
  104. // True when "window bits" parameter appears in the parameters.
  105. bool is_specified;
  106. // True when "window bits" parameter has the value.
  107. bool has_value;
  108. };
  109. // |server_context_take_over_mode| is set to DO_NOT_TAKE_OVER_CONTEXT if and
  110. // only if |server_no_context_takeover| is set in the parameters.
  111. ContextTakeOverMode server_context_take_over_mode_ =
  112. WebSocketDeflater::TAKE_OVER_CONTEXT;
  113. // |client_context_take_over_mode| is set to DO_NOT_TAKE_OVER_CONTEXT if and
  114. // only if |client_no_context_takeover| is set in the parameters.
  115. ContextTakeOverMode client_context_take_over_mode_ =
  116. WebSocketDeflater::TAKE_OVER_CONTEXT;
  117. WindowBits server_max_window_bits_;
  118. WindowBits client_max_window_bits_;
  119. };
  120. } // namespace net
  121. #endif // NET_WEBSOCKETS_WEBSOCKET_DEFLATE_PARAMETERS_H_