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