2010-01-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Replaced '/' and '_' with '_' in HTTP/FTP filename.
	* src/A2STR.cc
	* src/A2STR.h
	* src/FtpNegotiationCommand.cc
	* src/HttpResponseCommand.cc
	* src/util.cc
	* src/util.h
	* test/UtilTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2010-01-11 14:32:20 +00:00
parent 4c89170488
commit 768f78f771
8 changed files with 47 additions and 2 deletions

View File

@ -1,3 +1,14 @@
2010-01-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Replaced '/' and '_' with '_' in HTTP/FTP filename.
* src/A2STR.cc
* src/A2STR.h
* src/FtpNegotiationCommand.cc
* src/HttpResponseCommand.cc
* src/util.cc
* src/util.h
* test/UtilTest.cc
2010-01-11 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Treat --dir="" as --dir="."

View File

@ -56,4 +56,8 @@ const std::string A2STR::SEMICOLON_C(";");
const std::string A2STR::EQUAL_C("=");
const std::string A2STR::UNDERSCORE_C("_");
const std::string A2STR::BACK_SLASH_C("\\");
} // namespace aria2

View File

@ -62,6 +62,10 @@ public:
static const std::string SEMICOLON_C;
static const std::string EQUAL_C;
static const std::string UNDERSCORE_C;
static const std::string BACK_SLASH_C;
};
} // namespace aria2

View File

@ -337,7 +337,8 @@ bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength)
if(_fileEntry->getPath().empty()) {
_fileEntry->setPath
(util::applyDir
(getDownloadContext()->getDir(), util::urldecode(req->getFile())));
(getDownloadContext()->getDir(),
util::fixTaintedBasename(util::urldecode(req->getFile()))));
}
_requestGroup->preDownloadProcessing();
if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {

View File

@ -132,7 +132,8 @@ bool HttpResponseCommand::executeInternal()
if(_fileEntry->getPath().empty()) {
_fileEntry->setPath
(util::applyDir
(getDownloadContext()->getDir(), httpResponse->determinFilename()));
(getDownloadContext()->getDir(),
util::fixTaintedBasename(httpResponse->determinFilename())));
}
_fileEntry->setContentType(httpResponse->getContentType());
_requestGroup->preDownloadProcessing();

View File

@ -1011,6 +1011,12 @@ std::string applyDir(const std::string& dir, const std::string& relPath)
}
}
std::string fixTaintedBasename(const std::string& src)
{
return replace(replace(src, A2STR::SLASH_C, A2STR::UNDERSCORE_C),
A2STR::BACK_SLASH_C, A2STR::UNDERSCORE_C);
}
} // namespace util
} // namespace aria2

View File

@ -362,6 +362,15 @@ bool saveAs
// dir = "/", relPath = "foo" => "/foo"
std::string applyDir(const std::string& dir, const std::string& relPath);
// In HTTP/FTP, file name is file component in URI. In HTTP, filename
// may be a value of Content-Disposition header. They are likely
// percent encoded. If they contains, for example, %2F, when decoded,
// basename contains dir component. This should be avoided. This
// function is created to fix these issues. This function expects src
// should be non-percent-encoded basename. Currently, this function
// replaces '/' and '\' with '_'.
std::string fixTaintedBasename(const std::string& src);
} // namespace util
} // namespace aria2

View File

@ -59,6 +59,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testFromHex);
CPPUNIT_TEST(testParsePrioritizePieceRange);
CPPUNIT_TEST(testApplyDir);
CPPUNIT_TEST(testFixTaintedBasename);
CPPUNIT_TEST_SUITE_END();
private:
@ -106,6 +107,7 @@ public:
void testFromHex();
void testParsePrioritizePieceRange();
void testApplyDir();
void testFixTaintedBasename();
};
@ -910,4 +912,11 @@ void UtilTest::testApplyDir()
CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
}
void UtilTest::testFixTaintedBasename()
{
CPPUNIT_ASSERT_EQUAL(std::string("a_b"), util::fixTaintedBasename("a/b"));
CPPUNIT_ASSERT_EQUAL(std::string("a_b"), util::fixTaintedBasename("a\\b"));
CPPUNIT_ASSERT_EQUAL(std::string("a__b"), util::fixTaintedBasename("a\\/b"));
}
} // namespace aria2