mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 12:28:42 +00:00
add get hash endpoint
This commit is contained in:
1 parent
6f641073ae
commit
58fdfa9452
5 files changed
+73
-27
No files matched your search
@@ -26,6 +26,11 @@ public:
|
||||
DhtProxyServer& operator=(DhtProxyServer&& other) = default;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Method: GET
|
||||
* PathParameter: hash
|
||||
*/
|
||||
void get(const std::shared_ptr<restbed::Session>& session) const;
|
||||
/**
|
||||
* Method: GET
|
||||
*/
|
||||
|
||||
@@ -364,7 +364,9 @@ public:
|
||||
*/
|
||||
void join();
|
||||
|
||||
void startProxyInterface(); // TODO arguments
|
||||
void startProxyInterface(); // TODO arguments and move in the constructor
|
||||
void startProxyClient(const std::string& url);
|
||||
void stopProxyClient();
|
||||
|
||||
private:
|
||||
static constexpr std::chrono::seconds BOOTSTRAP_PERIOD {10};
|
||||
@@ -396,6 +398,8 @@ private:
|
||||
std::vector<std::pair<Blob, SockAddr>> rcv {};
|
||||
|
||||
std::unique_ptr<DhtProxyServer> proxy_server_;
|
||||
std::atomic_bool proxyfing {false};
|
||||
|
||||
/** true if currently actively boostraping */
|
||||
std::atomic_bool bootstraping {false};
|
||||
/* bootstrap nodes given as (host, service) pairs */
|
||||
|
||||
+49
-13
@@ -16,6 +16,15 @@ DhtProxyServer::DhtProxyServer(DhtRunner* dht) : dht_(dht)
|
||||
service_ = std::unique_ptr<restbed::Service>(new restbed::Service());
|
||||
|
||||
auto resource = std::make_shared<restbed::Resource>();
|
||||
resource->set_path("/get/{hash: .*}");
|
||||
resource->set_method_handler("GET",
|
||||
[this](const std::shared_ptr<restbed::Session> session)
|
||||
{
|
||||
this->get(session);
|
||||
}
|
||||
);
|
||||
service_->publish(resource);
|
||||
resource = std::make_shared<restbed::Resource>();
|
||||
resource->set_path("/getId");
|
||||
resource->set_method_handler("GET",
|
||||
[this](const std::shared_ptr<restbed::Session> session)
|
||||
@@ -81,10 +90,37 @@ DhtProxyServer::DhtProxyServer(DhtRunner* dht) : dht_(dht)
|
||||
|
||||
DhtProxyServer::~DhtProxyServer()
|
||||
{
|
||||
service_->stop();
|
||||
if (server_thread.joinable())
|
||||
server_thread.join();
|
||||
}
|
||||
|
||||
void
|
||||
DhtProxyServer::get(const std::shared_ptr<restbed::Session>& session) const
|
||||
{
|
||||
const auto request = session->get_request();
|
||||
|
||||
int content_length = std::stoi(request->get_header("Content-Length", "0"));
|
||||
auto hash = request->get_path_parameter("hash");
|
||||
|
||||
session->fetch(content_length,
|
||||
[&](const std::shared_ptr<restbed::Session> s, const restbed::Bytes& b)
|
||||
{
|
||||
if (dht_) {
|
||||
dht_->get(InfoHash(hash), [s](std::shared_ptr<Value> value) {
|
||||
s->yield(restbed::OK, value->toString());
|
||||
return true;
|
||||
}, [s](bool ok) {
|
||||
auto response = std::to_string(ok);
|
||||
s->close(restbed::OK, response);//);
|
||||
});
|
||||
} else {
|
||||
s->close(restbed::NOT_FOUND, "");
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void
|
||||
DhtProxyServer::getId(const std::shared_ptr<restbed::Session>& session) const
|
||||
{
|
||||
@@ -97,9 +133,9 @@ DhtProxyServer::getId(const std::shared_ptr<restbed::Session>& session) const
|
||||
{
|
||||
if (dht_) {
|
||||
auto id = dht_->getId().toString();
|
||||
s->close(restbed::OK, id, {{"Content-Length", std::to_string(id.length())}});
|
||||
s->close(restbed::OK, id);
|
||||
} else {
|
||||
s->close(restbed::NOT_FOUND, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::NOT_FOUND, "");
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -117,9 +153,9 @@ DhtProxyServer::getNodeId(const std::shared_ptr<restbed::Session>& session) cons
|
||||
{
|
||||
if (dht_) {
|
||||
auto id = dht_->getNodeId().toString();
|
||||
s->close(restbed::OK, id, {{"Content-Length", std::to_string(id.length())}});
|
||||
s->close(restbed::OK, id);
|
||||
} else {
|
||||
s->close(restbed::NOT_FOUND, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::NOT_FOUND, "");
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -138,7 +174,7 @@ DhtProxyServer::putSigned(const std::shared_ptr<restbed::Session>& session) cons
|
||||
if (dht_) {
|
||||
if(b.empty()) {
|
||||
std::string response("Missing parameters");
|
||||
s->close(restbed::BAD_REQUEST, response, {{"Content-Length", std::to_string(response.size())}});
|
||||
s->close(restbed::BAD_REQUEST, response);
|
||||
} else {
|
||||
restbed::Bytes buf(b);
|
||||
|
||||
@@ -164,10 +200,10 @@ DhtProxyServer::putSigned(const std::shared_ptr<restbed::Session>& session) cons
|
||||
auto value = std::make_shared<Value>(msg.get());
|
||||
|
||||
dht_->putSigned(hash, value);
|
||||
s->close(restbed::OK, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::OK, "");
|
||||
}
|
||||
} else {
|
||||
s->close(restbed::NOT_FOUND, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::NOT_FOUND, "");
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -186,7 +222,7 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c
|
||||
if (dht_) {
|
||||
if(b.empty()) {
|
||||
std::string response("Missing parameters");
|
||||
s->close(restbed::BAD_REQUEST, response, {{"Content-Length", std::to_string(response.size())}});
|
||||
s->close(restbed::BAD_REQUEST, response);
|
||||
} else {
|
||||
restbed::Bytes buf(b);
|
||||
|
||||
@@ -219,10 +255,10 @@ DhtProxyServer::putEncrypted(const std::shared_ptr<restbed::Session>& session) c
|
||||
auto value = std::make_shared<Value>(msg.get());
|
||||
|
||||
dht_->putEncrypted(hash, to, value);
|
||||
s->close(restbed::OK, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::OK, "");
|
||||
}
|
||||
} else {
|
||||
s->close(restbed::NOT_FOUND, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::NOT_FOUND, "");
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -242,7 +278,7 @@ DhtProxyServer::put(const std::shared_ptr<restbed::Session>& session) const
|
||||
if (dht_) {
|
||||
if(b.empty()) {
|
||||
std::string response("Missing parameters");
|
||||
s->close(restbed::BAD_REQUEST, response, {{"Content-Length", std::to_string(response.size())}});
|
||||
s->close(restbed::BAD_REQUEST, response);
|
||||
} else {
|
||||
restbed::Bytes buf(b);
|
||||
|
||||
@@ -269,10 +305,10 @@ DhtProxyServer::put(const std::shared_ptr<restbed::Session>& session) const
|
||||
|
||||
auto response = value->toString();
|
||||
dht_->put(hash, value);
|
||||
s->close(restbed::OK, response, {{"Content-Length", std::to_string(response.length())}});
|
||||
s->close(restbed::OK, response);
|
||||
}
|
||||
} else {
|
||||
s->close(restbed::NOT_FOUND, "", {{"Content-Length", "0"}});
|
||||
s->close(restbed::NOT_FOUND, "");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -807,4 +807,17 @@ DhtRunner::startProxyInterface()
|
||||
proxy_server_ = std::unique_ptr<DhtProxyServer>(new DhtProxyServer(this));
|
||||
}
|
||||
|
||||
void
|
||||
DhtRunner::startProxyClient(const std::string& url)
|
||||
{
|
||||
proxyfing = true;
|
||||
// TODO DhtProxyClient
|
||||
}
|
||||
|
||||
void
|
||||
DhtRunner::stopProxyClient()
|
||||
{
|
||||
proxyfing = false;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-13
@@ -20,8 +20,6 @@
|
||||
#include "tools_common.h"
|
||||
#include <opendht/rng.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
extern "C" {
|
||||
#include <gnutls/gnutls.h>
|
||||
}
|
||||
@@ -133,17 +131,7 @@ main(int argc, char **argv)
|
||||
std::cout << "Message publishing failed !" << std::endl;
|
||||
});
|
||||
} else {
|
||||
auto value = std::make_shared<Value>( dht::ImMessage(rand_id(rd), std::move(line), now));
|
||||
|
||||
// TODO TEMP
|
||||
auto valueStr = packMsg(value);
|
||||
std::cout << std::endl;
|
||||
for (const auto& v: valueStr) {
|
||||
std::cout << "\\x" << std::hex << static_cast<int>(v);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
dht.putSigned(room, value, [](bool ok) {
|
||||
dht.putSigned(room, dht::ImMessage(rand_id(rd), std::move(line), now), [](bool ok) {
|
||||
//dht.cancelPut(room, id);
|
||||
if (not ok)
|
||||
std::cout << "Message publishing failed !" << std::endl;
|
||||
|
||||
Reference in new issue
Block a user