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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 55 |
56 length_ += length; | 56 length_ += length; |
57 return true; | 57 return true; |
58 } | 58 } |
59 | 59 |
60 bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer, | 60 bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer, |
61 SpdyFrameType type, | 61 SpdyFrameType type, |
62 uint8 flags) { | 62 uint8 flags) { |
63 DCHECK_GE(type, FIRST_CONTROL_TYPE); | 63 DCHECK_GE(type, FIRST_CONTROL_TYPE); |
64 DCHECK_LE(type, LAST_CONTROL_TYPE); | 64 DCHECK_LE(type, LAST_CONTROL_TYPE); |
| 65 DCHECK_GT(4, framer.protocol_version()); |
65 bool success = true; | 66 bool success = true; |
66 if (framer.protocol_version() < 4) { | 67 FlagsAndLength flags_length = CreateFlagsAndLength( |
67 FlagsAndLength flags_length = CreateFlagsAndLength( | 68 flags, capacity_ - framer.GetControlFrameHeaderSize()); |
68 flags, capacity_ - framer.GetControlFrameHeaderSize()); | 69 success &= WriteUInt16(kControlFlagMask | framer.protocol_version()); |
69 success &= WriteUInt16(kControlFlagMask | framer.protocol_version()); | 70 success &= WriteUInt16(type); |
70 success &= WriteUInt16(type); | 71 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
71 success &= WriteBytes(&flags_length, sizeof(flags_length)); | |
72 } else { | |
73 DCHECK_GT(1u<<16, capacity_); // Make sure length fits in 2B. | |
74 success &= WriteUInt16(capacity_); | |
75 success &= WriteUInt8(type); | |
76 success &= WriteUInt8(flags); | |
77 } | |
78 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); | 72 DCHECK_EQ(framer.GetControlFrameHeaderSize(), length()); |
79 return success; | 73 return success; |
80 } | 74 } |
81 | 75 |
82 bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer, | 76 bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer, |
83 SpdyStreamId stream_id, | 77 SpdyStreamId stream_id, |
84 SpdyDataFlags flags) { | 78 SpdyDataFlags flags) { |
| 79 if (framer.protocol_version() >= 4) { |
| 80 return WriteFramePrefix(framer, DATA, flags, stream_id); |
| 81 } |
85 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); | 82 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
86 bool success = true; | 83 bool success = true; |
87 if (framer.protocol_version() < 4) { | 84 success &= WriteUInt32(stream_id); |
88 success &= WriteUInt32(stream_id); | 85 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); |
89 size_t length_field = capacity_ - framer.GetDataFrameMinimumSize(); | 86 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); |
90 DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask)); | 87 FlagsAndLength flags_length; |
91 FlagsAndLength flags_length; | 88 flags_length.length_ = htonl(length_field); |
92 flags_length.length_ = htonl(length_field); | 89 DCHECK_EQ(0, flags & ~kDataFlagsMask); |
93 DCHECK_EQ(0, flags & ~kDataFlagsMask); | 90 flags_length.flags_[0] = flags; |
94 flags_length.flags_[0] = flags; | 91 success &= WriteBytes(&flags_length, sizeof(flags_length)); |
95 success &= WriteBytes(&flags_length, sizeof(flags_length)); | |
96 } else { | |
97 DCHECK_GT(1u<<16, capacity_); // Make sure length fits in 2B. | |
98 success &= WriteUInt16(capacity_); | |
99 success &= WriteUInt8(0); | |
100 success &= WriteUInt8(flags); | |
101 success &= WriteUInt32(stream_id); | |
102 } | |
103 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); | 92 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); |
104 return success; | 93 return success; |
105 } | 94 } |
| 95 |
| 96 bool SpdyFrameBuilder::WriteFramePrefix(const SpdyFramer& framer, |
| 97 SpdyFrameType type, |
| 98 uint8 flags, |
| 99 SpdyStreamId stream_id) { |
| 100 DCHECK_LE(DATA, type); |
| 101 DCHECK_GE(LAST_CONTROL_TYPE, type); |
| 102 DCHECK_EQ(0u, stream_id & ~kStreamIdMask); |
| 103 DCHECK_LE(4, framer.protocol_version()); |
| 104 bool success = true; |
| 105 DCHECK_GT(1u<<16, capacity_); // Make sure length fits in 2B. |
| 106 success &= WriteUInt16(capacity_); |
| 107 success &= WriteUInt8(type); |
| 108 success &= WriteUInt8(flags); |
| 109 success &= WriteUInt32(stream_id); |
| 110 DCHECK_EQ(framer.GetDataFrameMinimumSize(), length()); |
| 111 return success; |
| 112 } |
106 | 113 |
107 bool SpdyFrameBuilder::WriteString(const std::string& value) { | 114 bool SpdyFrameBuilder::WriteString(const std::string& value) { |
108 if (value.size() > 0xffff) { | 115 if (value.size() > 0xffff) { |
109 DCHECK(false) << "Tried to write string with length > 16bit."; | 116 DCHECK(false) << "Tried to write string with length > 16bit."; |
110 return false; | 117 return false; |
111 } | 118 } |
112 | 119 |
113 if (!WriteUInt16(static_cast<int>(value.size()))) | 120 if (!WriteUInt16(static_cast<int>(value.size()))) |
114 return false; | 121 return false; |
115 | 122 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 182 |
176 if (length_ + length > capacity_) { | 183 if (length_ + length > capacity_) { |
177 DCHECK(false); | 184 DCHECK(false); |
178 return false; | 185 return false; |
179 } | 186 } |
180 | 187 |
181 return true; | 188 return true; |
182 } | 189 } |
183 | 190 |
184 } // namespace net | 191 } // namespace net |
OLD | NEW |