diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-11-04 23:43:32 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-11-04 23:43:32 +0900 |
| commit | 601ec0f44adcbf97e7566b64fa9677f3b117ae6e (patch) | |
| tree | 7f099351f741ddd0895c5fa269d288debc939c06 | |
| parent | f84d2253b2250ed5760398c189bfc8bf8dff1f69 (diff) | |
Removed util::startsWith(a, b)
| -rw-r--r-- | src/AuthConfigFactory.cc | 3 | ||||
| -rw-r--r-- | src/ChunkedDecodingStreamFilter.cc | 2 | ||||
| -rw-r--r-- | src/HttpConnection.cc | 12 | ||||
| -rw-r--r-- | src/HttpRequest.cc | 11 | ||||
| -rw-r--r-- | src/Netrc.cc | 6 | ||||
| -rw-r--r-- | src/NsCookieParser.cc | 11 | ||||
| -rw-r--r-- | src/OptionHandlerImpl.cc | 13 | ||||
| -rw-r--r-- | src/OptionParser.cc | 2 | ||||
| -rw-r--r-- | src/Request.cc | 5 | ||||
| -rw-r--r-- | src/UriListParser.cc | 14 | ||||
| -rw-r--r-- | src/bittorrent_helper.cc | 5 | ||||
| -rw-r--r-- | src/cookie_helper.cc | 3 | ||||
| -rw-r--r-- | src/magnet.cc | 5 | ||||
| -rw-r--r-- | src/option_processing.cc | 5 | ||||
| -rw-r--r-- | src/uri.cc | 2 | ||||
| -rw-r--r-- | src/util.cc | 73 | ||||
| -rw-r--r-- | src/version_usage.cc | 2 | ||||
| -rw-r--r-- | test/Sqlite3CookieParserTest.cc | 7 | ||||
| -rw-r--r-- | test/UtilTest.cc | 7 |
19 files changed, 109 insertions, 79 deletions
diff --git a/src/AuthConfigFactory.cc b/src/AuthConfigFactory.cc index 1a053a23..2ac2cde0 100644 --- a/src/AuthConfigFactory.cc +++ b/src/AuthConfigFactory.cc @@ -253,7 +253,8 @@ AuthConfigFactory::findBasicCred std::lower_bound(basicCreds_.begin(), basicCreds_.end(), bc); for(; i != basicCreds_.end() && (*i).host_ == host && (*i).port_ == port; ++i) { - if(util::startsWith(bc.path_, (*i).path_)) { + if(util::startsWith(bc.path_.begin(), bc.path_.end(), + (*i).path_.begin(), (*i).path_.end())) { return i; } } diff --git a/src/ChunkedDecodingStreamFilter.cc b/src/ChunkedDecodingStreamFilter.cc index 9f6cd3bc..c84374ea 100644 --- a/src/ChunkedDecodingStreamFilter.cc +++ b/src/ChunkedDecodingStreamFilter.cc @@ -173,7 +173,7 @@ bool ChunkedDecodingStreamFilter::readDataEnd size_t pbufSize = buf_.size(); buf_.append(&inbuf[inbufOffset], &inbuf[inlen]); if(buf_.size() >= 2) { - if(util::startsWith(buf_, A2STR::CRLF)) { + if(buf_[0] == '\r' && buf_[1] == '\n') { inbufOffset += 2-pbufSize; buf_.clear(); state_ = readChunkSizeStateHandler_; diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index 8f3ef334..ced5090a 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -57,6 +57,7 @@ #include "a2functional.h" #include "fmt.h" #include "SocketRecvBuffer.h" +#include "array_fun.h" namespace aria2 { @@ -86,11 +87,14 @@ std::string HttpConnection::eraseConfidentialInfo(const std::string& request) std::string result; std::string line; while(getline(istr, line)) { - static const std::string AUTH_HEADER("Authorization: Basic"); - static const std::string PROXY_AUTH_HEADER("Proxy-Authorization: Basic"); - if(util::startsWith(line, AUTH_HEADER)) { + const char A2_AUTH_HEADER[] = "Authorization: Basic"; + const char A2_PROXY_AUTH_HEADER[] = "Proxy-Authorization: Basic"; + if(util::startsWith(line.begin(), line.end(), + A2_AUTH_HEADER, vend(A2_AUTH_HEADER)-1)) { result += "Authorization: Basic ********\n"; - } else if(util::startsWith(line, PROXY_AUTH_HEADER)) { + } else if(util::startsWith(line.begin(), line.end(), + A2_PROXY_AUTH_HEADER, + vend(A2_PROXY_AUTH_HEADER)-1)) { result += "Proxy-Authorization: Basic ********\n"; } else { strappend(result, line, "\n"); diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index 19a0ddfa..df2ff365 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -254,7 +254,8 @@ std::string HttpRequest::createRequest() std::vector<std::string>::const_iterator j = headers_.begin(); std::vector<std::string>::const_iterator jend = headers_.end(); for(; j != jend; ++j) { - if(util::startsWith(*j, (*i).first)) { + if(util::startsWith((*j).begin(), (*j).end(), + (*i).first.begin(), (*i).first.end())) { break; } } @@ -440,11 +441,15 @@ bool HttpRequest::conditionalRequest() const if(!ifModSinceHeader_.empty()) { return true; } + const char A2_IF_MOD_SINCE[] = "if-modified-since"; + const char A2_IF_NONE_MATCH[] = "if-none-match"; for(std::vector<std::string>::const_iterator i = headers_.begin(), eoi = headers_.end(); i != eoi; ++i) { std::string hd = util::toLower(*i); - if(util::startsWith(hd, "if-modified-since") || - util::startsWith(hd, "if-none-match")) { + if(util::startsWith(hd.begin(), hd.end(), + A2_IF_MOD_SINCE, vend(A2_IF_MOD_SINCE)-1) || + util::startsWith(hd.begin(), hd.end(), + A2_IF_NONE_MATCH, vend(A2_IF_NONE_MATCH)-1)) { return true; } } diff --git a/src/Netrc.cc b/src/Netrc.cc index d362daed..b7c2546e 100644 --- a/src/Netrc.cc +++ b/src/Netrc.cc @@ -149,13 +149,13 @@ void Netrc::parse(const std::string& path) if(!fp.getsn(buf, sizeof(buf))) { break; } - std::string line(buf); - if(util::startsWith(line, "#")) { + size_t len = strlen(buf); + if(len == 0 || buf[0] == '#') { continue; } std::vector<Scip> tokens; const char A2_DELIMS[] = " \t"; - util::splitIterM(line.begin(), line.end(), std::back_inserter(tokens), + util::splitIterM(&buf[0], &buf[len], std::back_inserter(tokens), A2_DELIMS, vend(A2_DELIMS)-1, true); const char A2_MACHINE[] = "machine"; const char A2_DEFAULT[] = "default"; diff --git a/src/NsCookieParser.cc b/src/NsCookieParser.cc index 8e942c58..c2491538 100644 --- a/src/NsCookieParser.cc +++ b/src/NsCookieParser.cc @@ -55,11 +55,10 @@ NsCookieParser::~NsCookieParser() {} namespace { bool parseNsCookie -(Cookie& cookie, const std::string& nsCookieStr, time_t creationTime) +(Cookie& cookie, const char* buf, size_t buflen, time_t creationTime) { std::vector<Scip> vs; - util::splitIter(nsCookieStr.begin(), nsCookieStr.end(), - std::back_inserter(vs), '\t', true); + util::splitIter(&buf[0], &buf[buflen], std::back_inserter(vs), '\t', true); if(vs.size() < 6) { return false; } @@ -113,12 +112,12 @@ std::vector<Cookie> NsCookieParser::parse if(!fp.getsn(buf, sizeof(buf))) { break; } - std::string line(buf); - if(util::startsWith(line, A2STR::SHARP_C)) { + size_t len = strlen(buf); + if(len == 0 || buf[0] == '#') { continue; } Cookie c; - if(parseNsCookie(c, line, creationTime)) { + if(parseNsCookie(c, buf, len, creationTime)) { cookies.push_back(c); } } diff --git a/src/OptionHandlerImpl.cc b/src/OptionHandlerImpl.cc index 4fdc5e35..c0e1af5c 100644 --- a/src/OptionHandlerImpl.cc +++ b/src/OptionHandlerImpl.cc @@ -58,6 +58,7 @@ #include "LogFactory.h" #include "uri.h" #include "SegList.h" +#include "array_fun.h" #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigest.h" #endif // ENABLE_MESSAGE_DIGEST @@ -611,9 +612,15 @@ void HttpProxyOptionHandler::parseArg(Option& option, const std::string& optarg) } else { Request req; std::string uri; - if(util::startsWith(optarg, "http://") || - util::startsWith(optarg, "https://") || - util::startsWith(optarg, "ftp://")) { + const char A2_HTTP[] = "http://"; + const char A2_HTTPS[] = "https://"; + const char A2_FTP[] = "ftp://"; + if(util::startsWith(optarg.begin(), optarg.end(), + A2_HTTP, vend(A2_HTTP)-1) || + util::startsWith(optarg.begin(), optarg.end(), + A2_HTTPS, vend(A2_HTTPS)-1) || + util::startsWith(optarg.begin(), optarg.end(), + A2_FTP, vend(A2_FTP)-1)) { uri = optarg; } else { uri = "http://"; diff --git a/src/OptionParser.cc b/src/OptionParser.cc index 08a4301e..157d2adc 100644 --- a/src/OptionParser.cc +++ b/src/OptionParser.cc @@ -181,7 +181,7 @@ void OptionParser::parse(Option& option, std::istream& is) const { std::string line; while(getline(is, line)) { - if(util::startsWith(line, A2STR::SHARP_C)) { + if(line.empty() || line[0] == '#') { continue; } std::pair<Scip, Scip> nv; diff --git a/src/Request.cc b/src/Request.cc index d611cb24..d6fa6c53 100644 --- a/src/Request.cc +++ b/src/Request.cc @@ -106,11 +106,14 @@ void Request::setReferer(const std::string& uri) bool Request::redirectUri(const std::string& uri) { supportsPersistentConnection_ = true; ++redirectCount_; + if(uri.empty()) { + return false; + } std::string redirectedUri; if(uri.find("://") == std::string::npos) { // rfc2616 requires absolute URI should be provided by Location header // field, but some servers don't obey this rule. - if(util::startsWith(uri, "/")) { + if(uri[0] == '/') { // abosulute path redirectedUri = strconcat(protocol_, "://", host_, uri); } else { diff --git a/src/UriListParser.cc b/src/UriListParser.cc index b978a99b..853e40b4 100644 --- a/src/UriListParser.cc +++ b/src/UriListParser.cc @@ -65,7 +65,7 @@ void UriListParser::parseNext(std::vector<std::string>& uris, Option& op) } const SharedHandle<OptionParser>& optparser = OptionParser::getInstance(); while(1) { - if(!util::startsWith(line_, A2STR::SHARP_C) && !util::strip(line_).empty()){ + if(!line_.empty() && line_[0] != '#') { util::split(line_.begin(), line_.end(), std::back_inserter(uris), '\t', true); // Read options @@ -76,12 +76,16 @@ void UriListParser::parseNext(std::vector<std::string>& uris, Option& op) break; } line_.assign(&buf[0], &buf[strlen(buf)]); - if(util::startsWith(line_, " ") || util::startsWith(line_, "\t")) { - ss << line_ << "\n"; - } else if(util::startsWith(line_, A2STR::SHARP_C)) { + if(line_.empty()) { continue; } else { - break; + if(line_[0] == ' ' || line_[0] == '\t') { + ss << line_ << "\n"; + } else if(line_[0] == '#') { + continue; + } else { + break; + } } } optparser->parse(op, ss); diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 81939c38..625375e8 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -59,6 +59,7 @@ #include "prefs.h" #include "FileEntry.h" #include "error_code.h" +#include "array_fun.h" namespace aria2 { @@ -874,10 +875,12 @@ SharedHandle<TorrentAttribute> parseMagnet(const std::string& magnet) } SharedHandle<TorrentAttribute> attrs(new TorrentAttribute()); std::string infoHash; + const char A2_URN_BTIH[] = "urn:btih:"; for(List::ValueType::const_iterator xtiter = xts->begin(), eoi = xts->end(); xtiter != eoi && infoHash.empty(); ++xtiter) { const String* xt = downcast<String>(*xtiter); - if(util::startsWith(xt->s(), "urn:btih:")) { + if(util::startsWith(xt->s().begin(), xt->s().end(), + A2_URN_BTIH, vend(A2_URN_BTIH)-1)) { std::string xtarg = xt->s().substr(9); size_t size = xtarg.size(); if(size == 32) { diff --git a/src/cookie_helper.cc b/src/cookie_helper.cc index 7c0c6d47..1e15e793 100644 --- a/src/cookie_helper.cc +++ b/src/cookie_helper.cc @@ -376,7 +376,8 @@ bool domainMatch(const std::string& requestHost, const std::string& domain) bool pathMatch(const std::string& requestPath, const std::string& path) { return requestPath == path || - (util::startsWith(requestPath, path) && + (util::startsWith(requestPath.begin(), requestPath.end(), + path.begin(), path.end()) && (path[path.size()-1] == '/' || requestPath[path.size()] == '/')); } diff --git a/src/magnet.cc b/src/magnet.cc index 1d67f029..9d0e92da 100644 --- a/src/magnet.cc +++ b/src/magnet.cc @@ -34,6 +34,7 @@ /* copyright --> */ #include "magnet.h" #include "util.h" +#include "array_fun.h" namespace aria2 { @@ -42,7 +43,9 @@ namespace magnet { SharedHandle<Dict> parse(const std::string& magnet) { SharedHandle<Dict> dict; - if(!util::startsWith(magnet, "magnet:?")) { + const char A2_MSGNET[] = "magnet:?"; + if(!util::startsWith(magnet.begin(), magnet.end(), + A2_MSGNET, vend(A2_MSGNET)-1)) { return dict; } dict.reset(new Dict()); diff --git a/src/option_processing.cc b/src/option_processing.cc index 47eaad0c..d016173e 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -57,6 +57,7 @@ #include "bittorrent_helper.h" #include "BufferedFile.h" #include "console.h" +#include "array_fun.h" #ifndef HAVE_DAEMON #include "daemon.h" #endif // !HAVE_DAEMON @@ -114,7 +115,9 @@ void option_processing(Option& op, std::vector<std::string>& uris, keyword = TAG_BASIC; } else { keyword = op.get(PREF_HELP); - if(util::startsWith(keyword, "--")) { + const char A2_HH[] = "--"; + if(util::startsWith(keyword.begin(), keyword.end(), + A2_HH, vend(A2_HH)-1)) { keyword = keyword.substr(2); } std::string::size_type eqpos = keyword.find("="); @@ -306,7 +306,7 @@ std::string joinUri(const std::string& baseUri, const std::string& uri) return uri; } std::vector<std::string> parts; - if(!util::startsWith(uri, "/")) { + if(uri.empty() || uri[0] != '/') { util::split(bus.dir.begin(), bus.dir.end(), std::back_inserter(parts), '/'); } diff --git a/src/util.cc b/src/util.cc index b0ea2724..bc463614 100644 --- a/src/util.cc +++ b/src/util.cc @@ -230,20 +230,6 @@ int32_t difftvsec(struct timeval tv1, struct timeval tv2) { return tv1.tv_sec-tv2.tv_sec; } -bool startsWith(const std::string& target, const std::string& part) { - if(target.size() < part.size()) { - return false; - } - if(part.empty()) { - return true; - } - if(target.find(part) == 0) { - return true; - } else { - return false; - } -} - bool endsWith(const std::string& target, const std::string& part) { if(target.size() < part.size()) { return false; @@ -786,17 +772,20 @@ void parseParam(OutputIterator out, const std::string& header) std::string getContentDispositionFilename(const std::string& header) { + const char A2_KEYNAME[] = "filename"; std::string filename; std::vector<std::string> params; parseParam(std::back_inserter(params), header); for(std::vector<std::string>::const_iterator i = params.begin(), eoi = params.end(); i != eoi; ++i) { const std::string& param = *i; - static const std::string keyName = "filename"; - if(!startsWith(toLower(param), keyName) || param.size() == keyName.size()) { + std::string lowparam = toLower(param); + if(!startsWith(lowparam.begin(), lowparam.end(), + A2_KEYNAME, vend(A2_KEYNAME)-1) || + param.size() == sizeof(A2_KEYNAME)-1) { continue; } - std::string::const_iterator markeritr = param.begin()+keyName.size(); + std::string::const_iterator markeritr = param.begin()+sizeof(A2_KEYNAME)-1; if(*markeritr == '*') { // See RFC2231 Section4 and draft-reschke-rfc2231-in-http. // Please note that this function doesn't do charset conversion @@ -1370,13 +1359,23 @@ void generateRandomKey(unsigned char* key) // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) bool inPrivateAddress(const std::string& ipv4addr) { - if(util::startsWith(ipv4addr, "10.") || - util::startsWith(ipv4addr, "192.168.")) { + const char A2_IP10[] = "10."; + const char A2_IP192[] = "192.168."; + if(util::startsWith(ipv4addr.begin(), ipv4addr.end(), + A2_IP10, vend(A2_IP10)-1) || + util::startsWith(ipv4addr.begin(), ipv4addr.end(), + A2_IP192, vend(A2_IP192)-1)) { return true; } - if(util::startsWith(ipv4addr, "172.")) { + std::string p172("172."); + if(util::startsWith(ipv4addr.begin(), ipv4addr.end(), + p172.begin(), p172.end())) { for(int i = 16; i <= 31; ++i) { - if(util::startsWith(ipv4addr, "172."+util::itos(i)+".")) { + std::string t(p172); + t += util::itos(i); + t += '.'; + if(util::startsWith(ipv4addr.begin(), ipv4addr.end(), + t.begin(), t.end())) { return true; } } @@ -1386,6 +1385,9 @@ bool inPrivateAddress(const std::string& ipv4addr) bool detectDirTraversal(const std::string& s) { + if(s.empty()) { + return false; + } for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) { unsigned char c = *i; if(in(c, 0x00u, 0x1fu) || c == 0x7fu) { @@ -1393,24 +1395,24 @@ bool detectDirTraversal(const std::string& s) } } - static std::string A2_DS = "./"; - static std::string A2_DDS = "../"; - static std::string A2_SD = "/."; - static std::string A2_SDD = "/.."; - static std::string A2_SDDS = "/../"; - static std::string A2_SDS = "/./"; - static std::string A2_DD = ".."; + const char A2_DS[] = "./"; + const char A2_DDS[] = "../"; + const char A2_SD[] = "/."; + const char A2_SDD[] = "/.."; + const char A2_SDDS[] = "/../"; + const char A2_SDS[] = "/./"; + const char A2_DD[] = ".."; return s == A2STR::DOT_C || s == A2_DD || - util::startsWith(s, A2STR::SLASH_C) || - util::startsWith(s, A2_DS) || - util::startsWith(s, A2_DDS) || + s[0] == '/' || + util::startsWith(s.begin(), s.end(), A2_DS, vend(A2_DS)-1) || + util::startsWith(s.begin(), s.end(), A2_DDS, vend(A2_DDS)-1) || s.find(A2_SDDS) != std::string::npos || s.find(A2_SDS) != std::string::npos || - util::endsWith(s, A2STR::SLASH_C) || - util::endsWith(s, A2_SD) || - util::endsWith(s, A2_SDD); + s[s.size()-1] == '/' || + util::endsWith(s.begin(), s.end(), A2_SD, vend(A2_SD)-1) || + util::endsWith(s.begin(), s.end(), A2_SDD, vend(A2_SDD)-1); } std::string escapePath(const std::string& s) @@ -1615,8 +1617,7 @@ bool noProxyDomainMatch (const std::string& hostname, const std::string& domain) { - if(!util::isNumericHost(hostname) && - util::startsWith(domain, A2STR::DOT_C)) { + if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) { return util::endsWith(hostname, domain); } else { return hostname == domain; diff --git a/src/version_usage.cc b/src/version_usage.cc index 6ae98a84..818bc0fb 100644 --- a/src/version_usage.cc +++ b/src/version_usage.cc @@ -86,7 +86,7 @@ void showUsage std::cout << _("Usage: aria2c [OPTIONS] [URI | MAGNET | TORRENT_FILE |" " METALINK_FILE]...") << "\n" << "\n"; - if(util::startsWith(keyword, "#")) { + if(!keyword.empty() && keyword[0] == '#') { std::vector<SharedHandle<OptionHandler> > handlers = keyword == TAG_ALL ? oparser->findAll() : oparser->findByTag(keyword); if(keyword == TAG_ALL) { diff --git a/test/Sqlite3CookieParserTest.cc b/test/Sqlite3CookieParserTest.cc index 42b9718e..f312f821 100644 --- a/test/Sqlite3CookieParserTest.cc +++ b/test/Sqlite3CookieParserTest.cc @@ -1,11 +1,13 @@ #include "Sqlite3CookieParserImpl.h" +#include <cstring> #include <iostream> #include <cppunit/extensions/HelperMacros.h> #include "RecoverableException.h" #include "util.h" +#include "array_fun.h" namespace aria2 { @@ -85,8 +87,9 @@ void Sqlite3CookieParserTest::testMozParse_fileNotFound() CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { // SUCCESS - CPPUNIT_ASSERT(util::startsWith(e.what(), - "SQLite3 database is not opened")); + const char A2_SQLITE_ERR[] = "SQLite3 database is not opened"; + CPPUNIT_ASSERT(util::startsWith(e.what(), e.what()+strlen(e.what()), + A2_SQLITE_ERR, vend(A2_SQLITE_ERR)-1)); } } diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 9528021a..e5e05440 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -740,43 +740,36 @@ void UtilTest::testStartsWith() { target = "abcdefg"; part = "abc"; - CPPUNIT_ASSERT(util::startsWith(target, part)); CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); target = "abcdefg"; part = "abx"; - CPPUNIT_ASSERT(!util::startsWith(target, part)); CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.begin(), part.end())); target = "abcdefg"; part = "bcd"; - CPPUNIT_ASSERT(!util::startsWith(target, part)); CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.begin(), part.end())); target = ""; part = "a"; - CPPUNIT_ASSERT(!util::startsWith(target, part)); CPPUNIT_ASSERT(!util::startsWith(target.begin(), target.end(), part.begin(), part.end())); target = ""; part = ""; - CPPUNIT_ASSERT(util::startsWith(target, part)); CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); target = "a"; part = ""; - CPPUNIT_ASSERT(util::startsWith(target, part)); CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); target = "a"; part = "a"; - CPPUNIT_ASSERT(util::startsWith(target, part)); CPPUNIT_ASSERT(util::startsWith(target.begin(), target.end(), part.begin(), part.end())); } |
