This source file includes following definitions.
- CreateFlagsAndLength
- length_
- GetWritableBuffer
- Seek
- WriteControlFrameHeader
- WriteDataFrameHeader
- WriteFramePrefix
- WriteString
- WriteStringPiece32
- WriteBytes
- RewriteLength
- OverwriteLength
- OverwriteFlags
- CanWrite
#include "net/spdy/spdy_frame_builder.h"
#include <limits>
#include "base/logging.h"
#include "net/spdy/spdy_framer.h"
#include "net/spdy/spdy_protocol.h"
namespace net {
namespace {
union FlagsAndLength {
uint8 flags_[4];
uint32 length_;
};
FlagsAndLength CreateFlagsAndLength(uint8 flags, size_t length) {
DCHECK_EQ(0u, length & ~static_cast<size_t>(kLengthMask));
FlagsAndLength flags_length;
flags_length.length_ = htonl(static_cast<uint32>(length));
DCHECK_EQ(0, flags & ~kControlFlagsMask);
flags_length.flags_[0] = flags;
return flags_length;
}
}
SpdyFrameBuilder::SpdyFrameBuilder(size_t size)
: buffer_(new char[size]),
capacity_(size),
length_(0) {
}
SpdyFrameBuilder::~SpdyFrameBuilder() {
}
char* SpdyFrameBuilder::GetWritableBuffer(size_t length) {
if (!CanWrite(length)) {
return NULL;
}
return buffer_.get() + length_;
}
bool SpdyFrameBuilder::Seek(size_t length) {
if (!CanWrite(length)) {
return false;
}
length_ += length;
return true;
}
bool SpdyFrameBuilder::WriteControlFrameHeader(const SpdyFramer& framer,
SpdyFrameType type,
uint8 flags) {
DCHECK_GT(4, framer.protocol_version());
DCHECK_NE(-1,
SpdyConstants::SerializeFrameType(framer.protocol_version(), type));
bool success = true;
FlagsAndLength flags_length = CreateFlagsAndLength(
flags, capacity_ - framer.GetControlFrameHeaderSize());
success &= WriteUInt16(kControlFlagMask | framer.protocol_version());
success &= WriteUInt16(
SpdyConstants::SerializeFrameType(framer.protocol_version(), type));
success &= WriteBytes(&flags_length, sizeof(flags_length));
DCHECK_EQ(framer.GetControlFrameHeaderSize(), length());
return success;
}
bool SpdyFrameBuilder::WriteDataFrameHeader(const SpdyFramer& framer,
SpdyStreamId stream_id,
uint8 flags) {
if (framer.protocol_version() >= 4) {
return WriteFramePrefix(framer, DATA, flags, stream_id);
}
DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
bool success = true;
success &= WriteUInt32(stream_id);
size_t length_field = capacity_ - framer.GetDataFrameMinimumSize();
DCHECK_EQ(0u, length_field & ~static_cast<size_t>(kLengthMask));
FlagsAndLength flags_length;
flags_length.length_ = htonl(length_field);
DCHECK_EQ(0, flags & ~kDataFlagsMask);
flags_length.flags_[0] = flags;
success &= WriteBytes(&flags_length, sizeof(flags_length));
DCHECK_EQ(framer.GetDataFrameMinimumSize(), length());
return success;
}
bool SpdyFrameBuilder::WriteFramePrefix(const SpdyFramer& framer,
SpdyFrameType type,
uint8 flags,
SpdyStreamId stream_id) {
DCHECK_NE(-1,
SpdyConstants::SerializeFrameType(framer.protocol_version(), type));
DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
DCHECK_LE(4, framer.protocol_version());
bool success = true;
if (type != DATA) {
success &= WriteUInt16(capacity_ - framer.GetControlFrameHeaderSize());
} else {
success &= WriteUInt16(capacity_ - framer.GetDataFrameMinimumSize());
}
success &= WriteUInt8(
SpdyConstants::SerializeFrameType(framer.protocol_version(), type));
success &= WriteUInt8(flags);
success &= WriteUInt32(stream_id);
DCHECK_EQ(framer.GetDataFrameMinimumSize(), length());
return success;
}
bool SpdyFrameBuilder::WriteString(const std::string& value) {
if (value.size() > 0xffff) {
DCHECK(false) << "Tried to write string with length > 16bit.";
return false;
}
if (!WriteUInt16(static_cast<int>(value.size())))
return false;
return WriteBytes(value.data(), static_cast<uint16>(value.size()));
}
bool SpdyFrameBuilder::WriteStringPiece32(const base::StringPiece& value) {
if (!WriteUInt32(value.size())) {
return false;
}
return WriteBytes(value.data(), value.size());
}
bool SpdyFrameBuilder::WriteBytes(const void* data, uint32 data_len) {
if (!CanWrite(data_len)) {
return false;
}
char* dest = GetWritableBuffer(data_len);
memcpy(dest, data, data_len);
Seek(data_len);
return true;
}
bool SpdyFrameBuilder::RewriteLength(const SpdyFramer& framer) {
return OverwriteLength(framer,
length_ - framer.GetControlFrameHeaderSize());
}
bool SpdyFrameBuilder::OverwriteLength(const SpdyFramer& framer,
size_t length) {
if (framer.protocol_version() < 4) {
DCHECK_GT(framer.GetFrameMaximumSize() - framer.GetFrameMinimumSize(),
length);
} else {
DCHECK_GE(framer.GetFrameMaximumSize(), length);
}
bool success = false;
const size_t old_length = length_;
if (framer.protocol_version() < 4) {
FlagsAndLength flags_length = CreateFlagsAndLength(
0,
length);
length_ = 5;
success = WriteBytes(reinterpret_cast<char*>(&flags_length) + 1,
sizeof(flags_length) - 1);
} else {
length_ = 0;
success = WriteUInt16(length);
}
length_ = old_length;
return success;
}
bool SpdyFrameBuilder::OverwriteFlags(const SpdyFramer& framer,
uint8 flags) {
DCHECK_LE(SPDY4, framer.protocol_version());
bool success = false;
const size_t old_length = length_;
length_ = 3;
success = WriteUInt8(flags);
length_ = old_length;
return success;
}
bool SpdyFrameBuilder::CanWrite(size_t length) const {
if (length > kLengthMask) {
DCHECK(false);
return false;
}
if (length_ + length > capacity_) {
DCHECK(false);
return false;
}
return true;
}
}