mirror of https://github.com/aria2/aria2
2008-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Handle date before epoch. To create *bad* Time object, call Time::null(). * src/CookieParser.cc * src/FtpConnection.cc * src/FtpConnection.h * src/FtpNegotiationCommand.cc * src/RequestGroup.cc * src/TimeA2.cc * src/TimeA2.h * test/CookieParserTest.cc * test/FtpConnectionTest.cc * test/TimeTest.ccpull/1/head
parent
7513095042
commit
4797b0e72d
15
ChangeLog
15
ChangeLog
|
@ -1,3 +1,18 @@
|
||||||
|
2008-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
|
Handle date before epoch.
|
||||||
|
To create *bad* Time object, call Time::null().
|
||||||
|
* src/CookieParser.cc
|
||||||
|
* src/FtpConnection.cc
|
||||||
|
* src/FtpConnection.h
|
||||||
|
* src/FtpNegotiationCommand.cc
|
||||||
|
* src/RequestGroup.cc
|
||||||
|
* src/TimeA2.cc
|
||||||
|
* src/TimeA2.h
|
||||||
|
* test/CookieParserTest.cc
|
||||||
|
* test/FtpConnectionTest.cc
|
||||||
|
* test/TimeTest.cc
|
||||||
|
|
||||||
2008-11-04 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
2008-11-04 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
Added support for following envrionment variables: http_proxy,
|
Added support for following envrionment variables: http_proxy,
|
||||||
|
|
|
@ -33,14 +33,17 @@
|
||||||
*/
|
*/
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#include "CookieParser.h"
|
#include "CookieParser.h"
|
||||||
#include "Util.h"
|
|
||||||
#include "A2STR.h"
|
|
||||||
#include "TimeA2.h"
|
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
#include "Util.h"
|
||||||
|
#include "A2STR.h"
|
||||||
|
#include "TimeA2.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
const std::string CookieParser::C_SECURE("secure");
|
const std::string CookieParser::C_SECURE("secure");
|
||||||
|
@ -76,20 +79,17 @@ Cookie CookieParser::parse(const std::string& cookieStr, const std::string& defa
|
||||||
Util::split(nv, *itr, '=');
|
Util::split(nv, *itr, '=');
|
||||||
values[nv.first] = nv.second;
|
values[nv.first] = nv.second;
|
||||||
}
|
}
|
||||||
time_t expiry = -1;
|
time_t expiry = 0;
|
||||||
if(values.find(C_EXPIRES) != values.end()) {
|
if(values.find(C_EXPIRES) != values.end()) {
|
||||||
expiry = Time::parseHTTPDate(values[C_EXPIRES]).getTime();
|
Time expiryTime = Time::parseHTTPDate(values[C_EXPIRES]);
|
||||||
}
|
if(expiryTime.good()) {
|
||||||
if(expiry == -1) {
|
expiry = expiryTime.getTime();
|
||||||
return Cookie(nameValue.first, nameValue.second,
|
}
|
||||||
values[C_PATH], values[C_DOMAIN],
|
|
||||||
values.find(C_SECURE) != values.end());
|
|
||||||
} else {
|
|
||||||
return Cookie(nameValue.first, nameValue.second,
|
|
||||||
expiry,
|
|
||||||
values[C_PATH], values[C_DOMAIN],
|
|
||||||
values.find(C_SECURE) != values.end());
|
|
||||||
}
|
}
|
||||||
|
return Cookie(nameValue.first, nameValue.second,
|
||||||
|
expiry,
|
||||||
|
values[C_PATH], values[C_DOMAIN],
|
||||||
|
values.find(C_SECURE) != values.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -369,7 +369,7 @@ unsigned int FtpConnection::receiveMdtmResponse(Time& time)
|
||||||
if(strlen(buf) == 14) {
|
if(strlen(buf) == 14) {
|
||||||
time = Time::parse(buf, "%Y%m%d%H%M%S");
|
time = Time::parse(buf, "%Y%m%d%H%M%S");
|
||||||
} else {
|
} else {
|
||||||
time.setTimeInSec(-1);
|
time = Time::null();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return response.first;
|
return response.first;
|
||||||
|
|
|
@ -105,7 +105,7 @@ public:
|
||||||
// time-val and store it in time.
|
// time-val and store it in time.
|
||||||
// If a code other than 213 is returned, time is not touched.
|
// If a code other than 213 is returned, time is not touched.
|
||||||
// Expect MDTM reply is YYYYMMDDhhmmss in GMT. If status is 213 but returned
|
// Expect MDTM reply is YYYYMMDDhhmmss in GMT. If status is 213 but returned
|
||||||
// date cannot be parsed, then executes time.setTimeInSec(-1).
|
// date cannot be parsed, then assign Time::null() to given time.
|
||||||
// If reply is not received yet, returns 0.
|
// If reply is not received yet, returns 0.
|
||||||
unsigned int receiveMdtmResponse(Time& time);
|
unsigned int receiveMdtmResponse(Time& time);
|
||||||
unsigned int receivePasvResponse(std::pair<std::string, uint16_t>& dest);
|
unsigned int receivePasvResponse(std::pair<std::string, uint16_t>& dest);
|
||||||
|
|
|
@ -283,7 +283,7 @@ bool FtpNegotiationCommand::sendMdtm()
|
||||||
|
|
||||||
bool FtpNegotiationCommand::recvMdtm()
|
bool FtpNegotiationCommand::recvMdtm()
|
||||||
{
|
{
|
||||||
Time lastModifiedTime(-1);
|
Time lastModifiedTime = Time::null();
|
||||||
unsigned int status = ftp->receiveMdtmResponse(lastModifiedTime);
|
unsigned int status = ftp->receiveMdtmResponse(lastModifiedTime);
|
||||||
if(status == 0) {
|
if(status == 0) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -123,7 +123,7 @@ RequestGroup::RequestGroup(const Option* option,
|
||||||
_forceHaltRequested(false),
|
_forceHaltRequested(false),
|
||||||
_singleHostMultiConnectionEnabled(true),
|
_singleHostMultiConnectionEnabled(true),
|
||||||
_uriSelector(new InOrderURISelector()),
|
_uriSelector(new InOrderURISelector()),
|
||||||
_lastModifiedTime(-1),
|
_lastModifiedTime(Time::null()),
|
||||||
_fileNotFoundCount(0),
|
_fileNotFoundCount(0),
|
||||||
_option(option),
|
_option(option),
|
||||||
_logger(LogFactory::getInstance())
|
_logger(LogFactory::getInstance())
|
||||||
|
|
|
@ -42,19 +42,24 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
Time::Time() {
|
Time::Time():_good(true)
|
||||||
|
{
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
Time::Time(const Time& time) {
|
Time::Time(const Time& time)
|
||||||
|
{
|
||||||
tv = time.tv;
|
tv = time.tv;
|
||||||
|
_good = time._good;
|
||||||
}
|
}
|
||||||
|
|
||||||
Time::Time(time_t sec) {
|
Time::Time(time_t sec):_good(true)
|
||||||
|
{
|
||||||
setTimeInSec(sec);
|
setTimeInSec(sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
Time::Time(const struct timeval& tv) {
|
Time::Time(const struct timeval& tv):_good(true)
|
||||||
|
{
|
||||||
this->tv = tv;
|
this->tv = tv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +69,7 @@ Time& Time::operator=(const Time& time)
|
||||||
{
|
{
|
||||||
if(this != &time) {
|
if(this != &time) {
|
||||||
tv = time.tv;
|
tv = time.tv;
|
||||||
|
_good = time._good;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +158,12 @@ void Time::setTimeInSec(time_t sec) {
|
||||||
|
|
||||||
bool Time::good() const
|
bool Time::good() const
|
||||||
{
|
{
|
||||||
return tv.tv_sec >= 0;
|
return _good;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Time::bad() const
|
||||||
|
{
|
||||||
|
return !_good;
|
||||||
}
|
}
|
||||||
|
|
||||||
Time Time::parse(const std::string& datetime, const std::string& format)
|
Time Time::parse(const std::string& datetime, const std::string& format)
|
||||||
|
@ -161,7 +172,7 @@ Time Time::parse(const std::string& datetime, const std::string& format)
|
||||||
memset(&tm, 0, sizeof(tm));
|
memset(&tm, 0, sizeof(tm));
|
||||||
char* r = strptime(datetime.c_str(), format.c_str(), &tm);
|
char* r = strptime(datetime.c_str(), format.c_str(), &tm);
|
||||||
if(r != datetime.c_str()+datetime.size()) {
|
if(r != datetime.c_str()+datetime.size()) {
|
||||||
return Time(-1);
|
return Time::null();
|
||||||
}
|
}
|
||||||
time_t thetime = timegm(&tm);
|
time_t thetime = timegm(&tm);
|
||||||
if(thetime == -1) {
|
if(thetime == -1) {
|
||||||
|
@ -190,9 +201,9 @@ Time Time::parseRFC850Ext(const std::string& datetime)
|
||||||
Time Time::parseHTTPDate(const std::string& datetime)
|
Time Time::parseHTTPDate(const std::string& datetime)
|
||||||
{
|
{
|
||||||
Time (*funcs[])(const std::string&) = {
|
Time (*funcs[])(const std::string&) = {
|
||||||
|
&parseRFC850,
|
||||||
&parseRFC1123,
|
&parseRFC1123,
|
||||||
&parseRFC850Ext,
|
&parseRFC850Ext,
|
||||||
&parseRFC850,
|
|
||||||
};
|
};
|
||||||
for(Time (**funcsp)(const std::string&) = &funcs[0];
|
for(Time (**funcsp)(const std::string&) = &funcs[0];
|
||||||
funcsp != &funcs[arrayLength(funcs)]; ++funcsp) {
|
funcsp != &funcs[arrayLength(funcs)]; ++funcsp) {
|
||||||
|
@ -201,7 +212,14 @@ Time Time::parseHTTPDate(const std::string& datetime)
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Time(-1);
|
return Time::null();
|
||||||
|
}
|
||||||
|
|
||||||
|
Time Time::null()
|
||||||
|
{
|
||||||
|
Time t(0);
|
||||||
|
t._good = false;
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -49,6 +49,8 @@ class Time {
|
||||||
private:
|
private:
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
|
bool _good;
|
||||||
|
|
||||||
struct timeval getCurrentTime() const;
|
struct timeval getCurrentTime() const;
|
||||||
public:
|
public:
|
||||||
// The time value is initialized so that it represents the time at which
|
// The time value is initialized so that it represents the time at which
|
||||||
|
@ -95,6 +97,9 @@ public:
|
||||||
|
|
||||||
bool good() const;
|
bool good() const;
|
||||||
|
|
||||||
|
// Returns !good()
|
||||||
|
bool bad() const;
|
||||||
|
|
||||||
// Currently timezone is assumed as GMT.
|
// Currently timezone is assumed as GMT.
|
||||||
static Time parse(const std::string& datetime, const std::string& format);
|
static Time parse(const std::string& datetime, const std::string& format);
|
||||||
|
|
||||||
|
@ -111,6 +116,8 @@ public:
|
||||||
// Try parseRFC1123, parseRFC850Ex, parseRFC850 in that order and returns
|
// Try parseRFC1123, parseRFC850Ex, parseRFC850 in that order and returns
|
||||||
// the first "good" Time object returned by these functions.
|
// the first "good" Time object returned by these functions.
|
||||||
static Time parseHTTPDate(const std::string& datetime);
|
static Time parseHTTPDate(const std::string& datetime);
|
||||||
|
|
||||||
|
static Time null();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -52,10 +52,11 @@ void CookieParserTest::testParse()
|
||||||
c = CookieParser().parse(str3);
|
c = CookieParser().parse(str3);
|
||||||
CPPUNIT_ASSERT(!c.good());
|
CPPUNIT_ASSERT(!c.good());
|
||||||
|
|
||||||
std::string str4 = "UID=300; expires=Wed, 01-Jan-1890 00:00:00 GMT;";
|
std::string str4 = "UID=300; expires=Wed, 01-Jan-1890 00:00:00 GMT";
|
||||||
c = CookieParser().parse(str4, "localhost", "/");
|
c = CookieParser().parse(str4, "localhost", "/");
|
||||||
CPPUNIT_ASSERT(c.good());
|
CPPUNIT_ASSERT(c.good());
|
||||||
CPPUNIT_ASSERT(c.isSessionCookie());
|
CPPUNIT_ASSERT(!c.isSessionCookie());
|
||||||
|
CPPUNIT_ASSERT_EQUAL((time_t)-2524521600, c.getExpiry());
|
||||||
|
|
||||||
std::string str5 = "k=v; expires=Sun, 10-Jun-07 11:00:00 GMT";
|
std::string str5 = "k=v; expires=Sun, 10-Jun-07 11:00:00 GMT";
|
||||||
c = CookieParser().parse(str5);
|
c = CookieParser().parse(str5);
|
||||||
|
|
|
@ -149,14 +149,14 @@ void FtpConnectionTest::testReceiveMdtmResponse()
|
||||||
Time t;
|
Time t;
|
||||||
_serverSocket->writeData("213 20080908\r\n");
|
_serverSocket->writeData("213 20080908\r\n");
|
||||||
CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t));
|
CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t));
|
||||||
CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTime());
|
CPPUNIT_ASSERT(t.bad());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// invalid month: 19
|
// invalid month: 19
|
||||||
Time t;
|
Time t;
|
||||||
_serverSocket->writeData("213 20081908124312\r\n");
|
_serverSocket->writeData("213 20081908124312\r\n");
|
||||||
CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t));
|
CPPUNIT_ASSERT_EQUAL((unsigned int)213, _ftp->receiveMdtmResponse(t));
|
||||||
CPPUNIT_ASSERT_EQUAL((time_t)-1, t.getTime());
|
CPPUNIT_ASSERT(t.bad());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
Time t;
|
Time t;
|
||||||
|
|
|
@ -64,9 +64,8 @@ void TimeTest::testParseHTTPDate()
|
||||||
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
|
CPPUNIT_ASSERT_EQUAL((time_t)1220714793,
|
||||||
Time::parseHTTPDate
|
Time::parseHTTPDate
|
||||||
("Sat, 06-Sep-08 15:26:33 GMT").getTime());
|
("Sat, 06-Sep-08 15:26:33 GMT").getTime());
|
||||||
CPPUNIT_ASSERT_EQUAL((time_t)-1,
|
CPPUNIT_ASSERT(Time::parseHTTPDate
|
||||||
Time::parseHTTPDate
|
("Sat, 2008-09-06 15:26:33 GMT").bad());
|
||||||
("Sat, 2008-09-06 15:26:33 GMT").getTime());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimeTest::testOperatorLess()
|
void TimeTest::testOperatorLess()
|
||||||
|
|
Loading…
Reference in New Issue