Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
AddressBook + HTTP: remove unnecessary else statements
Browse files Browse the repository at this point in the history
References #336
  • Loading branch information
anonimal committed Sep 8, 2016
1 parent 418edac commit 6eddd9c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 125 deletions.
151 changes: 75 additions & 76 deletions src/client/address_book.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,69 +87,68 @@ void AddressBook::Start(
LogPrint(eLogError,
"AddressBook: won't start: we need a client destination");
return;
} else {
m_SharedLocalDestination = local_destination;
m_SubscriberUpdateTimer =
std::make_unique<boost::asio::deadline_timer>(
m_SharedLocalDestination->GetService());
m_SubscriberUpdateTimer->expires_from_now(
boost::posix_time::minutes(
static_cast<std::uint16_t>(SubscriberTimeout::InitialUpdate)));
m_SubscriberUpdateTimer->async_wait(
std::bind(
&AddressBook::SubscriberUpdateTimer,
this,
std::placeholders::_1));
}
m_SharedLocalDestination = local_destination;
m_SubscriberUpdateTimer =
std::make_unique<boost::asio::deadline_timer>(
m_SharedLocalDestination->GetService());
m_SubscriberUpdateTimer->expires_from_now(
boost::posix_time::minutes(
static_cast<std::uint16_t>(SubscriberTimeout::InitialUpdate)));
m_SubscriberUpdateTimer->async_wait(
std::bind(
&AddressBook::SubscriberUpdateTimer,
this,
std::placeholders::_1));
}

