| 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 // TODO(rtenhove) clean up frame buffer size calculations so that we aren't | 5 // TODO(rtenhove) clean up frame buffer size calculations so that we aren't |
| 6 // constantly adding and subtracting header sizes; this is ugly and error- | 6 // constantly adding and subtracting header sizes; this is ugly and error- |
| 7 // prone. | 7 // prone. |
| 8 | 8 |
| 9 #include "net/spdy/spdy_framer.h" | 9 #include "net/spdy/spdy_framer.h" |
| 10 | 10 |
| (...skipping 1239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1250 return reinterpret_cast<SpdyDataFrame*>(frame.take()); | 1250 return reinterpret_cast<SpdyDataFrame*>(frame.take()); |
| 1251 } | 1251 } |
| 1252 | 1252 |
| 1253 // The following compression setting are based on Brian Olson's analysis. See | 1253 // The following compression setting are based on Brian Olson's analysis. See |
| 1254 // https://groups.google.com/group/spdy-dev/browse_thread/thread/dfaf498542fac79
2 | 1254 // https://groups.google.com/group/spdy-dev/browse_thread/thread/dfaf498542fac79
2 |
| 1255 // for more details. | 1255 // for more details. |
| 1256 static const int kCompressorLevel = 9; | 1256 static const int kCompressorLevel = 9; |
| 1257 static const int kCompressorWindowSizeInBits = 11; | 1257 static const int kCompressorWindowSizeInBits = 11; |
| 1258 static const int kCompressorMemLevel = 1; | 1258 static const int kCompressorMemLevel = 1; |
| 1259 | 1259 |
| 1260 SpdyFrame* SpdyFramer::CompressFrame(const SpdyFrame& frame) { | |
| 1261 if (frame.is_control_frame()) { | |
| 1262 return CompressControlFrame( | |
| 1263 reinterpret_cast<const SpdyControlFrame&>(frame)); | |
| 1264 } | |
| 1265 return NULL; | |
| 1266 } | |
| 1267 | |
| 1268 bool SpdyFramer::IsCompressible(const SpdyFrame& frame) const { | |
| 1269 // The important frames to compress are those which contain large | |
| 1270 // amounts of compressible data - namely the headers in the SYN_STREAM | |
| 1271 // and SYN_REPLY. | |
| 1272 if (frame.is_control_frame()) { | |
| 1273 const SpdyControlFrame& control_frame = | |
| 1274 reinterpret_cast<const SpdyControlFrame&>(frame); | |
| 1275 return control_frame.type() == SYN_STREAM || | |
| 1276 control_frame.type() == SYN_REPLY; | |
| 1277 } | |
| 1278 | |
| 1279 // We don't compress Data frames. | |
| 1280 return false; | |
| 1281 } | |
| 1282 | |
| 1283 z_stream* SpdyFramer::GetHeaderCompressor() { | 1260 z_stream* SpdyFramer::GetHeaderCompressor() { |
| 1284 if (header_compressor_.get()) | 1261 if (header_compressor_.get()) |
| 1285 return header_compressor_.get(); // Already initialized. | 1262 return header_compressor_.get(); // Already initialized. |
| 1286 | 1263 |
| 1287 header_compressor_.reset(new z_stream); | 1264 header_compressor_.reset(new z_stream); |
| 1288 memset(header_compressor_.get(), 0, sizeof(z_stream)); | 1265 memset(header_compressor_.get(), 0, sizeof(z_stream)); |
| 1289 | 1266 |
| 1290 int success = deflateInit2(header_compressor_.get(), | 1267 int success = deflateInit2(header_compressor_.get(), |
| 1291 kCompressorLevel, | 1268 kCompressorLevel, |
| 1292 Z_DEFLATED, | 1269 Z_DEFLATED, |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1547 return read_successfully; | 1524 return read_successfully; |
| 1548 } | 1525 } |
| 1549 | 1526 |
| 1550 SpdyFrame* SpdyFramer::DuplicateFrame(const SpdyFrame& frame) { | 1527 SpdyFrame* SpdyFramer::DuplicateFrame(const SpdyFrame& frame) { |
| 1551 int size = SpdyFrame::kHeaderSize + frame.length(); | 1528 int size = SpdyFrame::kHeaderSize + frame.length(); |
| 1552 SpdyFrame* new_frame = new SpdyFrame(size); | 1529 SpdyFrame* new_frame = new SpdyFrame(size); |
| 1553 memcpy(new_frame->data(), frame.data(), size); | 1530 memcpy(new_frame->data(), frame.data(), size); |
| 1554 return new_frame; | 1531 return new_frame; |
| 1555 } | 1532 } |
| 1556 | 1533 |
| 1534 bool SpdyFramer::IsCompressible(const SpdyFrame& frame) const { |
| 1535 // The important frames to compress are those which contain large |
| 1536 // amounts of compressible data - namely the headers in the SYN_STREAM |
| 1537 // and SYN_REPLY. |
| 1538 if (frame.is_control_frame()) { |
| 1539 const SpdyControlFrame& control_frame = |
| 1540 reinterpret_cast<const SpdyControlFrame&>(frame); |
| 1541 return control_frame.type() == SYN_STREAM || |
| 1542 control_frame.type() == SYN_REPLY || |
| 1543 control_frame.type() == HEADERS; |
| 1544 } |
| 1545 |
| 1546 // We don't compress Data frames. |
| 1547 return false; |
| 1548 } |
| 1549 |
| 1557 size_t SpdyFramer::GetMinimumControlFrameSize(int version, | 1550 size_t SpdyFramer::GetMinimumControlFrameSize(int version, |
| 1558 SpdyControlType type) { | 1551 SpdyControlType type) { |
| 1559 switch (type) { | 1552 switch (type) { |
| 1560 case SYN_STREAM: | 1553 case SYN_STREAM: |
| 1561 return SpdySynStreamControlFrame::size(); | 1554 return SpdySynStreamControlFrame::size(); |
| 1562 case SYN_REPLY: | 1555 case SYN_REPLY: |
| 1563 return SpdySynReplyControlFrame::size(); | 1556 return SpdySynReplyControlFrame::size(); |
| 1564 case RST_STREAM: | 1557 case RST_STREAM: |
| 1565 return SpdyRstStreamControlFrame::size(); | 1558 return SpdyRstStreamControlFrame::size(); |
| 1566 case SETTINGS: | 1559 case SETTINGS: |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1638 | 1631 |
| 1639 void SpdyFramer::set_enable_compression(bool value) { | 1632 void SpdyFramer::set_enable_compression(bool value) { |
| 1640 enable_compression_ = value; | 1633 enable_compression_ = value; |
| 1641 } | 1634 } |
| 1642 | 1635 |
| 1643 void SpdyFramer::set_enable_compression_default(bool value) { | 1636 void SpdyFramer::set_enable_compression_default(bool value) { |
| 1644 g_enable_compression_default = value; | 1637 g_enable_compression_default = value; |
| 1645 } | 1638 } |
| 1646 | 1639 |
| 1647 } // namespace net | 1640 } // namespace net |
| OLD | NEW |