679
for(auto fi = fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
680
if((*fi)->getLength() == 0) {
676
for (const auto& fi: fileEntries) {
677
if(fi->getLength() == 0) {
684
((*fi)->getOffset()+std::min(head, (*fi)->getLength())-1)/pieceLength;
685
for(size_t index = (*fi)->getOffset()/pieceLength;
686
index <= lastIndex; ++index) {
687
indexes.push_back(index);
680
const size_t lastIndex = (fi->getOffset() +
681
std::min(head, fi->getLength()) - 1
683
for(size_t idx = fi->getOffset() / pieceLength; idx <= lastIndex; ++idx) {
684
indexes.push_back(idx);
703
for(auto fi = fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
704
if((*fi)->getLength() == 0) {
700
for (const auto& fi: fileEntries) {
701
if(fi->getLength() == 0) {
707
int64_t endOffset = (*fi)->getLastOffset();
709
(endOffset-1-(std::min(tail, (*fi)->getLength())-1))/pieceLength;
710
for(size_t index = fromIndex; index <= (endOffset-1)/pieceLength;
712
indexes.push_back(index);
704
int64_t endOffset = fi->getLastOffset();
705
size_t fromIndex = (endOffset - 1 - (std::min(tail, fi->getLength()) - 1)) /
707
const size_t toIndex = (endOffset - 1) / pieceLength;
708
while (fromIndex <= toIndex) {
709
indexes.push_back(fromIndex++);
724
721
std::vector<size_t> indexes;
725
722
std::vector<Scip> parts;
726
723
splitIter(src.begin(), src.end(), std::back_inserter(parts), ',', true);
727
for(std::vector<Scip>::const_iterator i = parts.begin(),
728
eoi = parts.end(); i != eoi; ++i) {
729
if(util::streq((*i).first, (*i).second, "head")) {
724
for (const auto& i: parts) {
725
if(util::streq(i.first, i.second, "head")) {
730
726
computeHeadPieces(indexes, fileEntries, pieceLength, defaultSize);
731
} else if(util::startsWith((*i).first, (*i).second, "head=")) {
732
std::string sizestr((*i).first+5, (*i).second);
728
else if(util::startsWith(i.first, i.second, "head=")) {
729
std::string sizestr(i.first + 5, i.second);
733
730
computeHeadPieces(indexes, fileEntries, pieceLength,
734
731
std::max((int64_t)0, getRealSize(sizestr)));
735
} else if(util::streq((*i).first, (*i).second, "tail")) {
733
else if(util::streq(i.first, i.second, "tail")) {
736
734
computeTailPieces(indexes, fileEntries, pieceLength, defaultSize);
737
} else if(util::startsWith((*i).first, (*i).second, "tail=")) {
738
std::string sizestr((*i).first+5, (*i).second);
736
else if(util::startsWith(i.first, i.second, "tail=")) {
737
std::string sizestr(i.first + 5, i.second);
739
738
computeTailPieces(indexes, fileEntries, pieceLength,
740
739
std::max((int64_t)0, getRealSize(sizestr)));
742
742
throw DL_ABORT_EX(fmt("Unrecognized token %s",
743
std::string((*i).first, (*i).second).c_str()));
743
std::string(i.first, i.second).c_str()));
746
746
std::sort(indexes.begin(), indexes.end());
1177
1177
header.c_str(), header.size());
1183
if(!charset || strieq(charset, charset+charsetlen, "iso-8859-1")) {
1184
res = iso8859p1ToUtf8(cdval, rv);
1182
if(!charset || strieq(charset, charset+charsetlen, "iso-8859-1")) {
1183
res = iso8859p1ToUtf8(cdval, rv);
1185
res.assign(cdval, rv);
1187
if(!detectDirTraversal(res) &&
1188
res.find_first_of("/\\") == std::string::npos) {
1186
res.assign(cdval, rv);
1188
if(!detectDirTraversal(res) &&
1189
res.find_first_of("/\\") == std::string::npos) {
1196
1195
std::string toUpper(std::string src)
1474
1472
if(!dir.mkdirs()) {
1475
1473
int errNum = errno;
1476
1474
if(!dir.isDir()) {
1479
fmt(EX_MAKE_DIR, dir.getPath().c_str(),
1480
safeStrerror(errNum).c_str()),
1481
error_code::DIR_CREATE_ERROR);
1475
throw DL_ABORT_EX3(errNum, fmt(EX_MAKE_DIR, dir.getPath().c_str(),
1476
safeStrerror(errNum).c_str()),
1477
error_code::DIR_CREATE_ERROR);
1520
1516
if((res = posix_memalign(&buffer, alignment, size)) != 0) {
1521
throw FATAL_EXCEPTION
1522
(fmt("Error in posix_memalign: %s",
1523
util::safeStrerror(res).c_str()));
1517
throw FATAL_EXCEPTION(fmt("Error in posix_memalign: %s",
1518
util::safeStrerror(res).c_str()));
1597
1592
void generateRandomDataRandom(unsigned char* data, size_t length)
1599
1594
const auto& rd = SimpleRandomizer::getInstance();
1600
for(size_t i = 0; i < length; ++i) {
1601
data[i] = static_cast<unsigned long>(rd->getRandomNumber(256));
1595
rd->getRandomBytes(data, length);
1728
1723
if(s.empty()) {
1731
for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
1732
unsigned char c = *i;
1733
if(in(c, 0x00u, 0x1fu) || c == 0x7fu) {
1727
unsigned char ch = c;
1728
if (in(ch, 0x00u, 0x1fu) || ch == 0x7fu) {
1737
1732
return s == "." || s == ".." || s[0] == '/' ||
1738
util::startsWith(s, "./") || util::startsWith(s, "../") ||
1733
util::startsWith(s, "./") ||
1734
util::startsWith(s, "../") ||
1739
1735
s.find("/../") != std::string::npos ||
1740
1736
s.find("/./") != std::string::npos ||
1741
1737
s[s.size()-1] == '/' ||
1742
util::endsWith(s, "/.") || util::endsWith(s, "/..");
1738
util::endsWith(s, "/.") ||
1739
util::endsWith(s, "/..");
1745
1742
std::string escapePath(const std::string& s)
1750
1747
{ '"', '*', ':', '<', '>', '?', '\\', '|' };
1751
1748
#endif // __MINGW32__
1753
for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
1754
unsigned char c = *i;
1751
unsigned char c = cc;
1755
1752
if(in(c, 0x00u, 0x1fu) || c == 0x7fu
1756
1753
#ifdef __MINGW32__
1757
1754
|| std::find(std::begin(WIN_INVALID_PATH_CHARS),
1886
1893
void executeHookByOptName
1887
1894
(const std::shared_ptr<RequestGroup>& group, const Option* option,
1890
1897
executeHookByOptName(group.get(), option, pref);
1893
1900
void executeHookByOptName
1894
(const RequestGroup* group, const Option* option, const Pref* pref)
1901
(const RequestGroup* group, const Option* option, PrefPtr pref)
1896
1903
const std::string& cmd = option->get(pref);
1897
1904
if(!cmd.empty()) {
1912
1919
std::string createSafePath
1913
1920
(const std::string& dir, const std::string& filename)
1915
return util::applyDir
1917
util::isUtf8(filename)?
1918
util::fixTaintedBasename(filename):
1919
util::escapePath(util::percentEncode(filename)));
1922
return util::applyDir(dir, util::isUtf8(filename) ?
1923
util::fixTaintedBasename(filename) :
1924
util::escapePath(util::percentEncode(filename))
1922
1928
std::string encodeNonUtf8(const std::string& s)
1945
1950
if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) {
1946
1951
return util::endsWith(hostname, domain);
1948
return hostname == domain;
1953
return hostname == domain;
1952
1956
bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname)
1987
1991
if(hnLeftLabelEnd - hostname.begin() < ptLeftLabelEnd - pattern.begin()) {
1991
istartsWith(hostname.begin(), hnLeftLabelEnd,
1992
pattern.begin(), ptWildcard) &&
1993
iendsWith(hostname.begin(), hnLeftLabelEnd,
1994
ptWildcard+1, ptLeftLabelEnd);
1994
return istartsWith(hostname.begin(), hnLeftLabelEnd, pattern.begin(),
1996
iendsWith(hostname.begin(), hnLeftLabelEnd, ptWildcard + 1,
1997
2000
bool strieq(const std::string& a, const char* b)