1
1
#include " source/common/common/hex.h"
2
+ #include " source/common/common/logger.h"
2
3
#include " source/common/http/utility.h"
3
4
#include " source/common/network/io_socket_handle_impl.h"
4
5
#include " source/common/network/listener_filter_buffer_impl.h"
@@ -33,8 +34,6 @@ class HttpInspectorTest : public testing::Test {
33
34
: cfg_(std::make_shared<Config>(*store_.rootScope())),
34
35
io_handle_ (Network::SocketInterfaceImpl::makePlatformSpecificSocket(42 , false ,
35
36
absl::nullopt, {})) {}
36
- ~HttpInspectorTest () override { io_handle_->close (); }
37
-
38
37
void init () {
39
38
filter_ = std::make_unique<Filter>(cfg_);
40
39
@@ -50,7 +49,7 @@ class HttpInspectorTest : public testing::Test {
50
49
DoAll (SaveArg<1 >(&file_event_callback_), ReturnNew<NiceMock<Event::MockFileEvent>>()));
51
50
buffer_ = std::make_unique<Network::ListenerFilterBufferImpl>(
52
51
*io_handle_, dispatcher_, [](bool ) {}, [](Network::ListenerFilterBuffer&) {},
53
- filter_-> maxReadBytes () == 0 , filter_-> maxReadBytes () );
52
+ Config::DEFAULT_INITIAL_BUFFER_SIZE == 0 , Config::DEFAULT_INITIAL_BUFFER_SIZE );
54
53
}
55
54
56
55
void testHttpInspectMultipleReadsNotFound (absl::string_view header, bool http2 = false ) {
@@ -516,7 +515,7 @@ TEST_F(HttpInspectorTest, Http1WithLargeRequestLine) {
516
515
// multiple recv calls.
517
516
init ();
518
517
absl::string_view method = " GET" , http = " /index HTTP/1.0\r " ;
519
- std::string spaces (Config::MAX_INSPECT_SIZE - method.size () - http.size (), ' ' );
518
+ std::string spaces (Config::DEFAULT_INITIAL_BUFFER_SIZE - method.size () - http.size (), ' ' );
520
519
const std::string data = absl::StrCat (method, spaces, http);
521
520
{
522
521
InSequence s;
@@ -529,7 +528,7 @@ TEST_F(HttpInspectorTest, Http1WithLargeRequestLine) {
529
528
}));
530
529
#endif
531
530
532
- uint64_t num_loops = Config::MAX_INSPECT_SIZE ;
531
+ uint64_t num_loops = Config::DEFAULT_INITIAL_BUFFER_SIZE ;
533
532
#if defined(__has_feature) && \
534
533
((__has_feature (thread_sanitizer)) || (__has_feature (address_sanitizer)))
535
534
num_loops = 2 ;
@@ -556,7 +555,7 @@ TEST_F(HttpInspectorTest, Http1WithLargeRequestLine) {
556
555
size_t len = (*ctr);
557
556
if (num_loops == 2 ) {
558
557
ASSERT (*ctr != 3 );
559
- len = size_t (Config::MAX_INSPECT_SIZE / (3 - (*ctr)));
558
+ len = size_t (Config::DEFAULT_INITIAL_BUFFER_SIZE / (3 - (*ctr)));
560
559
}
561
560
ASSERT (length >= len);
562
561
memcpy (buffer, data.data (), len);
@@ -584,8 +583,8 @@ TEST_F(HttpInspectorTest, Http1WithLargeRequestLine) {
584
583
TEST_F (HttpInspectorTest, Http1WithLargeHeader) {
585
584
init ();
586
585
absl::string_view request = " GET /index HTTP/1.0\r field: " ;
587
- // 0 20
588
- std::string value (Config::MAX_INSPECT_SIZE - request.size (), ' a' );
586
+ // 0 20
587
+ std::string value (Config::DEFAULT_INITIAL_BUFFER_SIZE - request.size (), ' a' );
589
588
const std::string data = absl::StrCat (request, value);
590
589
591
590
{
@@ -635,6 +634,96 @@ TEST_F(HttpInspectorTest, Http1WithLargeHeader) {
635
634
EXPECT_EQ (1 , cfg_->stats ().http10_found_ .value ());
636
635
}
637
636
637
+ TEST_F (HttpInspectorTest, HttpExceedMaxBufferSize) {
638
+ absl::string_view method = " GET" , http = " /index HTTP/1.0\r\n " ;
639
+ std::string spaces (Config::MAX_INSPECT_SIZE, ' ' );
640
+ const std::string data = absl::StrCat (method, spaces, http);
641
+
642
+ cfg_ = std::make_shared<Config>(*store_.rootScope ());
643
+
644
+ init ();
645
+ buffer_->resetCapacity (Config::MAX_INSPECT_SIZE);
646
+
647
+ EXPECT_CALL (os_sys_calls_, recv (42 , _, _, MSG_PEEK))
648
+ .WillOnce (
649
+ Invoke ([&data](os_fd_t , void * buffer, size_t length, int ) -> Api::SysCallSizeResult {
650
+ const size_t amount_to_copy = std::min (length, data.size ());
651
+ memcpy (buffer, data.data (), amount_to_copy);
652
+ return Api::SysCallSizeResult{ssize_t (amount_to_copy), 0 };
653
+ }));
654
+
655
+ EXPECT_TRUE (file_event_callback_ (Event::FileReadyType::Read).ok ());
656
+
657
+ auto accepted = filter_->onAccept (cb_);
658
+ EXPECT_EQ (accepted, Network::FilterStatus::StopIteration);
659
+ auto status = filter_->onData (*buffer_);
660
+ EXPECT_EQ (Network::FilterStatus::StopIteration, status);
661
+ EXPECT_EQ (1 , cfg_->stats ().read_error_ .value ());
662
+ EXPECT_FALSE (io_handle_->isOpen ());
663
+ }
664
+
665
+ TEST_F (HttpInspectorTest, HttpExceedInitialBufferSize) {
666
+ uint32_t buffer_size = Config::DEFAULT_INITIAL_BUFFER_SIZE;
667
+
668
+ absl::string_view method = " GET" , http = " /index HTTP/1.0\r\n " ;
669
+
670
+ // Want to test doubling a couple times.
671
+ std::string spaces (buffer_size * 3 , ' ' );
672
+
673
+ const std::string data = absl::StrCat (method, spaces, http);
674
+
675
+ init ();
676
+
677
+ EXPECT_CALL (os_sys_calls_, recv (42 , _, _, MSG_PEEK))
678
+ .WillOnce (
679
+ Invoke ([&data](os_fd_t , void * buffer, size_t length, int ) -> Api::SysCallSizeResult {
680
+ const size_t amount_to_copy = std::min (length, data.size ());
681
+ memcpy (buffer, data.data (), amount_to_copy);
682
+ return Api::SysCallSizeResult{ssize_t (amount_to_copy), 0 };
683
+ }));
684
+ EXPECT_TRUE (file_event_callback_ (Event::FileReadyType::Read).ok ());
685
+
686
+ auto accepted = filter_->onAccept (cb_);
687
+ EXPECT_EQ (accepted, Network::FilterStatus::StopIteration);
688
+ auto status = filter_->onData (*buffer_);
689
+ EXPECT_EQ (status, Network::FilterStatus::StopIteration);
690
+ EXPECT_EQ (filter_->maxReadBytes (), 2 * buffer_size);
691
+ buffer_size *= 2 ;
692
+ buffer_->resetCapacity (buffer_size);
693
+
694
+ EXPECT_CALL (os_sys_calls_, recv (42 , _, _, MSG_PEEK))
695
+ .WillOnce (
696
+ Invoke ([&data](os_fd_t , void * buffer, size_t length, int ) -> Api::SysCallSizeResult {
697
+ const size_t amount_to_copy = std::min (length, data.size ());
698
+ memcpy (buffer, data.data (), amount_to_copy);
699
+ return Api::SysCallSizeResult{ssize_t (amount_to_copy), 0 };
700
+ }));
701
+ EXPECT_TRUE (file_event_callback_ (Event::FileReadyType::Read).ok ());
702
+
703
+ status = filter_->onData (*buffer_);
704
+ EXPECT_EQ (status, Network::FilterStatus::StopIteration);
705
+ EXPECT_EQ (filter_->maxReadBytes (), 2 * buffer_size);
706
+ buffer_size *= 2 ;
707
+ buffer_->resetCapacity (buffer_size);
708
+
709
+ EXPECT_CALL (os_sys_calls_, recv (42 , _, _, MSG_PEEK))
710
+ .WillOnce (
711
+ Invoke ([&data](os_fd_t , void * buffer, size_t length, int ) -> Api::SysCallSizeResult {
712
+ const size_t amount_to_copy = std::min (length, data.size ());
713
+ memcpy (buffer, data.data (), amount_to_copy);
714
+ return Api::SysCallSizeResult{ssize_t (amount_to_copy), 0 };
715
+ }));
716
+ EXPECT_TRUE (file_event_callback_ (Event::FileReadyType::Read).ok ());
717
+
718
+ const std::vector<absl::string_view> alpn_protos{Http::Utility::AlpnNames::get ().Http10 };
719
+ EXPECT_CALL (socket_, setRequestedApplicationProtocols (alpn_protos));
720
+
721
+ status = filter_->onData (*buffer_);
722
+ EXPECT_EQ (status, Network::FilterStatus::Continue);
723
+
724
+ EXPECT_EQ (1 , cfg_->stats ().http10_found_ .value ());
725
+ }
726
+
638
727
} // namespace
639
728
} // namespace HttpInspector
640
729
} // namespace ListenerFilters
0 commit comments