void AddressBook::LoadPublishers() {
// TODO(unassigned): this is a one-shot: we won't be able to
// edit publisher's file manually with any effect after router start
if (m_Subscribers.empty()) {
auto publishers = GetDefaultPublishersFilename();
LogPrint(eLogInfo, "AddressBook: loading publisher file ", publishers);
std::ifstream file(i2p::util::filesystem::GetFullPath(publishers));
if (file) {
// Publisher URI
std::string publisher;
// Validate publisher URI
i2p::util::http::HTTP http;
// Read in publishers, line by line
while (std::getline(file, publisher)) {
// If found, clear whitespace before and after publisher (on the line)
publisher.erase(
std::remove_if(
publisher.begin(),
publisher.end(),
[](char whitespace) { return std::isspace(whitespace); }),
publisher.end());
// If found, skip empty line
if (!publisher.length())
continue;
// Perform URI sanity test
http.SetURI(publisher);
if (!http.GetURI().is_valid()) {
LogPrint(eLogWarn,
"AddressBook: invalid/malformed publisher URI, skipping");
continue;
}
// Save publisher to subscriber
m_Subscribers.push_back(
std::make_unique<AddressBookSubscriber>(*this, publisher));
if (!m_Subscribers.empty()) {
LogPrint(eLogError, "AddressBook: publisher(s) already loaded");
return;
}
auto publishers = GetDefaultPublishersFilename();
LogPrint(eLogInfo, "AddressBook: loading publisher file ", publishers);
std::ifstream file(i2p::util::filesystem::GetFullPath(publishers));
if (file) {
// Publisher URI
std::string publisher;
// Validate publisher URI
i2p::util::http::HTTP http;
// Read in publishers, line by line
while (std::getline(file, publisher)) {
// If found, clear whitespace before and after publisher (on the line)
publisher.erase(
std::remove_if(
publisher.begin(),
publisher.end(),
[](char whitespace) { return std::isspace(whitespace); }),
publisher.end());
// If found, skip empty line
if (!publisher.length())
continue;
// Perform URI sanity test
http.SetURI(publisher);
if (!http.GetURI().is_valid()) {
LogPrint(eLogWarn,
"AddressBook: invalid/malformed publisher URI, skipping");
continue;
}
LogPrint(eLogInfo,
"AddressBook: ", m_Subscribers.size(), " publishers loaded");
} else {
auto publisher = GetDefaultPublisherURI();
LogPrint(eLogWarn,
"AddressBook: ", publishers, " unavailable; using ", publisher);
// Save publisher to subscriber
m_Subscribers.push_back(
std::make_unique<AddressBookSubscriber>(*this, publisher));
// TODO(anonimal): create default publisher file if file is missing
}
LogPrint(eLogInfo,
"AddressBook: ", m_Subscribers.size(), " publishers loaded");
} else {
LogPrint(eLogError, "AddressBook: publisher(s) already loaded");
auto publisher = GetDefaultPublisherURI();
LogPrint(eLogWarn,
"AddressBook: ", publishers, " unavailable; using ", publisher);
m_Subscribers.push_back(
std::make_unique<AddressBookSubscriber>(*this, publisher));
// TODO(anonimal): create default publisher file if file is missing
}
}

Expand All @@ -158,32 +157,32 @@ void AddressBook::SubscriberUpdateTimer(
if (ecode) {
LogPrint(eLogError,
"AddressBook: SubscriberUpdateTimer() exception: ", ecode.message());
return;
}
if (m_SubscriptionIsLoaded
&& !m_SubscriberIsDownloading
&& m_SharedLocalDestination->IsReady()) {
// Pick a random subscription if subscriptions count is > 0
// At first this may look questionable but, by this point,
// we're guaranteed to have at least one subscriber
auto publisher =
i2p::crypto::RandInRange<std::size_t>(0, m_Subscribers.size() - 1);
m_SubscriberIsDownloading = true;
m_Subscribers.at(publisher)->DownloadSubscription();
} else {
if (m_SubscriptionIsLoaded
&& !m_SubscriberIsDownloading
&& m_SharedLocalDestination->IsReady()) {
// Pick a random subscription if subscriptions count is > 0
// At first this may look questionable but, by this point,
// we're guaranteed to have at least one subscriber
auto publisher =
i2p::crypto::RandInRange<std::size_t>(0, m_Subscribers.size() - 1);
m_SubscriberIsDownloading = true;
m_Subscribers.at(publisher)->DownloadSubscription();
} else {
if (!m_SubscriptionIsLoaded) {
// If subscription not available, will attempt download with subscriber
LoadSubscriptionFromPublisher();
}
// Try again after timeout
m_SubscriberUpdateTimer->expires_from_now(
boost::posix_time::minutes(
static_cast<std::uint16_t>(SubscriberTimeout::InitialRetry)));
m_SubscriberUpdateTimer->async_wait(
std::bind(
&AddressBook::SubscriberUpdateTimer,
this,
std::placeholders::_1));
if (!m_SubscriptionIsLoaded) {
// If subscription not available, will attempt download with subscriber
LoadSubscriptionFromPublisher();
}
// Try again after timeout
m_SubscriberUpdateTimer->expires_from_now(
boost::posix_time::minutes(
static_cast<std::uint16_t>(SubscriberTimeout::InitialRetry)));
m_SubscriberUpdateTimer->async_wait(
std::bind(
&AddressBook::SubscriberUpdateTimer,
this,
std::placeholders::_1));
}
}

Expand Down
51 changes: 23 additions & 28 deletions src/client/address_book_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,42 +60,37 @@ bool AddressBookStorage::GetAddress(
i2p::data::IdentityEx& address) const {
auto filename = GetAddressBookPath() / (ident.ToBase32() + ".b32");
std::ifstream file(filename.string(), std::ifstream::binary);
if (!file) {
if (!file)
return false;
file.seekg(0, std::ios::end);
const std::size_t len = file.tellg();
if (len < i2p::data::DEFAULT_IDENTITY_SIZE) {
LogPrint(eLogError,
"AddressBookStorage: file ", filename, " is too short. ", len);
return false;
} else {
file.seekg(0, std::ios::end);
const std::size_t len = file.tellg();
if (len < i2p::data::DEFAULT_IDENTITY_SIZE) {
LogPrint(eLogError,
"AddressBookStorage: file ", filename, " is too short. ", len);
return false;
}
file.seekg(0, std::ios::beg);
auto buf = std::make_unique<std::uint8_t[]>(len);
file.read(reinterpret_cast<char *>(buf.get()), len);
// For sanity, the validity of identity length is incumbent upon the parent caller.
// For now, we only care about returning success for an open/available file
// TODO(unassigned): triple check that this is the case
address.FromBuffer(buf.get(), len);
return true;
}
file.seekg(0, std::ios::beg);
auto buf = std::make_unique<std::uint8_t[]>(len);
file.read(reinterpret_cast<char *>(buf.get()), len);
// For sanity, the validity of identity length is incumbent upon the parent caller.
// For now, we only care about returning success for an open/available file
// TODO(unassigned): triple check that this is the case
address.FromBuffer(buf.get(), len);
return true;
}

void AddressBookStorage::AddAddress(
const i2p::data::IdentityEx& address) {
auto filename = GetAddressBookPath() / (address.GetIdentHash().ToBase32() + ".b32");
std::ofstream file(filename.string(), std::ofstream::binary);
if (!file) {
LogPrint(eLogError,
"AddressBookStorage: can't open file ", filename);
} else {
const std::size_t len = address.GetFullLen();
auto buf = std::make_unique<std::uint8_t[]>(len);
// For sanity, the validity of identity length is incumbent upon the parent caller.
// TODO(unassigned): triple check that this is the case
address.ToBuffer(buf.get(), len);
file.write(reinterpret_cast<char *>(buf.get()), len);
}
if (!file)
LogPrint(eLogError, "AddressBookStorage: can't open file ", filename);
const std::size_t len = address.GetFullLen();
auto buf = std::make_unique<std::uint8_t[]>(len);
// For sanity, the validity of identity length is incumbent upon the parent caller.
// TODO(unassigned): triple check that this is the case
address.ToBuffer(buf.get(), len);
file.write(reinterpret_cast<char *>(buf.get()), len);
}

/**
Expand Down
40 changes: 19 additions & 21 deletions src/core/util/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,27 @@ bool HTTP::Download() {

bool HTTP::HostIsI2P() {
auto uri = GetURI();
if (!(uri.host().substr(uri.host().size() - 4) == ".i2p")) {
if (!(uri.host().substr(uri.host().size() - 4) == ".i2p"))
return false;
} else {
if (!uri.port().empty())
return true;
// We must assign a port if none was assigned (for internal reasons)
std::string port;
if (uri.scheme() == "https")
port.assign("443");
else
port.assign("80");
// If user supplied user:password, we must append @
std::string user_info;
if (!uri.user_info().empty())
user_info.assign(uri.user_info() + "@");
// TODO(anonimal): easier way with cpp-netlib?
std::string new_uri(
uri.scheme() + "://" + user_info
+ uri.host() + ":" + port
+ uri.path() + uri.query() + uri.fragment());
SetURI(new_uri);
if (!uri.port().empty())
return true;
}
// We must assign a port if none was assigned (for internal reasons)
std::string port;
if (uri.scheme() == "https")
port.assign("443");
else
port.assign("80");
// If user supplied user:password, we must append @
std::string user_info;
if (!uri.user_info().empty())
user_info.assign(uri.user_info() + "@");
// TODO(anonimal): easier way with cpp-netlib?
std::string new_uri(
uri.scheme() + "://" + user_info
+ uri.host() + ":" + port
+ uri.path() + uri.query() + uri.fragment());
SetURI(new_uri);
return true;
}

bool HTTP::DownloadViaClearnet() {
Expand Down

0 comments on commit 6eddd9c

Please sign in to comment.