| 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/core/spdy_framer.h" | 5 #include "net/spdy/core/spdy_framer.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 last_push_promise_promised_stream_(0), | 403 last_push_promise_promised_stream_(0), |
| 404 data_bytes_(0), | 404 data_bytes_(0), |
| 405 fin_frame_count_(0), | 405 fin_frame_count_(0), |
| 406 fin_flag_count_(0), | 406 fin_flag_count_(0), |
| 407 end_of_stream_count_(0), | 407 end_of_stream_count_(0), |
| 408 control_frame_header_data_count_(0), | 408 control_frame_header_data_count_(0), |
| 409 zero_length_control_frame_header_data_count_(0), | 409 zero_length_control_frame_header_data_count_(0), |
| 410 data_frame_count_(0), | 410 data_frame_count_(0), |
| 411 last_payload_len_(0), | 411 last_payload_len_(0), |
| 412 last_frame_len_(0), | 412 last_frame_len_(0), |
| 413 header_buffer_(kDefaultHeaderBufferSize), | 413 header_buffer_(new char[kDefaultHeaderBufferSize]), |
| 414 header_buffer_length_(0), | 414 header_buffer_length_(0), |
| 415 header_buffer_size_(kDefaultHeaderBufferSize), |
| 415 header_stream_id_(static_cast<SpdyStreamId>(-1)), | 416 header_stream_id_(static_cast<SpdyStreamId>(-1)), |
| 416 header_control_type_(SpdyFrameType::DATA), | 417 header_control_type_(SpdyFrameType::DATA), |
| 417 header_buffer_valid_(false) {} | 418 header_buffer_valid_(false) {} |
| 418 | 419 |
| 419 void OnError(SpdyFramer* f) override { | 420 void OnError(SpdyFramer* f) override { |
| 420 VLOG(1) << "SpdyFramer Error: " | 421 VLOG(1) << "SpdyFramer Error: " |
| 421 << SpdyFramer::SpdyFramerErrorToString(f->spdy_framer_error()); | 422 << SpdyFramer::SpdyFramerErrorToString(f->spdy_framer_error()); |
| 422 ++error_count_; | 423 ++error_count_; |
| 423 } | 424 } |
| 424 | 425 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 input_ptr += bytes_processed; | 611 input_ptr += bytes_processed; |
| 611 } | 612 } |
| 612 } | 613 } |
| 613 | 614 |
| 614 void InitHeaderStreaming(SpdyFrameType header_control_type, | 615 void InitHeaderStreaming(SpdyFrameType header_control_type, |
| 615 SpdyStreamId stream_id) { | 616 SpdyStreamId stream_id) { |
| 616 if (!IsDefinedFrameType(SerializeFrameType(header_control_type))) { | 617 if (!IsDefinedFrameType(SerializeFrameType(header_control_type))) { |
| 617 DLOG(FATAL) << "Attempted to init header streaming with " | 618 DLOG(FATAL) << "Attempted to init header streaming with " |
| 618 << "invalid control frame type: " << header_control_type; | 619 << "invalid control frame type: " << header_control_type; |
| 619 } | 620 } |
| 620 std::fill(header_buffer_.begin(), header_buffer_.end(), 0); | 621 memset(header_buffer_.get(), 0, header_buffer_size_); |
| 621 header_buffer_length_ = 0; | 622 header_buffer_length_ = 0; |
| 622 header_stream_id_ = stream_id; | 623 header_stream_id_ = stream_id; |
| 623 header_control_type_ = header_control_type; | 624 header_control_type_ = header_control_type; |
| 624 header_buffer_valid_ = true; | 625 header_buffer_valid_ = true; |
| 625 DCHECK_NE(header_stream_id_, SpdyFramer::kInvalidStream); | 626 DCHECK_NE(header_stream_id_, SpdyFramer::kInvalidStream); |
| 626 } | 627 } |
| 627 | 628 |
| 628 void set_extension_visitor(ExtensionVisitorInterface* extension) { | 629 void set_extension_visitor(ExtensionVisitorInterface* extension) { |
| 629 framer_.set_extension_visitor(extension); | 630 framer_.set_extension_visitor(extension); |
| 630 } | 631 } |
| 631 | 632 |
| 632 // Override the default buffer size (16K). Call before using the framer! | 633 // Override the default buffer size (16K). Call before using the framer! |
| 633 void set_header_buffer_size(size_t header_buffer_size) { | 634 void set_header_buffer_size(size_t header_buffer_size) { |
| 634 header_buffer_.resize(header_buffer_size); | 635 header_buffer_size_ = header_buffer_size; |
| 636 header_buffer_.reset(new char[header_buffer_size]); |
| 635 } | 637 } |
| 636 | 638 |
| 637 // Largest control frame that the SPDY implementation sends, including the | 639 // Largest control frame that the SPDY implementation sends, including the |
| 638 // size of the header. | 640 // size of the header. |
| 639 static size_t sent_control_frame_max_size() { | 641 static size_t sent_control_frame_max_size() { |
| 640 return SpdyFramer::kMaxControlFrameSize; | 642 return SpdyFramer::kMaxControlFrameSize; |
| 641 } | 643 } |
| 642 | 644 |
| 643 // Largest control frame that the SPDY implementation is willing to receive, | 645 // Largest control frame that the SPDY implementation is willing to receive, |
| 644 // excluding the size of the header. | 646 // excluding the size of the header. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 674 int fin_flag_count_; // The count of frames with the FIN flag set. | 676 int fin_flag_count_; // The count of frames with the FIN flag set. |
| 675 int end_of_stream_count_; // The count of zero-length data frames. | 677 int end_of_stream_count_; // The count of zero-length data frames. |
| 676 int control_frame_header_data_count_; // The count of chunks received. | 678 int control_frame_header_data_count_; // The count of chunks received. |
| 677 // The count of zero-length control frame header data chunks received. | 679 // The count of zero-length control frame header data chunks received. |
| 678 int zero_length_control_frame_header_data_count_; | 680 int zero_length_control_frame_header_data_count_; |
| 679 int data_frame_count_; | 681 int data_frame_count_; |
| 680 size_t last_payload_len_; | 682 size_t last_payload_len_; |
| 681 size_t last_frame_len_; | 683 size_t last_frame_len_; |
| 682 | 684 |
| 683 // Header block streaming state: | 685 // Header block streaming state: |
| 684 std::vector<char> header_buffer_; | 686 std::unique_ptr<char[]> header_buffer_; |
| 685 size_t header_buffer_length_; | 687 size_t header_buffer_length_; |
| 688 size_t header_buffer_size_; |
| 686 size_t header_bytes_received_; | 689 size_t header_bytes_received_; |
| 687 SpdyStreamId header_stream_id_; | 690 SpdyStreamId header_stream_id_; |
| 688 SpdyFrameType header_control_type_; | 691 SpdyFrameType header_control_type_; |
| 689 bool header_buffer_valid_; | 692 bool header_buffer_valid_; |
| 690 std::unique_ptr<TestHeadersHandler> headers_handler_; | 693 std::unique_ptr<TestHeadersHandler> headers_handler_; |
| 691 SpdyHeaderBlock headers_; | 694 SpdyHeaderBlock headers_; |
| 692 bool header_has_priority_; | 695 bool header_has_priority_; |
| 693 SpdyStreamId header_parent_stream_id_; | 696 SpdyStreamId header_parent_stream_id_; |
| 694 bool header_exclusive_; | 697 bool header_exclusive_; |
| 695 }; | 698 }; |
| (...skipping 2324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3020 headers.SetHeader("aa", big_value); | 3023 headers.SetHeader("aa", big_value); |
| 3021 SpdySerializedFrame control_frame(SpdyFramerPeer::SerializeHeaders( | 3024 SpdySerializedFrame control_frame(SpdyFramerPeer::SerializeHeaders( |
| 3022 &framer, headers, use_output_ ? &output_ : nullptr)); | 3025 &framer, headers, use_output_ ? &output_ : nullptr)); |
| 3023 TestSpdyVisitor visitor(SpdyFramer::ENABLE_COMPRESSION); | 3026 TestSpdyVisitor visitor(SpdyFramer::ENABLE_COMPRESSION); |
| 3024 visitor.set_header_buffer_size(kHeaderBufferSize); | 3027 visitor.set_header_buffer_size(kHeaderBufferSize); |
| 3025 visitor.SimulateInFramer( | 3028 visitor.SimulateInFramer( |
| 3026 reinterpret_cast<unsigned char*>(control_frame.data()), | 3029 reinterpret_cast<unsigned char*>(control_frame.data()), |
| 3027 control_frame.size()); | 3030 control_frame.size()); |
| 3028 // It's up to the visitor to ignore extraneous header data; the framer | 3031 // It's up to the visitor to ignore extraneous header data; the framer |
| 3029 // won't throw an error. | 3032 // won't throw an error. |
| 3030 EXPECT_GT(visitor.header_bytes_received_, visitor.header_buffer_.size()); | 3033 EXPECT_GT(visitor.header_bytes_received_, visitor.header_buffer_size_); |
| 3031 EXPECT_EQ(1, visitor.end_of_stream_count_); | 3034 EXPECT_EQ(1, visitor.end_of_stream_count_); |
| 3032 } | 3035 } |
| 3033 | 3036 |
| 3034 TEST_P(SpdyFramerTest, ControlFrameSizesAreValidated) { | 3037 TEST_P(SpdyFramerTest, ControlFrameSizesAreValidated) { |
| 3035 SpdyFramer framer(SpdyFramer::ENABLE_COMPRESSION); | 3038 SpdyFramer framer(SpdyFramer::ENABLE_COMPRESSION); |
| 3036 // Create a GoAway frame that has a few extra bytes at the end. | 3039 // Create a GoAway frame that has a few extra bytes at the end. |
| 3037 // We create enough overhead to overflow the framer's control frame buffer. | 3040 // We create enough overhead to overflow the framer's control frame buffer. |
| 3038 ASSERT_LE(SpdyFramerPeer::ControlFrameBufferSize(), 250u); | 3041 ASSERT_LE(SpdyFramerPeer::ControlFrameBufferSize(), 250u); |
| 3039 const size_t length = SpdyFramerPeer::ControlFrameBufferSize() + 1; | 3042 const size_t length = SpdyFramerPeer::ControlFrameBufferSize() + 1; |
| 3040 | 3043 |
| (...skipping 1821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4862 | 4865 |
| 4863 EXPECT_EQ(1, visitor->data_frame_count_); | 4866 EXPECT_EQ(1, visitor->data_frame_count_); |
| 4864 EXPECT_EQ(strlen(four_score), static_cast<unsigned>(visitor->data_bytes_)); | 4867 EXPECT_EQ(strlen(four_score), static_cast<unsigned>(visitor->data_bytes_)); |
| 4865 EXPECT_EQ(0, visitor->headers_frame_count_); | 4868 EXPECT_EQ(0, visitor->headers_frame_count_); |
| 4866 } | 4869 } |
| 4867 } | 4870 } |
| 4868 | 4871 |
| 4869 } // namespace test | 4872 } // namespace test |
| 4870 | 4873 |
| 4871 } // namespace net | 4874 } // namespace net |
| OLD | NEW |