| 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 #ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_ | 5 #ifndef NET_SPDY_SPDY_FRAME_BUILDER_H_ |
| 6 #define NET_SPDY_SPDY_FRAME_BUILDER_H_ | 6 #define NET_SPDY_SPDY_FRAME_BUILDER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 8 #include <string> | 11 #include <string> |
| 9 | 12 |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 13 #include "base/sys_byteorder.h" | 15 #include "base/sys_byteorder.h" |
| 14 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
| 15 #include "net/spdy/spdy_protocol.h" | 17 #include "net/spdy/spdy_protocol.h" |
| 16 | 18 |
| 17 namespace net { | 19 namespace net { |
| 18 | 20 |
| 19 class SpdyFramer; | 21 class SpdyFramer; |
| 20 | 22 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 47 // Seeks forward by the given number of bytes. Useful in conjunction with | 49 // Seeks forward by the given number of bytes. Useful in conjunction with |
| 48 // GetWriteableBuffer() above. | 50 // GetWriteableBuffer() above. |
| 49 bool Seek(size_t length); | 51 bool Seek(size_t length); |
| 50 | 52 |
| 51 // Populates this frame with a SPDY control frame header using | 53 // Populates this frame with a SPDY control frame header using |
| 52 // version-specific information from the |framer| and length information from | 54 // version-specific information from the |framer| and length information from |
| 53 // capacity_. The given type must be a control frame type. | 55 // capacity_. The given type must be a control frame type. |
| 54 // Used only for SPDY versions <4. | 56 // Used only for SPDY versions <4. |
| 55 bool WriteControlFrameHeader(const SpdyFramer& framer, | 57 bool WriteControlFrameHeader(const SpdyFramer& framer, |
| 56 SpdyFrameType type, | 58 SpdyFrameType type, |
| 57 uint8 flags); | 59 uint8_t flags); |
| 58 | 60 |
| 59 // Populates this frame with a SPDY data frame header using version-specific | 61 // Populates this frame with a SPDY data frame header using version-specific |
| 60 // information from the |framer| and length information from capacity_. | 62 // information from the |framer| and length information from capacity_. |
| 61 bool WriteDataFrameHeader(const SpdyFramer& framer, | 63 bool WriteDataFrameHeader(const SpdyFramer& framer, |
| 62 SpdyStreamId stream_id, | 64 SpdyStreamId stream_id, |
| 63 uint8 flags); | 65 uint8_t flags); |
| 64 | 66 |
| 65 // Populates this frame with a SPDY4/HTTP2 frame prefix using | 67 // Populates this frame with a SPDY4/HTTP2 frame prefix using |
| 66 // version-specific information from the |framer| and length information from | 68 // version-specific information from the |framer| and length information from |
| 67 // capacity_. The given type must be a control frame type. | 69 // capacity_. The given type must be a control frame type. |
| 68 // Used only for SPDY versions >=4. | 70 // Used only for SPDY versions >=4. |
| 69 bool BeginNewFrame(const SpdyFramer& framer, | 71 bool BeginNewFrame(const SpdyFramer& framer, |
| 70 SpdyFrameType type, | 72 SpdyFrameType type, |
| 71 uint8 flags, | 73 uint8_t flags, |
| 72 SpdyStreamId stream_id); | 74 SpdyStreamId stream_id); |
| 73 | 75 |
| 74 // Takes the buffer from the SpdyFrameBuilder. | 76 // Takes the buffer from the SpdyFrameBuilder. |
| 75 SpdyFrame* take() { | 77 SpdyFrame* take() { |
| 76 if (version_ > SPDY3) { | 78 if (version_ > SPDY3) { |
| 77 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) | 79 DLOG_IF(DFATAL, SpdyConstants::GetFrameMaximumSize(version_) < length_) |
| 78 << "Frame length " << length_ | 80 << "Frame length " << length_ |
| 79 << " is longer than the maximum allowed length."; | 81 << " is longer than the maximum allowed length."; |
| 80 } | 82 } |
| 81 SpdyFrame* rv = new SpdyFrame(buffer_.release(), length(), true); | 83 SpdyFrame* rv = new SpdyFrame(buffer_.release(), length(), true); |
| 82 capacity_ = 0; | 84 capacity_ = 0; |
| 83 length_ = 0; | 85 length_ = 0; |
| 84 offset_ = 0; | 86 offset_ = 0; |
| 85 return rv; | 87 return rv; |
| 86 } | 88 } |
| 87 | 89 |
| 88 // Methods for adding to the payload. These values are appended to the end | 90 // Methods for adding to the payload. These values are appended to the end |
| 89 // of the SpdyFrameBuilder payload. Note - binary integers are converted from | 91 // of the SpdyFrameBuilder payload. Note - binary integers are converted from |
| 90 // host to network form. | 92 // host to network form. |
| 91 bool WriteUInt8(uint8 value) { | 93 bool WriteUInt8(uint8_t value) { return WriteBytes(&value, sizeof(value)); } |
| 92 return WriteBytes(&value, sizeof(value)); | 94 bool WriteUInt16(uint16_t value) { |
| 93 } | |
| 94 bool WriteUInt16(uint16 value) { | |
| 95 value = base::HostToNet16(value); | 95 value = base::HostToNet16(value); |
| 96 return WriteBytes(&value, sizeof(value)); | 96 return WriteBytes(&value, sizeof(value)); |
| 97 } | 97 } |
| 98 bool WriteUInt24(uint32 value) { | 98 bool WriteUInt24(uint32_t value) { |
| 99 value = base::HostToNet32(value); | 99 value = base::HostToNet32(value); |
| 100 return WriteBytes(reinterpret_cast<char*>(&value) + 1, | 100 return WriteBytes(reinterpret_cast<char*>(&value) + 1, |
| 101 sizeof(value) - 1); | 101 sizeof(value) - 1); |
| 102 } | 102 } |
| 103 bool WriteUInt32(uint32 value) { | 103 bool WriteUInt32(uint32_t value) { |
| 104 value = base::HostToNet32(value); | 104 value = base::HostToNet32(value); |
| 105 return WriteBytes(&value, sizeof(value)); | 105 return WriteBytes(&value, sizeof(value)); |
| 106 } | 106 } |
| 107 bool WriteUInt64(uint64 value) { | 107 bool WriteUInt64(uint64_t value) { |
| 108 uint32 upper = base::HostToNet32(static_cast<uint32>(value >> 32)); | 108 uint32_t upper = base::HostToNet32(static_cast<uint32_t>(value >> 32)); |
| 109 uint32 lower = base::HostToNet32(static_cast<uint32>(value)); | 109 uint32_t lower = base::HostToNet32(static_cast<uint32_t>(value)); |
| 110 return (WriteBytes(&upper, sizeof(upper)) && | 110 return (WriteBytes(&upper, sizeof(upper)) && |
| 111 WriteBytes(&lower, sizeof(lower))); | 111 WriteBytes(&lower, sizeof(lower))); |
| 112 } | 112 } |
| 113 bool WriteStringPiece16(const base::StringPiece& value); | 113 bool WriteStringPiece16(const base::StringPiece& value); |
| 114 bool WriteStringPiece32(const base::StringPiece& value); | 114 bool WriteStringPiece32(const base::StringPiece& value); |
| 115 bool WriteBytes(const void* data, uint32 data_len); | 115 bool WriteBytes(const void* data, uint32_t data_len); |
| 116 | 116 |
| 117 // Update (in-place) the length field in the frame being built to reflect the | 117 // Update (in-place) the length field in the frame being built to reflect the |
| 118 // current actual length of bytes written to said frame through this builder. | 118 // current actual length of bytes written to said frame through this builder. |
| 119 // The framer parameter is used to determine version-specific location and | 119 // The framer parameter is used to determine version-specific location and |
| 120 // size information of the length field to be written, and must be initialized | 120 // size information of the length field to be written, and must be initialized |
| 121 // with the correct version for the frame being written. | 121 // with the correct version for the frame being written. |
| 122 bool RewriteLength(const SpdyFramer& framer); | 122 bool RewriteLength(const SpdyFramer& framer); |
| 123 | 123 |
| 124 // Update (in-place) the length field in the frame being built to reflect the | 124 // Update (in-place) the length field in the frame being built to reflect the |
| 125 // given length. | 125 // given length. |
| 126 // The framer parameter is used to determine version-specific location and | 126 // The framer parameter is used to determine version-specific location and |
| 127 // size information of the length field to be written, and must be initialized | 127 // size information of the length field to be written, and must be initialized |
| 128 // with the correct version for the frame being written. | 128 // with the correct version for the frame being written. |
| 129 bool OverwriteLength(const SpdyFramer& framer, size_t length); | 129 bool OverwriteLength(const SpdyFramer& framer, size_t length); |
| 130 | 130 |
| 131 // Update (in-place) the flags field in the frame being built to reflect the | 131 // Update (in-place) the flags field in the frame being built to reflect the |
| 132 // given flags value. | 132 // given flags value. |
| 133 // Used only for SPDY versions >=4. | 133 // Used only for SPDY versions >=4. |
| 134 bool OverwriteFlags(const SpdyFramer& framer, uint8 flags); | 134 bool OverwriteFlags(const SpdyFramer& framer, uint8_t flags); |
| 135 | 135 |
| 136 private: | 136 private: |
| 137 // Checks to make sure that there is an appropriate amount of space for a | 137 // Checks to make sure that there is an appropriate amount of space for a |
| 138 // write of given size, in bytes. | 138 // write of given size, in bytes. |
| 139 bool CanWrite(size_t length) const; | 139 bool CanWrite(size_t length) const; |
| 140 | 140 |
| 141 scoped_ptr<char[]> buffer_; | 141 scoped_ptr<char[]> buffer_; |
| 142 size_t capacity_; // Allocation size of payload, set by constructor. | 142 size_t capacity_; // Allocation size of payload, set by constructor. |
| 143 size_t length_; // Length of the latest frame in the buffer. | 143 size_t length_; // Length of the latest frame in the buffer. |
| 144 size_t offset_; // Position at which the latest frame begins. | 144 size_t offset_; // Position at which the latest frame begins. |
| 145 | 145 |
| 146 const SpdyMajorVersion version_; | 146 const SpdyMajorVersion version_; |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 } // namespace net | 149 } // namespace net |
| 150 | 150 |
| 151 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ | 151 #endif // NET_SPDY_SPDY_FRAME_BUILDER_H_ |
| OLD | NEW |