summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorrtenneti@chromium.org <rtenneti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-26 08:45:20 +0000
committerrtenneti@chromium.org <rtenneti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-26 08:45:20 +0000
commitf62453549c08349ea69f9b87a51435d7bd415577 (patch)
treebb93ee779cfbbcce3b98a2d314ec255a85a5e21e /net
parent27171848be2352d750311f0c998e7cd95af85025 (diff)
downloadchromium_src-f62453549c08349ea69f9b87a51435d7bd415577.zip
chromium_src-f62453549c08349ea69f9b87a51435d7bd415577.tar.gz
chromium_src-f62453549c08349ea69f9b87a51435d7bd415577.tar.bz2
Updated spdy_framer.cc with the latest code. Code for PING
was added. TEST=spdy unit testsR=willchan Review URL: http://codereview.chromium.org/8036016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@102707 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/spdy/spdy_framer.cc586
-rw-r--r--net/spdy/spdy_framer.h140
-rw-r--r--net/spdy/spdy_framer_test.cc11
-rw-r--r--net/spdy/spdy_protocol.h68
-rw-r--r--net/spdy/spdy_session.cc11
-rw-r--r--net/spdy/spdy_session.h6
-rw-r--r--net/tools/flip_server/spdy_interface.cc11
-rw-r--r--net/tools/flip_server/spdy_interface.h6
8 files changed, 783 insertions, 56 deletions
diff --git a/net/spdy/spdy_framer.cc b/net/spdy/spdy_framer.cc
index 9cc93d5..9c15170 100644
--- a/net/spdy/spdy_framer.cc
+++ b/net/spdy/spdy_framer.cc
@@ -29,6 +29,7 @@ const int kCompressorLevel = 9;
const int kCompressorWindowSizeInBits = 11;
const int kCompressorMemLevel = 1;
+// Adler ID for the SPDY header compressor dictionary.
uLong dictionary_id = 0;
} // namespace
@@ -66,6 +67,9 @@ size_t SpdyFramer::kControlFrameBufferInitialSize = 8 * 1024;
// TODO(mbelshe): We should make this stream-based so there are no limits.
size_t SpdyFramer::kControlFrameBufferMaxSize = 16 * 1024;
+const SpdyStreamId SpdyFramer::kInvalidStream = -1;
+const size_t SpdyFramer::kHeaderDataChunkMaxSize = 1024;
+
#ifdef DEBUG_SPDY_STATE_CHANGES
#define CHANGE_STATE(newstate) \
{ \
@@ -80,14 +84,68 @@ size_t SpdyFramer::kControlFrameBufferMaxSize = 16 * 1024;
#define CHANGE_STATE(newstate) (state_ = newstate)
#endif
+int DecompressHeaderBlockInZStream(z_stream* decompressor) {
+ int rv = inflate(decompressor, Z_SYNC_FLUSH);
+ if (rv == Z_NEED_DICT) {
+ // Need to try again with the right dictionary.
+ if (decompressor->adler == dictionary_id) {
+ rv = inflateSetDictionary(decompressor,
+ (const Bytef*)SpdyFramer::kDictionary,
+ SpdyFramer::kDictionarySize);
+ if (rv == Z_OK)
+ rv = inflate(decompressor, Z_SYNC_FLUSH);
+ }
+ }
+ return rv;
+}
+
+// Retrieve serialized length of SpdyHeaderBlock.
+size_t GetSerializedLength(const SpdyHeaderBlock* headers) {
+ size_t total_length = SpdyControlFrame::kNumNameValuePairsSize;
+ SpdyHeaderBlock::const_iterator it;
+ for (it = headers->begin(); it != headers->end(); ++it) {
+ // We add space for the length of the name and the length of the value as
+ // well as the length of the name and the length of the value.
+ total_length += SpdyControlFrame::kLengthOfNameSize +
+ it->first.size() +
+ SpdyControlFrame::kLengthOfValueSize +
+ it->second.size();
+ }
+ return total_length;
+}
+
+// Serializes a SpdyHeaderBlock.
+void WriteHeaderBlock(SpdyFrameBuilder* frame, const SpdyHeaderBlock* headers) {
+ frame->WriteUInt16(headers->size()); // Number of headers.
+ SpdyHeaderBlock::const_iterator it;
+ for (it = headers->begin(); it != headers->end(); ++it) {
+ bool wrote_header;
+ wrote_header = frame->WriteString(it->first);
+ wrote_header &= frame->WriteString(it->second);
+ DCHECK(wrote_header);
+ }
+}
+
+// Creates a FlagsAndLength.
+FlagsAndLength CreateFlagsAndLength(SpdyControlFlags 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;
+}
+
SpdyFramer::SpdyFramer()
: state_(SPDY_RESET),
error_code_(SPDY_NO_ERROR),
- remaining_payload_(0),
+ remaining_data_(0),
remaining_control_payload_(0),
+ remaining_control_header_(0),
current_frame_buffer_(NULL),
current_frame_len_(0),
current_frame_capacity_(0),
+ validate_control_frame_sizes_(true),
enable_compression_(compression_default_),
visitor_(NULL) {
}
@@ -103,6 +161,54 @@ SpdyFramer::~SpdyFramer() {
delete [] current_frame_buffer_;
}
+const char* SpdyFramer::StatusCodeToString(int status_code) {
+ switch (status_code) {
+ case INVALID:
+ return "INVALID";
+ case PROTOCOL_ERROR:
+ return "PROTOCOL_ERROR";
+ case INVALID_STREAM:
+ return "INVALID_STREAM";
+ case REFUSED_STREAM:
+ return "REFUSED_STREAM";
+ case UNSUPPORTED_VERSION:
+ return "UNSUPPORTED_VERSION";
+ case CANCEL:
+ return "CANCEL";
+ case INTERNAL_ERROR:
+ return "INTERNAL_ERROR";
+ case FLOW_CONTROL_ERROR:
+ return "FLOW_CONTROL_ERROR";
+ }
+ return "UNKNOWN_STATUS";
+}
+
+const char* SpdyFramer::ControlTypeToString(SpdyControlType type) {
+ switch (type) {
+ case SYN_STREAM:
+ return "SYN_STREAM";
+ case SYN_REPLY:
+ return "SYN_REPLY";
+ case RST_STREAM:
+ return "RST_STREAM";
+ case SETTINGS:
+ return "SETTINGS";
+ case NOOP:
+ return "NOOP";
+ case PING:
+ return "PING";
+ case GOAWAY:
+ return "GOAWAY";
+ case HEADERS:
+ return "HEADERS";
+ case WINDOW_UPDATE:
+ return "WINDOW_UPDATE";
+ case NUM_CONTROL_FRAME_TYPES:
+ break;
+ }
+ return "UNKNOWN_CONTROL_TYPE";
+}
+
size_t SpdyFramer::ProcessInput(const char* data, size_t len) {
DCHECK(visitor_);
DCHECK(data);
@@ -130,10 +236,36 @@ size_t SpdyFramer::ProcessInput(const char* data, size_t len) {
// Arguably, this case is not necessary, as no bytes are consumed here.
// I felt it was a nice partitioning, however (which probably indicates
// that it should be refactored into its own function!)
+ // TODO(hkhalil): Remove -- while loop above prevents proper handling of
+ // zero-length control frames.
case SPDY_INTERPRET_CONTROL_FRAME_COMMON_HEADER:
ProcessControlFrameHeader();
continue;
+ case SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK: {
+ // Control frames that contain header blocks (SYN_STREAM, SYN_REPLY,
+ // HEADERS) take a different path through the state machine - they
+ // will go:
+ // 1. SPDY_INTERPRET_CONTROL_FRAME_COMMON HEADER
+ // 2. SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK
+ // 3. SPDY_CONTROL_FRAME_HEADER_BLOCK
+ //
+ // All other control frames will use the alternate route:
+ // 1. SPDY_INTERPRET_CONTROL_FRAME_COMMON_HEADER
+ // 2. SPDY_CONTROL_FRAME_PAYLOAD
+ int bytes_read = ProcessControlFrameBeforeHeaderBlock(data, len);
+ len -= bytes_read;
+ data += bytes_read;
+ continue;
+ }
+
+ case SPDY_CONTROL_FRAME_HEADER_BLOCK: {
+ int bytes_read = ProcessControlFrameHeaderBlock(data, len);
+ len -= bytes_read;
+ data += bytes_read;
+ continue;
+ }
+
case SPDY_CONTROL_FRAME_PAYLOAD: {
size_t bytes_read = ProcessControlFramePayload(data, len);
len -= bytes_read;
@@ -160,8 +292,9 @@ size_t SpdyFramer::ProcessInput(const char* data, size_t len) {
void SpdyFramer::Reset() {
state_ = SPDY_RESET;
error_code_ = SPDY_NO_ERROR;
- remaining_payload_ = 0;
+ remaining_data_ = 0;
remaining_control_payload_ = 0;
+ remaining_control_header_ = 0;
current_frame_len_ = 0;
if (current_frame_capacity_ != kControlFrameBufferInitialSize) {
delete [] current_frame_buffer_;
@@ -236,9 +369,169 @@ bool SpdyFramer::ParseHeaderBlock(const SpdyFrame* frame,
return false;
}
+size_t SpdyFramer::UpdateCurrentFrameBuffer(const char** data, size_t* len,
+ size_t max_bytes) {
+ size_t bytes_to_read = std::min(*len, max_bytes);
+ DCHECK_GE(current_frame_capacity_, current_frame_len_ + bytes_to_read);
+ memcpy(&current_frame_buffer_[current_frame_len_], *data, bytes_to_read);
+ current_frame_len_ += bytes_to_read;
+ *data += bytes_to_read;
+ *len -= bytes_to_read;
+ return bytes_to_read;
+}
+
+size_t SpdyFramer::ProcessControlFrameBeforeHeaderBlock(const char* data,
+ size_t len) {
+ DCHECK_EQ(SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK, state_);
+ DCHECK_GT(remaining_control_header_, 0u);
+ size_t original_len = len;
+
+ if (remaining_control_header_) {
+ size_t bytes_read = UpdateCurrentFrameBuffer(&data, &len,
+ remaining_control_header_);
+ remaining_control_header_ -= bytes_read;
+ if (remaining_control_header_ == 0) {
+ SpdyControlFrame control_frame(current_frame_buffer_, false);
+ DCHECK(control_frame.type() == SYN_STREAM ||
+ control_frame.type() == SYN_REPLY ||
+ control_frame.type() == HEADERS);
+ visitor_->OnControl(&control_frame);
+
+ CHANGE_STATE(SPDY_CONTROL_FRAME_HEADER_BLOCK);
+ }
+ }
+ return original_len - len;
+}
+
+// Does not buffer the control payload. Instead, either passes directly to the
+// visitor or decompresses and then passes directly to the visitor, via
+// IncrementallyDeliverControlFrameHeaderData() or
+// IncrementallyDecompressControlFrameHeaderData() respectively.
+size_t SpdyFramer::NewProcessControlFrameHeaderBlock(const char* data,
+ size_t data_len) {
+ DCHECK_EQ(SPDY_CONTROL_FRAME_HEADER_BLOCK, state_);
+ SpdyControlFrame control_frame(current_frame_buffer_, false);
+ bool processed_successfully = true;
+ DCHECK(control_frame.type() == SYN_STREAM ||
+ control_frame.type() == SYN_REPLY ||
+ control_frame.type() == HEADERS);
+ size_t process_bytes = std::min(data_len, remaining_control_payload_);
+ DCHECK_GT(process_bytes, 0u);
+
+ if (enable_compression_) {
+ processed_successfully = IncrementallyDecompressControlFrameHeaderData(
+ &control_frame, data, process_bytes);
+ } else {
+ processed_successfully = IncrementallyDeliverControlFrameHeaderData(
+ &control_frame, data, process_bytes);
+ }
+ remaining_control_payload_ -= process_bytes;
+
+ // Handle the case that there is no futher data in this frame.
+ if (remaining_control_payload_ == 0 && processed_successfully) {
+ // The complete header block has been delivered. We send a zero-length
+ // OnControlFrameHeaderData() to indicate this.
+ visitor_->OnControlFrameHeaderData(
+ GetControlFrameStreamId(&control_frame), NULL, 0);
+
+ // If this is a FIN, tell the caller.
+ if (control_frame.flags() & CONTROL_FLAG_FIN) {
+ visitor_->OnStreamFrameData(GetControlFrameStreamId(&control_frame),
+ NULL, 0);
+ }
+
+ CHANGE_STATE(SPDY_RESET);
+ }
+
+ // Handle error.
+ if (!processed_successfully) {
+ return data_len;
+ }
+
+ // Return amount processed.
+ return process_bytes;
+}
+
+size_t SpdyFramer::ProcessControlFrameHeaderBlock(const char* data,
+ size_t data_len) {
+ DCHECK_EQ(SPDY_CONTROL_FRAME_HEADER_BLOCK, state_);
+ size_t original_data_len = data_len;
+ SpdyControlFrame control_frame(current_frame_buffer_, false);
+ bool read_successfully = true;
+ DCHECK(control_frame.type() == SYN_STREAM ||
+ control_frame.type() == SYN_REPLY ||
+ control_frame.type() == HEADERS);
+
+ if (enable_compression_) {
+ // Note that the header block is held in the frame's payload, and is not
+ // part of the frame's headers.
+ if (remaining_control_payload_ > 0) {
+ size_t bytes_read = UpdateCurrentFrameBuffer(
+ &data,
+ &data_len,
+ remaining_control_payload_);
+ remaining_control_payload_ -= bytes_read;
+ if (remaining_control_payload_ == 0) {
+ read_successfully = IncrementallyDecompressControlFrameHeaderData(
+ &control_frame);
+ }
+ }
+ } else {
+ size_t bytes_to_send = std::min(data_len, remaining_control_payload_);
+ DCHECK_GT(bytes_to_send, 0u);
+ read_successfully = IncrementallyDeliverControlFrameHeaderData(
+ &control_frame, data, bytes_to_send);
+ data_len -= bytes_to_send;
+ remaining_control_payload_ -= bytes_to_send;
+ }
+ if (remaining_control_payload_ == 0 && read_successfully) {
+ // The complete header block has been delivered.
+ visitor_->OnControlFrameHeaderData(GetControlFrameStreamId(&control_frame),
+ NULL, 0);
+
+ // If this is a FIN, tell the caller.
+ if (control_frame.flags() & CONTROL_FLAG_FIN) {
+ visitor_->OnStreamFrameData(GetControlFrameStreamId(&control_frame),
+ NULL, 0);
+ }
+
+ CHANGE_STATE(SPDY_RESET);
+ }
+ if (!read_successfully) {
+ return original_data_len;
+ }
+ return original_data_len - data_len;
+}
+
+/* static */
+bool SpdyFramer::ParseHeaderBlockInBuffer(const char* header_data,
+ size_t header_length,
+ SpdyHeaderBlock* block) {
+ SpdyFrameBuilder builder(header_data, header_length);
+ void* iter = NULL;
+ uint16 num_headers;
+ if (builder.ReadUInt16(&iter, &num_headers)) {
+ for (int index = 0; index < num_headers; ++index) {
+ std::string name;
+ std::string value;
+ if (!builder.ReadString(&iter, &name))
+ return false;
+ if (!builder.ReadString(&iter, &value))
+ return false;
+ if (block->find(name) == block->end()) {
+ (*block)[name] = value;
+ } else {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+}
+
SpdySynStreamControlFrame* SpdyFramer::CreateSynStream(
SpdyStreamId stream_id, SpdyStreamId associated_stream_id, int priority,
- SpdyControlFlags flags, bool compressed, SpdyHeaderBlock* headers) {
+ SpdyControlFlags flags, bool compressed, const SpdyHeaderBlock* headers) {
SpdyFrameBuilder frame;
DCHECK_GT(stream_id, static_cast<SpdyStreamId>(0));
@@ -253,7 +546,7 @@ SpdySynStreamControlFrame* SpdyFramer::CreateSynStream(
frame.WriteUInt16(ntohs(priority) << 6); // Priority.
frame.WriteUInt16(headers->size()); // Number of headers.
- SpdyHeaderBlock::iterator it;
+ SpdyHeaderBlock::const_iterator it;
for (it = headers->begin(); it != headers->end(); ++it) {
bool wrote_header;
wrote_header = frame.WriteString(it->first);
@@ -270,16 +563,17 @@ SpdySynStreamControlFrame* SpdyFramer::CreateSynStream(
flags_length.flags_[0] = flags;
frame.WriteBytesToOffset(4, &flags_length, sizeof(flags_length));
- scoped_ptr<SpdyFrame> syn_frame(frame.take());
+ scoped_ptr<SpdySynStreamControlFrame> syn_frame(
+ reinterpret_cast<SpdySynStreamControlFrame*>(frame.take()));
if (compressed) {
return reinterpret_cast<SpdySynStreamControlFrame*>(
- CompressFrame(*syn_frame.get()));
+ CompressControlFrame(*syn_frame.get()));
}
- return reinterpret_cast<SpdySynStreamControlFrame*>(syn_frame.release());
+ return syn_frame.release();
}
SpdySynReplyControlFrame* SpdyFramer::CreateSynReply(SpdyStreamId stream_id,
- SpdyControlFlags flags, bool compressed, SpdyHeaderBlock* headers) {
+ SpdyControlFlags flags, bool compressed, const SpdyHeaderBlock* headers) {
DCHECK_GT(stream_id, 0u);
DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
@@ -292,7 +586,7 @@ SpdySynReplyControlFrame* SpdyFramer::CreateSynReply(SpdyStreamId stream_id,
frame.WriteUInt16(0); // Unused
frame.WriteUInt16(headers->size()); // Number of headers.
- SpdyHeaderBlock::iterator it;
+ SpdyHeaderBlock::const_iterator it;
for (it = headers->begin(); it != headers->end(); ++it) {
bool wrote_header;
wrote_header = frame.WriteString(it->first);
@@ -309,12 +603,13 @@ SpdySynReplyControlFrame* SpdyFramer::CreateSynReply(SpdyStreamId stream_id,
flags_length.flags_[0] = flags;
frame.WriteBytesToOffset(4, &flags_length, sizeof(flags_length));
- scoped_ptr<SpdyFrame> reply_frame(frame.take());
+ scoped_ptr<SpdySynReplyControlFrame> reply_frame(
+ reinterpret_cast<SpdySynReplyControlFrame*>(frame.take()));
if (compressed) {
return reinterpret_cast<SpdySynReplyControlFrame*>(
- CompressFrame(*reply_frame.get()));
+ CompressControlFrame(*reply_frame.get()));
}
- return reinterpret_cast<SpdySynReplyControlFrame*>(reply_frame.release());
+ return reply_frame.release();
}
/* static */
@@ -354,12 +649,23 @@ SpdySettingsControlFrame* SpdyFramer::CreateSettings(
}
/* static */
-SpdyControlFrame* SpdyFramer::CreateNopFrame() {
+SpdyNoOpControlFrame* SpdyFramer::CreateNopFrame() {
SpdyFrameBuilder frame;
frame.WriteUInt16(kControlFlagMask | spdy_version_);
frame.WriteUInt16(NOOP);
frame.WriteUInt32(0);
- return reinterpret_cast<SpdyControlFrame*>(frame.take());
+ return reinterpret_cast<SpdyNoOpControlFrame*>(frame.take());
+}
+
+/* static */
+SpdyPingControlFrame* SpdyFramer::CreatePingFrame(uint32 unique_id) {
+ SpdyFrameBuilder frame;
+ frame.WriteUInt16(kControlFlagMask | kSpdyProtocolVersion);
+ frame.WriteUInt16(PING);
+ size_t ping_size = SpdyPingControlFrame::size() - SpdyFrame::size();
+ frame.WriteUInt32(ping_size);
+ frame.WriteUInt32(unique_id);
+ return reinterpret_cast<SpdyPingControlFrame*>(frame.take());
}
/* static */
@@ -377,7 +683,7 @@ SpdyGoAwayControlFrame* SpdyFramer::CreateGoAway(
}
SpdyHeadersControlFrame* SpdyFramer::CreateHeaders(SpdyStreamId stream_id,
- SpdyControlFlags flags, bool compressed, SpdyHeaderBlock* headers) {
+ SpdyControlFlags flags, bool compressed, const SpdyHeaderBlock* headers) {
// Basically the same as CreateSynReply().
DCHECK_GT(stream_id, 0u);
DCHECK_EQ(0u, stream_id & ~kStreamIdMask);
@@ -390,7 +696,7 @@ SpdyHeadersControlFrame* SpdyFramer::CreateHeaders(SpdyStreamId stream_id,
frame.WriteUInt16(0); // Unused
frame.WriteUInt16(headers->size()); // Number of headers.
- SpdyHeaderBlock::iterator it;
+ SpdyHeaderBlock::const_iterator it;
for (it = headers->begin(); it != headers->end(); ++it) {
bool wrote_header;
wrote_header = frame.WriteString(it->first);
@@ -407,12 +713,13 @@ SpdyHeadersControlFrame* SpdyFramer::CreateHeaders(SpdyStreamId stream_id,
flags_length.flags_[0] = flags;
frame.WriteBytesToOffset(4, &flags_length, sizeof(flags_length));
- scoped_ptr<SpdyFrame> headers_frame(frame.take());
+ scoped_ptr<SpdyHeadersControlFrame> headers_frame(
+ reinterpret_cast<SpdyHeadersControlFrame*>(frame.take()));
if (compressed) {
return reinterpret_cast<SpdyHeadersControlFrame*>(
- CompressFrame(*headers_frame.get()));
+ CompressControlFrame(*headers_frame.get()));
}
- return reinterpret_cast<SpdyHeadersControlFrame*>(headers_frame.release());
+ return headers_frame.release();
}
/* static */
@@ -548,6 +855,10 @@ const char* SpdyFramer::StateToString(int state) {
return "IGNORE_REMAINING_PAYLOAD";
case SPDY_FORWARD_STREAM_FRAME:
return "FORWARD_STREAM_FRAME";
+ case SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK:
+ return "SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK";
+ case SPDY_CONTROL_FRAME_HEADER_BLOCK:
+ return "SPDY_CONTROL_FRAME_HEADER_BLOCK";
}
return "UNKNOWN_STATE";
}
@@ -591,12 +902,7 @@ size_t SpdyFramer::ProcessCommonHeader(const char* data, size_t len) {
do {
if (current_frame_len_ < SpdyFrame::size()) {
size_t bytes_desired = SpdyFrame::size() - current_frame_len_;
- size_t bytes_to_append = std::min(bytes_desired, len);
- char* header_buffer = current_frame_buffer_;
- memcpy(&header_buffer[current_frame_len_], data, bytes_to_append);
- current_frame_len_ += bytes_to_append;
- data += bytes_to_append;
- len -= bytes_to_append;
+ UpdateCurrentFrameBuffer(&data, &len, bytes_desired);
// Check for an empty data frame.
if (current_frame_len_ == SpdyFrame::size() &&
!current_frame.is_control_frame() &&
@@ -609,10 +915,10 @@ size_t SpdyFramer::ProcessCommonHeader(const char* data, size_t len) {
}
break;
}
- remaining_payload_ = current_frame.length();
+ remaining_data_ = current_frame.length();
// This is just a sanity check for help debugging early frame errors.
- if (remaining_payload_ > 1000000u) {
+ if (remaining_data_ > 1000000u) {
LOG(WARNING) <<
"Unexpectedly large frame. Spdy session is likely corrupt.";
}
@@ -670,8 +976,10 @@ void SpdyFramer::ProcessControlFrameHeader() {
SpdySettingsControlFrame::size() - SpdyControlFrame::size())
set_error(SPDY_INVALID_CONTROL_FRAME);
break;
+ // TODO(hkhalil): Remove NOOP.
case NOOP:
// NOOP. Swallow it.
+ DLOG(INFO) << "Attempted frame size validation for NOOP. Resetting.";
CHANGE_STATE(SPDY_AUTO_RESET);
return;
case GOAWAY:
@@ -689,8 +997,13 @@ void SpdyFramer::ProcessControlFrameHeader() {
SpdyWindowUpdateControlFrame::size() - SpdyControlFrame::size())
set_error(SPDY_INVALID_CONTROL_FRAME);
break;
+ case PING:
+ if (current_control_frame.length() !=
+ SpdyPingControlFrame::size() - SpdyControlFrame::size())
+ set_error(SPDY_INVALID_CONTROL_FRAME);
+ break;
default:
- LOG(WARNING) << "Valid spdy control frame with unknown type: "
+ LOG(WARNING) << "Valid spdy control frame with unhandled type: "
<< current_control_frame.type();
DCHECK(false);
set_error(SPDY_INVALID_CONTROL_FRAME);
@@ -711,14 +1024,10 @@ size_t SpdyFramer::ProcessControlFramePayload(const char* data, size_t len) {
size_t original_len = len;
do {
if (remaining_control_payload_) {
- size_t amount_to_consume = std::min(remaining_control_payload_, len);
- memcpy(&current_frame_buffer_[current_frame_len_], data,
- amount_to_consume);
- current_frame_len_ += amount_to_consume;
- data += amount_to_consume;
- len -= amount_to_consume;
- remaining_control_payload_ -= amount_to_consume;
- remaining_payload_ -= amount_to_consume;
+ size_t bytes_read = UpdateCurrentFrameBuffer(&data, &len,
+ remaining_control_payload_);
+ remaining_control_payload_ -= bytes_read;
+ remaining_data_ -= bytes_read;
if (remaining_control_payload_)
break;
}
@@ -742,8 +1051,8 @@ size_t SpdyFramer::ProcessDataFramePayload(const char* data, size_t len) {
size_t original_len = len;
SpdyDataFrame current_data_frame(current_frame_buffer_, false);
- if (remaining_payload_) {
- size_t amount_to_forward = std::min(remaining_payload_, len);
+ if (remaining_data_) {
+ size_t amount_to_forward = std::min(remaining_data_, len);
if (amount_to_forward && state_ != SPDY_IGNORE_REMAINING_PAYLOAD) {
if (current_data_frame.flags() & DATA_FLAG_COMPRESSED) {
z_stream* decompressor =
@@ -785,11 +1094,11 @@ size_t SpdyFramer::ProcessDataFramePayload(const char* data, size_t len) {
}
data += amount_to_forward;
len -= amount_to_forward;
- remaining_payload_ -= amount_to_forward;
+ remaining_data_ -= amount_to_forward;
// If the FIN flag is set, and there is no more data in this data
// frame, inform the visitor of EOF via a 0-length data frame.
- if (!remaining_payload_ &&
+ if (!remaining_data_ &&
current_data_frame.flags() & DATA_FLAG_FIN) {
visitor_->OnStreamFrameData(current_data_frame.stream_id(), NULL, 0);
CleanupDecompressorForStream(current_data_frame.stream_id());
@@ -913,6 +1222,194 @@ SpdyControlFrame* SpdyFramer::DecompressControlFrame(
DecompressFrameWithZStream(frame, decompressor));
}
+// Incrementally decompress the control frame's header block, feeding the
+// result to the visitor in chunks. Continue this until the visitor
+// indicates that it cannot process any more data, or (more commonly) we
+// run out of data to deliver.
+bool SpdyFramer::IncrementallyDecompressControlFrameHeaderData(
+ const SpdyControlFrame* control_frame) {
+ z_stream* decomp = GetHeaderDecompressor();
+ int payload_length;
+ int header_length;
+ const char* payload;
+ bool read_successfully = true;
+ bool more = true;
+ char buffer[kHeaderDataChunkMaxSize];
+
+ if (!GetFrameBoundaries(
+ *control_frame, &payload_length, &header_length, &payload)) {
+ DLOG(ERROR) << "Control frame of type "
+ << SpdyFramer::ControlTypeToString(control_frame->type())
+ <<" doesn't have headers";
+ return false;
+ }
+ decomp->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(payload));
+ decomp->avail_in = payload_length;
+ const SpdyStreamId stream_id = GetControlFrameStreamId(control_frame);
+ DCHECK_LT(0u, stream_id);
+ while (more && read_successfully) {
+ decomp->next_out = reinterpret_cast<Bytef*>(buffer);
+ decomp->avail_out = arraysize(buffer);
+ int rv = DecompressHeaderBlockInZStream(decomp);
+ if (rv != Z_OK) {
+ set_error(SPDY_DECOMPRESS_FAILURE);
+ DLOG(WARNING) << "inflate failure: " << rv;
+ more = read_successfully = false;
+ } else {
+ DCHECK_GT(arraysize(buffer), decomp->avail_out);
+ size_t len = arraysize(buffer) - decomp->avail_out;
+ read_successfully = visitor_->OnControlFrameHeaderData(stream_id, buffer,
+ len);
+ if (!read_successfully) {
+ // Assume that the problem was the header block was too large for the
+ // visitor.
+ set_error(SPDY_CONTROL_PAYLOAD_TOO_LARGE);
+ }
+ more = decomp->avail_in > 0;
+ }
+ }
+ return read_successfully;
+}
+
+// Incrementally decompress the control frame's header block, feeding the
+// result to the visitor in chunks. Continue this until the visitor
+// indicates that it cannot process any more data, or (more commonly) we
+// run out of data to deliver.
+bool SpdyFramer::IncrementallyDecompressControlFrameHeaderData(
+ const SpdyControlFrame* control_frame,
+ const char* data,
+ size_t len) {
+ // Get a decompressor or set error.
+ z_stream* decomp = GetHeaderDecompressor();
+ if (decomp == NULL) {
+ LOG(DFATAL) << "Couldn't get decompressor for handling compressed headers.";
+ set_error(SPDY_DECOMPRESS_FAILURE);
+ return false;
+ }
+
+ bool processed_successfully = true;
+ char buffer[kHeaderDataChunkMaxSize];
+
+ decomp->next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data));
+ decomp->avail_in = len;
+ const SpdyStreamId stream_id = GetControlFrameStreamId(control_frame);
+ DCHECK_LT(0u, stream_id);
+ while (decomp->avail_in > 0 && processed_successfully) {
+ decomp->next_out = reinterpret_cast<Bytef*>(buffer);
+ decomp->avail_out = arraysize(buffer);
+ int rv = DecompressHeaderBlockInZStream(decomp);
+ if (rv != Z_OK) {
+ set_error(SPDY_DECOMPRESS_FAILURE);
+ DLOG(WARNING) << "inflate failure: " << rv;
+ processed_successfully = false;
+ } else {
+ size_t decompressed_len = arraysize(buffer) - decomp->avail_out;
+ if (decompressed_len > 0) {
+ processed_successfully = visitor_->OnControlFrameHeaderData(
+ stream_id, buffer, decompressed_len);
+ }
+ if (!processed_successfully) {
+ // Assume that the problem was the header block was too large for the
+ // visitor.
+ set_error(SPDY_CONTROL_PAYLOAD_TOO_LARGE);
+ }
+ }
+ }
+ return processed_successfully;
+}
+
+bool SpdyFramer::IncrementallyDeliverControlFrameHeaderData(
+ const SpdyControlFrame* control_frame, const char* data, size_t len) {
+ bool read_successfully = true;
+ const SpdyStreamId stream_id = GetControlFrameStreamId(control_frame);
+ DCHECK_LT(0u, stream_id);
+ while (read_successfully && len > 0) {
+ size_t bytes_to_deliver = std::min(len, kHeaderDataChunkMaxSize);
+ read_successfully = visitor_->OnControlFrameHeaderData(stream_id, data,
+ bytes_to_deliver);
+ data += bytes_to_deliver;
+ len -= bytes_to_deliver;
+ if (!read_successfully) {
+ // Assume that the problem was the header block was too large for the
+ // visitor.
+ set_error(SPDY_CONTROL_PAYLOAD_TOO_LARGE);
+ }
+ }
+ return read_successfully;
+}
+
+size_t SpdyFramer::GetMinimumControlFrameSize(SpdyControlType type) {
+ switch (type) {
+ case SYN_STREAM:
+ return SpdySynStreamControlFrame::size();
+ case SYN_REPLY:
+ return SpdySynReplyControlFrame::size();
+ case RST_STREAM:
+ return SpdyRstStreamControlFrame::size();
+ case SETTINGS:
+ return SpdySettingsControlFrame::size();
+ case NOOP:
+ return SpdyNoOpControlFrame::size();
+ case PING:
+ return SpdyPingControlFrame::size();
+ case GOAWAY:
+ return SpdyGoAwayControlFrame::size();
+ case HEADERS:
+ return SpdyHeadersControlFrame::size();
+ case WINDOW_UPDATE:
+ return SpdyWindowUpdateControlFrame::size();
+ case NUM_CONTROL_FRAME_TYPES:
+ break;
+ }
+ LOG(ERROR) << "Unknown SPDY control frame type " << type;
+ return 0x7FFFFFFF; // Max signed 32bit int
+}
+
+/* static */
+SpdyStreamId SpdyFramer::GetControlFrameStreamId(
+ const SpdyControlFrame* control_frame) {
+ SpdyStreamId stream_id = kInvalidStream;
+ if (control_frame != NULL) {
+ switch (control_frame->type()) {
+ case SYN_STREAM:
+ stream_id = reinterpret_cast<const SpdySynStreamControlFrame*>(
+ control_frame)->stream_id();
+ break;
+ case SYN_REPLY:
+ stream_id = reinterpret_cast<const SpdySynReplyControlFrame*>(
+ control_frame)->stream_id();
+ break;
+ case HEADERS:
+ stream_id = reinterpret_cast<const SpdyHeadersControlFrame*>(
+ control_frame)->stream_id();
+ break;
+ case RST_STREAM:
+ stream_id = reinterpret_cast<const SpdyRstStreamControlFrame*>(
+ control_frame)->stream_id();
+ break;
+ case WINDOW_UPDATE:
+ stream_id = reinterpret_cast<const SpdyWindowUpdateControlFrame*>(
+ control_frame)->stream_id();
+ break;
+ // All of the following types are not part of a particular stream.
+ // They all fall through to the invalid control frame type case.
+ // (The default case isn't used so that the compile will break if a new
+ // control frame type is added but not included here.)
+ case SETTINGS:
+ case NOOP:
+ case PING:
+ case GOAWAY:
+ case NUM_CONTROL_FRAME_TYPES: // makes compiler happy
+ break;
+ }
+ }
+ return stream_id;
+}
+
+void SpdyFramer::set_validate_control_frame_sizes(bool value) {
+ validate_control_frame_sizes_ = value;
+}
+
SpdyDataFrame* SpdyFramer::DecompressDataFrame(const SpdyDataFrame& frame) {
z_stream* decompressor = GetStreamDecompressor(frame.stream_id());
if (!decompressor)
@@ -1115,10 +1612,15 @@ size_t SpdyFramer::BytesSafeToRead() const {
return SpdyFrame::size() - current_frame_len_;
case SPDY_INTERPRET_CONTROL_FRAME_COMMON_HEADER:
return 0;
+ // TODO(rtenneti): Add support for SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK
+ // and SPDY_CONTROL_FRAME_HEADER_BLOCK.
+ case SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK:
+ case SPDY_CONTROL_FRAME_HEADER_BLOCK:
+ return 0;
case SPDY_CONTROL_FRAME_PAYLOAD:
case SPDY_IGNORE_REMAINING_PAYLOAD:
case SPDY_FORWARD_STREAM_FRAME:
- return remaining_payload_;
+ return remaining_data_;
}
// We should never get to here.
return 0;
@@ -1133,7 +1635,7 @@ void SpdyFramer::set_error(SpdyError error) {
void SpdyFramer::ExpandControlFrameBuffer(size_t size) {
size_t alloc_size = size + SpdyFrame::size();
- DCHECK_LT(alloc_size, kControlFrameBufferMaxSize);
+ DCHECK_LE(alloc_size, kControlFrameBufferMaxSize);
if (alloc_size <= current_frame_capacity_)
return;
char* new_buffer = new char[alloc_size];
diff --git a/net/spdy/spdy_framer.h b/net/spdy/spdy_framer.h
index c7dcf5a..a3c82fd 100644
--- a/net/spdy/spdy_framer.h
+++ b/net/spdy/spdy_framer.h
@@ -61,9 +61,28 @@ class NET_EXPORT_PRIVATE SpdyFramerVisitorInterface {
// Called if an error is detected in the SpdyFrame protocol.
virtual void OnError(SpdyFramer* framer) = 0;
- // Called when a Control Frame is received.
+ // Called when a control frame is received.
virtual void OnControl(const SpdyControlFrame* frame) = 0;
+ // Called when a chunk of header data is available. This is called
+ // after OnControl() is called with the control frame associated with the
+ // header data being delivered here.
+ // |stream_id| The stream receiving the header data.
+ // |header_data| A buffer containing the header data chunk received.
+ // |len| The length of the header data buffer. A length of zero indicates
+ // that the header data block has been completely sent.
+ // When this function returns true the visitor indicates that it accepted
+ // all of the data. Returning false indicates that that an unrecoverable
+ // error has occurred, such as bad header data or resource exhaustion.
+ virtual bool OnControlFrameHeaderData(SpdyStreamId stream_id,
+ const char* header_data,
+ size_t len) = 0;
+
+ // Called when a data frame header is received. The frame's data
+ // payload will be provided via subsequent calls to
+ // OnStreamFrameData().
+ virtual void OnDataFrameHeader(const SpdyDataFrame* frame) = 0;
+
// Called when data is received.
// |stream_id| The stream receiving data.
// |data| A buffer containing the data received.
@@ -89,7 +108,9 @@ class NET_EXPORT_PRIVATE SpdyFramer {
SPDY_INTERPRET_CONTROL_FRAME_COMMON_HEADER,
SPDY_CONTROL_FRAME_PAYLOAD,
SPDY_IGNORE_REMAINING_PAYLOAD,
- SPDY_FORWARD_STREAM_FRAME
+ SPDY_FORWARD_STREAM_FRAME,
+ SPDY_CONTROL_FRAME_BEFORE_HEADER_BLOCK,
+ SPDY_CONTROL_FRAME_HEADER_BLOCK,
};
// SPDY error codes.
@@ -105,6 +126,14 @@ class NET_EXPORT_PRIVATE SpdyFramer {
LAST_ERROR, // Must be the last entry in the enum.
};
+ // Constant for invalid (or unknown) stream IDs.
+ static const SpdyStreamId kInvalidStream;
+
+ // The maximum size of header data chunks delivered to the framer visitor
+ // through OnControlFrameHeaderData. (It is exposed here for unit test
+ // purposes.)
+ static const size_t kHeaderDataChunkMaxSize;
+
// Create a new Framer.
SpdyFramer();
virtual ~SpdyFramer();
@@ -141,6 +170,14 @@ class NET_EXPORT_PRIVATE SpdyFramer {
// Returns true if successfully parsed, false otherwise.
bool ParseHeaderBlock(const SpdyFrame* frame, SpdyHeaderBlock* block);
+ // Given a buffer containing a decompressed header block in SPDY
+ // serialized format, parse out a SpdyHeaderBlock, putting the results
+ // in the given header block.
+ // Returns true if successfully parsed, false otherwise.
+ static bool ParseHeaderBlockInBuffer(const char* header_data,
+ size_t header_length,
+ SpdyHeaderBlock* block);
+
// Create a SpdySynStreamControlFrame.
// |stream_id| is the id for this stream.
// |associated_stream_id| is the associated stream id for this stream.
@@ -154,7 +191,7 @@ class NET_EXPORT_PRIVATE SpdyFramer {
int priority,
SpdyControlFlags flags,
bool compressed,
- SpdyHeaderBlock* headers);
+ const SpdyHeaderBlock* headers);
// Create a SpdySynReplyControlFrame.
// |stream_id| is the stream for this frame.
@@ -165,7 +202,7 @@ class NET_EXPORT_PRIVATE SpdyFramer {
SpdySynReplyControlFrame* CreateSynReply(SpdyStreamId stream_id,
SpdyControlFlags flags,
bool compressed,
- SpdyHeaderBlock* headers);
+ const SpdyHeaderBlock* headers);
static SpdyRstStreamControlFrame* CreateRstStream(SpdyStreamId stream_id,
SpdyStatusCodes status);
@@ -175,7 +212,11 @@ class NET_EXPORT_PRIVATE SpdyFramer {
// TODO(mbelshe): add the name/value pairs!!
static SpdySettingsControlFrame* CreateSettings(const SpdySettings& values);
- static SpdyControlFrame* CreateNopFrame();
+ static SpdyNoOpControlFrame* CreateNopFrame();
+
+ // Creates an instance of SpdyPingControlFrame. The unique_id is used to
+ // identify the ping request/response.
+ static SpdyPingControlFrame* CreatePingFrame(uint32 unique_id);
// Creates an instance of SpdyGoAwayControlFrame. The GOAWAY frame is used
// prior to the shutting down of the TCP connection, and includes the
@@ -190,7 +231,7 @@ class NET_EXPORT_PRIVATE SpdyFramer {
SpdyHeadersControlFrame* CreateHeaders(SpdyStreamId stream_id,
SpdyControlFlags flags,
bool compressed,
- SpdyHeaderBlock* headers);
+ const SpdyHeaderBlock* headers);
// Creates an instance of SpdyWindowUpdateControlFrame. The WINDOW_UPDATE
// frame is used to implement per stream flow control in SPDY.
@@ -245,9 +286,29 @@ class NET_EXPORT_PRIVATE SpdyFramer {
// Returns true if a frame could be compressed.
bool IsCompressible(const SpdyFrame& frame) const;
+ // Get the minimum size of the control frame for the given control frame
+ // type. This is useful for validating frame blocks.
+ static size_t GetMinimumControlFrameSize(SpdyControlType type);
+
+ // Get the stream ID for the given control frame (SYN_STREAM, SYN_REPLY, and
+ // HEADERS). If the control frame is NULL or of another type, this
+ // function returns kInvalidStream.
+ static SpdyStreamId GetControlFrameStreamId(
+ const SpdyControlFrame* control_frame);
+
+ // For ease of testing and experimentation we can tweak compression on/off.
+ void set_enable_compression(bool value);
+
+ // SPDY will by default validate the length of incoming control
+ // frames. Set validation to false if you do not want this behavior.
+ void set_validate_control_frame_sizes(bool value);
+ static void set_enable_compression_default(bool value);
+
// For debugging.
static const char* StateToString(int state);
static const char* ErrorCodeToString(int error_code);
+ static const char* StatusCodeToString(int status_code);
+ static const char* ControlTypeToString(SpdyControlType type);
static void set_protocol_version(int version) { spdy_version_= version; }
static int protocol_version() { return spdy_version_; }
@@ -276,14 +337,11 @@ class NET_EXPORT_PRIVATE SpdyFramer {
friend void test::FramerSetEnableCompressionHelper(SpdyFramer* framer,
bool compress);
- // For ease of testing we can tweak compression on/off.
- void set_enable_compression(bool value);
- static void set_enable_compression_default(bool value);
-
-
// The initial size of the control frame buffer; this is used internally
// as we parse through control frames. (It is exposed here for unit test
// purposes.)
+ // This is only used when compression is enabled; otherwise,
+ // kUncompressedControlFrameBufferInitialSize is used.
static size_t kControlFrameBufferInitialSize;
// The maximum size of the control frame buffer that we support.
@@ -298,6 +356,11 @@ class NET_EXPORT_PRIVATE SpdyFramer {
size_t ProcessCommonHeader(const char* data, size_t len);
void ProcessControlFrameHeader();
size_t ProcessControlFramePayload(const char* data, size_t len);
+ size_t ProcessControlFrameBeforeHeaderBlock(const char* data, size_t len);
+ // TODO(hkhalil): Rename to ProcessControlFrameHeaderBlock once said flag is
+ // removed, replacing the old method.
+ size_t NewProcessControlFrameHeaderBlock(const char* data, size_t len);
+ size_t ProcessControlFrameHeaderBlock(const char* data, size_t len);
size_t ProcessDataFramePayload(const char* data, size_t len);
// Get (and lazily initialize) the ZLib state.
@@ -322,6 +385,36 @@ class NET_EXPORT_PRIVATE SpdyFramer {
// Not used (yet)
size_t BytesSafeToRead() const;
+ // Deliver the given control frame's compressed headers block to the visitor
+ // in decompressed form, in chunks. Returns true if the visitor has
+ // accepted all of the chunks.
+ bool IncrementallyDecompressControlFrameHeaderData(
+ const SpdyControlFrame* frame);
+
+ // Deliver the given control frame's compressed headers block to the visitor
+ // in decompressed form, in chunks. Returns true if the visitor has
+ // accepted all of the chunks.
+ bool IncrementallyDecompressControlFrameHeaderData(
+ const SpdyControlFrame* frame,
+ const char* data,
+ size_t len);
+
+ // Deliver the given control frame's uncompressed headers block to the
+ // visitor in chunks. Returns true if the visitor has accepted all of the
+ // chunks.
+ bool IncrementallyDeliverControlFrameHeaderData(const SpdyControlFrame* frame,
+ const char* data,
+ size_t len);
+
+ // Utility to copy the given data block to the current frame buffer, up
+ // to the given maximum number of bytes, and update the buffer
+ // data (pointer and length). Returns the number of bytes
+ // read, and:
+ // *data is advanced the number of bytes read.
+ // *len is reduced by the number of bytes read.
+ size_t UpdateCurrentFrameBuffer(const char** data, size_t* len,
+ size_t max_bytes);
+
// Set the error code and moves the framer into the error state.
void set_error(SpdyError error);
@@ -336,15 +429,38 @@ class NET_EXPORT_PRIVATE SpdyFramer {
int num_stream_compressors() const { return stream_compressors_.size(); }
int num_stream_decompressors() const { return stream_decompressors_.size(); }
+ // The initial size of the control frame buffer when compression is disabled.
+ // This exists because we don't do stream (de)compressed control frame data to
+ // our visitor; we instead buffer the entirety of the control frame and then
+ // decompress in one fell swoop.
+ // Since this is only used for control frame headers, the maximum control
+ // frame header size (18B) is sufficient; all remaining control frame data is
+ // streamed to the visitor.
+ // In addition to the maximum control frame header size, we account for any
+ // LOAS checksumming (16B) that may occur in the VTL case.
+ static const size_t kUncompressedControlFrameBufferInitialSize = 18 + 16;
+
+ // The size of the buffer into which compressed frames are inflated.
+ static const size_t kDecompressionBufferSize = 8 * 1024;
+
SpdyState state_;
SpdyError error_code_;
- size_t remaining_payload_;
+ size_t remaining_data_;
+
+ // The number of bytes remaining to read from the current control frame's
+ // payload.
size_t remaining_control_payload_;
+ // The number of bytes remaining to read from the current control frame's
+ // headers. Note that header data blocks (for control types that have them)
+ // are part of the frame's payload, and not the frame's headers.
+ size_t remaining_control_header_;
+
char* current_frame_buffer_;
size_t current_frame_len_; // Number of bytes read into the current_frame_.
size_t current_frame_capacity_;
+ bool validate_control_frame_sizes_;
bool enable_compression_; // Controls all compression
// SPDY header compressors.
scoped_ptr<z_stream> header_compressor_;
diff --git a/net/spdy/spdy_framer_test.cc b/net/spdy/spdy_framer_test.cc
index a4f7b81..157bf74 100644
--- a/net/spdy/spdy_framer_test.cc
+++ b/net/spdy/spdy_framer_test.cc
@@ -142,6 +142,17 @@ class TestSpdyVisitor : public SpdyFramerVisitorInterface {
++fin_flag_count_;
}
+ bool OnControlFrameHeaderData(SpdyStreamId stream_id,
+ const char* header_data,
+ size_t len) {
+ DCHECK(false);
+ return false;
+ }
+
+ void OnDataFrameHeader(const SpdyDataFrame* frame) {
+ DCHECK(false);
+ }
+
// Convenience function which runs a framer simulation with particular input.
void SimulateInFramer(const unsigned char* input, size_t size) {
framer_.set_enable_compression(false);
diff --git a/net/spdy/spdy_protocol.h b/net/spdy/spdy_protocol.h
index 851a4a6..ac6cd6b 100644
--- a/net/spdy/spdy_protocol.h
+++ b/net/spdy/spdy_protocol.h
@@ -191,8 +191,10 @@ enum SpdySettingsFlags {
enum SpdySettingsIds {
SETTINGS_UPLOAD_BANDWIDTH = 0x1,
SETTINGS_DOWNLOAD_BANDWIDTH = 0x2,
+ // Network round trip time in milliseconds.
SETTINGS_ROUND_TRIP_TIME = 0x3,
SETTINGS_MAX_CONCURRENT_STREAMS = 0x4,
+ // TCP congestion window in packets.
SETTINGS_CURRENT_CWND = 0x5,
// Downstream byte retransmission rate in percentage.
SETTINGS_DOWNLOAD_RETRANS_RATE = 0x6,
@@ -278,6 +280,15 @@ struct SpdySettingsControlFrameBlock : SpdyFrameBlock {
// Variable data here.
};
+// A NOOP Control Frame structure.
+struct SpdyNoopControlFrameBlock : SpdyFrameBlock {
+};
+
+// A PING Control Frame structure.
+struct SpdyPingControlFrameBlock : SpdyFrameBlock {
+ uint32 unique_id_;
+};
+
// A GOAWAY Control Frame structure.
struct SpdyGoAwayControlFrameBlock : SpdyFrameBlock {
SpdyStreamId last_accepted_stream_id_;
@@ -297,6 +308,7 @@ struct SpdyWindowUpdateControlFrameBlock : SpdyFrameBlock {
// A structure for the 8 bit flags and 24 bit ID fields.
union SettingsFlagsAndId {
+ // Sets both flags and id to the value for flags-and-id as sent over the wire
SettingsFlagsAndId(uint32 val) : id_(val) {}
uint8 flags() const { return flags_[0]; }
void set_flags(uint8 flags) { flags_[0] = flags; }
@@ -450,10 +462,25 @@ class SpdyControlFrame : public SpdyFrame {
mutable_block()->control_.type_ = htons(type);
}
+ // Returns true if this control frame is of a type that has a header block,
+ // otherwise it returns false.
+ bool has_header_block() const {
+ return type() == SYN_STREAM || type() == SYN_REPLY || type() == HEADERS;
+ }
+
// Returns the size of the SpdyFrameBlock structure.
// Note: this is not the size of the SpdyControlFrame class.
static size_t size() { return sizeof(SpdyFrameBlock); }
+ // The size of the 'Number of Name/Value pairs' field in a Name/Value block.
+ static const size_t kNumNameValuePairsSize = 2;
+
+ // The size of the 'Length of a name' field in a Name/Value block.
+ static const size_t kLengthOfNameSize = 2;
+
+ // The size of the 'Length of a value' field in a Name/Value block.
+ static const size_t kLengthOfValueSize = 2;
+
private:
const struct SpdyFrameBlock* block() const {
return frame_;
@@ -567,7 +594,12 @@ class SpdyRstStreamControlFrame : public SpdyControlFrame {
}
SpdyStatusCodes status() const {
- return static_cast<SpdyStatusCodes>(ntohl(block()->status_));
+ SpdyStatusCodes status =
+ static_cast<SpdyStatusCodes>(ntohl(block()->status_));
+ if (status < INVALID || status >= NUM_STATUS_CODES) {
+ status = INVALID;
+ }
+ return status;
}
void set_status(SpdyStatusCodes status) {
mutable_block()->status_ = htonl(static_cast<uint32>(status));
@@ -623,6 +655,40 @@ class SpdySettingsControlFrame : public SpdyControlFrame {
DISALLOW_COPY_AND_ASSIGN(SpdySettingsControlFrame);
};
+class SpdyNoOpControlFrame : public SpdyControlFrame {
+ public:
+ SpdyNoOpControlFrame() : SpdyControlFrame(size()) {}
+ SpdyNoOpControlFrame(char* data, bool owns_buffer)
+ : SpdyControlFrame(data, owns_buffer) {}
+
+ static size_t size() { return sizeof(SpdyNoopControlFrameBlock); }
+};
+
+class SpdyPingControlFrame : public SpdyControlFrame {
+ public:
+ SpdyPingControlFrame() : SpdyControlFrame(size()) {}
+ SpdyPingControlFrame(char* data, bool owns_buffer)
+ : SpdyControlFrame(data, owns_buffer) {}
+
+ uint32 unique_id() const {
+ return ntohl(block()->unique_id_);
+ }
+
+ void set_unique_id(uint32 unique_id) {
+ mutable_block()->unique_id_ = htonl(unique_id);
+ }
+
+ static size_t size() { return sizeof(SpdyPingControlFrameBlock); }
+
+ private:
+ const struct SpdyPingControlFrameBlock* block() const {
+ return static_cast<SpdyPingControlFrameBlock*>(frame_);
+ }
+ struct SpdyPingControlFrameBlock* mutable_block() {
+ return static_cast<SpdyPingControlFrameBlock*>(frame_);
+ }
+};
+
class SpdyGoAwayControlFrame : public SpdyControlFrame {
public:
SpdyGoAwayControlFrame() : SpdyControlFrame(size()) {}
diff --git a/net/spdy/spdy_session.cc b/net/spdy/spdy_session.cc
index 08fe3f5..317ec58 100644
--- a/net/spdy/spdy_session.cc
+++ b/net/spdy/spdy_session.cc
@@ -1255,6 +1255,17 @@ void SpdySession::OnControl(const spdy::SpdyControlFrame* frame) {
}
}
+bool SpdySession::OnControlFrameHeaderData(spdy::SpdyStreamId stream_id,
+ const char* header_data,
+ size_t len) {
+ DCHECK(false);
+ return false;
+}
+
+void SpdySession::OnDataFrameHeader(const spdy::SpdyDataFrame* frame) {
+ DCHECK(false);
+}
+
void SpdySession::OnRst(const spdy::SpdyRstStreamControlFrame& frame) {
spdy::SpdyStreamId stream_id = frame.stream_id();
diff --git a/net/spdy/spdy_session.h b/net/spdy/spdy_session.h
index 1a038f8..ad6ff84 100644
--- a/net/spdy/spdy_session.h
+++ b/net/spdy/spdy_session.h
@@ -336,6 +336,12 @@ class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>,
size_t len);
virtual void OnControl(const spdy::SpdyControlFrame* frame);
+ virtual bool OnControlFrameHeaderData(spdy::SpdyStreamId stream_id,
+ const char* header_data,
+ size_t len);
+
+ virtual void OnDataFrameHeader(const spdy::SpdyDataFrame* frame);
+
// Callbacks for the Spdy session.
CompletionCallbackImpl<SpdySession> read_callback_;
CompletionCallbackImpl<SpdySession> write_callback_;
diff --git a/net/tools/flip_server/spdy_interface.cc b/net/tools/flip_server/spdy_interface.cc
index 30cb401..d31048b 100644
--- a/net/tools/flip_server/spdy_interface.cc
+++ b/net/tools/flip_server/spdy_interface.cc
@@ -280,6 +280,17 @@ void SpdySM::OnControl(const SpdyControlFrame* frame) {
}
}
+bool SpdySM::OnControlFrameHeaderData(spdy::SpdyStreamId stream_id,
+ const char* header_data,
+ size_t len) {
+ DCHECK(false);
+ return false;
+}
+
+void SpdySM::OnDataFrameHeader(const spdy::SpdyDataFrame* frame) {
+ DCHECK(false);
+}
+
void SpdySM::OnStreamFrameData(SpdyStreamId stream_id,
const char* data, size_t len) {
VLOG(2) << ACCEPTOR_CLIENT_IDENT << "SpdySM: StreamData(" << stream_id
diff --git a/net/tools/flip_server/spdy_interface.h b/net/tools/flip_server/spdy_interface.h
index f407d2e..179b10c 100644
--- a/net/tools/flip_server/spdy_interface.h
+++ b/net/tools/flip_server/spdy_interface.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -62,6 +62,10 @@ class SpdySM : public spdy::SpdyFramerVisitorInterface,
// SpdyFramerVisitor interface.
virtual void OnControl(const spdy::SpdyControlFrame* frame);
+ virtual bool OnControlFrameHeaderData(spdy::SpdyStreamId stream_id,
+ const char* header_data,
+ size_t len);
+ virtual void OnDataFrameHeader(const spdy::SpdyDataFrame* frame);
virtual void OnStreamFrameData(spdy::SpdyStreamId stream_id,
const char* data, size_t len);