From 10e7d436b14af1d15d56dd094adde2bcca2262ec Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 10 Aug 2013 03:37:03 -0700 Subject: [PATCH] Add PROXY TestPeer client --- .../Builds/VisualStudio2012/beast.vcxproj | 5 +++ .../VisualStudio2012/beast.vcxproj.filters | 6 +++ .../beast/modules/beast_asio/beast_asio.h | 1 + .../protocol/beast_ProxyHandshake.cpp | 4 +- .../tests/beast_TestPeerLogicProxyClient.cpp | 37 +++++++++++++++++++ .../tests/beast_TestPeerLogicProxyClient.h | 31 ++++++++++++++++ .../tests/beast_TestPeerLogicSyncClient.cpp | 11 ++++++ .../tests/beast_TestPeerLogicSyncClient.h | 1 + 8 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.cpp create mode 100644 Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.h diff --git a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj index c4b9f5ae4..eca86d367 100644 --- a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj +++ b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj @@ -86,6 +86,7 @@ + @@ -334,6 +335,10 @@ true true + + true + true + true true diff --git a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters index 33811bf7c..52c594a7a 100644 --- a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters @@ -815,6 +815,9 @@ beast_asio\protocol + + beast_asio\tests + @@ -1270,6 +1273,9 @@ beast_asio\protocol + + beast_asio\tests + diff --git a/Subtrees/beast/modules/beast_asio/beast_asio.h b/Subtrees/beast/modules/beast_asio/beast_asio.h index 077268e00..f8f52d841 100644 --- a/Subtrees/beast/modules/beast_asio/beast_asio.h +++ b/Subtrees/beast/modules/beast_asio/beast_asio.h @@ -64,6 +64,7 @@ namespace beast #include "tests/beast_TestPeerLogic.h" #include "tests/beast_TestPeerLogicSyncServer.h" #include "tests/beast_TestPeerLogicSyncClient.h" +#include "tests/beast_TestPeerLogicProxyClient.h" #include "tests/beast_TestPeerLogicAsyncServer.h" #include "tests/beast_TestPeerLogicAsyncClient.h" #include "tests/beast_TestPeerType.h" diff --git a/Subtrees/beast/modules/beast_asio/protocol/beast_ProxyHandshake.cpp b/Subtrees/beast/modules/beast_asio/protocol/beast_ProxyHandshake.cpp index 99dc22beb..94856fcbc 100644 --- a/Subtrees/beast/modules/beast_asio/protocol/beast_ProxyHandshake.cpp +++ b/Subtrees/beast/modules/beast_asio/protocol/beast_ProxyHandshake.cpp @@ -316,7 +316,7 @@ public: expect (h.getStatus () == ProxyHandshake::statusHandshake); - for (int i = 0; i < s.size () && h.getStatus () == ProxyHandshake::statusHandshake ; ++i) + for (std::size_t i = 0; i < s.size () && h.getStatus () == ProxyHandshake::statusHandshake ; ++i) { std::size_t const bytesConsumed = h.feed (& s[i], 1); @@ -354,7 +354,7 @@ public: unexpected (p.parse (s.c_str (), s.size ())); } - for (int i = 1; i < s.size () - 1; ++i) + for (std::size_t i = 1; i < s.size () - 1; ++i) { String const partial = String (s).dropLastCharacters (i); std::string ss (partial.toStdString ()); diff --git a/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.cpp b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.cpp new file mode 100644 index 000000000..fb57deaf5 --- /dev/null +++ b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.cpp @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +TestPeerLogicProxyClient::TestPeerLogicProxyClient (Socket& socket) + : TestPeerLogicSyncClient (socket) +{ +} + +void TestPeerLogicProxyClient::on_pre_handshake () +{ + ProxyHandshake h; + + static std::string line ( + "PROXY TCP4 255.255.255.255 255.255.255.255 65535 65535\r\n" + // 56 chars + ); + + std::size_t const amount = boost::asio::write ( + socket (), boost::asio::buffer (line), error ()); +} + diff --git a/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.h b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.h new file mode 100644 index 000000000..fd1829adc --- /dev/null +++ b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicProxyClient.h @@ -0,0 +1,31 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef BEAST_TESTPEERLOGICPROXYCLIENT_H_INCLUDED +#define BEAST_TESTPEERLOGICPROXYCLIENT_H_INCLUDED + +/** A synchronous client logic that sends a PROXY protocol pre-handshake. */ +class TestPeerLogicProxyClient : public TestPeerLogicSyncClient +{ +public: + explicit TestPeerLogicProxyClient (Socket& socket); + void on_pre_handshake (); +}; + +#endif diff --git a/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp index efcaf6558..87775fd6b 100644 --- a/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp +++ b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.cpp @@ -34,6 +34,13 @@ TestPeerBasics::Model TestPeerLogicSyncClient::get_model () const noexcept void TestPeerLogicSyncClient::on_connect () { + { + // pre-handshake hook is optional + on_pre_handshake (); + if (failure (error ())) + return ; + } + if (socket ().requires_handshake ()) { if (failure (socket ().handshake (get_role (), error ()))) @@ -95,3 +102,7 @@ void TestPeerLogicSyncClient::on_connect () if (failure (socket ().close (error ()))) return; } + +void TestPeerLogicSyncClient::on_pre_handshake () +{ +} diff --git a/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h index ca0624ffa..2e1a55585 100644 --- a/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h +++ b/Subtrees/beast/modules/beast_asio/tests/beast_TestPeerLogicSyncClient.h @@ -27,6 +27,7 @@ public: Role get_role () const noexcept; Model get_model () const noexcept; void on_connect (); + virtual void on_pre_handshake (); }; #endif