| 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_bug_tracker.h" | 10 #include "net/spdy/spdy_bug_tracker.h" |
| 11 #include "net/spdy/spdy_framer.h" | 11 #include "net/spdy/spdy_framer.h" |
| 12 #include "net/spdy/spdy_protocol.h" | 12 #include "net/spdy/spdy_protocol.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 SpdyFrameBuilder::SpdyFrameBuilder(size_t size, SpdyMajorVersion version) | 16 SpdyFrameBuilder::SpdyFrameBuilder(size_t size) |
| 17 : buffer_(new char[size]), | 17 : buffer_(new char[size]), capacity_(size), length_(0), offset_(0) {} |
| 18 capacity_(size), | |
| 19 length_(0), | |
| 20 offset_(0), | |
| 21 version_(version) { | |
| 22 } | |
| 23 | 18 |
| 24 SpdyFrameBuilder::~SpdyFrameBuilder() { | 19 SpdyFrameBuilder::~SpdyFrameBuilder() { |
| 25 } | 20 } |
| 26 | 21 |
| 27 char* SpdyFrameBuilder::GetWritableBuffer(size_t length) { | 22 char* SpdyFrameBuilder::GetWritableBuffer(size_t length) { |
| 28 if (!CanWrite(length)) { | 23 if (!CanWrite(length)) { |
| 29 return NULL; | 24 return NULL; |
| 30 } | 25 } |
| 31 return buffer_.get() + offset_ + length_; | 26 return buffer_.get() + offset_ + length_; |
| 32 } | 27 } |
| 33 | 28 |
| 34 bool SpdyFrameBuilder::Seek(size_t length) { | 29 bool SpdyFrameBuilder::Seek(size_t length) { |
| 35 if (!CanWrite(length)) { | 30 if (!CanWrite(length)) { |
| 36 return false; | 31 return false; |
| 37 } | 32 } |
| 38 | 33 |
| 39 length_ += length; | 34 length_ += length; |
| 40 return true; | 35 return true; |
| 41 } | 36 } |
| 42 | 37 |
| 43 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, | 38 bool SpdyFrameBuilder::BeginNewFrame(const SpdyFramer& framer, |
| 44 SpdyFrameType type, | 39 SpdyFrameType type, |
| 45 uint8_t flags, | 40 uint8_t flags, |
| 46 SpdyStreamId stream_id) { | 41 SpdyStreamId stream_id) { |
| 47 DCHECK(SpdyConstants::IsValidFrameType( | 42 DCHECK( |
| 48 version_, SpdyConstants::SerializeFrameType(version_, type))); | 43 SpdyConstants::IsValidFrameType(SpdyConstants::SerializeFrameType(type))); |
| 49 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 44 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
| 50 DCHECK_EQ(HTTP2, framer.protocol_version()); | |
| 51 bool success = true; | 45 bool success = true; |
| 52 size_t frame_header_length = | 46 size_t frame_header_length = SpdyConstants::kFrameHeaderSize; |
| 53 SpdyConstants::GetFrameHeaderSize(framer.protocol_version()); | |
| 54 if (length_ > 0) { | 47 if (length_ > 0) { |
| 55 // Update length field for previous frame. | 48 // Update length field for previous frame. |
| 56 OverwriteLength(framer, length_ - frame_header_length); | 49 OverwriteLength(framer, length_ - frame_header_length); |
| 57 SPDY_BUG_IF(framer.GetFrameMaximumSize() < length_) | 50 SPDY_BUG_IF(framer.GetFrameMaximumSize() < length_) |
| 58 << "Frame length " << length_ | 51 << "Frame length " << length_ |
| 59 << " is longer than the maximum allowed length."; | 52 << " is longer than the maximum allowed length."; |
| 60 } | 53 } |
| 61 | 54 |
| 62 offset_ += length_; | 55 offset_ += length_; |
| 63 length_ = 0; | 56 length_ = 0; |
| 64 | 57 |
| 65 // Assume all remaining capacity will be used for this frame. If not, | 58 // Assume all remaining capacity will be used for this frame. If not, |
| 66 // the length will get overwritten when we begin the next frame. | 59 // the length will get overwritten when we begin the next frame. |
| 67 // Don't check for length limits here because this may be larger than the | 60 // Don't check for length limits here because this may be larger than the |
| 68 // actual frame length. | 61 // actual frame length. |
| 69 success &= WriteUInt24(capacity_ - offset_ - frame_header_length); | 62 success &= WriteUInt24(capacity_ - offset_ - frame_header_length); |
| 70 success &= WriteUInt8(SpdyConstants::SerializeFrameType(version_, type)); | 63 success &= WriteUInt8(SpdyConstants::SerializeFrameType(type)); |
| 71 success &= WriteUInt8(flags); | 64 success &= WriteUInt8(flags); |
| 72 success &= WriteUInt32(stream_id); | 65 success &= WriteUInt32(stream_id); |
| 73 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); | 66 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length_); |
| 74 return success; | 67 return success; |
| 75 } | 68 } |
| 76 | 69 |
| 77 bool SpdyFrameBuilder::WriteStringPiece16(const base::StringPiece& value) { | 70 bool SpdyFrameBuilder::WriteStringPiece16(const base::StringPiece& value) { |
| 78 if (value.size() > 0xffff) { | 71 if (value.size() > 0xffff) { |
| 79 DCHECK(false) << "Tried to write string with length > 16bit."; | 72 DCHECK(false) << "Tried to write string with length > 16bit."; |
| 80 return false; | 73 return false; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 const size_t old_length = length_; | 110 const size_t old_length = length_; |
| 118 | 111 |
| 119 length_ = 0; | 112 length_ = 0; |
| 120 success = WriteUInt24(length); | 113 success = WriteUInt24(length); |
| 121 | 114 |
| 122 length_ = old_length; | 115 length_ = old_length; |
| 123 return success; | 116 return success; |
| 124 } | 117 } |
| 125 | 118 |
| 126 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, uint8_t flags) { | 119 bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer, uint8_t flags) { |
| 127 DCHECK_EQ(HTTP2, framer.protocol_version()); | |
| 128 bool success = false; | 120 bool success = false; |
| 129 const size_t old_length = length_; | 121 const size_t old_length = length_; |
| 130 // Flags are the fifth octet in the frame prefix. | 122 // Flags are the fifth octet in the frame prefix. |
| 131 length_ = 4; | 123 length_ = 4; |
| 132 success = WriteUInt8(flags); | 124 success = WriteUInt8(flags); |
| 133 length_ = old_length; | 125 length_ = old_length; |
| 134 return success; | 126 return success; |
| 135 } | 127 } |
| 136 | 128 |
| 137 bool SpdyFrameBuilder::CanWrite(size_t length) const { | 129 bool SpdyFrameBuilder::CanWrite(size_t length) const { |
| 138 if (length > kLengthMask) { | 130 if (length > kLengthMask) { |
| 139 DCHECK(false); | 131 DCHECK(false); |
| 140 return false; | 132 return false; |
| 141 } | 133 } |
| 142 | 134 |
| 143 if (offset_ + length_ + length > capacity_) { | 135 if (offset_ + length_ + length > capacity_) { |
| 144 DCHECK(false); | 136 DCHECK(false); |
| 145 return false; | 137 return false; |
| 146 } | 138 } |
| 147 | 139 |
| 148 return true; | 140 return true; |
| 149 } | 141 } |
| 150 | 142 |
| 151 } // namespace net | 143 } // namespace net |
| OLD | NEW |