Skip to content

Commit

Permalink
Set classId & methodId for Close method (#108)
Browse files Browse the repository at this point in the history
* Update amqpprox_connector.cpp

* Update amqpprox_connector.h

* Update amqpprox_dnsresolver.h

* Update amqpprox_hostnamemapper.h

* Update amqpprox_maybesecuresocketadaptor.h

* Update amqpprox_methods_close.h

* Update amqpprox_session.cpp

* Update amqpprox_farmstore.t.cpp

* Update amqpprox_maybesecuresocketadaptor.t.cpp

* Update amqpprox_session.t.cpp
  • Loading branch information
ThisIsNSH authored Dec 11, 2024
1 parent 5fc9270 commit df3e4e1
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 24 deletions.
13 changes: 9 additions & 4 deletions libamqpprox/amqpprox_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,12 @@ void Connector::synthesizeCloseError(bool sendToIngressSide)

void Connector::synthesizeCustomCloseError(bool sendToIngressSide,
uint16_t code,
std::string_view text)
std::string_view text,
uint16_t classId,
uint16_t methodId)
{
synthesizeMessage(d_close, sendToIngressSide, code, text);
synthesizeMessage(
d_close, sendToIngressSide, code, text, classId, methodId);
}

Buffer Connector::outBuffer()
Expand Down Expand Up @@ -416,10 +419,12 @@ void Connector::sendResponse(const T &response, bool sendToIngressSide)
void Connector::synthesizeMessage(methods::Close &replyMethod,
bool sendToIngressSide,
uint64_t code,
std::string_view text)
std::string_view text,
uint16_t classId,
uint16_t methodId)
{
d_buffer = Buffer(); // forget inbound buffer
replyMethod.setReply(code, std::string(text));
replyMethod.setReply(code, std::string(text), classId, methodId);
sendResponse(replyMethod, sendToIngressSide);
}

Expand Down
10 changes: 8 additions & 2 deletions libamqpprox/amqpprox_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class Connector {
inline void synthesizeMessage(methods::Close &replyMethod,
bool sendToIngressSide,
uint64_t code,
std::string_view text);
std::string_view text,
uint16_t classId = 0,
uint16_t methodId = 0);

public:
Connector(SessionState *sessionState,
Expand Down Expand Up @@ -167,10 +169,14 @@ class Connector {
* for communicating with server
* \param code custom error code
* \param text custom error text
* \param classId custom class id
* \param methodId custom method id
*/
void synthesizeCustomCloseError(bool sendToIngressSide,
uint16_t code,
std::string_view text);
std::string_view text,
uint16_t classId = 0,
uint16_t methodId = 0);

/**
* \brief Synthesize AMQP protocol header buffer, which will eventually be
Expand Down
4 changes: 2 additions & 2 deletions libamqpprox/amqpprox_dnsresolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ struct PairHash {
class DNSResolver {
using TcpEndpoint = boost::asio::ip::tcp::endpoint;
using CacheType = std::unordered_map<std::pair<std::string, std::string>,
std::vector<TcpEndpoint>,
PairHash>;
std::vector<TcpEndpoint>,
PairHash>;

public:
using OverrideFunction =
Expand Down
2 changes: 1 addition & 1 deletion libamqpprox/amqpprox_hostnamemapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace amqpprox {
*/
class HostnameMapper {
public:
virtual ~HostnameMapper(){};
virtual ~HostnameMapper() {};

/**
* \brief prime the cache of hostnames with a list of endpoints
Expand Down
10 changes: 2 additions & 8 deletions libamqpprox/amqpprox_maybesecuresocketadaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,9 @@ class MaybeSecureSocketAdaptor
src.d_dataRateTimer->cancel();
}

~MaybeSecureSocketAdaptor()
{
d_dataRateTimer->cancel();
}
~MaybeSecureSocketAdaptor() { d_dataRateTimer->cancel(); }

boost::asio::ip::tcp::socket &socket()
{
return d_socket->next_layer();
}
boost::asio::ip::tcp::socket &socket() { return d_socket->next_layer(); }

void setSecure(bool secure)
{
Expand Down
16 changes: 15 additions & 1 deletion libamqpprox/amqpprox_methods_close.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,24 @@ class Close {

const uint16_t methodId() const { return d_methodId; }

inline void setReply(uint16_t code, const std::string &text)
inline void setReply(uint16_t code,
const std::string &text,
uint16_t classId = 0,
uint16_t methodId = 0)
{
d_replyCode = code;
d_replyString = text;

// Close may be due to internal conditions or due to an exception
// during handling a specific method. When a close is due to an
// exception, the class and method id of the method which caused the
// exception is provided.
if (classId) {
d_classId = classId;
}
if (methodId) {
d_methodId = methodId;
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion libamqpprox/amqpprox_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,9 @@ void Session::disconnectUnauthClient(const FieldTable &clientProperties,
d_connector.synthesizeCustomCloseError(
true,
Reply::Codes::access_refused,
reason.substr(0, sendSize));
reason.substr(0, sendSize),
methods::StartOk::classType(),
methods::StartOk::methodType());
sendSyntheticData();
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/amqpprox_farmstore.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ TEST(FarmStore, GettingMissingFarm)
{
FarmStore farmStore;

EXPECT_THROW({ farmStore.getFarmByName("nosuchfarm"); },
std::runtime_error);
EXPECT_THROW(
{ farmStore.getFarmByName("nosuchfarm"); }, std::runtime_error);
}

TEST(FarmStore, AddTwice)
Expand Down
4 changes: 2 additions & 2 deletions tests/amqpprox_maybesecuresocketadaptor.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SocketInterface {
virtual size_t read_some(const boost::asio::mutable_buffers_1 &buffer,
boost::system::error_code &error) = 0;

virtual ~SocketInterface(){};
virtual ~SocketInterface() {};
};

class MockSocket : public SocketInterface {
Expand Down Expand Up @@ -76,7 +76,7 @@ class MockSocket : public SocketInterface {

class TimerInterface {
public:
virtual ~TimerInterface(){};
virtual ~TimerInterface() {};
virtual void cancel() = 0;

virtual size_t
Expand Down
4 changes: 3 additions & 1 deletion tests/amqpprox_session.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,9 @@ TEST_F(SessionTest,
std::shared_ptr<methods::Close> closeMethodPtr =
std::make_shared<methods::Close>();
closeMethodPtr->setReply(Reply::Codes::access_refused,
"Unauthorized test client");
"Unauthorized test client",
methods::StartOk::classType(),
methods::StartOk::methodType());
testSetupClientOpenWithProxyClose(4, closeMethodPtr);

std::shared_ptr<MaybeSecureSocketAdaptor<>> clientSocket =
Expand Down

0 comments on commit df3e4e1

Please sign in to comment.