From b1132d6b10cd3791b9529d7031d723fd0e2d8215 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 27 Dec 2015 18:39:47 +0900 Subject: make clang-format using clang-format-3.6 --- src/util.cc | 1217 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 627 insertions(+), 590 deletions(-) (limited to 'src/util.cc') diff --git a/src/util.cc b/src/util.cc index a49547ca..93d6092f 100644 --- a/src/util.cc +++ b/src/util.cc @@ -37,20 +37,20 @@ #ifdef __sun // For opensolaris, just include signal.h which includes sys/signal.h #ifdef HAVE_SIGNAL_H -# include +#include #endif // HAVE_SIGNAL_H -#else // !__sun +#else // !__sun #ifdef HAVE_SYS_SIGNAL_H -# include +#include #endif // HAVE_SYS_SIGNAL_H #ifdef HAVE_SIGNAL_H -# include +#include #endif // HAVE_SIGNAL_H #endif // !__sun #include #ifdef HAVE_PWD_H -# include +#include #endif // HAVE_PWD_H #include @@ -91,7 +91,7 @@ // For libc6 which doesn't define ULLONG_MAX properly because of broken limits.h #ifndef ULLONG_MAX -# define ULLONG_MAX 18446744073709551615ULL +#define ULLONG_MAX 18446744073709551615ULL #endif // ULLONG_MAX namespace aria2 { @@ -107,22 +107,23 @@ int utf8ToWChar(wchar_t* out, size_t outLength, const char* src) namespace { int wCharToUtf8(char* out, size_t outLength, const wchar_t* src) { - return WideCharToMultiByte(CP_UTF8, 0, src, -1, out, outLength, - nullptr, nullptr); + return WideCharToMultiByte(CP_UTF8, 0, src, -1, out, outLength, nullptr, + nullptr); } } // namespace std::wstring utf8ToWChar(const char* src) { int len = utf8ToWChar(nullptr, 0, src); - if(len <= 0) { + if (len <= 0) { abort(); } auto buf = make_unique((size_t)len); len = utf8ToWChar(buf.get(), len, src); - if(len <= 0) { + if (len <= 0) { abort(); - } else { + } + else { return buf.get(); } } @@ -135,19 +136,21 @@ std::wstring utf8ToWChar(const std::string& src) std::string wCharToUtf8(const std::wstring& wsrc) { int len = wCharToUtf8(nullptr, 0, wsrc.c_str()); - if(len <= 0) { + if (len <= 0) { abort(); } auto buf = make_unique((size_t)len); len = wCharToUtf8(buf.get(), len, wsrc.c_str()); - if(len <= 0) { + if (len <= 0) { abort(); - } else { + } + else { return buf.get(); } } -std::string toForwardSlash(const std::string &src) { +std::string toForwardSlash(const std::string& src) +{ auto dst = src; std::transform(std::begin(dst), std::end(dst), std::begin(dst), [](char c) { return c == '\\' ? '/' : c; }); @@ -162,9 +165,8 @@ const char DEFAULT_STRIP_CHARSET[] = "\r\n\t "; std::string strip(const std::string& str, const char* chars) { - std::pair p = - stripIter(str.begin(), str.end(), chars); + std::pair p = + stripIter(str.begin(), str.end(), chars); return std::string(p.first, p.second); } @@ -172,11 +174,12 @@ std::string itos(int64_t value, bool comma) { bool flag = false; std::string str; - if(value < 0) { - if(value == INT64_MIN) { - if(comma) { + if (value < 0) { + if (value == INT64_MIN) { + if (comma) { str = "-9,223,372,036,854,775,808"; - } else { + } + else { str = "-9223372036854775808"; } return str; @@ -185,42 +188,46 @@ std::string itos(int64_t value, bool comma) value = -value; } str = uitos(value, comma); - if(flag) { + if (flag) { str.insert(str.begin(), '-'); } return str; } -int64_t difftv(struct timeval tv1, struct timeval tv2) { - if((tv1.tv_sec < tv2.tv_sec) || - ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec < tv2.tv_usec))) { +int64_t difftv(struct timeval tv1, struct timeval tv2) +{ + if ((tv1.tv_sec < tv2.tv_sec) || + ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec < tv2.tv_usec))) { return 0; } - return ((int64_t)(tv1.tv_sec-tv2.tv_sec)*1000000+ - tv1.tv_usec-tv2.tv_usec); + return ((int64_t)(tv1.tv_sec - tv2.tv_sec) * 1000000 + tv1.tv_usec - + tv2.tv_usec); } -int32_t difftvsec(struct timeval tv1, struct timeval tv2) { - if(tv1.tv_sec < tv2.tv_sec) { +int32_t difftvsec(struct timeval tv1, struct timeval tv2) +{ + if (tv1.tv_sec < tv2.tv_sec) { return 0; } - return tv1.tv_sec-tv2.tv_sec; + return tv1.tv_sec - tv2.tv_sec; } -std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr) { - if(target.empty() || oldstr.empty()) { +std::string replace(const std::string& target, const std::string& oldstr, + const std::string& newstr) +{ + if (target.empty() || oldstr.empty()) { return target; } std::string result; std::string::size_type p = 0; std::string::size_type np = target.find(oldstr); - while(np != std::string::npos) { - result.append(target.begin()+p, target.begin()+np); + while (np != std::string::npos) { + result.append(target.begin() + p, target.begin() + np); result += newstr; - p = np+oldstr.size(); + p = np + oldstr.size(); np = target.find(oldstr, p); } - result.append(target.begin()+p, target.end()); + result.append(target.begin() + p, target.end()); return result; } @@ -229,10 +236,7 @@ bool isAlpha(const char c) return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'); } -bool isDigit(const char c) -{ - return '0' <= c && c <= '9'; -} +bool isDigit(const char c) { return '0' <= c && c <= '9'; } bool isHexDigit(const char c) { @@ -242,7 +246,7 @@ bool isHexDigit(const char c) bool isHexDigit(const std::string& s) { for (const auto& c : s) { - if(!isHexDigit(c)) { + if (!isHexDigit(c)) { return false; } } @@ -251,41 +255,34 @@ bool isHexDigit(const std::string& s) bool inRFC3986ReservedChars(const char c) { - static const char reserved[] = { - ':' , '/' , '?' , '#' , '[' , ']' , '@', - '!' , '$' , '&' , '\'' , '(' , ')', - '*' , '+' , ',' , ';' , '=' }; - return std::find(std::begin(reserved), std::end(reserved), c) - != std::end(reserved); + static const char reserved[] = {':', '/', '?', '#', '[', ']', '@', '!', '$', + '&', '\'', '(', ')', '*', '+', ',', ';', '='}; + return std::find(std::begin(reserved), std::end(reserved), c) != + std::end(reserved); } bool inRFC3986UnreservedChars(const char c) { - static const char unreserved[] = { '-', '.', '_', '~' }; + static const char unreserved[] = {'-', '.', '_', '~'}; return isAlpha(c) || isDigit(c) || - std::find(std::begin(unreserved), std::end(unreserved), c) - != std::end(unreserved); + std::find(std::begin(unreserved), std::end(unreserved), c) != + std::end(unreserved); } bool inRFC2978MIMECharset(const char c) { - static const char chars[] = { - '!', '#', '$', '%', '&', - '\'', '+', '-', '^', '_', - '`', '{', '}', '~' - }; + static const char chars[] = {'!', '#', '$', '%', '&', '\'', '+', + '-', '^', '_', '`', '{', '}', '~'}; return isAlpha(c) || isDigit(c) || - std::find(std::begin(chars), std::end(chars), c) != std::end(chars); + std::find(std::begin(chars), std::end(chars), c) != std::end(chars); } bool inRFC2616HttpToken(const char c) { - static const char chars[] = { - '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', - '^', '_', '`', '|', '~' - }; + static const char chars[] = {'!', '#', '$', '%', '&', '\'', '*', '+', + '-', '.', '^', '_', '`', '|', '~'}; return isAlpha(c) || isDigit(c) || - std::find(std::begin(chars), std::end(chars), c) != std::end(chars); + std::find(std::begin(chars), std::end(chars), c) != std::end(chars); } bool inRFC5987AttrChar(const char c) @@ -299,90 +296,84 @@ bool isIso8859p1(unsigned char c) return (0x20u <= c && c <= 0x7eu) || 0xa0u <= c; } -bool isLws(const char c) -{ - return c == ' ' || c == '\t'; -} -bool isCRLF(const char c) -{ - return c == '\r' || c == '\n'; -} +bool isLws(const char c) { return c == ' ' || c == '\t'; } +bool isCRLF(const char c) { return c == '\r' || c == '\n'; } namespace { -bool isUtf8Tail(unsigned char ch) -{ - return in(ch, 0x80u, 0xbfu); -} +bool isUtf8Tail(unsigned char ch) { return in(ch, 0x80u, 0xbfu); } bool inPercentEncodeMini(const unsigned char c) { return c > 0x20 && c < 0x7fu && - // Chromium escapes following characters. Firefox4 escapes more. - c != '"' && c != '<' && c != '>'; + // Chromium escapes following characters. Firefox4 escapes more. + c != '"' && c != '<' && c != '>'; } } // namespace bool isUtf8(const std::string& str) { - for(std::string::const_iterator s = str.begin(), eos = str.end(); s != eos; - ++s) { + for (std::string::const_iterator s = str.begin(), eos = str.end(); s != eos; + ++s) { unsigned char firstChar = *s; // See ABNF in http://tools.ietf.org/search/rfc3629#section-4 - if(in(firstChar, 0x20u, 0x7eu) || - firstChar == 0x08u || // \b - firstChar == 0x09u || // \t - firstChar == 0x0au || // \n - firstChar == 0x0cu || // \f - firstChar == 0x0du // \r - ) { + if (in(firstChar, 0x20u, 0x7eu) || firstChar == 0x08u || // \b + firstChar == 0x09u || // \t + firstChar == 0x0au || // \n + firstChar == 0x0cu || // \f + firstChar == 0x0du // \r + ) { // UTF8-1 (without ctrl chars) - } else if(in(firstChar, 0xc2u, 0xdfu)) { + } + else if (in(firstChar, 0xc2u, 0xdfu)) { // UTF8-2 - if(++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !isUtf8Tail(*s)) { return false; } - } else if(0xe0u == firstChar) { + } + else if (0xe0u == firstChar) { // UTF8-3 - if(++s == eos || !in(static_cast(*s), 0xa0u, 0xbfu) || - ++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !in(static_cast(*s), 0xa0u, 0xbfu) || + ++s == eos || !isUtf8Tail(*s)) { return false; } - } else if(in(firstChar, 0xe1u, 0xecu) || in(firstChar, 0xeeu, 0xefu)) { + } + else if (in(firstChar, 0xe1u, 0xecu) || in(firstChar, 0xeeu, 0xefu)) { // UTF8-3 - if(++s == eos || !isUtf8Tail(*s) || - ++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !isUtf8Tail(*s) || ++s == eos || !isUtf8Tail(*s)) { return false; } - } else if(0xedu == firstChar) { + } + else if (0xedu == firstChar) { // UTF8-3 - if(++s == eos || !in(static_cast(*s), 0x80u, 0x9fu) || - ++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !in(static_cast(*s), 0x80u, 0x9fu) || + ++s == eos || !isUtf8Tail(*s)) { return false; } - } else if(0xf0u == firstChar) { + } + else if (0xf0u == firstChar) { // UTF8-4 - if(++s == eos || !in(static_cast(*s), 0x90u, 0xbfu) || - ++s == eos || !isUtf8Tail(*s) || - ++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !in(static_cast(*s), 0x90u, 0xbfu) || + ++s == eos || !isUtf8Tail(*s) || ++s == eos || !isUtf8Tail(*s)) { return false; } - } else if(in(firstChar, 0xf1u, 0xf3u)) { + } + else if (in(firstChar, 0xf1u, 0xf3u)) { // UTF8-4 - if(++s == eos || !isUtf8Tail(*s) || - ++s == eos || !isUtf8Tail(*s) || - ++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !isUtf8Tail(*s) || ++s == eos || !isUtf8Tail(*s) || + ++s == eos || !isUtf8Tail(*s)) { return false; } - } else if(0xf4u == firstChar) { + } + else if (0xf4u == firstChar) { // UTF8-4 - if(++s == eos || !in(static_cast(*s), 0x80u, 0x8fu) || - ++s == eos || !isUtf8Tail(*s) || - ++s == eos || !isUtf8Tail(*s)) { + if (++s == eos || !in(static_cast(*s), 0x80u, 0x8fu) || + ++s == eos || !isUtf8Tail(*s) || ++s == eos || !isUtf8Tail(*s)) { return false; } - } else { + } + else { return false; } } @@ -392,10 +383,11 @@ bool isUtf8(const std::string& str) std::string percentEncode(const unsigned char* target, size_t len) { std::string dest; - for(size_t i = 0; i < len; ++i) { - if(inRFC3986UnreservedChars(target[i])) { + for (size_t i = 0; i < len; ++i) { + if (inRFC3986UnreservedChars(target[i])) { dest += target[i]; - } else { + } + else { dest.append(fmt("%%%02X", target[i])); } } @@ -419,22 +411,25 @@ std::string percentEncodeMini(const std::string& src) return src; } std::string result; - for (auto c: src) { - if(!inPercentEncodeMini(c)) { + for (auto c : src) { + if (!inPercentEncodeMini(c)) { result += fmt("%%%02X", static_cast(c)); - } else { + } + else { result += c; } } return result; } -std::string torrentPercentEncode(const unsigned char* target, size_t len) { +std::string torrentPercentEncode(const unsigned char* target, size_t len) +{ std::string dest; - for(size_t i = 0; i < len; ++i) { - if(isAlpha(target[i]) || isDigit(target[i])) { + for (size_t i = 0; i < len; ++i) { + if (isAlpha(target[i]) || isDigit(target[i])) { dest += target[i]; - } else { + } + else { dest.append(fmt("%%%02X", target[i])); } } @@ -443,42 +438,47 @@ std::string torrentPercentEncode(const unsigned char* target, size_t len) { std::string torrentPercentEncode(const std::string& target) { - return torrentPercentEncode - (reinterpret_cast(target.c_str()), target.size()); + return torrentPercentEncode( + reinterpret_cast(target.c_str()), target.size()); } -std::string percentDecode -(std::string::const_iterator first, std::string::const_iterator last) +std::string percentDecode(std::string::const_iterator first, + std::string::const_iterator last) { std::string result; - for(; first != last; ++first) { - if(*first == '%') { - if(first+1 != last && first+2 != last && - isHexDigit(*(first+1)) && isHexDigit(*(first+2))) { - result += hexCharToUInt(*(first+1))*16+hexCharToUInt(*(first+2)); + for (; first != last; ++first) { + if (*first == '%') { + if (first + 1 != last && first + 2 != last && isHexDigit(*(first + 1)) && + isHexDigit(*(first + 2))) { + result += + hexCharToUInt(*(first + 1)) * 16 + hexCharToUInt(*(first + 2)); first += 2; - } else { + } + else { result += *first; } - } else { + } + else { result += *first; } } return result; } -std::string toHex(const unsigned char* src, size_t len) { - std::string out(len*2, '\0'); +std::string toHex(const unsigned char* src, size_t len) +{ + std::string out(len * 2, '\0'); std::string::iterator o = out.begin(); - const unsigned char* last = src+len; - for(const unsigned char* i = src; i != last; ++i) { + const unsigned char* last = src + len; + for (const unsigned char* i = src; i != last; ++i) { *o = (*i >> 4); - *(o+1) = (*i)&0x0fu; - for(int j = 0; j < 2; ++j) { - if(*o < 10) { + *(o + 1) = (*i) & 0x0fu; + for (int j = 0; j < 2; ++j) { + if (*o < 10) { *o += '0'; - } else { - *o += 'a'-10; + } + else { + *o += 'a' - 10; } ++o; } @@ -498,53 +498,57 @@ std::string toHex(const std::string& src) unsigned int hexCharToUInt(unsigned char ch) { - if('a' <= ch && ch <= 'f') { + if ('a' <= ch && ch <= 'f') { ch -= 'a'; ch += 10; - } else if('A' <= ch && ch <= 'F') { + } + else if ('A' <= ch && ch <= 'F') { ch -= 'A'; ch += 10; - } else if('0' <= ch && ch <= '9') { + } + else if ('0' <= ch && ch <= '9') { ch -= '0'; - } else { + } + else { ch = 255; } return ch; } -std::string secfmt(time_t sec) { +std::string secfmt(time_t sec) +{ time_t tsec = sec; std::string str; - if(sec >= 3600) { - str = fmt("%" PRId64 "h", static_cast(sec/3600)); + if (sec >= 3600) { + str = fmt("%" PRId64 "h", static_cast(sec / 3600)); sec %= 3600; } - if(sec >= 60) { - str += fmt("%dm", static_cast(sec/60)); + if (sec >= 60) { + str += fmt("%dm", static_cast(sec / 60)); sec %= 60; } - if(sec || tsec == 0) { + if (sec || tsec == 0) { str += fmt("%ds", static_cast(sec)); } return str; } namespace { -template +template bool parseLong(T& res, F f, const std::string& s, int base) { - if(s.empty()) { + if (s.empty()) { return false; } char* endptr; errno = 0; res = f(s.c_str(), &endptr, base); - if(errno == ERANGE) { + if (errno == ERANGE) { return false; } - if(*endptr != '\0') { - for(const char* i = endptr, *eoi = s.c_str()+s.size(); i < eoi; ++i) { - if(!isspace(*i)) { + if (*endptr != '\0') { + for (const char* i = endptr, * eoi = s.c_str() + s.size(); i < eoi; ++i) { + if (!isspace(*i)) { return false; } } @@ -556,12 +560,13 @@ bool parseLong(T& res, F f, const std::string& s, int base) bool parseIntNoThrow(int32_t& res, const std::string& s, int base) { long int t; - if(parseLong(t, strtol, s, base) && - t >= std::numeric_limits::min() && - t <= std::numeric_limits::max()) { + if (parseLong(t, strtol, s, base) && + t >= std::numeric_limits::min() && + t <= std::numeric_limits::max()) { res = t; return true; - } else { + } + else { return false; } } @@ -569,12 +574,12 @@ bool parseIntNoThrow(int32_t& res, const std::string& s, int base) bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base) { long int t; - if(parseLong(t, strtol, s, base) && - t >= 0 && - t <= std::numeric_limits::max()) { + if (parseLong(t, strtol, s, base) && t >= 0 && + t <= std::numeric_limits::max()) { res = t; return true; - } else { + } + else { return false; } } @@ -582,12 +587,13 @@ bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base) bool parseLLIntNoThrow(int64_t& res, const std::string& s, int base) { long long int t; - if(parseLong(t, strtoll, s, base) && - t >= std::numeric_limits::min() && - t <= std::numeric_limits::max()) { + if (parseLong(t, strtoll, s, base) && + t >= std::numeric_limits::min() && + t <= std::numeric_limits::max()) { res = t; return true; - } else { + } + else { return false; } } @@ -595,57 +601,60 @@ bool parseLLIntNoThrow(int64_t& res, const std::string& s, int base) SegList parseIntSegments(const std::string& src) { SegList sgl; - for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;) { + for (std::string::const_iterator i = src.begin(), eoi = src.end(); + i != eoi;) { std::string::const_iterator j = std::find(i, eoi, ','); - if(j == i) { + if (j == i) { ++i; continue; } std::string::const_iterator p = std::find(i, j, '-'); - if(p == j) { + if (p == j) { int a; - if(parseIntNoThrow(a, std::string(i, j))) { - sgl.add(a, a+1); - } else { + if (parseIntNoThrow(a, std::string(i, j))) { + sgl.add(a, a + 1); + } + else { throw DL_ABORT_EX(fmt("Bad range %s", std::string(i, j).c_str())); } - } else if(p == i || p+1 == j) { + } + else if (p == i || p + 1 == j) { throw DL_ABORT_EX(fmt(MSG_INCOMPLETE_RANGE, std::string(i, j).c_str())); - } else { + } + else { int a, b; - if(parseIntNoThrow(a, std::string(i, p)) && - parseIntNoThrow(b, (std::string(p+1, j)))) { - sgl.add(a, b+1); - } else { + if (parseIntNoThrow(a, std::string(i, p)) && + parseIntNoThrow(b, (std::string(p + 1, j)))) { + sgl.add(a, b + 1); + } + else { throw DL_ABORT_EX(fmt("Bad range %s", std::string(i, j).c_str())); } } - if(j == eoi) { + if (j == eoi) { break; } - i = j+1; + i = j + 1; } return sgl; } namespace { -void computeHeadPieces -(std::vector& indexes, - const std::vector >& fileEntries, - size_t pieceLength, - int64_t head) +void computeHeadPieces( + std::vector& indexes, + const std::vector>& fileEntries, + size_t pieceLength, int64_t head) { - if(head == 0) { + if (head == 0) { return; } - for (const auto& fi: fileEntries) { - if(fi->getLength() == 0) { + for (const auto& fi : fileEntries) { + if (fi->getLength() == 0) { continue; } - const size_t lastIndex = (fi->getOffset() + - std::min(head, fi->getLength()) - 1 - ) / pieceLength; - for(size_t idx = fi->getOffset() / pieceLength; idx <= lastIndex; ++idx) { + const size_t lastIndex = + (fi->getOffset() + std::min(head, fi->getLength()) - 1) / pieceLength; + for (size_t idx = fi->getOffset() / pieceLength; idx <= lastIndex; ++idx) { indexes.push_back(idx); } } @@ -653,22 +662,21 @@ void computeHeadPieces } // namespace namespace { -void computeTailPieces -(std::vector& indexes, - const std::vector >& fileEntries, - size_t pieceLength, - int64_t tail) +void computeTailPieces( + std::vector& indexes, + const std::vector>& fileEntries, + size_t pieceLength, int64_t tail) { - if(tail == 0) { + if (tail == 0) { return; } - for (const auto& fi: fileEntries) { - if(fi->getLength() == 0) { + for (const auto& fi : fileEntries) { + if (fi->getLength() == 0) { continue; } int64_t endOffset = fi->getLastOffset(); - size_t fromIndex = (endOffset - 1 - (std::min(tail, fi->getLength()) - 1)) / - pieceLength; + size_t fromIndex = + (endOffset - 1 - (std::min(tail, fi->getLength()) - 1)) / pieceLength; const size_t toIndex = (endOffset - 1) / pieceLength; while (fromIndex <= toIndex) { indexes.push_back(fromIndex++); @@ -677,35 +685,34 @@ void computeTailPieces } } // namespace -void parsePrioritizePieceRange -(std::vector& result, const std::string& src, - const std::vector >& fileEntries, - size_t pieceLength, - int64_t defaultSize) +void parsePrioritizePieceRange( + std::vector& result, const std::string& src, + const std::vector>& fileEntries, + size_t pieceLength, int64_t defaultSize) { std::vector indexes; std::vector parts; splitIter(src.begin(), src.end(), std::back_inserter(parts), ',', true); - for (const auto& i: parts) { - if(util::streq(i.first, i.second, "head")) { + for (const auto& i : parts) { + if (util::streq(i.first, i.second, "head")) { computeHeadPieces(indexes, fileEntries, pieceLength, defaultSize); } - else if(util::startsWith(i.first, i.second, "head=")) { + else if (util::startsWith(i.first, i.second, "head=")) { std::string sizestr(i.first + 5, i.second); computeHeadPieces(indexes, fileEntries, pieceLength, std::max((int64_t)0, getRealSize(sizestr))); } - else if(util::streq(i.first, i.second, "tail")) { + else if (util::streq(i.first, i.second, "tail")) { computeTailPieces(indexes, fileEntries, pieceLength, defaultSize); } - else if(util::startsWith(i.first, i.second, "tail=")) { + else if (util::startsWith(i.first, i.second, "tail=")) { std::string sizestr(i.first + 5, i.second); computeTailPieces(indexes, fileEntries, pieceLength, std::max((int64_t)0, getRealSize(sizestr))); } else { - throw DL_ABORT_EX(fmt("Unrecognized token %s", - std::string(i.first, i.second).c_str())); + throw DL_ABORT_EX( + fmt("Unrecognized token %s", std::string(i.first, i.second).c_str())); } } std::sort(indexes.begin(), indexes.end()); @@ -718,18 +725,21 @@ void parsePrioritizePieceRange std::string iso8859p1ToUtf8(const char* src, size_t len) { std::string dest; - for(const char* p = src, *last = src+len; p != last; ++p) { + for (const char* p = src, * last = src + len; p != last; ++p) { unsigned char c = *p; - if(0xa0u <= c) { - if(c <= 0xbfu) { + if (0xa0u <= c) { + if (c <= 0xbfu) { dest += 0xc2u; - } else { + } + else { dest += 0xc3u; } - dest += c&(~0x40u); - } else if(0x80u <= c && c <= 0x9fu) { + dest += c & (~0x40u); + } + else if (0x80u <= c && c <= 0x9fu) { return ""; - } else { + } + else { dest += c; } } @@ -771,37 +781,43 @@ std::string iso8859p1ToUtf8(const std::string& src) #define UTF8_REJECT 12 static const uint8_t utf8d[] = { - /* - * The first part of the table maps bytes to character classes that - * to reduce the size of the transition table and create bitmasks. - */ - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, - 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, - - /* - * The second part is a transition table that maps a combination - * of a state of the automaton and a character class to a state. - */ - 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12, - 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12, - 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12, - 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12, - 12,36,12,12,12,12,12,12,12,12,12,12, + /* + * The first part of the table maps bytes to character classes that + * to reduce the size of the transition table and create bitmasks. + */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 10, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 11, 6, 6, 6, 5, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, + + /* + * The second part is a transition table that maps a combination + * of a state of the automaton and a character class to a state. + */ + 0, 12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 0, 12, 12, 12, 12, 12, 0, 12, 0, 12, 12, 12, 24, + 12, 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 24, 12, + 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, + 12, 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, }; -static uint32_t -utf8dfa(uint32_t* state, uint32_t* codep, uint32_t byte) { +static uint32_t utf8dfa(uint32_t* state, uint32_t* codep, uint32_t byte) +{ uint32_t type = utf8d[byte]; - *codep = (*state != UTF8_ACCEPT) ? - (byte & 0x3fu) | (*codep << 6) : - (0xff >> type) & (byte); + *codep = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6) + : (0xff >> type) & (byte); *state = utf8d[256 + *state + type]; return *state; @@ -839,18 +855,19 @@ typedef enum { CD_ENC_ISO_8859_1 } content_disposition_charset; -ssize_t parse_content_disposition(char *dest, size_t destlen, - const char **charsetp, size_t *charsetlenp, - const char *in, size_t len) +ssize_t parse_content_disposition(char* dest, size_t destlen, + const char** charsetp, size_t* charsetlenp, + const char* in, size_t len) { - const char *p = in, *eop = in + len, *mark_first = nullptr, *mark_last = nullptr; + const char* p = in, * eop = in + len, * mark_first = nullptr, + * mark_last = nullptr; int state = CD_BEFORE_DISPOSITION_TYPE; int in_file_parm = 0; int flags = 0; int quoted_seen = 0; int charset = 0; /* To suppress warnings */ - char *dp = dest; + char* dp = dest; size_t dlen = destlen; uint32_t dfa_state = 0; uint32_t dfa_code = 0; @@ -859,120 +876,136 @@ ssize_t parse_content_disposition(char *dest, size_t destlen, *charsetp = nullptr; *charsetlenp = 0; - for(; p != eop; ++p) { - switch(state) { + for (; p != eop; ++p) { + switch (state) { case CD_BEFORE_DISPOSITION_TYPE: - if(inRFC2616HttpToken(*p)) { + if (inRFC2616HttpToken(*p)) { state = CD_DISPOSITION_TYPE; - } else if(!isLws(*p)) { + } + else if (!isLws(*p)) { return -1; } break; case CD_AFTER_DISPOSITION_TYPE: case CD_DISPOSITION_TYPE: - if(*p == ';') { + if (*p == ';') { state = CD_BEFORE_DISPOSITION_PARM_NAME; - } else if(isLws(*p)) { + } + else if (isLws(*p)) { state = CD_AFTER_DISPOSITION_TYPE; - } else if(state == CD_AFTER_DISPOSITION_TYPE || - !inRFC2616HttpToken(*p)) { + } + else if (state == CD_AFTER_DISPOSITION_TYPE || !inRFC2616HttpToken(*p)) { return -1; } break; case CD_BEFORE_DISPOSITION_PARM_NAME: - if(inRFC2616HttpToken(*p)) { + if (inRFC2616HttpToken(*p)) { mark_first = p; state = CD_DISPOSITION_PARM_NAME; - } else if(!isLws(*p)) { + } + else if (!isLws(*p)) { return -1; } break; case CD_AFTER_DISPOSITION_PARM_NAME: case CD_DISPOSITION_PARM_NAME: - if(*p == '=') { - if(state == CD_DISPOSITION_PARM_NAME) { + if (*p == '=') { + if (state == CD_DISPOSITION_PARM_NAME) { mark_last = p; } in_file_parm = 0; - if(strieq(mark_first, mark_last, "filename*")) { - if((flags & CD_EXT_FILENAME_FOUND) == 0) { + if (strieq(mark_first, mark_last, "filename*")) { + if ((flags & CD_EXT_FILENAME_FOUND) == 0) { in_file_parm = 1; - } else { + } + else { return -1; } state = CD_BEFORE_EXT_VALUE; - } else if(strieq(mark_first, mark_last, "filename")) { - if(flags & CD_FILENAME_FOUND) { + } + else if (strieq(mark_first, mark_last, "filename")) { + if (flags & CD_FILENAME_FOUND) { return -1; } - if((flags & CD_EXT_FILENAME_FOUND) == 0) { + if ((flags & CD_EXT_FILENAME_FOUND) == 0) { in_file_parm = 1; } state = CD_BEFORE_VALUE; - } else { + } + else { /* ext-token must be characters in token, followed by "*" */ - if(mark_first != mark_last-1 && *(mark_last-1) == '*') { + if (mark_first != mark_last - 1 && *(mark_last - 1) == '*') { state = CD_BEFORE_EXT_VALUE; - } else { + } + else { state = CD_BEFORE_VALUE; } } - if(in_file_parm) { + if (in_file_parm) { dp = dest; dlen = destlen; } - } else if(isLws(*p)) { + } + else if (isLws(*p)) { mark_last = p; state = CD_AFTER_DISPOSITION_PARM_NAME; - } else if(state == CD_AFTER_DISPOSITION_PARM_NAME || - !inRFC2616HttpToken(*p)) { + } + else if (state == CD_AFTER_DISPOSITION_PARM_NAME || + !inRFC2616HttpToken(*p)) { return -1; } break; case CD_BEFORE_VALUE: - if(*p == '"') { + if (*p == '"') { quoted_seen = 0; state = CD_QUOTED_STRING; - } else if(inRFC2616HttpToken(*p)) { - if(in_file_parm) { - if(dlen == 0) { + } + else if (inRFC2616HttpToken(*p)) { + if (in_file_parm) { + if (dlen == 0) { return -1; - } else { + } + else { *dp++ = *p; --dlen; } } state = CD_TOKEN; - } else if(!isLws(*p)) { + } + else if (!isLws(*p)) { return -1; } break; case CD_AFTER_VALUE: - if(*p == ';') { + if (*p == ';') { state = CD_BEFORE_DISPOSITION_PARM_NAME; - } else if(!isLws(*p)) { + } + else if (!isLws(*p)) { return -1; } break; case CD_QUOTED_STRING: - if(*p == '\\' && quoted_seen == 0) { + if (*p == '\\' && quoted_seen == 0) { quoted_seen = 1; - } else if(*p == '"' && quoted_seen == 0) { - if(in_file_parm) { + } + else if (*p == '"' && quoted_seen == 0) { + if (in_file_parm) { flags |= CD_FILENAME_FOUND; } state = CD_AFTER_VALUE; - } else { + } + else { /* TEXT which is OCTET except CTLs, but including LWS. We only accept ISO-8859-1 chars. */ quoted_seen = 0; - if(!isIso8859p1(*p)) { + if (!isIso8859p1(*p)) { return -1; } - if(in_file_parm) { - if(dlen == 0) { + if (in_file_parm) { + if (dlen == 0) { return -1; - } else { + } + else { *dp++ = *p; --dlen; } @@ -980,149 +1013,167 @@ ssize_t parse_content_disposition(char *dest, size_t destlen, } break; case CD_TOKEN: - if(inRFC2616HttpToken(*p)) { - if(in_file_parm) { - if(dlen == 0) { + if (inRFC2616HttpToken(*p)) { + if (in_file_parm) { + if (dlen == 0) { return -1; - } else { + } + else { *dp++ = *p; --dlen; } } - } else if(*p == ';') { - if(in_file_parm) { + } + else if (*p == ';') { + if (in_file_parm) { flags |= CD_FILENAME_FOUND; } state = CD_BEFORE_DISPOSITION_PARM_NAME; - } else if(isLws(*p)) { - if(in_file_parm) { + } + else if (isLws(*p)) { + if (in_file_parm) { flags |= CD_FILENAME_FOUND; } state = CD_AFTER_VALUE; - } else { + } + else { return -1; } break; case CD_BEFORE_EXT_VALUE: - if(*p == '\'') { + if (*p == '\'') { /* Empty charset is not allowed */ return -1; - } else if(inRFC2978MIMECharset(*p)) { + } + else if (inRFC2978MIMECharset(*p)) { mark_first = p; state = CD_CHARSET; - } else if(!isLws(*p)) { + } + else if (!isLws(*p)) { return -1; } break; case CD_CHARSET: - if(*p == '\'') { + if (*p == '\'') { mark_last = p; *charsetp = mark_first; *charsetlenp = mark_last - mark_first; - if(strieq(mark_first, mark_last, "utf-8")) { + if (strieq(mark_first, mark_last, "utf-8")) { charset = CD_ENC_UTF8; dfa_state = UTF8_ACCEPT; dfa_code = 0; - } else if(strieq(mark_first, mark_last, "iso-8859-1")) { + } + else if (strieq(mark_first, mark_last, "iso-8859-1")) { charset = CD_ENC_ISO_8859_1; - } else { + } + else { charset = CD_ENC_UNKNOWN; } state = CD_LANGUAGE; - } else if(!inRFC2978MIMECharset(*p)) { + } + else if (!inRFC2978MIMECharset(*p)) { return -1; } break; case CD_LANGUAGE: - if(*p == '\'') { - if(in_file_parm) { + if (*p == '\'') { + if (in_file_parm) { dp = dest; dlen = destlen; } state = CD_VALUE_CHARS; - } else if(*p != '-' && !isAlpha(*p) && !isDigit(*p)) { + } + else if (*p != '-' && !isAlpha(*p) && !isDigit(*p)) { return -1; } break; case CD_VALUE_CHARS: - if(inRFC5987AttrChar(*p)) { - if(charset == CD_ENC_UTF8) { - if(utf8dfa(&dfa_state, &dfa_code, *p) == UTF8_REJECT) { + if (inRFC5987AttrChar(*p)) { + if (charset == CD_ENC_UTF8) { + if (utf8dfa(&dfa_state, &dfa_code, *p) == UTF8_REJECT) { return -1; } } - if(in_file_parm) { - if(dlen == 0) { + if (in_file_parm) { + if (dlen == 0) { return -1; - } else { + } + else { *dp++ = *p; --dlen; } } - } else if(*p == '%') { - if(in_file_parm) { - if(dlen == 0) { + } + else if (*p == '%') { + if (in_file_parm) { + if (dlen == 0) { return -1; } } pctval = 0; state = CD_VALUE_CHARS_PCT_ENCODED1; - } else if(*p == ';' || isLws(*p)) { - if(charset == CD_ENC_UTF8 && dfa_state != UTF8_ACCEPT) { + } + else if (*p == ';' || isLws(*p)) { + if (charset == CD_ENC_UTF8 && dfa_state != UTF8_ACCEPT) { return -1; } - if(in_file_parm) { + if (in_file_parm) { flags |= CD_EXT_FILENAME_FOUND; } - if(*p == ';') { + if (*p == ';') { state = CD_BEFORE_DISPOSITION_PARM_NAME; - } else { + } + else { state = CD_AFTER_VALUE; } - } else if(!inRFC5987AttrChar(*p)) { + } + else if (!inRFC5987AttrChar(*p)) { return -1; } break; case CD_VALUE_CHARS_PCT_ENCODED1: - if(isHexDigit(*p)) { + if (isHexDigit(*p)) { pctval |= hexCharToUInt(*p) << 4; state = CD_VALUE_CHARS_PCT_ENCODED2; - } else { + } + else { return -1; } break; case CD_VALUE_CHARS_PCT_ENCODED2: - if(isHexDigit(*p)) { + if (isHexDigit(*p)) { pctval |= hexCharToUInt(*p); - if(charset == CD_ENC_UTF8) { - if(utf8dfa(&dfa_state, &dfa_code, pctval) == UTF8_REJECT) { + if (charset == CD_ENC_UTF8) { + if (utf8dfa(&dfa_state, &dfa_code, pctval) == UTF8_REJECT) { return -1; } - } else if(charset == CD_ENC_ISO_8859_1) { - if(!isIso8859p1(pctval)) { + } + else if (charset == CD_ENC_ISO_8859_1) { + if (!isIso8859p1(pctval)) { return -1; } } - if(in_file_parm) { + if (in_file_parm) { *dp++ = pctval; --dlen; } state = CD_VALUE_CHARS; - } else { + } + else { return -1; } break; } } - switch(state) { + switch (state) { case CD_BEFORE_DISPOSITION_TYPE: case CD_AFTER_DISPOSITION_TYPE: case CD_DISPOSITION_TYPE: case CD_AFTER_VALUE: case CD_TOKEN: - return destlen-dlen; + return destlen - dlen; case CD_VALUE_CHARS: - if(charset == CD_ENC_UTF8 && dfa_state != UTF8_ACCEPT) { + if (charset == CD_ENC_UTF8 && dfa_state != UTF8_ACCEPT) { return -1; } return destlen - dlen; @@ -1137,20 +1188,21 @@ std::string getContentDispositionFilename(const std::string& header) size_t cdvallen = cdval.size(); const char* charset; size_t charsetlen; - ssize_t rv = parse_content_disposition(cdval.data(), cdvallen, - &charset, &charsetlen, - header.c_str(), header.size()); - if(rv == -1) { + ssize_t rv = + parse_content_disposition(cdval.data(), cdvallen, &charset, &charsetlen, + header.c_str(), header.size()); + if (rv == -1) { return ""; } std::string res; - if(!charset || strieq(charset, charset+charsetlen, "iso-8859-1")) { + if (!charset || strieq(charset, charset + charsetlen, "iso-8859-1")) { res = iso8859p1ToUtf8(cdval.data(), rv); - } else { + } + else { res.assign(cdval.data(), rv); } - if(!detectDirTraversal(res) && + if (!detectDirTraversal(res) && res.find_first_of("/\\") == std::string::npos) { return res; } @@ -1163,7 +1215,8 @@ std::string toUpper(std::string src) return src; } -std::string toLower(std::string src) { +std::string toLower(std::string src) +{ lowercase(src); return src; } @@ -1180,16 +1233,16 @@ void lowercase(std::string& s) char toUpperChar(char c) { - if('a' <= c && c <= 'z') { - c += 'A'-'a'; + if ('a' <= c && c <= 'z') { + c += 'A' - 'a'; } return c; } char toLowerChar(char c) { - if('A' <= c && c <= 'Z') { - c += 'a'-'A'; + if ('A' <= c && c <= 'Z') { + c += 'a' - 'A'; } return c; } @@ -1201,7 +1254,7 @@ bool isNumericHost(const std::string& name) memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_flags = AI_NUMERICHOST; - if(getaddrinfo(name.c_str(), nullptr, &hints, &res)) { + if (getaddrinfo(name.c_str(), nullptr, &hints, &res)) { return false; } freeaddrinfo(res); @@ -1210,52 +1263,52 @@ bool isNumericHost(const std::string& name) #if _WIN32 namespace { - static Lock win_signal_lock; +static Lock win_signal_lock; - static signal_handler_t win_int_handler = nullptr; - static signal_handler_t win_term_handler = nullptr; +static signal_handler_t win_int_handler = nullptr; +static signal_handler_t win_term_handler = nullptr; - static void win_ign_handler(int) {} +static void win_ign_handler(int) {} - static BOOL WINAPI HandlerRoutine(DWORD ctrlType) - { - void(*handler)(int) = nullptr; - switch (ctrlType) { - case CTRL_C_EVENT: - case CTRL_BREAK_EVENT: - { - // Handler will be called on a new/different thread. - LockGuard lg(win_signal_lock); - handler = win_int_handler; - } +static BOOL WINAPI HandlerRoutine(DWORD ctrlType) +{ + void (*handler)(int) = nullptr; + switch (ctrlType) { + case CTRL_C_EVENT: + case CTRL_BREAK_EVENT: { + // Handler will be called on a new/different thread. + LockGuard lg(win_signal_lock); + handler = win_int_handler; + } - if (handler) { - handler(SIGINT); - return TRUE; - } - return FALSE; - - case CTRL_LOGOFF_EVENT: - case CTRL_CLOSE_EVENT: - case CTRL_SHUTDOWN_EVENT: - { - // Handler will be called on a new/different thread. - LockGuard lg(win_signal_lock); - handler = win_term_handler;; - } - if (handler) { - handler(SIGTERM); - return TRUE; - } - return FALSE; + if (handler) { + handler(SIGINT); + return TRUE; } return FALSE; + + case CTRL_LOGOFF_EVENT: + case CTRL_CLOSE_EVENT: + case CTRL_SHUTDOWN_EVENT: { + // Handler will be called on a new/different thread. + LockGuard lg(win_signal_lock); + handler = win_term_handler; + ; } + if (handler) { + handler(SIGTERM); + return TRUE; + } + return FALSE; + } + return FALSE; +} } #endif void setGlobalSignalHandler(int sig, sigset_t* mask, signal_handler_t handler, - int flags) { + int flags) +{ #if _WIN32 if (sig == SIGINT || sig == SIGTERM) { // Handler will be called on a new/different thread. @@ -1312,7 +1365,7 @@ std::string getHomeDir() return A2STR::NIL; } -#else // __MINGW32__ +#else // __MINGW32__ std::string getHomeDir() { @@ -1344,26 +1397,30 @@ std::string getXDGDir(const std::string& environmentVariable, const char* p = getenv(environmentVariable.c_str()); if (p && p[0] == '/') { filename = p; - } else { + } + else { filename = fallbackDirectory; } return filename; } -std::string getConfigFile() { +std::string getConfigFile() +{ std::string filename = getHomeDir() + "/.aria2/aria2.conf"; if (!File(filename).exists()) { - filename = getXDGDir("XDG_CONFIG_HOME", getHomeDir()+"/.config") + - "/aria2/aria2.conf"; + filename = getXDGDir("XDG_CONFIG_HOME", getHomeDir() + "/.config") + + "/aria2/aria2.conf"; } return filename; } -std::string getDHTFile(bool ipv6) { - std::string filename = getHomeDir() + (ipv6 ? "/.aria2/dht6.dat" : "/.aria2/dht.dat"); +std::string getDHTFile(bool ipv6) +{ + std::string filename = + getHomeDir() + (ipv6 ? "/.aria2/dht6.dat" : "/.aria2/dht.dat"); if (!File(filename).exists()) { - filename = getXDGDir("XDG_CACHE_HOME", getHomeDir()+"/.cache") + - (ipv6 ? "/aria2/dht6.dat" : "/aria2/dht.dat"); + filename = getXDGDir("XDG_CACHE_HOME", getHomeDir() + "/.cache") + + (ipv6 ? "/aria2/dht6.dat" : "/aria2/dht.dat"); } return filename; } @@ -1373,10 +1430,11 @@ int64_t getRealSize(const std::string& sizeWithUnit) std::string::size_type p = sizeWithUnit.find_first_of("KMkm"); std::string size; int32_t mult = 1; - if(p == std::string::npos) { + if (p == std::string::npos) { size = sizeWithUnit; - } else { - switch(sizeWithUnit[p]) { + } + else { + switch (sizeWithUnit[p]) { case 'K': case 'k': mult = 1_k; @@ -1386,41 +1444,41 @@ int64_t getRealSize(const std::string& sizeWithUnit) mult = 1_m; break; } - size.assign(sizeWithUnit.begin(), sizeWithUnit.begin()+p); + size.assign(sizeWithUnit.begin(), sizeWithUnit.begin() + p); } int64_t v; - if(!parseLLIntNoThrow(v, size) || v < 0) { - throw DL_ABORT_EX(fmt("Bad or negative value detected: %s", - sizeWithUnit.c_str())); + if (!parseLLIntNoThrow(v, size) || v < 0) { + throw DL_ABORT_EX( + fmt("Bad or negative value detected: %s", sizeWithUnit.c_str())); } - if(INT64_MAX/mult < v) { - throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE, - "overflow/underflow")); + if (INT64_MAX / mult < v) { + throw DL_ABORT_EX( + fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE, "overflow/underflow")); } - return v*mult; + return v * mult; } std::string abbrevSize(int64_t size) { - static const char* UNITS[] = { "", "Ki", "Mi", "Gi" }; + static const char* UNITS[] = {"", "Ki", "Mi", "Gi"}; int64_t t = size; size_t uidx = 0; int r = 0; - while(t >= static_cast(1_k) && - uidx + 1 < sizeof(UNITS) / sizeof(UNITS[0])) { + while (t >= static_cast(1_k) && + uidx + 1 < sizeof(UNITS) / sizeof(UNITS[0])) { lldiv_t d = lldiv(t, 1_k); t = d.quot; r = d.rem; ++uidx; } - if(uidx+1 < sizeof(UNITS)/sizeof(UNITS[0]) && t >= 922) { + if (uidx + 1 < sizeof(UNITS) / sizeof(UNITS[0]) && t >= 922) { ++uidx; r = t; t = 0; } std::string res; res += itos(t, true); - if(t < 10 && uidx > 0) { + if (t < 10 && uidx > 0) { res += "."; res += itos(r * 10 / 1_k); } @@ -1428,7 +1486,8 @@ std::string abbrevSize(int64_t size) return res; } -void sleep(long seconds) { +void sleep(long seconds) +{ #if defined(HAVE_WINSOCK2_H) ::Sleep(seconds * 1000); #elif HAVE_SLEEP @@ -1436,18 +1495,23 @@ void sleep(long seconds) { #elif defined(HAVE_USLEEP) ::usleep(seconds * 1000000); #else -# error no sleep function is available (nanosleep?) +#error no sleep function is available (nanosleep?) #endif } -void usleep(long microseconds) { +void usleep(long microseconds) +{ #ifdef HAVE_USLEEP ::usleep(microseconds); #elif defined(HAVE_WINSOCK2_H) LARGE_INTEGER current, freq, end; - static enum {GET_FREQUENCY, GET_MICROSECONDS, SKIP_MICROSECONDS} state = GET_FREQUENCY; + static enum { + GET_FREQUENCY, + GET_MICROSECONDS, + SKIP_MICROSECONDS + } state = GET_FREQUENCY; if (state == GET_FREQUENCY) { if (QueryPerformanceFrequency(&freq)) @@ -1464,8 +1528,9 @@ void usleep(long microseconds) { end.QuadPart += (freq.QuadPart * microseconds) / 1000000; - while (QueryPerformanceCounter(¤t) && (current.QuadPart <= end.QuadPart)) - /* noop */ ; + while (QueryPerformanceCounter(¤t) && + (current.QuadPart <= end.QuadPart)) + /* noop */; } if (msec) @@ -1478,9 +1543,9 @@ void usleep(long microseconds) { void mkdirs(const std::string& dirpath) { File dir(dirpath); - if(!dir.mkdirs()) { + if (!dir.mkdirs()) { int errNum = errno; - if(!dir.isDir()) { + if (!dir.isDir()) { throw DL_ABORT_EX3(errNum, fmt(EX_MAKE_DIR, dir.getPath().c_str(), safeStrerror(errNum).c_str()), error_code::DIR_CREATE_ERROR); @@ -1491,9 +1556,9 @@ void mkdirs(const std::string& dirpath) void convertBitfield(BitfieldMan* dest, const BitfieldMan* src) { size_t numBlock = dest->countBlock(); - for(size_t index = 0; index < numBlock; ++index) { - if(src->isBitSetOffsetRange((int64_t)index*dest->getBlockLength(), - dest->getBlockLength())) { + for (size_t index = 0; index < numBlock; ++index) { + if (src->isBitSetOffsetRange((int64_t)index * dest->getBlockLength(), + dest->getBlockLength())) { dest->setBit(index); } } @@ -1503,11 +1568,11 @@ std::string toString(const std::shared_ptr& binaryStream) { std::stringstream strm; char data[2048]; - while(1) { - int32_t dataLength = binaryStream->readData - (reinterpret_cast(data), sizeof(data), strm.tellp()); + while (1) { + int32_t dataLength = binaryStream->readData( + reinterpret_cast(data), sizeof(data), strm.tellp()); strm.write(data, dataLength); - if(dataLength == 0) { + if (dataLength == 0) { break; } } @@ -1522,9 +1587,9 @@ void* allocateAlignedMemory(size_t alignment, size_t size) { void* buffer; int res; - if((res = posix_memalign(&buffer, alignment, size)) != 0) { - throw FATAL_EXCEPTION(fmt("Error in posix_memalign: %s", - util::safeStrerror(res).c_str())); + if ((res = posix_memalign(&buffer, alignment, size)) != 0) { + throw FATAL_EXCEPTION( + fmt("Error in posix_memalign: %s", util::safeStrerror(res).c_str())); } return buffer; } @@ -1536,10 +1601,10 @@ getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len) char host[NI_MAXHOST]; char service[NI_MAXSERV]; int s = getnameinfo(sockaddr, len, host, NI_MAXHOST, service, NI_MAXSERV, - NI_NUMERICHOST|NI_NUMERICSERV); - if(s != 0) { - throw DL_ABORT_EX(fmt("Failed to get hostname and port. cause: %s", - gai_strerror(s))); + NI_NUMERICHOST | NI_NUMERICSERV); + if (s != 0) { + throw DL_ABORT_EX( + fmt("Failed to get hostname and port. cause: %s", gai_strerror(s))); } return std::pair(host, atoi(service)); // TODO } @@ -1549,20 +1614,25 @@ std::string htmlEscape(const std::string& src) std::string dest; dest.reserve(src.size()); auto j = std::begin(src); - for(auto i = std::begin(src); i != std::end(src); ++i) { + for (auto i = std::begin(src); i != std::end(src); ++i) { char ch = *i; - const char *repl; - if(ch == '<') { + const char* repl; + if (ch == '<') { repl = "<"; - } else if(ch == '>') { + } + else if (ch == '>') { repl = ">"; - } else if(ch == '&') { + } + else if (ch == '&') { repl = "&"; - } else if(ch == '\'') { + } + else if (ch == '\'') { repl = "'"; - } else if(ch == '"') { + } + else if (ch == '"') { repl = """; - } else { + } + else { continue; } dest.append(j, i); @@ -1573,25 +1643,24 @@ std::string htmlEscape(const std::string& src) return dest; } -std::pair -parseIndexPath(const std::string& line) +std::pair parseIndexPath(const std::string& line) { auto p = divide(std::begin(line), std::end(line), '='); uint32_t index; - if(!parseUIntNoThrow(index, std::string(p.first.first, p.first.second))) { + if (!parseUIntNoThrow(index, std::string(p.first.first, p.first.second))) { throw DL_ABORT_EX("Bad path index"); } - if(p.second.first == p.second.second) { + if (p.second.first == p.second.second) { throw DL_ABORT_EX(fmt("Path with index=%u is empty.", index)); } return std::make_pair(index, std::string(p.second.first, p.second.second)); } -std::vector > createIndexPaths(std::istream& i) +std::vector> createIndexPaths(std::istream& i) { - std::vector > indexPaths; + std::vector> indexPaths; std::string line; - while(getline(i, line)) { + while (getline(i, line)) { indexPaths.push_back(parseIndexPath(line)); } return indexPaths; @@ -1603,23 +1672,23 @@ void generateRandomData(unsigned char* data, size_t length) return rd->getRandomBytes(data, length); } -bool saveAs -(const std::string& filename, const std::string& data, bool overwrite) +bool saveAs(const std::string& filename, const std::string& data, + bool overwrite) { - if(!overwrite && File(filename).exists()) { + if (!overwrite && File(filename).exists()) { return false; } std::string tempFilename = filename; tempFilename += "__temp"; { BufferedFile fp(tempFilename.c_str(), BufferedFile::WRITE); - if(!fp) { + if (!fp) { return false; } - if(fp.write(data.data(), data.size()) != data.size()) { + if (fp.write(data.data(), data.size()) != data.size()) { return false; } - if(fp.close() == EOF) { + if (fp.close() == EOF) { return false; } } @@ -1629,13 +1698,13 @@ bool saveAs std::string applyDir(const std::string& dir, const std::string& relPath) { std::string s; - if(dir.empty()) { + if (dir.empty()) { s = "./"; s += relPath; } else { s = dir; - if(dir == "/") { + if (dir == "/") { s += relPath; } else { @@ -1644,8 +1713,8 @@ std::string applyDir(const std::string& dir, const std::string& relPath) } } #ifdef __MINGW32__ - for(std::string::iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) { - if(*i == '\\') { + for (std::string::iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) { + if (*i == '\\') { *i = '/'; } } @@ -1674,15 +1743,15 @@ 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.")) { + if (util::startsWith(ipv4addr, "10.") || + util::startsWith(ipv4addr, "192.168.")) { return true; } - if(util::startsWith(ipv4addr, "172.")) { - for(int i = 16; i <= 31; ++i) { + if (util::startsWith(ipv4addr, "172.")) { + for (int i = 16; i <= 31; ++i) { std::string t(fmt("%d.", i)); - if(util::startsWith(ipv4addr.begin()+4, ipv4addr.end(), - t.begin(), t.end())) { + if (util::startsWith(ipv4addr.begin() + 4, ipv4addr.end(), t.begin(), + t.end())) { return true; } } @@ -1692,7 +1761,7 @@ bool inPrivateAddress(const std::string& ipv4addr) bool detectDirTraversal(const std::string& s) { - if(s.empty()) { + if (s.empty()) { return false; } for (auto c : s) { @@ -1701,60 +1770,57 @@ bool detectDirTraversal(const std::string& s) return true; } } - return s == "." || s == ".." || s[0] == '/' || - util::startsWith(s, "./") || - util::startsWith(s, "../") || - s.find("/../") != std::string::npos || - s.find("/./") != std::string::npos || - s[s.size()-1] == '/' || - util::endsWith(s, "/.") || - util::endsWith(s, "/.."); + return s == "." || s == ".." || s[0] == '/' || util::startsWith(s, "./") || + util::startsWith(s, "../") || s.find("/../") != std::string::npos || + s.find("/./") != std::string::npos || s[s.size() - 1] == '/' || + util::endsWith(s, "/.") || util::endsWith(s, "/.."); } std::string escapePath(const std::string& s) { - // We don't escape '/' because we use it as a path separator. +// We don't escape '/' because we use it as a path separator. #ifdef __MINGW32__ - static const char WIN_INVALID_PATH_CHARS[] = - { '"', '*', ':', '<', '>', '?', '\\', '|' }; + static const char WIN_INVALID_PATH_CHARS[] = {'"', '*', ':', '<', + '>', '?', '\\', '|'}; #endif // __MINGW32__ std::string d; - for(auto cc: s) { + for (auto cc : s) { unsigned char c = cc; - if(in(c, 0x00u, 0x1fu) || c == 0x7fu + if (in(c, 0x00u, 0x1fu) || c == 0x7fu #ifdef __MINGW32__ - || std::find(std::begin(WIN_INVALID_PATH_CHARS), - std::end(WIN_INVALID_PATH_CHARS), - c) != std::end(WIN_INVALID_PATH_CHARS) + || + std::find(std::begin(WIN_INVALID_PATH_CHARS), + std::end(WIN_INVALID_PATH_CHARS), + c) != std::end(WIN_INVALID_PATH_CHARS) #endif // __MINGW32__ - ){ + ) { d += fmt("%%%02X", c); - } else { + } + else { d += c; } } return d; } -bool inSameCidrBlock -(const std::string& ip1, const std::string& ip2, size_t bits) +bool inSameCidrBlock(const std::string& ip1, const std::string& ip2, + size_t bits) { unsigned char s1[16], s2[16]; size_t len1, len2; - if((len1 = net::getBinAddr(s1, ip1)) == 0 || - (len2 = net::getBinAddr(s2, ip2)) == 0 || - len1 != len2) { + if ((len1 = net::getBinAddr(s1, ip1)) == 0 || + (len2 = net::getBinAddr(s2, ip2)) == 0 || len1 != len2) { return false; } - if(bits == 0) { + if (bits == 0) { return true; } - if(bits > 8*len1) { - bits = 8*len1; + if (bits > 8 * len1) { + bits = 8 * len1; } - int last = (bits-1)/8; - for(int i = 0; i < last; ++i) { - if(s1[i] != s2[i]) { + int last = (bits - 1) / 8; + for (int i = 0; i < last; ++i) { + if (s1[i] != s2[i]) { return false; } } @@ -1764,35 +1830,26 @@ bool inSameCidrBlock namespace { -void executeHook -(const std::string& command, - a2_gid_t gid, - size_t numFiles, - const std::string& firstFilename) +void executeHook(const std::string& command, a2_gid_t gid, size_t numFiles, + const std::string& firstFilename) { const std::string gidStr = GroupId::toHex(gid); const std::string numFilesStr = util::uitos(numFiles); #ifndef __MINGW32__ - A2_LOG_INFO(fmt("Executing user command: %s %s %s %s", - command.c_str(), - gidStr.c_str(), - numFilesStr.c_str(), - firstFilename.c_str())); + A2_LOG_INFO(fmt("Executing user command: %s %s %s %s", command.c_str(), + gidStr.c_str(), numFilesStr.c_str(), firstFilename.c_str())); pid_t cpid = fork(); if (cpid == 0) { // child! - execlp(command.c_str(), - command.c_str(), - gidStr.c_str(), - numFilesStr.c_str(), - firstFilename.c_str(), + execlp(command.c_str(), command.c_str(), gidStr.c_str(), + numFilesStr.c_str(), firstFilename.c_str(), reinterpret_cast(0)); - perror(("Could not execute user command: "+command).c_str()); + perror(("Could not execute user command: " + command).c_str()); _exit(EXIT_FAILURE); return; } - if(cpid == -1) { + if (cpid == -1) { A2_LOG_ERROR("fork() failed. Cannot execute user command."); } return; @@ -1801,20 +1858,21 @@ void executeHook PROCESS_INFORMATION pi; STARTUPINFOW si; - memset(&si, 0, sizeof (si)); + memset(&si, 0, sizeof(si)); si.cb = sizeof(STARTUPINFO); - memset(&pi, 0, sizeof (pi)); + memset(&pi, 0, sizeof(pi)); bool batch = util::iendsWith(command, ".bat"); std::string cmdline; std::string cmdexe; // XXX batch handling, in particular quoting, correct? - if(batch) { + if (batch) { const char* p = getenv("windir"); - if(p) { + if (p) { cmdexe = p; cmdexe += "\\system32\\cmd.exe"; - } else { + } + else { A2_LOG_INFO("Failed to get windir environment variable." " Executing batch file will fail."); // TODO Might be useless. @@ -1832,7 +1890,7 @@ void executeHook cmdline += " \""; cmdline += firstFilename; cmdline += "\""; - if(batch) { + if (batch) { cmdline += "\""; } int cmdlineLen = utf8ToWChar(nullptr, 0, cmdline.c_str()); @@ -1842,17 +1900,10 @@ void executeHook assert(cmdlineLen > 0); A2_LOG_INFO(fmt("Executing user command: %s", cmdline.c_str())); DWORD rc = CreateProcessW(batch ? utf8ToWChar(cmdexe).c_str() : nullptr, - wcharCmdline.get(), - nullptr, - nullptr, - true, - 0, - nullptr, - 0, - &si, - &pi); - - if(!rc) { + wcharCmdline.get(), nullptr, nullptr, true, 0, + nullptr, 0, &si, &pi); + + if (!rc) { A2_LOG_ERROR("CreateProcess() failed. Cannot execute user command."); } return; @@ -1862,24 +1913,23 @@ void executeHook } // namespace -void executeHookByOptName -(const std::shared_ptr& group, const Option* option, - PrefPtr pref) +void executeHookByOptName(const std::shared_ptr& group, + const Option* option, PrefPtr pref) { executeHookByOptName(group.get(), option, pref); } -void executeHookByOptName -(const RequestGroup* group, const Option* option, PrefPtr pref) +void executeHookByOptName(const RequestGroup* group, const Option* option, + PrefPtr pref) { const std::string& cmd = option->get(pref); - if(!cmd.empty()) { + if (!cmd.empty()) { const std::shared_ptr dctx = group->getDownloadContext(); std::string firstFilename; size_t numFiles = 0; - if(!group->inMemoryDownload()) { + if (!group->inMemoryDownload()) { std::shared_ptr file = dctx->getFirstRequestedFileEntry(); - if(file) { + if (file) { firstFilename = file->getPath(); } numFiles = dctx->countRequestedFileEntry(); @@ -1888,45 +1938,39 @@ void executeHookByOptName } } -std::string createSafePath -(const std::string& dir, const std::string& filename) +std::string createSafePath(const std::string& dir, const std::string& filename) { - return util::applyDir(dir, util::isUtf8(filename) ? - util::fixTaintedBasename(filename) : - util::escapePath(util::percentEncode(filename)) - ); + return util::applyDir(dir, + util::isUtf8(filename) + ? util::fixTaintedBasename(filename) + : util::escapePath(util::percentEncode(filename))); } std::string createSafePath(const std::string& filename) { - return util::isUtf8(filename) ? - util::fixTaintedBasename(filename) : - util::escapePath(util::percentEncode(filename)); + return util::isUtf8(filename) + ? util::fixTaintedBasename(filename) + : util::escapePath(util::percentEncode(filename)); } std::string encodeNonUtf8(const std::string& s) { - return util::isUtf8(s)?s:util::percentEncode(s); + return util::isUtf8(s) ? s : util::percentEncode(s); } std::string makeString(const char* str) { - if(!str) { + if (!str) { return A2STR::NIL; } return str; } -std::string safeStrerror(int errNum) -{ - return makeString(strerror(errNum)); -} +std::string safeStrerror(int errNum) { return makeString(strerror(errNum)); } -bool noProxyDomainMatch -(const std::string& hostname, - const std::string& domain) +bool noProxyDomainMatch(const std::string& hostname, const std::string& domain) { - if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) { + if (!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) { return util::endsWith(hostname, domain); } return hostname == domain; @@ -1934,40 +1978,36 @@ bool noProxyDomainMatch bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname) { - std::string::const_iterator ptWildcard = std::find(pattern.begin(), - pattern.end(), - '*'); - if(ptWildcard == pattern.end()) { - return strieq(pattern.begin(), pattern.end(), - hostname.begin(), hostname.end()); + std::string::const_iterator ptWildcard = + std::find(pattern.begin(), pattern.end(), '*'); + if (ptWildcard == pattern.end()) { + return strieq(pattern.begin(), pattern.end(), hostname.begin(), + hostname.end()); } - std::string::const_iterator ptLeftLabelEnd = std::find(pattern.begin(), - pattern.end(), - '.'); + std::string::const_iterator ptLeftLabelEnd = + std::find(pattern.begin(), pattern.end(), '.'); bool wildcardEnabled = true; // Do case-insensitive match. At least 2 dots are required to enable // wildcard match. Also wildcard must be in the left-most label. // Don't attempt to match a presented identifier where the wildcard // character is embedded within an A-label. - if(ptLeftLabelEnd == pattern.end() || - std::find(ptLeftLabelEnd+1, pattern.end(), '.') == pattern.end() || - ptLeftLabelEnd < ptWildcard || - istartsWith(pattern, "xn--")) { + if (ptLeftLabelEnd == pattern.end() || + std::find(ptLeftLabelEnd + 1, pattern.end(), '.') == pattern.end() || + ptLeftLabelEnd < ptWildcard || istartsWith(pattern, "xn--")) { wildcardEnabled = false; } - if(!wildcardEnabled) { - return strieq(pattern.begin(), pattern.end(), - hostname.begin(), hostname.end()); + if (!wildcardEnabled) { + return strieq(pattern.begin(), pattern.end(), hostname.begin(), + hostname.end()); } - std::string::const_iterator hnLeftLabelEnd = std::find(hostname.begin(), - hostname.end(), - '.'); - if(!strieq(ptLeftLabelEnd, pattern.end(), hnLeftLabelEnd, hostname.end())) { + std::string::const_iterator hnLeftLabelEnd = + std::find(hostname.begin(), hostname.end(), '.'); + if (!strieq(ptLeftLabelEnd, pattern.end(), hnLeftLabelEnd, hostname.end())) { return false; } // Perform wildcard match. Here '*' must match at least one // character. - if(hnLeftLabelEnd - hostname.begin() < ptLeftLabelEnd - pattern.begin()) { + if (hnLeftLabelEnd - hostname.begin() < ptLeftLabelEnd - pattern.begin()) { return false; } return istartsWith(hostname.begin(), hnLeftLabelEnd, pattern.begin(), @@ -2003,7 +2043,7 @@ bool istartsWith(const std::string& a, const char* b) bool endsWith(const std::string& a, const char* b) { - return endsWith(a.begin(), a.end(), b, b+strlen(b)); + return endsWith(a.begin(), a.end(), b, b + strlen(b)); } bool endsWith(const std::string& a, const std::string& b) @@ -2013,7 +2053,7 @@ bool endsWith(const std::string& a, const std::string& b) bool iendsWith(const std::string& a, const char* b) { - return iendsWith(a.begin(), a.end(), b, b+strlen(b)); + return iendsWith(a.begin(), a.end(), b, b + strlen(b)); } bool iendsWith(const std::string& a, const std::string& b) @@ -2021,24 +2061,21 @@ bool iendsWith(const std::string& a, const std::string& b) return iendsWith(a.begin(), a.end(), b.begin(), b.end()); } -bool strless(const char* a, const char* b) -{ - return strcmp(a, b) < 0; -} +bool strless(const char* a, const char* b) { return strcmp(a, b) < 0; } #ifdef ENABLE_SSL TLSVersion toTLSVersion(const std::string& ver) { - if(ver == A2_V_SSL3) { + if (ver == A2_V_SSL3) { return TLS_PROTO_SSL3; } - if(ver == A2_V_TLS10) { + if (ver == A2_V_TLS10) { return TLS_PROTO_TLS10; } - if(ver == A2_V_TLS11) { + if (ver == A2_V_TLS11) { return TLS_PROTO_TLS11; } - if(ver == A2_V_TLS12) { + if (ver == A2_V_TLS12) { return TLS_PROTO_TLS12; } return TLS_PROTO_TLS10; -- cgit From ea4d99ea087d8f1bafa812017efa26cb90d99947 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 9 Jan 2016 18:16:08 +0900 Subject: Refactor SocketCore::getPeerInfo, getAddrInfo to return Endpoint object --- src/DHTConnectionImpl.cc | 18 ++++++++---------- src/DownloadEngine.cc | 13 ++++++------- src/FtpConnection.cc | 28 +++++++++++----------------- src/FtpNegotiationCommand.cc | 7 +++---- src/HttpListenCommand.cc | 5 ++--- src/InitiateConnectionCommand.cc | 5 ++--- src/LpdMessageReceiver.cc | 10 +++++----- src/NameResolver.cc | 5 ++--- src/PeerListenCommand.cc | 12 ++++-------- src/SocketCore.cc | 19 +++++++------------ src/SocketCore.h | 19 ++++++------------- src/a2netcompat.h | 12 ++++++++++++ src/util.cc | 6 +++--- src/util.h | 3 +-- test/FtpConnectionTest.cc | 4 +--- test/HttpServerTest.cc | 5 ++--- test/LpdMessageDispatcherTest.cc | 4 ++-- test/MSEHandshakeTest.cc | 5 ++--- test/SocketCoreTest.cc | 20 ++++++++++++-------- 19 files changed, 91 insertions(+), 109 deletions(-) (limited to 'src/util.cc') diff --git a/src/DHTConnectionImpl.cc b/src/DHTConnectionImpl.cc index e2ed12f4..5bae0ccf 100644 --- a/src/DHTConnectionImpl.cc +++ b/src/DHTConnectionImpl.cc @@ -77,9 +77,8 @@ bool DHTConnectionImpl::bind(uint16_t& port, const std::string& addr) try { socket_->bind(addr.c_str(), port, family_); socket_->setNonBlockingMode(); - std::pair svaddr; - socket_->getAddrInfo(svaddr); - port = svaddr.second; + auto endpoint = socket_->getAddrInfo(); + port = endpoint.port; A2_LOG_NOTICE(fmt(_("IPv%d DHT: listening on UDP port %u"), ipv, port)); return true; } @@ -92,16 +91,15 @@ bool DHTConnectionImpl::bind(uint16_t& port, const std::string& addr) ssize_t DHTConnectionImpl::receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port) { - std::pair remoteHost; - ssize_t length = socket_->readDataFrom(data, len, remoteHost); + Endpoint remoteEndpoint; + ssize_t length = socket_->readDataFrom(data, len, remoteEndpoint); if (length == 0) { return length; } - else { - host = remoteHost.first; - port = remoteHost.second; - return length; - } + + host = remoteEndpoint.addr; + port = remoteEndpoint.port; + return length; } ssize_t DHTConnectionImpl::sendMessage(const unsigned char* data, size_t len, diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 9ea907ac..72e617c2 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -371,11 +371,10 @@ void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port, } namespace { -bool getPeerInfo(std::pair& res, - const std::shared_ptr& socket) +bool getPeerInfo(Endpoint& res, const std::shared_ptr& socket) { try { - socket->getPeerInfo(res); + res = socket->getPeerInfo(); return true; } catch (RecoverableException& e) { @@ -399,9 +398,9 @@ void DownloadEngine::poolSocket(const std::shared_ptr& request, return; } - std::pair peerInfo; + Endpoint peerInfo; if (getPeerInfo(peerInfo, socket)) { - poolSocket(peerInfo.first, peerInfo.second, A2STR::NIL, 0, socket, + poolSocket(peerInfo.addr, peerInfo.port, A2STR::NIL, 0, socket, std::move(timeout)); } } @@ -421,9 +420,9 @@ void DownloadEngine::poolSocket(const std::shared_ptr& request, return; } - std::pair peerInfo; + Endpoint peerInfo; if (getPeerInfo(peerInfo, socket)) { - poolSocket(peerInfo.first, peerInfo.second, username, A2STR::NIL, 0, socket, + poolSocket(peerInfo.addr, peerInfo.port, username, A2STR::NIL, 0, socket, options, std::move(timeout)); } } diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index f41c5509..5a52b869 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -193,10 +193,9 @@ bool FtpConnection::sendPasv() std::shared_ptr FtpConnection::createServerSocket() { - std::pair addrinfo; - socket_->getAddrInfo(addrinfo); + auto endpoint = socket_->getAddrInfo(); auto serverSocket = std::make_shared(); - serverSocket->bind(addrinfo.first.c_str(), 0, AF_UNSPEC); + serverSocket->bind(endpoint.addr.c_str(), 0, AF_UNSPEC); serverSocket->beginListen(); return serverSocket; } @@ -204,14 +203,10 @@ std::shared_ptr FtpConnection::createServerSocket() bool FtpConnection::sendEprt(const std::shared_ptr& serverSocket) { if (socketBuffer_.sendBufferIsEmpty()) { - sockaddr_union sockaddr; - socklen_t len = sizeof(sockaddr); - serverSocket->getAddrInfo(sockaddr, len); - std::pair addrinfo = - util::getNumericNameInfo(&sockaddr.sa, len); - std::string request = fmt("EPRT |%d|%s|%u|\r\n", - sockaddr.storage.ss_family == AF_INET ? 1 : 2, - addrinfo.first.c_str(), addrinfo.second); + auto endpoint = serverSocket->getAddrInfo(); + auto request = + fmt("EPRT |%d|%s|%u|\r\n", endpoint.family == AF_INET ? 1 : 2, + endpoint.addr.c_str(), endpoint.port); A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } @@ -222,15 +217,14 @@ bool FtpConnection::sendEprt(const std::shared_ptr& serverSocket) bool FtpConnection::sendPort(const std::shared_ptr& serverSocket) { if (socketBuffer_.sendBufferIsEmpty()) { - std::pair addrinfo; - socket_->getAddrInfo(addrinfo); + auto endpoint = socket_->getAddrInfo(); int ipaddr[4]; - sscanf(addrinfo.first.c_str(), "%d.%d.%d.%d", &ipaddr[0], &ipaddr[1], + sscanf(endpoint.addr.c_str(), "%d.%d.%d.%d", &ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]); - serverSocket->getAddrInfo(addrinfo); - std::string request = + auto svEndpoint = serverSocket->getAddrInfo(); + auto request = fmt("PORT %d,%d,%d,%d,%d,%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], - ipaddr[3], addrinfo.second / 256, addrinfo.second % 256); + ipaddr[3], svEndpoint.port / 256, svEndpoint.port % 256); A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); socketBuffer_.pushStr(std::move(request)); } diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index b66e55b9..b2e877c5 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -708,13 +708,12 @@ bool FtpNegotiationCommand::preparePasvConnect() return true; } else { - std::pair dataAddr; - getSocket()->getPeerInfo(dataAddr); + auto endpoint = getSocket()->getPeerInfo(); // make a data connection to the server. - A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), dataAddr.first.c_str(), + A2_LOG_INFO(fmt(MSG_CONNECTING_TO_SERVER, getCuid(), endpoint.addr.c_str(), pasvPort_)); dataSocket_ = std::make_shared(); - dataSocket_->establishConnection(dataAddr.first, pasvPort_, false); + dataSocket_->establishConnection(endpoint.addr, pasvPort_, false); disableReadCheckSocket(); setWriteCheckSocket(dataSocket_); sequence_ = SEQ_SEND_REST_PASV; diff --git a/src/HttpListenCommand.cc b/src/HttpListenCommand.cc index 6dc0d280..7c7794aa 100644 --- a/src/HttpListenCommand.cc +++ b/src/HttpListenCommand.cc @@ -72,11 +72,10 @@ bool HttpListenCommand::execute() if (serverSocket_->isReadable(0)) { std::shared_ptr socket(serverSocket_->acceptConnection()); socket->setTcpNodelay(true); - std::pair peerInfo; - socket->getPeerInfo(peerInfo); + auto endpoint = socket->getPeerInfo(); A2_LOG_INFO(fmt("RPC: Accepted the connection from %s:%u.", - peerInfo.first.c_str(), peerInfo.second)); + endpoint.addr.c_str(), endpoint.port)); e_->setNoWait(true); e_->addCommand( diff --git a/src/InitiateConnectionCommand.cc b/src/InitiateConnectionCommand.cc index 92c32ace..7f32e2a3 100644 --- a/src/InitiateConnectionCommand.cc +++ b/src/InitiateConnectionCommand.cc @@ -124,9 +124,8 @@ void InitiateConnectionCommand::setConnectedAddrInfo( const std::shared_ptr& req, const std::string& hostname, const std::shared_ptr& socket) { - std::pair peerAddr; - socket->getPeerInfo(peerAddr); - req->setConnectedAddrInfo(hostname, peerAddr.first, peerAddr.second); + auto endpoint = socket->getPeerInfo(); + req->setConnectedAddrInfo(hostname, endpoint.addr, endpoint.port); } std::shared_ptr diff --git a/src/LpdMessageReceiver.cc b/src/LpdMessageReceiver.cc index 6c27729a..13a0ebe9 100644 --- a/src/LpdMessageReceiver.cc +++ b/src/LpdMessageReceiver.cc @@ -82,10 +82,10 @@ std::unique_ptr LpdMessageReceiver::receiveMessage() { while (1) { unsigned char buf[200]; - std::pair peerAddr; + Endpoint remoteEndpoint; ssize_t length; try { - length = socket_->readDataFrom(buf, sizeof(buf), peerAddr); + length = socket_->readDataFrom(buf, sizeof(buf), remoteEndpoint); if (length == 0) { return nullptr; } @@ -114,7 +114,7 @@ std::unique_ptr LpdMessageReceiver::receiveMessage() continue; } A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s", - infoHashString.c_str(), port, peerAddr.first.c_str())); + infoHashString.c_str(), port, remoteEndpoint.addr.c_str())); std::string infoHash; if (infoHashString.size() != 40 || (infoHash = util::fromHex(infoHashString.begin(), infoHashString.end())) @@ -122,8 +122,8 @@ std::unique_ptr LpdMessageReceiver::receiveMessage() A2_LOG_INFO(fmt("LPD bad request. infohash=%s", infoHashString.c_str())); continue; } - auto peer = std::make_shared(peerAddr.first, port, false); - if (util::inPrivateAddress(peerAddr.first)) { + auto peer = std::make_shared(remoteEndpoint.addr, port, false); + if (util::inPrivateAddress(remoteEndpoint.addr)) { peer->setLocalPeer(true); } return make_unique(peer, infoHash); diff --git a/src/NameResolver.cc b/src/NameResolver.cc index 6ea3c402..a10eed93 100644 --- a/src/NameResolver.cc +++ b/src/NameResolver.cc @@ -63,9 +63,8 @@ void NameResolver::resolve(std::vector& resolvedAddresses, freeaddrinfo); struct addrinfo* rp; for (rp = res; rp; rp = rp->ai_next) { - std::pair addressPort = - util::getNumericNameInfo(rp->ai_addr, rp->ai_addrlen); - resolvedAddresses.push_back(addressPort.first); + auto endpoint = util::getNumericNameInfo(rp->ai_addr, rp->ai_addrlen); + resolvedAddresses.push_back(endpoint.addr); } } diff --git a/src/PeerListenCommand.cc b/src/PeerListenCommand.cc index d6784b05..96fc5069 100644 --- a/src/PeerListenCommand.cc +++ b/src/PeerListenCommand.cc @@ -93,11 +93,8 @@ uint16_t PeerListenCommand::getPort() const if (!socket_) { return 0; } - else { - std::pair addr; - socket_->getAddrInfo(addr); - return addr.second; - } + + return socket_->getAddrInfo().port; } bool PeerListenCommand::execute() @@ -110,10 +107,9 @@ bool PeerListenCommand::execute() try { peerSocket = socket_->acceptConnection(); peerSocket->applyIpDscp(); - std::pair peerInfo; - peerSocket->getPeerInfo(peerInfo); + auto endpoint = peerSocket->getPeerInfo(); - auto peer = std::make_shared(peerInfo.first, peerInfo.second, true); + auto peer = std::make_shared(endpoint.addr, endpoint.port, true); cuid_t cuid = e_->newCUID(); e_->addCommand( make_unique(cuid, peer, e_, peerSocket)); diff --git a/src/SocketCore.cc b/src/SocketCore.cc index 9eab25a0..49a6dc99 100644 --- a/src/SocketCore.cc +++ b/src/SocketCore.cc @@ -387,13 +387,12 @@ std::shared_ptr SocketCore::acceptConnection() const return sock; } -int SocketCore::getAddrInfo(std::pair& addrinfo) const +Endpoint SocketCore::getAddrInfo() const { sockaddr_union sockaddr; socklen_t len = sizeof(sockaddr); getAddrInfo(sockaddr, len); - addrinfo = util::getNumericNameInfo(&sockaddr.sa, len); - return sockaddr.storage.ss_family; + return util::getNumericNameInfo(&sockaddr.sa, len); } void SocketCore::getAddrInfo(sockaddr_union& sockaddr, socklen_t& len) const @@ -412,7 +411,7 @@ int SocketCore::getAddressFamily() const return sockaddr.storage.ss_family; } -int SocketCore::getPeerInfo(std::pair& peerinfo) const +Endpoint SocketCore::getPeerInfo() const { sockaddr_union sockaddr; socklen_t len = sizeof(sockaddr); @@ -420,8 +419,7 @@ int SocketCore::getPeerInfo(std::pair& peerinfo) const int errNum = SOCKET_ERRNO; throw DL_ABORT_EX(fmt(EX_SOCKET_GET_NAME, errorMsg(errNum).c_str())); } - peerinfo = util::getNumericNameInfo(&sockaddr.sa, len); - return sockaddr.storage.ss_family; + return util::getNumericNameInfo(&sockaddr.sa, len); } void SocketCore::establishConnection(const std::string& host, uint16_t port, @@ -965,9 +963,8 @@ bool SocketCore::tlsHandshake(TLSContext* tlsctx, const std::string& hostname) if (!hostname.empty()) { ss << hostname << " ("; } - std::pair peer; - getPeerInfo(peer); - ss << peer.first << ":" << peer.second; + auto peerEndpoint = getPeerInfo(); + ss << peerEndpoint.addr << ":" << peerEndpoint.port; if (!hostname.empty()) { ss << ")"; } @@ -1219,9 +1216,7 @@ ssize_t SocketCore::writeData(const void* data, size_t len, return r; } -ssize_t SocketCore::readDataFrom( - void* data, size_t len, - std::pair& sender) +ssize_t SocketCore::readDataFrom(void* data, size_t len, Endpoint& sender) { wantRead_ = false; wantWrite_ = false; diff --git a/src/SocketCore.h b/src/SocketCore.h index 2f57da14..48c9c7d2 100644 --- a/src/SocketCore.h +++ b/src/SocketCore.h @@ -169,12 +169,9 @@ public: void beginListen(); /** - * Stores host address and port of this socket to addrinfo and - * returns address family. - * - * @param addrinfo placeholder to store host address and port. + * Returns host address, family and port of this socket. */ - int getAddrInfo(std::pair& addrinfo) const; + Endpoint getAddrInfo() const; /** * Stores address of this socket to sockaddr. len must be @@ -191,12 +188,9 @@ public: int getAddressFamily() const; /** - * Stores peer's address and port to peerinfo and returns address - * family. - * - * @param peerinfo placeholder to store peer's address and port. + * Returns peer's address, family and port. */ - int getPeerInfo(std::pair& peerinfo) const; + Endpoint getPeerInfo() const; /** * Accepts incoming connection on this socket. @@ -288,9 +282,8 @@ public: */ void readData(void* data, size_t& len); - ssize_t readDataFrom( - void* data, size_t len, - std::pair& sender); + // sender.addr will be numerihost assigned. + ssize_t readDataFrom(void* data, size_t len, Endpoint& sender); #ifdef ENABLE_SSL // Performs TLS server side handshake. If handshake is completed, diff --git a/src/a2netcompat.h b/src/a2netcompat.h index 3d7c2d8f..4c5475ec 100644 --- a/src/a2netcompat.h +++ b/src/a2netcompat.h @@ -90,6 +90,8 @@ #include "gai_strerror.h" #endif // HAVE_GAI_STRERROR +#include + #ifdef HAVE_WINSOCK2_H #define sock_t SOCKET #else @@ -120,6 +122,16 @@ struct SockAddr { socklen_t suLength; }; +// Human readable address, family and port. In other words, addr is +// text name, usually obtained from getnameinfo(3). The family field +// is the protocol family if it is known when generating this object. +// If it is unknown, this is AF_UNSPEC. +struct Endpoint { + std::string addr; + int family; + uint16_t port; +}; + #define A2_DEFAULT_IOV_MAX 128 #if defined(IOV_MAX) && IOV_MAX < A2_DEFAULT_IOV_MAX diff --git a/src/util.cc b/src/util.cc index 93d6092f..7294ce91 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1595,8 +1595,7 @@ void* allocateAlignedMemory(size_t alignment, size_t size) } #endif // HAVE_POSIX_MEMALIGN -std::pair -getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len) +Endpoint getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len) { char host[NI_MAXHOST]; char service[NI_MAXSERV]; @@ -1606,7 +1605,8 @@ getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len) throw DL_ABORT_EX( fmt("Failed to get hostname and port. cause: %s", gai_strerror(s))); } - return std::pair(host, atoi(service)); // TODO + return {host, sockaddr->sa_family, + static_cast(strtoul(service, nullptr, 10))}; } std::string htmlEscape(const std::string& src) diff --git a/src/util.h b/src/util.h index 9f067425..15862f2c 100644 --- a/src/util.h +++ b/src/util.h @@ -444,8 +444,7 @@ std::string toString(const std::shared_ptr& binaryStream); void* allocateAlignedMemory(size_t alignment, size_t size); #endif // HAVE_POSIX_MEMALIGN -std::pair -getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len); +Endpoint getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len); std::string htmlEscape(const std::string& src); diff --git a/test/FtpConnectionTest.cc b/test/FtpConnectionTest.cc index fb554e02..698477f9 100644 --- a/test/FtpConnectionTest.cc +++ b/test/FtpConnectionTest.cc @@ -55,9 +55,7 @@ public: listenSocket->bind(0); listenSocket->beginListen(); listenSocket->setBlockingMode(); - std::pair addrinfo; - listenSocket->getAddrInfo(addrinfo); - listenPort_ = addrinfo.second; + listenPort_ = listenSocket->getAddrInfo().port; req_.reset(new Request()); req_->setUri("ftp://localhost/dir%20sp/hello%20world.img"); diff --git a/test/HttpServerTest.cc b/test/HttpServerTest.cc index 8667b827..7f457b7f 100644 --- a/test/HttpServerTest.cc +++ b/test/HttpServerTest.cc @@ -22,11 +22,10 @@ namespace { std::unique_ptr performHttpRequest(SocketCore& server, std::string request) { - std::pair addr; - server.getAddrInfo(addr); + auto endpoint = server.getAddrInfo(); SocketCore client; - client.establishConnection("localhost", addr.second); + client.establishConnection("localhost", endpoint.port); while (!client.isWritable(0)) { } diff --git a/test/LpdMessageDispatcherTest.cc b/test/LpdMessageDispatcherTest.cc index 7245ea7a..e819d5f8 100644 --- a/test/LpdMessageDispatcherTest.cc +++ b/test/LpdMessageDispatcherTest.cc @@ -60,11 +60,11 @@ void LpdMessageDispatcherTest::testSendMessage() unsigned char buf[200]; - std::pair peer; + Endpoint remoteEndpoint; ssize_t nbytes; int trycnt; for (trycnt = 0; trycnt < 5; ++trycnt) { - nbytes = recvsock->readDataFrom(buf, sizeof(buf), peer); + nbytes = recvsock->readDataFrom(buf, sizeof(buf), remoteEndpoint); if (nbytes == 0) { util::sleep(1); } diff --git a/test/MSEHandshakeTest.cc b/test/MSEHandshakeTest.cc index ba50bb95..53ea91f6 100644 --- a/test/MSEHandshakeTest.cc +++ b/test/MSEHandshakeTest.cc @@ -57,9 +57,8 @@ createSocketPair() receiverServerSock.beginListen(); receiverServerSock.setBlockingMode(); - std::pair receiverAddrInfo; - receiverServerSock.getAddrInfo(receiverAddrInfo); - initiatorSock->establishConnection("localhost", receiverAddrInfo.second); + auto endpoint = receiverServerSock.getAddrInfo(); + initiatorSock->establishConnection("localhost", endpoint.port); initiatorSock->setBlockingMode(); std::shared_ptr receiverSock( diff --git a/test/SocketCoreTest.cc b/test/SocketCoreTest.cc index 228295fa..443ca73c 100644 --- a/test/SocketCoreTest.cc +++ b/test/SocketCoreTest.cc @@ -43,26 +43,30 @@ void SocketCoreTest::testWriteAndReadDatagram() SocketCore c(SOCK_DGRAM); c.bind(0); - std::pair svaddr; - s.getAddrInfo(svaddr); + auto remoteEndpoint = s.getAddrInfo(); std::string message1 = "hello world."; - c.writeData(message1.c_str(), message1.size(), "localhost", svaddr.second); + c.writeData(message1.c_str(), message1.size(), "localhost", + remoteEndpoint.port); std::string message2 = "chocolate coated pie"; - c.writeData(message2.c_str(), message2.size(), "localhost", svaddr.second); + c.writeData(message2.c_str(), message2.size(), "localhost", + remoteEndpoint.port); char readbuffer[100]; - std::pair peer; + { - ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer); + ssize_t rlength = + s.readDataFrom(readbuffer, sizeof(readbuffer), remoteEndpoint); // commented out because ip address may vary - // CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"), peer.first); + // CPPUNIT_ASSERT_EQUAL(std::std::string("127.0.0.1"), + // remoteEndpoint.addr); CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength); readbuffer[rlength] = '\0'; CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer)); } { - ssize_t rlength = s.readDataFrom(readbuffer, sizeof(readbuffer), peer); + ssize_t rlength = + s.readDataFrom(readbuffer, sizeof(readbuffer), remoteEndpoint); CPPUNIT_ASSERT_EQUAL((ssize_t)message2.size(), rlength); readbuffer[rlength] = '\0'; CPPUNIT_ASSERT_EQUAL(message2, std::string(readbuffer)); -- cgit From aa06a17fda0776436da36a6a1359ed24d8198c4c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 10 Jan 2016 16:41:57 +0900 Subject: Refactor HttpRequest This also makes comparison performed when replacing builtin header field with user-defined one case-insensitive. --- src/HttpRequest.cc | 167 ++++++++++++++++++++++++++--------------------------- src/HttpRequest.h | 12 ++-- src/util.cc | 5 ++ src/util.h | 1 + 4 files changed, 94 insertions(+), 91 deletions(-) (limited to 'src/util.cc') diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index b8bc630e..42359c88 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -74,14 +74,14 @@ HttpRequest::HttpRequest() HttpRequest::~HttpRequest() {} -void HttpRequest::setSegment(const std::shared_ptr& segment) +void HttpRequest::setSegment(std::shared_ptr segment) { - segment_ = segment; + segment_ = std::move(segment); } -void HttpRequest::setRequest(const std::shared_ptr& request) +void HttpRequest::setRequest(std::shared_ptr request) { - request_ = request; + request_ = std::move(request); } int64_t HttpRequest::getStartByte() const @@ -89,9 +89,8 @@ int64_t HttpRequest::getStartByte() const if (!segment_) { return 0; } - else { - return fileEntry_->gtoloff(segment_->getPositionToWrite()); - } + + return fileEntry_->gtoloff(segment_->getPositionToWrite()); } int64_t HttpRequest::getEndByte() const @@ -100,8 +99,8 @@ int64_t HttpRequest::getEndByte() const return 0; } if (request_->isPipeliningEnabled()) { - int64_t endByte = fileEntry_->gtoloff(segment_->getPosition() + - segment_->getLength() - 1); + auto endByte = fileEntry_->gtoloff(segment_->getPosition() + + segment_->getLength() - 1); return std::min(endByte, fileEntry_->getLength() - 1); } if (endOffsetOverride_ > 0) { @@ -124,22 +123,19 @@ bool HttpRequest::isRangeSatisfied(const Range& range) const if (!segment_) { return true; } - if ((getStartByte() == range.startByte) && - ((getEndByte() == 0) || (getEndByte() == range.endByte)) && - ((fileEntry_->getLength() == 0) || - (fileEntry_->getLength() == range.entityLength))) { - return true; - } - return false; + return getStartByte() == range.startByte && + (getEndByte() == 0 || getEndByte() == range.endByte) && + (fileEntry_->getLength() == 0 || + fileEntry_->getLength() == range.entityLength); } namespace { std::string getHostText(const std::string& host, uint16_t port) { - std::string hosttext = host; + auto hosttext = host; if (!(port == 80 || port == 443)) { - hosttext += fmt(":%u", port); - ; + hosttext += ':'; + hosttext += util::uitos(port); } return hosttext; } @@ -148,15 +144,15 @@ std::string getHostText(const std::string& host, uint16_t port) std::string HttpRequest::createRequest() { authConfig_ = authConfigFactory_->createAuthConfig(request_, option_); - std::string requestLine = request_->getMethod(); - requestLine += " "; + auto requestLine = request_->getMethod(); + requestLine += ' '; if (proxyRequest_) { if (getProtocol() == "ftp" && request_->getUsername().empty() && authConfig_) { // Insert user into URI, like ftp://USER@host/ - std::string uri = getCurrentURI(); + auto uri = getCurrentURI(); assert(uri.size() >= 6); - uri.insert(6, util::percentEncode(authConfig_->getUser()) + "@"); + uri.insert(6, util::percentEncode(authConfig_->getUser()) + '@'); requestLine += uri; } else { @@ -172,17 +168,17 @@ std::string HttpRequest::createRequest() std::vector> builtinHds; builtinHds.reserve(20); - builtinHds.push_back(std::make_pair("User-Agent:", userAgent_)); + builtinHds.emplace_back("User-Agent:", userAgent_); std::string acceptTypes = "*/*"; if (acceptMetalink_) { // The mime types of Metalink are used for "transparent metalink". - const char** metalinkTypes = getMetalinkContentTypes(); + const auto metalinkTypes = getMetalinkContentTypes(); for (size_t i = 0; metalinkTypes[i]; ++i) { - acceptTypes += ","; + acceptTypes += ','; acceptTypes += metalinkTypes[i]; } } - builtinHds.push_back(std::make_pair("Accept:", acceptTypes)); + builtinHds.emplace_back("Accept:", acceptTypes); if (contentEncodingEnabled_) { std::string acceptableEncodings; #ifdef HAVE_ZLIB @@ -191,65 +187,65 @@ std::string HttpRequest::createRequest() } #endif // HAVE_ZLIB if (!acceptableEncodings.empty()) { - builtinHds.push_back( - std::make_pair("Accept-Encoding:", acceptableEncodings)); + builtinHds.emplace_back("Accept-Encoding:", acceptableEncodings); } } - builtinHds.push_back( - std::make_pair("Host:", getHostText(getURIHost(), getPort()))); + builtinHds.emplace_back("Host:", getHostText(getURIHost(), getPort())); if (noCache_) { - builtinHds.push_back(std::make_pair("Pragma:", "no-cache")); - builtinHds.push_back(std::make_pair("Cache-Control:", "no-cache")); + builtinHds.emplace_back("Pragma:", "no-cache"); + builtinHds.emplace_back("Cache-Control:", "no-cache"); } if (!request_->isKeepAliveEnabled() && !request_->isPipeliningEnabled()) { - builtinHds.push_back(std::make_pair("Connection:", "close")); + builtinHds.emplace_back("Connection:", "close"); } if (segment_ && segment_->getLength() > 0 && (request_->isPipeliningEnabled() || getStartByte() > 0 || getEndByte() > 0)) { - std::string rangeHeader(fmt("bytes=%" PRId64 "-", getStartByte())); + std::string rangeHeader = "bytes="; + rangeHeader += util::uitos(getStartByte()); + rangeHeader += '-'; + if (request_->isPipeliningEnabled() || getEndByte() > 0) { // FTP via http proxy does not support endbytes, but in that // case, request_->isPipeliningEnabled() is false and // getEndByte() is 0. rangeHeader += util::itos(getEndByte()); } - builtinHds.push_back(std::make_pair("Range:", rangeHeader)); + builtinHds.emplace_back("Range:", rangeHeader); } if (proxyRequest_) { if (request_->isKeepAliveEnabled() || request_->isPipeliningEnabled()) { - builtinHds.push_back(std::make_pair("Connection:", "Keep-Alive")); + builtinHds.emplace_back("Connection:", "Keep-Alive"); } } if (proxyRequest_ && !proxyRequest_->getUsername().empty()) { builtinHds.push_back(getProxyAuthString()); } if (authConfig_) { - std::string authText = authConfig_->getAuthText(); + auto authText = authConfig_->getAuthText(); std::string val = "Basic "; - val += base64::encode(authText.begin(), authText.end()); - builtinHds.push_back(std::make_pair("Authorization:", val)); + val += base64::encode(std::begin(authText), std::end(authText)); + builtinHds.emplace_back("Authorization:", val); } if (!request_->getReferer().empty()) { - builtinHds.push_back(std::make_pair("Referer:", request_->getReferer())); + builtinHds.emplace_back("Referer:", request_->getReferer()); } if (cookieStorage_) { std::string cookiesValue; - std::string path = getDir(); + auto path = getDir(); path += getFile(); auto cookies = cookieStorage_->criteriaFind( getHost(), path, Time().getTimeFromEpoch(), getProtocol() == "https"); for (auto c : cookies) { cookiesValue += c->toString(); - cookiesValue += ";"; + cookiesValue += ';'; } if (!cookiesValue.empty()) { - builtinHds.push_back(std::make_pair("Cookie:", cookiesValue)); + builtinHds.emplace_back("Cookie:", cookiesValue); } } if (!ifModSinceHeader_.empty()) { - builtinHds.push_back( - std::make_pair("If-Modified-Since:", ifModSinceHeader_)); + builtinHds.emplace_back("If-Modified-Since:", ifModSinceHeader_); } if (!noWantDigest_) { // Send Want-Digest header field with only limited hash algorithms: @@ -269,29 +265,21 @@ std::string HttpRequest::createRequest() builtinHds.emplace_back("Want-Digest:", wantDigest); } } - for (std::vector>::const_iterator - i = builtinHds.begin(), - eoi = builtinHds.end(); - i != eoi; ++i) { - std::vector::const_iterator j = headers_.begin(); - std::vector::const_iterator jend = headers_.end(); - for (; j != jend; ++j) { - if (util::startsWith(*j, (*i).first)) { - break; - } - } - if (j == jend) { - requestLine += (*i).first; - requestLine += " "; - requestLine += (*i).second; + for (const auto& builtinHd : builtinHds) { + auto it = std::find_if(std::begin(headers_), std::end(headers_), + [&builtinHd](const std::string& hd) { + return util::istartsWith(hd, builtinHd.first); + }); + if (it == std::end(headers_)) { + requestLine += builtinHd.first; + requestLine += ' '; + requestLine += builtinHd.second; requestLine += "\r\n"; } } // append additional headers given by user. - for (std::vector::const_iterator i = headers_.begin(), - eoi = headers_.end(); - i != eoi; ++i) { - requestLine += *i; + for (const auto& hd : headers_) { + requestLine += hd; requestLine += "\r\n"; } requestLine += "\r\n"; @@ -301,30 +289,39 @@ std::string HttpRequest::createRequest() std::string HttpRequest::createProxyRequest() const { assert(proxyRequest_); - std::string requestLine(fmt("CONNECT %s:%u HTTP/1.1\r\n" - "User-Agent: %s\r\n" - "Host: %s:%u\r\n", - getURIHost().c_str(), getPort(), - userAgent_.c_str(), getURIHost().c_str(), - getPort())); + + std::string requestLine = "CONNECT "; + requestLine += getURIHost(); + requestLine += ':'; + requestLine += util::uitos(getPort()); + requestLine += " HTTP/1.1\r\nUser-Agent: "; + requestLine += userAgent_; + requestLine += "\r\nHost: "; + requestLine += getURIHost(); + requestLine += ':'; + requestLine += util::uitos(getPort()); + requestLine += "\r\n"; + if (!proxyRequest_->getUsername().empty()) { - std::pair auth = getProxyAuthString(); + auto auth = getProxyAuthString(); requestLine += auth.first; - requestLine += " "; + requestLine += ' '; requestLine += auth.second; requestLine += "\r\n"; } + requestLine += "\r\n"; + return requestLine; } std::pair HttpRequest::getProxyAuthString() const { - std::string authText = proxyRequest_->getUsername(); - authText += ":"; + auto authText = proxyRequest_->getUsername(); + authText += ':'; authText += proxyRequest_->getPassword(); std::string val = "Basic "; - val += base64::encode(authText.begin(), authText.end()); + val += base64::encode(std::begin(authText), std::end(authText)); return std::make_pair("Proxy-Authorization:", val); } @@ -334,7 +331,7 @@ void HttpRequest::disableContentEncoding() { contentEncodingEnabled_ = false; } void HttpRequest::addHeader(const std::string& headersString) { - util::split(headersString.begin(), headersString.end(), + util::split(std::begin(headersString), std::end(headersString), std::back_inserter(headers_), '\n', true); } @@ -354,9 +351,9 @@ void HttpRequest::setAuthConfigFactory(AuthConfigFactory* factory) void HttpRequest::setOption(const Option* option) { option_ = option; } -void HttpRequest::setProxyRequest(const std::shared_ptr& proxyRequest) +void HttpRequest::setProxyRequest(std::shared_ptr proxyRequest) { - proxyRequest_ = proxyRequest; + proxyRequest_ = std::move(proxyRequest); } bool HttpRequest::isProxyRequestSet() const { return proxyRequest_.get(); } @@ -404,19 +401,19 @@ const std::string& HttpRequest::getQuery() const std::string HttpRequest::getURIHost() const { return request_->getURIHost(); } -void HttpRequest::setUserAgent(const std::string& userAgent) +void HttpRequest::setUserAgent(std::string userAgent) { - userAgent_ = userAgent; + userAgent_ = std::move(userAgent); } -void HttpRequest::setFileEntry(const std::shared_ptr& fileEntry) +void HttpRequest::setFileEntry(std::shared_ptr fileEntry) { - fileEntry_ = fileEntry; + fileEntry_ = std::move(fileEntry); } -void HttpRequest::setIfModifiedSinceHeader(const std::string& hd) +void HttpRequest::setIfModifiedSinceHeader(std::string value) { - ifModSinceHeader_ = hd; + ifModSinceHeader_ = std::move(value); } bool HttpRequest::conditionalRequest() const diff --git a/src/HttpRequest.h b/src/HttpRequest.h index 63260709..b461fbe4 100644 --- a/src/HttpRequest.h +++ b/src/HttpRequest.h @@ -109,9 +109,9 @@ public: const std::shared_ptr& getSegment() const { return segment_; } - void setSegment(const std::shared_ptr& segment); + void setSegment(std::shared_ptr segment); - void setRequest(const std::shared_ptr& request); + void setRequest(std::shared_ptr request); int64_t getEntityLength() const; @@ -166,7 +166,7 @@ public: void disableContentEncoding(); - void setUserAgent(const std::string& userAgent); + void setUserAgent(std::string userAgent); // accepts multiline headers, delimited by LF void addHeader(const std::string& headers); @@ -188,7 +188,7 @@ public: * To use proxy, pass proxy string to Request::setUri() and set it this * object. */ - void setProxyRequest(const std::shared_ptr& proxyRequest); + void setProxyRequest(std::shared_ptr proxyRequest); /* * Returns true if non-Null proxy request is set by setProxyRequest(). @@ -204,7 +204,7 @@ public: // createRequest(). const std::unique_ptr& getAuthConfig() const; - void setFileEntry(const std::shared_ptr& fileEntry); + void setFileEntry(std::shared_ptr fileEntry); const std::shared_ptr& getFileEntry() const { return fileEntry_; } @@ -220,7 +220,7 @@ public: void setEndOffsetOverride(int64_t offset) { endOffsetOverride_ = offset; } - void setIfModifiedSinceHeader(const std::string& hd); + void setIfModifiedSinceHeader(std::string value); const std::string& getIfModifiedSinceHeader() const { diff --git a/src/util.cc b/src/util.cc index 7294ce91..5353ff04 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2041,6 +2041,11 @@ bool istartsWith(const std::string& a, const char* b) return istartsWith(a.begin(), a.end(), b); } +bool istartsWith(const std::string& a, const std::string& b) +{ + return istartsWith(std::begin(a), std::end(a), std::begin(b), std::end(b)); +} + bool endsWith(const std::string& a, const char* b) { return endsWith(a.begin(), a.end(), b, b + strlen(b)); diff --git a/src/util.h b/src/util.h index 15862f2c..e7ebb816 100644 --- a/src/util.h +++ b/src/util.h @@ -674,6 +674,7 @@ bool istartsWith(InputIterator first, InputIterator last, const char* b) } bool istartsWith(const std::string& a, const char* b); +bool istartsWith(const std::string& a, const std::string& b); template bool endsWith(InputIterator1 first1, InputIterator1 last1, -- cgit From 25243da03917b7e36a96a3d28bf99cb578501602 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 28 Jan 2016 22:25:46 +0900 Subject: mingw: Add formatLastError as wrapper function for FormatMessage --- src/AbstractDiskWriter.cc | 12 +++++------- src/SocketCore.cc | 11 +++++------ src/util.cc | 18 ++++++++++++++++++ src/util.h | 7 +++++++ 4 files changed, 35 insertions(+), 13 deletions(-) (limited to 'src/util.cc') diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index 931ee88a..b3b4bd8d 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -93,15 +93,13 @@ namespace { std::string fileStrerror(int errNum) { #ifdef __MINGW32__ - static char buf[256]; - if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - 0, errNum, - // Default language - MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR)&buf, - sizeof(buf), 0) == 0) { + auto msg = util::formatLastError(errNum); + if (msg.empty()) { + char buf[256]; snprintf(buf, sizeof(buf), "File I/O error %x", errNum); + return buf; } - return buf; + return msg; #else // !__MINGW32__ return util::safeStrerror(errNum); #endif // !__MINGW32__ diff --git a/src/SocketCore.cc b/src/SocketCore.cc index 8358527e..50e974f5 100644 --- a/src/SocketCore.cc +++ b/src/SocketCore.cc @@ -105,14 +105,13 @@ std::string errorMsg(int errNum) #ifndef __MINGW32__ return util::safeStrerror(errNum); #else - static char buf[256]; - if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, errNum, - MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR)&buf, - sizeof(buf), nullptr) == 0) { + auto msg = util::formatLastError(errNum); + if (msg.empty()) { + char buf[256]; snprintf(buf, sizeof(buf), EX_SOCKET_UNKNOWN_ERROR, errNum, errNum); + return buf; } - return buf; + return msg; #endif // __MINGW32__ } } // namespace diff --git a/src/util.cc b/src/util.cc index 5353ff04..7cfbf237 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2087,6 +2087,24 @@ TLSVersion toTLSVersion(const std::string& ver) } #endif // ENABLE_SSL + +#ifdef __MINGW32__ +std::string formatLastError(int errNum) +{ + std::array buf; + if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, errNum, + // Default language + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + static_cast(buf.data()), buf.size(), + nullptr) == 0) { + return ""; + } + + return buf.data(); +} +#endif // __MINGW32__ + } // namespace util } // namespace aria2 diff --git a/src/util.h b/src/util.h index e7ebb816..fe802af4 100644 --- a/src/util.h +++ b/src/util.h @@ -855,6 +855,13 @@ bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname); TLSVersion toTLSVersion(const std::string& ver); #endif // ENABLE_SSL +#ifdef __MINGW32__ +// Formats error message for error code errNum, which is the return +// value of GetLastError(). On error, this function returns empty +// string. +std::string formatLastError(int errNum); +#endif // __MINGW32__ + } // namespace util } // namespace aria2 -- cgit From 5e48e01bf9941d1d257ee9b5c79142b2d43d9af1 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 8 Feb 2016 00:09:17 +0900 Subject: Don't inherit file descriptor to child process For non-Mingw32 build, set FD_CLOEXEC to file descriptors which live beyond function scope. For Mingw32 build, just pass false to bInheritHandles parameter of CreateProcessW. --- src/AbstractDiskWriter.cc | 1 + src/SocketCore.cc | 3 +++ src/util.cc | 22 ++++++++++++++++++++-- src/util.h | 5 +++++ 4 files changed, 29 insertions(+), 2 deletions(-) (limited to 'src/util.cc') diff --git a/src/AbstractDiskWriter.cc b/src/AbstractDiskWriter.cc index 2420b6a8..4d62bf97 100644 --- a/src/AbstractDiskWriter.cc +++ b/src/AbstractDiskWriter.cc @@ -221,6 +221,7 @@ int openFileWithFlags(const std::string& filename, int flags, util::safeStrerror(errNum).c_str()), errCode); } + util::make_fd_cloexec(fd); #if defined(__APPLE__) && defined(__MACH__) // This may reduce memory consumption on Mac OS X. fcntl(fd, F_NOCACHE, 1); diff --git a/src/SocketCore.cc b/src/SocketCore.cc index 3f04ee54..e73f5dcd 100644 --- a/src/SocketCore.cc +++ b/src/SocketCore.cc @@ -202,6 +202,7 @@ void SocketCore::create(int family, int protocol) throw DL_ABORT_EX( fmt("Failed to create socket. Cause:%s", errorMsg(errNum).c_str())); } + util::make_fd_cloexec(fd); int sockopt = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (a2_sockopt_t)&sockopt, sizeof(sockopt)) < 0) { @@ -227,6 +228,7 @@ static sock_t bindInternal(int family, int socktype, int protocol, error = errorMsg(errNum); return -1; } + util::make_fd_cloexec(fd); int sockopt = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (a2_sockopt_t)&sockopt, sizeof(sockopt)) < 0) { @@ -445,6 +447,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port, error = errorMsg(errNum); continue; } + util::make_fd_cloexec(fd); int sockopt = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (a2_sockopt_t)&sockopt, sizeof(sockopt)) < 0) { diff --git a/src/util.cc b/src/util.cc index 7cfbf237..1d9d8bbf 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1900,7 +1900,7 @@ void executeHook(const std::string& command, a2_gid_t gid, size_t numFiles, assert(cmdlineLen > 0); A2_LOG_INFO(fmt("Executing user command: %s", cmdline.c_str())); DWORD rc = CreateProcessW(batch ? utf8ToWChar(cmdexe).c_str() : nullptr, - wcharCmdline.get(), nullptr, nullptr, true, 0, + wcharCmdline.get(), nullptr, nullptr, false, 0, nullptr, 0, &si, &pi); if (!rc) { @@ -2087,7 +2087,6 @@ TLSVersion toTLSVersion(const std::string& ver) } #endif // ENABLE_SSL - #ifdef __MINGW32__ std::string formatLastError(int errNum) { @@ -2105,6 +2104,25 @@ std::string formatLastError(int errNum) } #endif // __MINGW32__ +void make_fd_cloexec(int fd) +{ +#ifndef __MINGW32__ + int flags; + + // TODO from linux man page, fcntl() with F_GETFD or F_SETFD does + // not return -1 with errno == EINTR. Historically, aria2 code base + // checks this case. Probably, it is not needed. + while ((flags = fcntl(fd, F_GETFD)) == -1 && errno == EINTR) + ; + if (flags == -1) { + return; + } + + while (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1 && errno == EINTR) + ; +#endif // !__MINGW32__ +} + } // namespace util } // namespace aria2 diff --git a/src/util.h b/src/util.h index fe802af4..14b5d146 100644 --- a/src/util.h +++ b/src/util.h @@ -862,6 +862,11 @@ TLSVersion toTLSVersion(const std::string& ver); std::string formatLastError(int errNum); #endif // __MINGW32__ +// Sets file descriptor file FD_CLOEXEC to |fd|. This function is +// noop for Mingw32 build, since we disable inheritance in +// CreateProcess call. +void make_fd_cloexec(int fd); + } // namespace util } // namespace aria2 -- cgit