mirror of https://github.com/aria2/aria2
Use uri_split instead of uri::parse where appropriate
parent
eebc704b19
commit
c904aa50fe
|
@ -345,9 +345,11 @@ std::string AdaptiveURISelector::getFirstToTestUri
|
||||||
SharedHandle<ServerStat> AdaptiveURISelector::getServerStats
|
SharedHandle<ServerStat> AdaptiveURISelector::getServerStats
|
||||||
(const std::string& uri) const
|
(const std::string& uri) const
|
||||||
{
|
{
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(uri::parse(us, uri)) {
|
if(uri_split(&us, uri.c_str()) == 0) {
|
||||||
return serverStatMan_->find(us.host, us.protocol);
|
std::string host = uri::getFieldString(us, USR_HOST, uri.c_str());
|
||||||
|
std::string protocol = uri::getFieldString(us, USR_SCHEME, uri.c_str());
|
||||||
|
return serverStatMan_->find(host, protocol);
|
||||||
} else {
|
} else {
|
||||||
return SharedHandle<ServerStat>();
|
return SharedHandle<ServerStat>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,9 +106,9 @@ bool DefaultBtAnnounce::isAnnounceReady() {
|
||||||
namespace {
|
namespace {
|
||||||
bool uriHasQuery(const std::string& uri)
|
bool uriHasQuery(const std::string& uri)
|
||||||
{
|
{
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(uri::parse(us, uri)) {
|
if(uri_split(&us, uri.c_str()) == 0) {
|
||||||
return !us.query.empty();
|
return (us.field_set & (1 << USR_QUERY)) && us.fields[USR_QUERY].len > 0;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,17 +94,18 @@ std::string FeedbackURISelector::selectRarer
|
||||||
std::vector<std::pair<std::string, std::string> > cands;
|
std::vector<std::pair<std::string, std::string> > cands;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
||||||
eoi = uris.end(); i != eoi; ++i) {
|
eoi = uris.end(); i != eoi; ++i) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(!uri::parse(us, *i)) {
|
if(uri_split(&us, (*i).c_str()) == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
SharedHandle<ServerStat> ss =
|
std::string host = uri::getFieldString(us, USR_HOST, (*i).c_str());
|
||||||
serverStatMan_->find(us.host, us.protocol);
|
std::string protocol = uri::getFieldString(us, USR_SCHEME, (*i).c_str());
|
||||||
|
SharedHandle<ServerStat> ss = serverStatMan_->find(host, protocol);
|
||||||
if(ss && ss->isError()) {
|
if(ss && ss->isError()) {
|
||||||
A2_LOG_DEBUG(fmt("Error not considered: %s", (*i).c_str()));
|
A2_LOG_DEBUG(fmt("Error not considered: %s", (*i).c_str()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cands.push_back(std::make_pair(us.host, *i));
|
cands.push_back(std::make_pair(host, *i));
|
||||||
}
|
}
|
||||||
for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
|
for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
|
||||||
usedHosts.begin(), eoi = usedHosts.end(); i != eoi; ++i) {
|
usedHosts.begin(), eoi = usedHosts.end(); i != eoi; ++i) {
|
||||||
|
@ -131,17 +132,18 @@ std::string FeedbackURISelector::selectFaster
|
||||||
std::vector<std::string> normCands;
|
std::vector<std::string> normCands;
|
||||||
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
for(std::deque<std::string>::const_iterator i = uris.begin(),
|
||||||
eoi = uris.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
|
eoi = uris.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(!uri::parse(us, *i)) {
|
if(uri_split(&us, (*i).c_str()) == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(findSecond(usedHosts.begin(), usedHosts.end(), us.host) !=
|
std::string host = uri::getFieldString(us, USR_HOST, (*i).c_str());
|
||||||
|
if(findSecond(usedHosts.begin(), usedHosts.end(), host) !=
|
||||||
usedHosts.end()) {
|
usedHosts.end()) {
|
||||||
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
SharedHandle<ServerStat> ss =
|
std::string protocol = uri::getFieldString(us, USR_SCHEME, (*i).c_str());
|
||||||
serverStatMan_->find(us.host, us.protocol);
|
SharedHandle<ServerStat> ss = serverStatMan_->find(host, protocol);
|
||||||
if(!ss) {
|
if(!ss) {
|
||||||
normCands.push_back(*i);
|
normCands.push_back(*i);
|
||||||
} else if(ss->isOK()) {
|
} else if(ss->isOK()) {
|
||||||
|
|
|
@ -130,9 +130,9 @@ OutputIterator enumerateInFlightHosts
|
||||||
(InputIterator first, InputIterator last, OutputIterator out)
|
(InputIterator first, InputIterator last, OutputIterator out)
|
||||||
{
|
{
|
||||||
for(; first != last; ++first) {
|
for(; first != last; ++first) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(uri::parse(us, (*first)->getUri())) {
|
if(uri_split(&us, (*first)->getUri().c_str()) == 0) {
|
||||||
*out++ = us.host;
|
*out++ = uri::getFieldString(us, USR_HOST, (*first)->getUri().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
@ -261,23 +261,25 @@ FileEntry::findFasterRequest
|
||||||
std::vector<std::string> normCands;
|
std::vector<std::string> normCands;
|
||||||
for(std::deque<std::string>::const_iterator i = uris_.begin(),
|
for(std::deque<std::string>::const_iterator i = uris_.begin(),
|
||||||
eoi = uris_.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
|
eoi = uris_.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(!uri::parse(us, *i)) {
|
if(uri_split(&us, (*i).c_str()) == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(std::count(inFlightHosts.begin(), inFlightHosts.end(),us.host)
|
std::string host = uri::getFieldString(us, USR_HOST, (*i).c_str());
|
||||||
|
std::string protocol = uri::getFieldString(us, USR_SCHEME, (*i).c_str());
|
||||||
|
if(std::count(inFlightHosts.begin(), inFlightHosts.end(), host)
|
||||||
>= maxConnectionPerServer_) {
|
>= maxConnectionPerServer_) {
|
||||||
A2_LOG_DEBUG(fmt("%s has already used %d times, not considered.",
|
A2_LOG_DEBUG(fmt("%s has already used %d times, not considered.",
|
||||||
(*i).c_str(),
|
(*i).c_str(),
|
||||||
maxConnectionPerServer_));
|
maxConnectionPerServer_));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(findSecond(usedHosts.begin(), usedHosts.end(), us.host) !=
|
if(findSecond(usedHosts.begin(), usedHosts.end(), host) !=
|
||||||
usedHosts.end()) {
|
usedHosts.end()) {
|
||||||
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
SharedHandle<ServerStat> ss = serverStatMan->find(us.host, us.protocol);
|
SharedHandle<ServerStat> ss = serverStatMan->find(host, protocol);
|
||||||
if(ss && ss->isOK()) {
|
if(ss && ss->isOK()) {
|
||||||
if((basestat &&
|
if((basestat &&
|
||||||
ss->getDownloadSpeed() > basestat->calculateDownloadSpeed()*1.5) ||
|
ss->getDownloadSpeed() > basestat->calculateDownloadSpeed()*1.5) ||
|
||||||
|
@ -332,11 +334,13 @@ void FileEntry::removeURIWhoseHostnameIs(const std::string& hostname)
|
||||||
std::deque<std::string> newURIs;
|
std::deque<std::string> newURIs;
|
||||||
for(std::deque<std::string>::const_iterator itr = uris_.begin(),
|
for(std::deque<std::string>::const_iterator itr = uris_.begin(),
|
||||||
eoi = uris_.end(); itr != eoi; ++itr) {
|
eoi = uris_.end(); itr != eoi; ++itr) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(!uri::parse(us, *itr)) {
|
if(uri_split(&us, (*itr).c_str()) == -1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(us.host != hostname) {
|
if(us.fields[USR_HOST].len != hostname.size() ||
|
||||||
|
memcmp((*itr).c_str()+us.fields[USR_HOST].off, hostname.c_str(),
|
||||||
|
hostname.size()) != 0) {
|
||||||
newURIs.push_back(*itr);
|
newURIs.push_back(*itr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -411,9 +415,11 @@ void FileEntry::reuseUri(const std::vector<std::string>& ignore)
|
||||||
std::vector<std::string>::iterator insertionPoint = reusableURIs.begin();
|
std::vector<std::string>::iterator insertionPoint = reusableURIs.begin();
|
||||||
for(std::vector<std::string>::iterator i = reusableURIs.begin(),
|
for(std::vector<std::string>::iterator i = reusableURIs.begin(),
|
||||||
eoi = reusableURIs.end(); i != eoi; ++i) {
|
eoi = reusableURIs.end(); i != eoi; ++i) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(uri::parse(us, *i) &&
|
if(uri_split(&us, (*i).c_str()) == 0 &&
|
||||||
std::find(ignore.begin(), ignore.end(), us.host) == ignore.end()) {
|
std::find(ignore.begin(), ignore.end(),
|
||||||
|
uri::getFieldString(us, USR_HOST, (*i).c_str()))
|
||||||
|
== ignore.end()) {
|
||||||
if(i != insertionPoint) {
|
if(i != insertionPoint) {
|
||||||
*insertionPoint = *i;
|
*insertionPoint = *i;
|
||||||
}
|
}
|
||||||
|
@ -524,9 +530,8 @@ size_t FileEntry::setUris(const std::vector<std::string>& uris)
|
||||||
|
|
||||||
bool FileEntry::addUri(const std::string& uri)
|
bool FileEntry::addUri(const std::string& uri)
|
||||||
{
|
{
|
||||||
uri::UriStruct us;
|
|
||||||
std::string peUri = util::percentEncodeMini(uri);
|
std::string peUri = util::percentEncodeMini(uri);
|
||||||
if(uri::parse(us, peUri)) {
|
if(uri_split(NULL, peUri.c_str()) == 0) {
|
||||||
uris_.push_back(peUri);
|
uris_.push_back(peUri);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -536,9 +541,8 @@ bool FileEntry::addUri(const std::string& uri)
|
||||||
|
|
||||||
bool FileEntry::insertUri(const std::string& uri, size_t pos)
|
bool FileEntry::insertUri(const std::string& uri, size_t pos)
|
||||||
{
|
{
|
||||||
uri::UriStruct us;
|
|
||||||
std::string peUri = util::percentEncodeMini(uri);
|
std::string peUri = util::percentEncodeMini(uri);
|
||||||
if(uri::parse(us, peUri)) {
|
if(uri_split(NULL, peUri.c_str()) == 0) {
|
||||||
pos = std::min(pos, uris_.size());
|
pos = std::min(pos, uris_.size());
|
||||||
uris_.insert(uris_.begin()+pos, peUri);
|
uris_.insert(uris_.begin()+pos, peUri);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -239,9 +239,11 @@ bool HttpResponseCommand::executeInternal()
|
||||||
if(getFileEntry()->isUniqueProtocol()) {
|
if(getFileEntry()->isUniqueProtocol()) {
|
||||||
// Redirection should be considered here. We need to parse
|
// Redirection should be considered here. We need to parse
|
||||||
// original URI to get hostname.
|
// original URI to get hostname.
|
||||||
uri::UriStruct us;
|
const std::string& uri = getRequest()->getUri();
|
||||||
if(uri::parse(us, getRequest()->getUri())) {
|
uri_split_result us;
|
||||||
getFileEntry()->removeURIWhoseHostnameIs(us.host);
|
if(uri_split(&us, uri.c_str()) == 0) {
|
||||||
|
std::string host = uri::getFieldString(us, USR_HOST, uri.c_str());
|
||||||
|
getFileEntry()->removeURIWhoseHostnameIs(host);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!getPieceStorage()) {
|
if(!getPieceStorage()) {
|
||||||
|
|
|
@ -176,11 +176,11 @@ void MetalinkParserController::setURLOfResource(const std::string& url)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string u = uri::joinUri(baseUri_, url);
|
std::string u = uri::joinUri(baseUri_, url);
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(uri::parse(us, u)) {
|
if(uri_split(&us, u.c_str()) == 0) {
|
||||||
tResource_->url = u;
|
tResource_->url = u;
|
||||||
if(tResource_->type == MetalinkResource::TYPE_UNKNOWN) {
|
if(tResource_->type == MetalinkResource::TYPE_UNKNOWN) {
|
||||||
setTypeOfResource(us.protocol);
|
setTypeOfResource(uri::getFieldString(us, USR_SCHEME, u.c_str()));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tResource_->url = url;
|
tResource_->url = url;
|
||||||
|
@ -579,8 +579,7 @@ void MetalinkParserController::setURLOfMetaurl(const std::string& url)
|
||||||
#endif // ENABLE_BITTORRENT
|
#endif // ENABLE_BITTORRENT
|
||||||
{
|
{
|
||||||
std::string u = uri::joinUri(baseUri_, url);
|
std::string u = uri::joinUri(baseUri_, url);
|
||||||
uri::UriStruct us;
|
if(uri_split(NULL, u.c_str()) == 0) {
|
||||||
if(uri::parse(us, u)) {
|
|
||||||
tMetaurl_->url = u;
|
tMetaurl_->url = u;
|
||||||
} else {
|
} else {
|
||||||
tMetaurl_->url = url;
|
tMetaurl_->url = url;
|
||||||
|
|
|
@ -55,8 +55,7 @@ ProtocolDetector::~ProtocolDetector() {}
|
||||||
|
|
||||||
bool ProtocolDetector::isStreamProtocol(const std::string& uri) const
|
bool ProtocolDetector::isStreamProtocol(const std::string& uri) const
|
||||||
{
|
{
|
||||||
uri::UriStruct us;
|
return uri_split(NULL, uri.c_str()) == 0;
|
||||||
return uri::parse(us, uri);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ProtocolDetector::guessTorrentFile(const std::string& uri) const
|
bool ProtocolDetector::guessTorrentFile(const std::string& uri) const
|
||||||
|
|
|
@ -172,10 +172,12 @@ const SharedHandle<PeerStat>& Request::initPeerStat()
|
||||||
{
|
{
|
||||||
// Use host and protocol in original URI, because URI selector
|
// Use host and protocol in original URI, because URI selector
|
||||||
// selects URI based on original URI, not redirected one.
|
// selects URI based on original URI, not redirected one.
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
bool v = uri::parse(us, uri_);
|
int v = uri_split(&us, uri_.c_str());
|
||||||
assert(v);
|
assert(v == 0);
|
||||||
peerStat_.reset(new PeerStat(0, us.host, us.protocol));
|
std::string host = uri::getFieldString(us, USR_HOST, uri_.c_str());
|
||||||
|
std::string protocol = uri::getFieldString(us, USR_SCHEME, uri_.c_str());
|
||||||
|
peerStat_.reset(new PeerStat(0, host, protocol));
|
||||||
return peerStat_;
|
return peerStat_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1056,22 +1056,26 @@ void RequestGroupMan::getUsedHosts
|
||||||
(*i)->getDownloadContext()->getFirstFileEntry()->getInFlightRequests();
|
(*i)->getDownloadContext()->getFirstFileEntry()->getInFlightRequests();
|
||||||
for(FileEntry::InFlightRequestSet::iterator j =
|
for(FileEntry::InFlightRequestSet::iterator j =
|
||||||
inFlightReqs.begin(), eoj = inFlightReqs.end(); j != eoj; ++j) {
|
inFlightReqs.begin(), eoj = inFlightReqs.end(); j != eoj; ++j) {
|
||||||
uri::UriStruct us;
|
uri_split_result us;
|
||||||
if(uri::parse(us, (*j)->getUri())) {
|
if(uri_split(&us, (*j)->getUri().c_str()) == 0) {
|
||||||
std::vector<Triplet<size_t, int, std::string> >::iterator k;
|
std::vector<Triplet<size_t, int, std::string> >::iterator k;
|
||||||
std::vector<Triplet<size_t, int, std::string> >::iterator eok =
|
std::vector<Triplet<size_t, int, std::string> >::iterator eok =
|
||||||
tempHosts.end();
|
tempHosts.end();
|
||||||
|
std::string host = uri::getFieldString(us, USR_HOST,
|
||||||
|
(*j)->getUri().c_str());
|
||||||
for(k = tempHosts.begin(); k != eok; ++k) {
|
for(k = tempHosts.begin(); k != eok; ++k) {
|
||||||
if((*k).third == us.host) {
|
if((*k).third == host) {
|
||||||
++(*k).first;
|
++(*k).first;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(k == eok) {
|
if(k == eok) {
|
||||||
SharedHandle<ServerStat> ss = findServerStat(us.host, us.protocol);
|
std::string protocol = uri::getFieldString(us, USR_SCHEME,
|
||||||
|
(*j)->getUri().c_str());
|
||||||
|
SharedHandle<ServerStat> ss = findServerStat(host, protocol);
|
||||||
int invDlSpeed = (ss && ss->isOK()) ?
|
int invDlSpeed = (ss && ss->isOK()) ?
|
||||||
-(static_cast<int>(ss->getDownloadSpeed())) : 0;
|
-(static_cast<int>(ss->getDownloadSpeed())) : 0;
|
||||||
tempHosts.push_back(makeTriplet(1, invDlSpeed, us.host));
|
tempHosts.push_back(makeTriplet(1, invDlSpeed, host));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/uri.cc
11
src/uri.cc
|
@ -36,7 +36,6 @@
|
||||||
#include "A2STR.h"
|
#include "A2STR.h"
|
||||||
#include "FeatureConfig.h"
|
#include "FeatureConfig.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "uri_split.h"
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -167,6 +166,16 @@ bool parse(UriStruct& result, const std::string& uri)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getFieldString(const uri_split_result& res, int field,
|
||||||
|
const char* base)
|
||||||
|
{
|
||||||
|
if(res.field_set & (1 << field)) {
|
||||||
|
return std::string(base + res.fields[field].off, res.fields[field].len);
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string construct(const UriStruct& us)
|
std::string construct(const UriStruct& us)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
|
|
|
@ -39,6 +39,8 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "uri_split.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace uri {
|
namespace uri {
|
||||||
|
@ -70,6 +72,12 @@ void swap(UriStruct& lhs, UriStruct& rhs);
|
||||||
// undefined.
|
// undefined.
|
||||||
bool parse(UriStruct& result, const std::string& uri);
|
bool parse(UriStruct& result, const std::string& uri);
|
||||||
|
|
||||||
|
// Returns string specified by field in res. The base pointer in res
|
||||||
|
// is given as base. If the given field is not stored in res, returns
|
||||||
|
// empty string.
|
||||||
|
std::string getFieldString(const uri_split_result& res, int field,
|
||||||
|
const char* base);
|
||||||
|
|
||||||
std::string construct(const UriStruct& us);
|
std::string construct(const UriStruct& us);
|
||||||
|
|
||||||
std::string joinUri(const std::string& baseUri, const std::string& uri);
|
std::string joinUri(const std::string& baseUri, const std::string& uri);
|
||||||
|
|
Loading…
Reference in New Issue