Skip to content
Snippets Groups Projects
Unverified Commit fae6b138 authored by Sébastien Blin's avatar Sébastien Blin Committed by GitHub
Browse files

Merge pull request #275 from savoirfairelinux/proxy

proxy server: catch exceptions in requests, always close connection
parents ab0f4f6f a48a965c
Branches
Tags
No related merge requests found
...@@ -49,6 +49,8 @@ struct DhtProxyServer::SearchPuts { ...@@ -49,6 +49,8 @@ struct DhtProxyServer::SearchPuts {
DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port , const std::string& pushServer) DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port , const std::string& pushServer)
: dht_(dht) , pushServer_(pushServer) : dht_(dht) , pushServer_(pushServer)
{ {
if (not dht_)
throw std::invalid_argument("A DHT instance must be provided");
// NOTE in c++14, use make_unique // NOTE in c++14, use make_unique
service_ = std::unique_ptr<restbed::Service>(new restbed::Service()); service_ = std::unique_ptr<restbed::Service>(new restbed::Service());
...@@ -187,6 +189,7 @@ DhtProxyServer::getNodeInfo(const std::shared_ptr<restbed::Session>& session) co ...@@ -187,6 +189,7 @@ DhtProxyServer::getNodeInfo(const std::shared_ptr<restbed::Session>& session) co
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b*/) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b*/)
{ {
try {
if (dht_) { if (dht_) {
Json::Value result; Json::Value result;
auto id = dht_->getId(); auto id = dht_->getId();
...@@ -204,6 +207,9 @@ DhtProxyServer::getNodeInfo(const std::shared_ptr<restbed::Session>& session) co ...@@ -204,6 +207,9 @@ DhtProxyServer::getNodeInfo(const std::shared_ptr<restbed::Session>& session) co
} }
else else
s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
...@@ -218,6 +224,7 @@ DhtProxyServer::get(const std::shared_ptr<restbed::Session>& session) const ...@@ -218,6 +224,7 @@ DhtProxyServer::get(const std::shared_ptr<restbed::Session>& session) const
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b* */) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b* */)
{ {
try {
if (dht_) { if (dht_) {
InfoHash infoHash(hash); InfoHash infoHash(hash);
if (!infoHash) { if (!infoHash) {
...@@ -244,6 +251,9 @@ DhtProxyServer::get(const std::shared_ptr<restbed::Session>& session) const ...@@ -244,6 +251,9 @@ DhtProxyServer::get(const std::shared_ptr<restbed::Session>& session) const
} else { } else {
s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} }
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
...@@ -261,6 +271,7 @@ DhtProxyServer::listen(const std::shared_ptr<restbed::Session>& session) ...@@ -261,6 +271,7 @@ DhtProxyServer::listen(const std::shared_ptr<restbed::Session>& session)
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b* */) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b* */)
{ {
try {
if (dht_) { if (dht_) {
InfoHash infoHash(hash); InfoHash infoHash(hash);
if (!infoHash) { if (!infoHash) {
...@@ -296,6 +307,9 @@ DhtProxyServer::listen(const std::shared_ptr<restbed::Session>& session) ...@@ -296,6 +307,9 @@ DhtProxyServer::listen(const std::shared_ptr<restbed::Session>& session)
} else { } else {
session->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); session->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} }
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
...@@ -399,7 +413,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session) ...@@ -399,7 +413,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
} }
s->close(restbed::OK, "{\"token\": " + std::to_string(tokenFromReq) + "}\n"); s->close(restbed::OK, "{\"token\": " + std::to_string(tokenFromReq) + "}\n");
} catch (...) { } catch (...) {
// do nothing s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
} }
} }
); );
...@@ -437,8 +451,9 @@ DhtProxyServer::unsubscribe(const std::shared_ptr<restbed::Session>& session) ...@@ -437,8 +451,9 @@ DhtProxyServer::unsubscribe(const std::shared_ptr<restbed::Session>& session)
if (token == 0) return; if (token == 0) return;
cancelPushListen(pushToken, infoHash, token); cancelPushListen(pushToken, infoHash, token);
s->close(restbed::OK);
} catch (...) { } catch (...) {
// do nothing s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
} }
} }
); );
...@@ -545,6 +560,7 @@ DhtProxyServer::put(const std::shared_ptr<restbed::Session>& session) ...@@ -545,6 +560,7 @@ DhtProxyServer::put(const std::shared_ptr<restbed::Session>& session)
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b)
{ {
try {
if (dht_) { if (dht_) {
if(b.empty()) { if(b.empty()) {
std::string response("{\"err\":\"Missing parameters\"}"); std::string response("{\"err\":\"Missing parameters\"}");
...@@ -622,6 +638,9 @@ DhtProxyServer::put(const std::shared_ptr<restbed::Session>& session) ...@@ -622,6 +638,9 @@ DhtProxyServer::put(const std::shared_ptr<restbed::Session>& session)
} else { } else {
s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} }
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
...@@ -641,6 +660,7 @@ DhtProxyServer::putSigned(const std::shared_ptr<restbed::Session>& session) cons ...@@ -641,6 +660,7 @@ DhtProxyServer::putSigned(const std::shared_ptr<restbed::Session>& session) cons
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b)
{ {
try {
if (dht_) { if (dht_) {
if(b.empty()) { if(b.empty()) {
std::string response("{\"err\":\"Missing parameters\"}"); std::string response("{\"err\":\"Missing parameters\"}");
...@@ -670,6 +690,9 @@ DhtProxyServer::putSigned(const std::shared_ptr<restbed::Session>& session) cons ...@@ -670,6 +690,9 @@ DhtProxyServer::putSigned(const std::shared_ptr<restbed::Session>& session) cons
} else { } else {
s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} }
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
...@@ -688,6 +711,7 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c ...@@ -688,6 +711,7 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b)
{ {
try {
if (dht_) { if (dht_) {
if(b.empty()) { if(b.empty()) {
std::string response("{\"err\":\"Missing parameters\"}"); std::string response("{\"err\":\"Missing parameters\"}");
...@@ -721,6 +745,9 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c ...@@ -721,6 +745,9 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c
} else { } else {
s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} }
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
...@@ -751,6 +778,7 @@ DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) co ...@@ -751,6 +778,7 @@ DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) co
session->fetch(content_length, session->fetch(content_length,
[=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b* */) [=](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& /*b* */)
{ {
try {
if (dht_) { if (dht_) {
InfoHash infoHash(hash); InfoHash infoHash(hash);
if (!infoHash) { if (!infoHash) {
...@@ -773,6 +801,9 @@ DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) co ...@@ -773,6 +801,9 @@ DhtProxyServer::getFiltered(const std::shared_ptr<restbed::Session>& session) co
} else { } else {
s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}"); s->close(restbed::SERVICE_UNAVAILABLE, "{\"err\":\"Incorrect DhtRunner\"}");
} }
} catch (...) {
s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
}
} }
); );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment