OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "net/spdy/spdy_frame_builder.h" | 5 #include "net/spdy/spdy_frame_builder.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "net/spdy/spdy_framer.h" | 10 #include "net/spdy/spdy_framer.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 } | 56 } |
57 | 57 |
58 length_ += length; | 58 length_ += length; |
59 return true; | 59 return true; |
60 } | 60 } |
61 | 61 |
62 bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer, | 62 bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer, |
63 SpdyFrameType type, | 63 SpdyFrameType type, |
64 uint8 flags) { | 64 uint8 flags) { |
65 DCHECK_GE(SPDY3, version_); | 65 DCHECK_GE(SPDY3, version_); |
66 DCHECK_NE(-1, | 66 DCHECK(SpdyConstants::IsValidFrameType( |
67 SpdyConstants::SerializeFrameType(version_, type)); | 67 version_, SpdyConstants::SerializeFrameType(version_, type))); |
68 bool success = true; | 68 bool success = true; |
69 FlagsAndLength flags_length = CreateFlagsAndLength( | 69 FlagsAndLength flags_length = CreateFlagsAndLength( |
70 flags, capacity_ - framer.GetControlFrameHeaderSize()); | 70 flags, capacity_ - framer.GetControlFrameHeaderSize()); |
71 success &= WriteUInt16(kControlFlagMask | | 71 success &= WriteUInt16(kControlFlagMask | |
72 SpdyConstants::SerializeMajorVersion(version_)); | 72 SpdyConstants::SerializeMajorVersion(version_)); |
73 success &= WriteUInt16( | 73 success &= WriteUInt16( |
74 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); | 74 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); |
75 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 75 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
76 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); | 76 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); |
77 return success; | 77 return success; |
(...skipping 16 matching lines...) Expand all Loading... | |
94 flags_length.flags[0] = flags; | 94 flags_length.flags[0] = flags; |
95 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 95 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
96 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); | 96 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); |
97 return success; | 97 return success; |
98 } | 98 } |
99 | 99 |
100 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, | 100 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, |
101 SpdyFrameType type, | 101 SpdyFrameType type, |
102 uint8 flags, | 102 uint8 flags, |
103 SpdyStreamId stream_id) { | 103 SpdyStreamId stream_id) { |
104 DCHECK(SpdyConstants::IsValidFrameType(version_, | 104 DCHECK(SpdyConstants::IsValidFrameType( |
105 SpdyConstants::SerializeFrameType(version_, type))); | 105 version_, SpdyConstants::SerializeFrameType(version_, type))); |
106 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 106 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
107 DCHECK_LT(SPDY3, framer.protocol_version()); | 107 DCHECK_GT(framer.protocol_version(), SPDY3); |
108 bool success = true; | 108 bool success = true; |
109 if (length_ > 0) { | 109 if (length_ > 0) { |
110 // Update length field for previous frame. | 110 // Update length field for previous frame. |
111 OverwriteLength(framer, length_ - framer.GetPrefixLength(type)); | 111 OverwriteLength(framer, length_ - framer.GetPrefixLength(type)); |
112 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) | 112 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) |
113 << "Frame length " << length_ | 113 << "Frame length " << length_ |
114 << " is longer than the maximum allowed length."; | 114 << " is longer than the maximum allowed length."; |
115 } | 115 } |
116 | 116 |
117 offset_ += length_; | 117 offset_ += length_; |
(...skipping 11 matching lines...) Expand all Loading... | |
129 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); | 129 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); |
130 return success; | 130 return success; |
131 } | 131 } |
132 | 132 |
133 bool SpdyFrameBuilder::WriteString(const std::string& value) { | 133 bool SpdyFrameBuilder::WriteString(const std::string& value) { |
134 if (value.size() > 0xffff) { | 134 if (value.size() > 0xffff) { |
135 DCHECK(false) << "Tried to write string with length > 16bit."; | 135 DCHECK(false) << "Tried to write string with length > 16bit."; |
136 return false; | 136 return false; |
137 } | 137 } |
138 | 138 |
139 if (!WriteUInt16(static_cast<int>(value.size()))) | 139 if (!WriteUInt16(static_cast<uint16>(value.size()))) |
140 return false; | 140 return false; |
141 | 141 |
142 return WriteBytes(value.data(), static_cast<uint16>(value.size())); | 142 return WriteBytes(value.data(), static_cast<uint16>(value.size())); |
143 } | 143 } |
144 | 144 |
145 bool SpdyFrameBuilder::WriteStringPiece32(const base::StringPiece& value) { | 145 bool SpdyFrameBuilder::WriteStringPiece32(const base::StringPiece& value) { |
146 if (!WriteUInt32(value.size())) { | 146 if (!WriteUInt32(value.size())) { |
147 return false; | 147 return false; |
148 } | 148 } |
149 | 149 |
(...skipping 11 matching lines...) Expand all Loading... | |
161 return true; | 161 return true; |
162 } | 162 } |
163 | 163 |
164 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { | 164 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { |
165 return OverwriteLength(framer, | 165 return OverwriteLength(framer, |
166 length_ - framer.GetControlFrameHeaderSize()); | 166 length_ - framer.GetControlFrameHeaderSize()); |
167 } | 167 } |
168 | 168 |
169 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, | 169 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, |
170 size_t length) { | 170 size_t length) { |
171 if (version_ <= SPDY3) { | 171 if (version_ < SPDY4) { |
Peter Kasting
2014/10/08 19:21:56
This is just a consistency change -- all the DCHEC
| |
172 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_) - | 172 DCHECK_LE(length, |
173 framer.GetFrameMinimumSize(), | 173 SpdyConstants::GetFrameMaximumSize(version_) - |
174 length); | 174 framer.GetFrameMinimumSize()); |
175 } else { | 175 } else { |
176 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_), length); | 176 DCHECK_LE(length, SpdyConstants::GetFrameMaximumSize(version_)); |
177 } | 177 } |
178 bool success = false; | 178 bool success = false; |
179 const size_t old_length = length_; | 179 const size_t old_length = length_; |
180 | 180 |
181 if (version_ <= SPDY3) { | 181 if (version_ < SPDY4) { |
182 FlagsAndLength flags_length = CreateFlagsAndLength( | 182 FlagsAndLength flags_length = CreateFlagsAndLength( |
183 0, // We're not writing over the flags value anyway. | 183 0, // We're not writing over the flags value anyway. |
184 length); | 184 length); |
185 | 185 |
186 // Write into the correct location by temporarily faking the offset. | 186 // Write into the correct location by temporarily faking the offset. |
187 length_ = 5; // Offset at which the length field occurs. | 187 length_ = 5; // Offset at which the length field occurs. |
188 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, | 188 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, |
189 sizeof(flags_length) - 1); | 189 sizeof(flags_length) - 1); |
190 } else { | 190 } else { |
191 length_ = 0; | 191 length_ = 0; |
192 success = WriteUInt24(length); | 192 success = WriteUInt24(length); |
193 } | 193 } |
194 | 194 |
195 length_ = old_length; | 195 length_ = old_length; |
196 return success; | 196 return success; |
197 } | 197 } |
198 | 198 |
199 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, | 199 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, |
200 uint8 flags) { | 200 uint8 flags) { |
201 DCHECK_LT(SPDY3, framer.protocol_version()); | 201 DCHECK_GT(framer.protocol_version(), SPDY3); |
202 bool success = false; | 202 bool success = false; |
203 const size_t old_length = length_; | 203 const size_t old_length = length_; |
204 // Flags are the fifth octet in the frame prefix. | 204 // Flags are the fifth octet in the frame prefix. |
205 length_ = 4; | 205 length_ = 4; |
206 success = WriteUInt8(flags); | 206 success = WriteUInt8(flags); |
207 length_ = old_length; | 207 length_ = old_length; |
208 return success; | 208 return success; |
209 } | 209 } |
210 | 210 |
211 bool SpdyFrameBuilder::CanWrite(size_t length) const { | 211 bool SpdyFrameBuilder::CanWrite(size_t length) const { |
212 if (length > kLengthMask) { | 212 if (length > kLengthMask) { |
213 DCHECK(false); | 213 DCHECK(false); |
214 return false; | 214 return false; |
215 } | 215 } |
216 | 216 |
217 if (offset_ + length_ + length > capacity_) { | 217 if (offset_ + length_ + length > capacity_) { |
218 DCHECK(false); | 218 DCHECK(false); |
219 return false; | 219 return false; |
220 } | 220 } |
221 | 221 |
222 return true; | 222 return true; |
223 } | 223 } |
224 | 224 |
225 } // namespace net | 225 } // namespace net |
OLD | NEW |