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) | 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), |
| 40 version_(version) { |
39 } | 41 } |
40 | 42 |
41 SpdyFrameBuilder::~SpdyFrameBuilder() { | 43 SpdyFrameBuilder::~SpdyFrameBuilder() { |
42 } | 44 } |
43 | 45 |
44 char* SpdyFrameBuilder::GetWritableBuffer(size_t length) { | 46 char* SpdyFrameBuilder::GetWritableBuffer(size_t length) { |
45 if (!CanWrite(length)) { | 47 if (!CanWrite(length)) { |
46 return NULL; | 48 return NULL; |
47 } | 49 } |
48 return buffer_.get() + length_; | 50 return buffer_.get() + offset_ + length_; |
49 } | 51 } |
50 | 52 |
51 bool SpdyFrameBuilder::Seek(size_t length) { | 53 bool SpdyFrameBuilder::Seek(size_t length) { |
52 if (!CanWrite(length)) { | 54 if (!CanWrite(length)) { |
53 return false; | 55 return false; |
54 } | 56 } |
55 | 57 |
56 length_ += length; | 58 length_ += length; |
57 return true; | 59 return true; |
58 } | 60 } |
(...skipping 12 matching lines...) Expand all Loading... |
71 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); | 73 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); |
72 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 74 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
73 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); | 75 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); |
74 return success; | 76 return success; |
75 } | 77 } |
76 | 78 |
77 bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer, | 79 bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer, |
78 SpdyStreamId stream_id, | 80 SpdyStreamId stream_id, |
79 uint8 flags) { | 81 uint8 flags) { |
80 if (framer.protocol_version() >= 4) { | 82 if (framer.protocol_version() >= 4) { |
81 return WriteFramePrefix(framer, DATA, flags, stream_id); | 83 return BeginNewFrame(framer, DATA, flags, stream_id); |
82 } | 84 } |
83 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 85 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
84 bool success = true; | 86 bool success = true; |
85 success &= WriteUInt32(stream_id); | 87 success &= WriteUInt32(stream_id); |
86 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); | 88 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); |
87 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); | 89 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); |
88 FlagsAndLength flags_length; | 90 FlagsAndLength flags_length; |
89 flags_length.length_ = htonl(length_field); | 91 flags_length.length_ = htonl(length_field); |
90 DCHECK_EQ(0, flags & ~kDataFlagsMask); | 92 DCHECK_EQ(0, flags & ~kDataFlagsMask); |
91 flags_length.flags_[0] = flags; | 93 flags_length.flags_[0] = flags; |
92 success &= WriteBytes(&flags_length, sizeof(flags_length)); | 94 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
93 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); | 95 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); |
94 return success; | 96 return success; |
95 } | 97 } |
96 | 98 |
97 bool SpdyFrameBuilder::WriteFramePrefix(const SpdyFramer& framer, | 99 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, |
98 SpdyFrameType type, | 100 SpdyFrameType type, |
99 uint8 flags, | 101 uint8 flags, |
100 SpdyStreamId stream_id) { | 102 SpdyStreamId stream_id) { |
101 DCHECK_NE(-1, | 103 DCHECK(SpdyConstants::IsValidFrameType(version_, |
102 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); | 104 SpdyConstants::SerializeFrameType(version_, type))); |
103 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 105 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
104 DCHECK_LE(4, framer.protocol_version()); | 106 DCHECK_LE(4, version_); |
| 107 |
105 bool success = true; | 108 bool success = true; |
106 // Upstream DCHECK's that capacity_ is under the maximum frame size at this | 109 if (length_ > 0) { |
107 // point. Chromium does not, because of the large additional zlib inflation | 110 // Update length field for previous frame. |
108 // factor we use. (Frame size is is still checked by OverwriteLength() below). | 111 OverwriteLength(framer, length_ - framer.GetPrefixLength(type)); |
109 if (type != DATA) { | 112 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) |
110 success &= WriteUInt16(capacity_ - framer.GetControlFrameHeaderSize()); | 113 << "Frame length " << length_ |
111 } else { | 114 << " is longer than the maximum allowed length."; |
112 success &= WriteUInt16(capacity_ - framer.GetDataFrameMinimumSize()); | |
113 } | 115 } |
| 116 |
| 117 offset_ += length_; |
| 118 length_ = 0; |
| 119 |
| 120 // Assume all remaining capacity will be used for this frame. If not, |
| 121 // 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 |
| 123 // actual frame length. |
| 124 success &= WriteUInt16(capacity_ - offset_ - framer.GetPrefixLength(type)); |
114 success &= WriteUInt8( | 125 success &= WriteUInt8( |
115 SpdyConstants::SerializeFrameType(framer.protocol_version(), type)); | 126 SpdyConstants::SerializeFrameType(version_, type)); |
116 success &= WriteUInt8(flags); | 127 success &= WriteUInt8(flags); |
117 success &= WriteUInt32(stream_id); | 128 success &= WriteUInt32(stream_id); |
118 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); | 129 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); |
119 return success; | 130 return success; |
120 } | 131 } |
121 | 132 |
122 bool SpdyFrameBuilder::WriteString(const std::string& value) { | 133 bool SpdyFrameBuilder::WriteString(const std::string& value) { |
123 if (value.size() > 0xffff) { | 134 if (value.size() > 0xffff) { |
124 DCHECK(false) << "Tried to write string with length > 16bit."; | 135 DCHECK(false) << "Tried to write string with length > 16bit."; |
125 return false; | 136 return false; |
126 } | 137 } |
127 | 138 |
128 if (!WriteUInt16(static_cast<int>(value.size()))) | 139 if (!WriteUInt16(static_cast<int>(value.size()))) |
(...skipping 21 matching lines...) Expand all Loading... |
150 return true; | 161 return true; |
151 } | 162 } |
152 | 163 |
153 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { | 164 bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) { |
154 return OverwriteLength(framer, | 165 return OverwriteLength(framer, |
155 length_ - framer.GetControlFrameHeaderSize()); | 166 length_ - framer.GetControlFrameHeaderSize()); |
156 } | 167 } |
157 | 168 |
158 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, | 169 bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer, |
159 size_t length) { | 170 size_t length) { |
160 if (framer.protocol_version() < 4) { | 171 if (version_ < 4) { |
161 DCHECK_GE(framer.GetFrameMaximumSize() - framer.GetFrameMinimumSize(), | 172 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_) - |
| 173 framer.GetFrameMinimumSize(), |
162 length); | 174 length); |
163 } else { | 175 } else { |
164 DCHECK_GE(framer.GetFrameMaximumSize(), length); | 176 DCHECK_GE(SpdyConstants::GetFrameMaximumSize(version_), length); |
165 } | 177 } |
166 bool success = false; | 178 bool success = false; |
167 const size_t old_length = length_; | 179 const size_t old_length = length_; |
168 | 180 |
169 if (framer.protocol_version() < 4) { | 181 if (version_ < 4) { |
170 FlagsAndLength flags_length = CreateFlagsAndLength( | 182 FlagsAndLength flags_length = CreateFlagsAndLength( |
171 0, // We're not writing over the flags value anyway. | 183 0, // We're not writing over the flags value anyway. |
172 length); | 184 length); |
173 | 185 |
174 // Write into the correct location by temporarily faking the offset. | 186 // Write into the correct location by temporarily faking the offset. |
175 length_ = 5; // Offset at which the length field occurs. | 187 length_ = 5; // Offset at which the length field occurs. |
176 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, | 188 success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1, |
177 sizeof(flags_length) - 1); | 189 sizeof(flags_length) - 1); |
178 } else { | 190 } else { |
179 length_ = 0; | 191 length_ = 0; |
(...skipping 15 matching lines...) Expand all Loading... |
195 length_ = old_length; | 207 length_ = old_length; |
196 return success; | 208 return success; |
197 } | 209 } |
198 | 210 |
199 bool SpdyFrameBuilder::CanWrite(size_t length) const { | 211 bool SpdyFrameBuilder::CanWrite(size_t length) const { |
200 if (length > kLengthMask) { | 212 if (length > kLengthMask) { |
201 DCHECK(false); | 213 DCHECK(false); |
202 return false; | 214 return false; |
203 } | 215 } |
204 | 216 |
205 if (length_ + length > capacity_) { | 217 if (offset_ + length_ + length > capacity_) { |
206 DCHECK(false); | 218 DCHECK(false); |
207 return false; | 219 return false; |
208 } | 220 } |
209 | 221 |
210 return true; | 222 return true; |
211 } | 223 } |
212 | 224 |
213 } // namespace net | 225 } // namespace net |
OLD | NEW |