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 14 matching lines...) Expand all Loading... |
25 DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask)); | 25 DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask)); |
26 FlagsAndLength flags_length; | 26 FlagsAndLength flags_length; |
27 flags_length.length_ = htonl(static_cast<uint32>(length)); | 27 flags_length.length_ = htonl(static_cast<uint32>(length)); |
28 DCHECK_EQ(0, flags & ~kControlFlagsMask); | 28 DCHECK_EQ(0, flags & ~kControlFlagsMask); |
29 flags_length.flags_[0] = flags; | 29 flags_length.flags_[0] = flags; |
30 return flags_length; | 30 return flags_length; |
31 } | 31 } |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 SpdyFrameBuilder::SpdyFrameBuilder(size_t size, SpdyMajorVersion version) | 35 SpdyFrameBuilder::SpdyFrameBuilder(size_t size, SpdyMajorVersion version) |
36 : buffer_(new char[size]), | 36 : buffer_(new char[size]), |
37 capacity_(size), | 37 capacity_(size), |
38 length_(0), | 38 length_(0), |
39 offset_(0), | 39 offset_(0), |
40 version_(version) { | 40 version_(version) { |
41 } | 41 } |
42 | 42 |
43 SpdyFrameBuilder::~SpdyFrameBuilder() { | 43 SpdyFrameBuilder::~SpdyFrameBuilder() { |
44 } | 44 } |
45 | 45 |
(...skipping 10 matching lines...) Expand all 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_NE(-1, SpdyConstants::SerializeFrameType(version_, type)); |
67 SpdyConstants::SerializeFrameType(version_, type)); | |
68 bool success = true; | 67 bool success = true; |
69 FlagsAndLength flags_length = CreateFlagsAndLength( | 68 FlagsAndLength flags_length = CreateFlagsAndLength( |
70 flags, capacity_ - framer.GetControlFrameHeaderSize()); | 69 flags, capacity_ - framer.GetControlFrameHeaderSize()); |
71 success &= WriteUInt16(kControlFlagMask | | 70 success &= WriteUInt16(kControlFlagMask | |
72 SpdyConstants::SerializeMajorVersion(version_)); | 71 SpdyConstants::SerializeMajorVersion(version_)); |
73 success &= WriteUInt16( | 72 success &= WriteUInt16( |
74 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); | 73 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); |
75 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 74 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
76 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); | 75 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); |
77 return success; | 76 return success; |
(...skipping 16 matching lines...) Expand all Loading... |
94 flags_length.flags_[0] = flags; | 93 flags_length.flags_[0] = flags; |
95 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 94 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
96 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); | 95 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); |
97 return success; | 96 return success; |
98 } | 97 } |
99 | 98 |
100 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, | 99 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, |
101 SpdyFrameType type, | 100 SpdyFrameType type, |
102 uint8 flags, | 101 uint8 flags, |
103 SpdyStreamId stream_id) { | 102 SpdyStreamId stream_id) { |
104 DCHECK(SpdyConstants::IsValidFrameType(version_, | 103 DCHECK(SpdyConstants::IsValidFrameType( |
105 SpdyConstants::SerializeFrameType(version_, type))); | 104 version_, SpdyConstants::SerializeFrameType(version_, type))); |
106 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 105 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
107 DCHECK_LT(SPDY3, framer.protocol_version()); | 106 DCHECK_LT(SPDY3, framer.protocol_version()); |
108 bool success = true; | 107 bool success = true; |
109 if (length_ > 0) { | 108 if (length_ > 0) { |
110 // Update length field for previous frame. | 109 // Update length field for previous frame. |
111 OverwriteLength(framer, length_ - framer.GetPrefixLength(type)); | 110 OverwriteLength(framer, length_ - framer.GetPrefixLength(type)); |
112 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) | 111 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) |
113 << "Frame length " << length_ | 112 << "Frame length " << length_ |
114 << " is longer than the maximum allowed length."; | 113 << " is longer than the maximum allowed length."; |
115 } | 114 } |
116 | 115 |
117 offset_ += length_; | 116 offset_ += length_; |
118 length_ = 0; | 117 length_ = 0; |
119 | 118 |
120 // Assume all remaining capacity will be used for this frame. If not, | 119 // Assume all remaining capacity will be used for this frame. If not, |
121 // the length will get overwritten when we begin the next frame. | 120 // the length will get overwritten when we begin the next frame. |
122 // Don't check for length limits here because this may be larger than the | 121 // Don't check for length limits here because this may be larger than the |
123 // actual frame length. | 122 // actual frame length. |
124 success &= WriteUInt16(capacity_ - offset_ - framer.GetPrefixLength(type)); | 123 success &= WriteUInt16(capacity_ - offset_ - framer.GetPrefixLength(type)); |
125 success &= WriteUInt8( | 124 success &= WriteUInt8(SpdyConstants::SerializeFrameType(version_, type)); |
126 SpdyConstants::SerializeFrameType(version_, type)); | |
127 success &= WriteUInt8(flags); | 125 success &= WriteUInt8(flags); |
128 success &= WriteUInt32(stream_id); | 126 success &= WriteUInt32(stream_id); |
129 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); | 127 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); |
130 return success; | 128 return success; |
131 } | 129 } |
132 | 130 |
133 bool SpdyFrameBuilder::WriteString(const std::string& value) { | 131 bool SpdyFrameBuilder::WriteString(const std::string& value) { |
134 if (value.size() > 0xffff) { | 132 if (value.size() > 0xffff) { |
135 DCHECK(false) << "Tried to write string with length > 16bit."; | 133 DCHECK(false) << "Tried to write string with length > 16bit."; |
136 return false; | 134 return false; |
(...skipping 18 matching lines...) Expand all Loading... |
155 return false; | 153 return false; |
156 } | 154 } |
157 | 155 |
158 char* dest = GetWritableBuffer(data_len); | 156 char* dest = GetWritableBuffer(data_len); |
159 memcpy(dest, data, data_len); | 157 memcpy(dest, data, data_len); |
160 Seek(data_len); | 158 Seek(data_len); |
161 return true; | 159 return true; |
162 } | 160 } |
163 | 161 |
164 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { | 162 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { |
165 return OverwriteLength(framer, | 163 return OverwriteLength(framer, length_ - framer.GetControlFrameHeaderSize()); |
166 length_ - framer.GetControlFrameHeaderSize()); | |
167 } | 164 } |
168 | 165 |
169 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, | 166 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, |
170 size_t length) { | 167 size_t length) { |
171 if (version_ <= SPDY3) { | 168 if (version_ <= SPDY3) { |
172 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_) - | 169 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_) - |
173 framer.GetFrameMinimumSize(), | 170 framer.GetFrameMinimumSize(), |
174 length); | 171 length); |
175 } else { | 172 } else { |
176 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_), length); | 173 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_), length); |
177 } | 174 } |
178 bool success = false; | 175 bool success = false; |
179 const size_t old_length = length_; | 176 const size_t old_length = length_; |
180 | 177 |
181 if (version_ <= SPDY3) { | 178 if (version_ <= SPDY3) { |
182 FlagsAndLength flags_length = CreateFlagsAndLength( | 179 FlagsAndLength flags_length = CreateFlagsAndLength( |
183 0, // We're not writing over the flags value anyway. | 180 0, // We're not writing over the flags value anyway. |
184 length); | 181 length); |
185 | 182 |
186 // Write into the correct location by temporarily faking the offset. | 183 // Write into the correct location by temporarily faking the offset. |
187 length_ = 5; // Offset at which the length field occurs. | 184 length_ = 5; // Offset at which the length field occurs. |
188 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, | 185 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, |
189 sizeof(flags_length) - 1); | 186 sizeof(flags_length) - 1); |
190 } else { | 187 } else { |
191 length_ = 0; | 188 length_ = 0; |
192 success = WriteUInt16(length); | 189 success = WriteUInt16(length); |
193 } | 190 } |
194 | 191 |
195 length_ = old_length; | 192 length_ = old_length; |
196 return success; | 193 return success; |
197 } | 194 } |
198 | 195 |
199 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, | 196 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, uint8 flags) { |
200 uint8 flags) { | |
201 DCHECK_LT(SPDY3, framer.protocol_version()); | 197 DCHECK_LT(SPDY3, framer.protocol_version()); |
202 bool success = false; | 198 bool success = false; |
203 const size_t old_length = length_; | 199 const size_t old_length = length_; |
204 // Flags are the fourth octet in the frame prefix. | 200 // Flags are the fourth octet in the frame prefix. |
205 length_ = 3; | 201 length_ = 3; |
206 success = WriteUInt8(flags); | 202 success = WriteUInt8(flags); |
207 length_ = old_length; | 203 length_ = old_length; |
208 return success; | 204 return success; |
209 } | 205 } |
210 | 206 |
211 bool SpdyFrameBuilder::CanWrite(size_t length) const { | 207 bool SpdyFrameBuilder::CanWrite(size_t length) const { |
212 if (length > kLengthMask) { | 208 if (length > kLengthMask) { |
213 DCHECK(false); | 209 DCHECK(false); |
214 return false; | 210 return false; |
215 } | 211 } |
216 | 212 |
217 if (offset_ + length_ + length > capacity_) { | 213 if (offset_ + length_ + length > capacity_) { |
218 DCHECK(false); | 214 DCHECK(false); |
219 return false; | 215 return false; |
220 } | 216 } |
221 | 217 |
222 return true; | 218 return true; |
223 } | 219 } |
224 | 220 |
225 } // namespace net | 221 } // namespace net |
OLD | NEW |