summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2007-03-18 15:42:34 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2007-03-18 15:42:34 +0000
commit7ff627079f6ed776dea1ce6fede1c1a07ef7f288 (patch)
tree8f7b7e5b763dc3841df03928fe2aa50abd35a810 /src
parent58d4dde223e815b75a3bf6bfbd4169818faca3bd (diff)
2007-03-19 Tatsuhiro Tsujikawa <tujikawa at valkyrie dot rednoah com>
To integrate Netrc into exsiting classes: * src/Request.h (_userDefinedAuthConfig): New variable. (findNetrcAuthenticator): New function. (segment): Removed. (setUserDefinedAuthConfig): New function. (resolveHttpAuthConfigItem): New function. (resolveFtpAuthConfigItem): New function. (resolveHttpProxyAuthConfigItem): New function. * src/HttpRequest.h (authConfig): Removed. (proxyAuthConfig): Removed. (setAuthConfig): Removed. (setProxyAuthConfig): Removed. * src/UrlRequest.h (getHeadResult): Added a parameter: authConfigHandle * src/common.h (SingletonHolder.h): New include. * src/main.cc (Netrc.h): New include. (main): Removed initial values of PREF_FTP_USER, PREF_FTP_PASSWD. Added initial value of PREF_NETRC_PATH. Added the initialization of netrc. * src/AuthConfig.h: New class. * src/prefs.h (PREF_NETRC_PATH): New definition. * src/HttpAuthConfig.h: Removed. * src/Netrc.cc (getRequiredNextToken): New function. (skipMacdef): New function. (parse): Rewritten. * src/Netrc.h (getRequiredNextToken): New function. (skipMacdef): New function. * src/Util.h, src/Util.cc (getHomeDir): New function. * src/TrackerWatcherComand.cc (createRequestCommand): Use AuthConfig. * src/FtpConnection.cc (sendUser): Use Request::resolveFtpAuthConfigItem(). (sendPass): Use Request::resolveFtpAuthConfigItem(). * src/Request.cc (findNetrcAuthenticator): New function. (resolveHttpAuthConfigItem): New function. (resolveFtpAuthConfigItem): New function. (resolveHttpProxyAuthConfigItem): New function. * src/UrlRequestInfo.cc: Use AuthConfig. * src/HttpRequest.cc (createRequest): Use authConfig. (getProxyAuthString): Use authConfig. (configure): Removed PREF_HTTP_USER, PREF_HTTP_PASSWD, PREF_HTTP_PROXY_USER, PREF_HTTP_PROXY_PASSWD.
Diffstat (limited to 'src')
-rw-r--r--src/AuthConfig.h106
-rw-r--r--src/AuthConfigItem.h68
-rw-r--r--src/FtpConnection.cc4
-rw-r--r--src/HttpRequest.cc8
-rw-r--r--src/HttpRequest.h17
-rw-r--r--src/Netrc.cc54
-rw-r--r--src/Netrc.h4
-rw-r--r--src/Request.cc56
-rw-r--r--src/Request.h27
-rw-r--r--src/SingletonHolder.h (renamed from src/HttpAuthConfig.h)31
-rw-r--r--src/TrackerWatcherCommand.cc3
-rw-r--r--src/UrlRequestInfo.cc16
-rw-r--r--src/UrlRequestInfo.h2
-rw-r--r--src/Util.cc9
-rw-r--r--src/Util.h2
-rw-r--r--src/common.h1
-rw-r--r--src/main.cc8
-rw-r--r--src/prefs.h2
18 files changed, 345 insertions, 73 deletions
diff --git a/src/AuthConfig.h b/src/AuthConfig.h
new file mode 100644
index 00000000..580c4853
--- /dev/null
+++ b/src/AuthConfig.h
@@ -0,0 +1,106 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_AUTH_CONFIG_H_
+#define _D_AUTH_CONFIG_H_
+
+#include "common.h"
+#include "AuthConfigItem.h"
+#include "prefs.h"
+#include "Option.h"
+
+class AuthConfig {
+private:
+ AuthConfigItemHandle httpAuthConfigItem;
+ AuthConfigItemHandle ftpAuthConfigItem;
+ AuthConfigItemHandle httpProxyAuthConfigItem;
+
+ AuthConfigItemHandle createAuthConfigItem(const string& user, const string& password)
+ {
+ if(user.length() > 0) {
+ return new AuthConfigItem(user, password);
+ } else {
+ return 0;
+ }
+ }
+public:
+
+ AuthConfig():httpAuthConfigItem(0),
+ ftpAuthConfigItem(0),
+ httpProxyAuthConfigItem(0) {}
+
+ AuthConfigItemHandle getHttpAuthConfigItem() const
+ {
+ return httpAuthConfigItem;
+ }
+
+ void setHttpAuthConfigItem(const string& user, const string& password)
+ {
+ httpAuthConfigItem = createAuthConfigItem(user, password);
+ }
+
+ AuthConfigItemHandle getFtpAuthConfigItem() const
+ {
+ return ftpAuthConfigItem;
+ }
+
+ void setFtpAuthConfigItem(const string& user, const string& password)
+ {
+ ftpAuthConfigItem = createAuthConfigItem(user, password);
+ }
+
+ AuthConfigItemHandle getHttpProxyAuthConfigItem() const
+ {
+ return httpProxyAuthConfigItem;
+ }
+
+ void setHttpProxyAuthConfigItem(const string& user, const string& password)
+ {
+ httpProxyAuthConfigItem = createAuthConfigItem(user, password);
+ }
+
+ void configure(const Option* op)
+ {
+ setHttpAuthConfigItem(op->get(PREF_HTTP_USER),
+ op->get(PREF_HTTP_PASSWD));
+ setFtpAuthConfigItem(op->get(PREF_FTP_USER),
+ op->get(PREF_FTP_PASSWD));
+ setHttpProxyAuthConfigItem(op->get(PREF_HTTP_PROXY_USER),
+ op->get(PREF_HTTP_PROXY_PASSWD));
+ }
+};
+
+typedef SharedHandle<AuthConfig> AuthConfigHandle;
+
+#endif // _D_AUTH_CONFIG_H_
diff --git a/src/AuthConfigItem.h b/src/AuthConfigItem.h
new file mode 100644
index 00000000..68904251
--- /dev/null
+++ b/src/AuthConfigItem.h
@@ -0,0 +1,68 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_AUTH_CONFIG_ITEM_H_
+#define _D_AUTH_CONFIG_ITEM_H_
+
+#include "common.h"
+
+class AuthConfigItem {
+private:
+ string _authScheme;
+ string _user;
+ string _password;
+public:
+
+ AuthConfigItem(const string& user, const string& password):
+ _user(user), _password(password) {}
+
+ string getAuthText() const
+ {
+ return _user+":"+_password;
+ }
+
+ const string& getUser() const
+ {
+ return _user;
+ }
+
+ const string& getPassword() const
+ {
+ return _password;
+ }
+};
+
+typedef SharedHandle<AuthConfigItem> AuthConfigItemHandle;
+
+#endif // _D_AUTH_CONFIG_ITEM_H_
diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc
index 48bd416a..edb58109 100644
--- a/src/FtpConnection.cc
+++ b/src/FtpConnection.cc
@@ -49,13 +49,13 @@ FtpConnection::FtpConnection(int cuid, const SocketHandle& socket,
FtpConnection::~FtpConnection() {}
void FtpConnection::sendUser() const {
- string request = "USER "+option->get(PREF_FTP_USER)+"\r\n";
+ string request = "USER "+req->resolveFtpAuthConfigItem()->getUser()+"\r\n";
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
socket->writeData(request);
}
void FtpConnection::sendPass() const {
- string request = "PASS "+option->get(PREF_FTP_PASSWD)+"\r\n";
+ string request = "PASS "+req->resolveFtpAuthConfigItem()->getPassword()+"\r\n";
logger->info(MSG_SENDING_REQUEST, cuid, "PASS ********");
socket->writeData(request);
}
diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc
index b746f612..19cfdbe1 100644
--- a/src/HttpRequest.cc
+++ b/src/HttpRequest.cc
@@ -107,7 +107,7 @@ string HttpRequest::createRequest() const
}
if(authEnabled) {
requestLine += "Authorization: Basic "+
- Base64::encode(authConfig->getAuthText())+"\r\n";
+ Base64::encode(request->resolveHttpAuthConfigItem()->getAuthText())+"\r\n";
}
if(getPreviousURI().size()) {
requestLine += "Referer: "+getPreviousURI()+"\r\n";
@@ -145,7 +145,7 @@ string HttpRequest::createProxyRequest() const
string HttpRequest::getProxyAuthString() const {
return "Proxy-Authorization: Basic "+
- Base64::encode(proxyAuthConfig->getAuthText())+"\r\n";
+ Base64::encode(request->resolveHttpProxyAuthConfigItem()->getAuthText())+"\r\n";
}
void HttpRequest::configure(const Option* option)
@@ -155,8 +155,4 @@ void HttpRequest::configure(const Option* option)
option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE &&
option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
proxyAuthEnabled = option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
- authConfig = new HttpAuthConfig(option->get(PREF_HTTP_USER),
- option->get(PREF_HTTP_PASSWD));
- proxyAuthConfig = new HttpAuthConfig(option->get(PREF_HTTP_PROXY_USER),
- option->get(PREF_HTTP_PROXY_PASSWD));
}
diff --git a/src/HttpRequest.h b/src/HttpRequest.h
index d6f4ca4f..8cfe45d9 100644
--- a/src/HttpRequest.h
+++ b/src/HttpRequest.h
@@ -39,7 +39,6 @@
#include "Segment.h"
#include "Range.h"
#include "Request.h"
-#include "HttpAuthConfig.h"
#include "Option.h"
#include <netinet/in.h>
@@ -54,14 +53,10 @@ private:
bool authEnabled;
- HttpAuthConfigHandle authConfig;
-
bool proxyEnabled;
bool proxyAuthEnabled;
- HttpAuthConfigHandle proxyAuthConfig;
-
string userAgent;
string getHostText(const string& host, in_port_t port) const;
@@ -73,10 +68,8 @@ public:
segment(0),
entityLength(0),
authEnabled(false),
- authConfig(0),
proxyEnabled(false),
proxyAuthEnabled(false),
- proxyAuthConfig(0),
userAgent(USER_AGENT)
{}
@@ -221,16 +214,6 @@ public:
this->authEnabled = authEnabled;
}
- void setAuthConfig(const HttpAuthConfigHandle& authConfig)
- {
- this->authConfig = authConfig;
- }
-
- void setProxyAuthConfig(const HttpAuthConfigHandle& proxyAuthConfig)
- {
- this->proxyAuthConfig = proxyAuthConfig;
- }
-
void setUserAgent(const string& userAgent)
{
this->userAgent = userAgent;
diff --git a/src/Netrc.cc b/src/Netrc.cc
index a30e8485..4538f526 100644
--- a/src/Netrc.cc
+++ b/src/Netrc.cc
@@ -37,6 +37,27 @@
#include "RecoverableException.h"
#include <fstream>
+string Netrc::getRequiredNextToken(ifstream& f) const
+{
+ string token;
+ if(f >> token) {
+ return token;
+ } else {
+ throw new RecoverableException("Netrc:parse error. EOF reached where a token expected.");
+ }
+}
+
+void Netrc::skipMacdef(ifstream& f) const
+{
+ string line;
+ getline(f, line);
+ while(getline(f, line)) {
+ if(line == "\r" || line == "") {
+ break;
+ }
+ }
+}
+
void Netrc::parse(const string& path)
{
authenticators.clear();
@@ -46,32 +67,29 @@ void Netrc::parse(const string& path)
throw new RecoverableException("File not found: %s", path.c_str());
}
- int32_t lineNum = 0;
- string line;
AuthenticatorHandle authenticator = 0;
- while(getline(f, line)) {
- ++lineNum;
- if(Util::trim(line).empty()) {
- continue;
- }
- pair<string, string> nameValuePair = Util::split(line, "\r\n\t ");
- if(nameValuePair.first == "machine") {
+ string token;
+ while(f >> token) {
+ if(token == "machine") {
storeAuthenticator(authenticator);
authenticator = new Authenticator();
- authenticator->setMachine(nameValuePair.second);
- } else if(nameValuePair.first == "default") {
+ authenticator->setMachine(getRequiredNextToken(f));
+ } else if(token == "default") {
storeAuthenticator(authenticator);
authenticator = new DefaultAuthenticator();
} else {
if(authenticator.isNull()) {
- throw new RecoverableException("Malformed netrc file: line %d", lineNum);
+ throw new RecoverableException("Netrc:parse error. %s encounterd where 'machine' or 'default' expected.");
}
- if(nameValuePair.first == "login") {
- authenticator->setLogin(nameValuePair.second);
- } else if(nameValuePair.first == "password") {
- authenticator->setPassword(nameValuePair.second);
- } else if(nameValuePair.first == "account") {
- authenticator->setAccount(nameValuePair.second);
+ if(token == "login") {
+ authenticator->setLogin(getRequiredNextToken(f));
+ } else if(token == "password") {
+ authenticator->setPassword(getRequiredNextToken(f));
+ } else if(token == "account") {
+ authenticator->setAccount(getRequiredNextToken(f));
+ } else if(token == "macdef") {
+ getRequiredNextToken(f);
+ skipMacdef(f);
}
}
}
diff --git a/src/Netrc.h b/src/Netrc.h
index b36599eb..07f7c738 100644
--- a/src/Netrc.h
+++ b/src/Netrc.h
@@ -127,6 +127,10 @@ private:
Authenticators authenticators;
void storeAuthenticator(const AuthenticatorHandle& authenticator);
+
+ string getRequiredNextToken(ifstream& f) const;
+
+ void skipMacdef(ifstream& f) const;
public:
Netrc() {}
diff --git a/src/Request.cc b/src/Request.cc
index 62248826..8ff53a88 100644
--- a/src/Request.cc
+++ b/src/Request.cc
@@ -35,12 +35,16 @@
#include "Request.h"
#include "Util.h"
#include "FeatureConfig.h"
+#include "Netrc.h"
const string Request::METHOD_GET = "get";
const string Request::METHOD_HEAD = "head";
-Request::Request():port(0), tryCount(0), keepAlive(true), method(METHOD_GET), isTorrent(false) {
+Request::Request():port(0), tryCount(0), keepAlive(true), method(METHOD_GET),
+ _userDefinedAuthConfig(0),
+ isTorrent(false)
+{
cookieBox = new CookieBox();
}
@@ -55,7 +59,6 @@ bool Request::setUrl(const string& url) {
bool Request::resetUrl() {
previousUrl = referer;
- segment = Segment();
return setUrl(url);
}
@@ -130,3 +133,52 @@ bool Request::parseUrl(const string& url) {
file += query;
return true;
}
+
+AuthConfigItemHandle Request::findNetrcAuthenticator() const
+{
+ if(!NetrcSingletonHolder::instance().isNull()) {
+ AuthenticatorHandle auth = NetrcSingletonHolder::instance()->findAuthenticator(getHost());
+ if(auth.isNull()) {
+ return 0;
+ } else {
+ return new AuthConfigItem(auth->getLogin(), auth->getPassword());
+ }
+ } else {
+ return 0;
+ }
+}
+
+AuthConfigItemHandle Request::resolveHttpAuthConfigItem() const
+{
+ if(!_userDefinedAuthConfig.isNull() &&
+ !_userDefinedAuthConfig->getHttpAuthConfigItem().isNull()) {
+ return _userDefinedAuthConfig->getHttpAuthConfigItem();
+ } else {
+ return findNetrcAuthenticator();
+ }
+}
+
+AuthConfigItemHandle Request::resolveFtpAuthConfigItem() const
+{
+ if(!_userDefinedAuthConfig.isNull() &&
+ !_userDefinedAuthConfig->getFtpAuthConfigItem().isNull()) {
+ return _userDefinedAuthConfig->getFtpAuthConfigItem();
+ } else {
+ AuthConfigItemHandle authConfig = findNetrcAuthenticator();
+ if(authConfig.isNull()) {
+ return new AuthConfigItem("anonymous", "ARIA2USER@");
+ } else {
+ return authConfig;
+ }
+ }
+}
+
+AuthConfigItemHandle Request::resolveHttpProxyAuthConfigItem() const
+{
+ if(!_userDefinedAuthConfig.isNull() &&
+ !_userDefinedAuthConfig->getHttpProxyAuthConfigItem().isNull()) {
+ return _userDefinedAuthConfig->getHttpProxyAuthConfigItem();
+ } else {
+ return findNetrcAuthenticator();
+ }
+}
diff --git a/src/Request.h b/src/Request.h
index 04f8aad7..f7b9c9ec 100644
--- a/src/Request.h
+++ b/src/Request.h
@@ -34,14 +34,9 @@
/* copyright --> */
#ifndef _D_REQUEST_H_
#define _D_REQUEST_H_
-#include <string>
-#include <map>
-#include "CookieBox.h"
-#include "Segment.h"
#include "common.h"
-#include "SharedHandle.h"
-
-using namespace std;
+#include "CookieBox.h"
+#include "AuthConfig.h"
#define SAFE_CHARS "abcdefghijklmnopqrstuvwxyz"\
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"\
@@ -75,9 +70,14 @@ private:
int trackerEvent;
bool keepAlive;
string method;
+
+ AuthConfigHandle _userDefinedAuthConfig;
+
bool parseUrl(const string& url);
+
+ AuthConfigItemHandle findNetrcAuthenticator() const;
+
public:
- Segment segment;
CookieBox* cookieBox;
bool isTorrent;
public:
@@ -116,6 +116,17 @@ public:
this->method = method;
}
+ void setUserDefinedAuthConfig(const AuthConfigHandle& authConfig)
+ {
+ _userDefinedAuthConfig = authConfig;
+ }
+
+ AuthConfigItemHandle resolveHttpAuthConfigItem() const;
+
+ AuthConfigItemHandle resolveFtpAuthConfigItem() const;
+
+ AuthConfigItemHandle resolveHttpProxyAuthConfigItem() const;
+
const string& getMethod() const {
return method;
}
diff --git a/src/HttpAuthConfig.h b/src/SingletonHolder.h
index 4367a78e..d6d2eebf 100644
--- a/src/HttpAuthConfig.h
+++ b/src/SingletonHolder.h
@@ -32,27 +32,30 @@
* files in the program, then also delete it here.
*/
/* copyright --> */
-#ifndef _D_HTTP_AUTH_CONFIG_H_
-#define _D_HTTP_AUTH_CONFIG_H_
+#ifndef _D_SINGLETON_HOLDER_H_
+#define _D_SINGLETON_HOLDER_H_
-#include "common.h"
-
-class HttpAuthConfig {
+template<typename T>
+class SingletonHolder {
private:
- string authScheme;
- string authUser;
- string authPassword;
+ static T _instance;
+
+ SingletonHolder() {}
public:
+ ~SingletonHolder() {}
- HttpAuthConfig(const string& authUser, const string& authPassword):
- authUser(authUser), authPassword(authPassword) {}
+ static T& instance()
+ {
+ return _instance;
+ }
- string getAuthText() const
+ static void instance(T& instance)
{
- return authUser+":"+authPassword;
+ _instance = instance;
}
};
-typedef SharedHandle<HttpAuthConfig> HttpAuthConfigHandle;
+template<typename T>
+T SingletonHolder<T>::_instance = 0;
-#endif // _D_HTTP_AUTH_CONFIG_H_
+#endif // _D_SINGLETON_HOLDER_H_
diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc
index 8d98e682..2afb6dc2 100644
--- a/src/TrackerWatcherCommand.cc
+++ b/src/TrackerWatcherCommand.cc
@@ -81,8 +81,11 @@ Command* TrackerWatcherCommand::createCommand() {
}
Command* TrackerWatcherCommand::createRequestCommand(const string& url) {
+ AuthConfigHandle authConfig = new AuthConfig();
+ authConfig->configure(e->option);
RequestHandle req;
req->setUrl(url);
+ req->setUserDefinedAuthConfig(authConfig);
req->isTorrent = true;
Command* command =
InitiateConnectionCommandFactory::createInitiateConnectionCommand(btRuntime->getNewCuid(), req, e);
diff --git a/src/UrlRequestInfo.cc b/src/UrlRequestInfo.cc
index d33924fb..38778c36 100644
--- a/src/UrlRequestInfo.cc
+++ b/src/UrlRequestInfo.cc
@@ -88,15 +88,18 @@ private:
Requests* requestsPtr;
string referer;
int split;
+ AuthConfigHandle _userDefinedAuthConfig;
string method;
public:
CreateRequest(Requests* requestsPtr,
const string& referer,
int split,
+ const AuthConfigHandle& userDefinedAuthConfig,
const string& method = Request::METHOD_GET)
:requestsPtr(requestsPtr),
referer(referer),
split(split),
+ _userDefinedAuthConfig(userDefinedAuthConfig),
method(method) {}
void operator()(const string& url) {
@@ -104,6 +107,7 @@ public:
RequestHandle req;
req->setReferer(referer);
req->setMethod(method);
+ req->setUserDefinedAuthConfig(_userDefinedAuthConfig);
if(req->setUrl(url)) {
requestsPtr->push_back(req);
} else {
@@ -120,12 +124,13 @@ void UrlRequestInfo::printUrls(const Strings& urls) const {
}
}
-HeadResultHandle UrlRequestInfo::getHeadResult() {
+HeadResultHandle UrlRequestInfo::getHeadResult(const AuthConfigHandle& authConfig) {
Requests requests;
for_each(urls.begin(), urls.end(),
CreateRequest(&requests,
op->get(PREF_REFERER),
1,
+ authConfig,
Request::METHOD_HEAD));
if(requests.size() == 0) {
return 0;
@@ -153,12 +158,17 @@ RequestInfos UrlRequestInfo::execute() {
Requests requests;
Requests reserved;
printUrls(urls);
- HeadResultHandle hr = getHeadResult();
+ AuthConfigHandle authConfig = new AuthConfig();
+ authConfig->configure(op);
+
+ HeadResultHandle hr = getHeadResult(authConfig);
+
for_each(urls.begin(), urls.end(),
CreateRequest(&requests,
op->get(PREF_REFERER),
- op->getAsInt(PREF_SPLIT)));
+ op->getAsInt(PREF_SPLIT),
+ authConfig));
logger->info("Head result: filename=%s, total length=%s",
hr->filename.c_str(), Util::ullitos(hr->totalLength, true).c_str());
diff --git a/src/UrlRequestInfo.h b/src/UrlRequestInfo.h
index 94e1680b..e349f19b 100644
--- a/src/UrlRequestInfo.h
+++ b/src/UrlRequestInfo.h
@@ -64,7 +64,7 @@ private:
Requests& reserved,
int maxConnections) const;
void printUrls(const Strings& urls) const;
- HeadResultHandle getHeadResult();
+ HeadResultHandle getHeadResult(const AuthConfigHandle& authConfig);
public:
UrlRequestInfo(const Strings& urls, int maxConnections, Option* op):
RequestInfo(op),
diff --git a/src/Util.cc b/src/Util.cc
index 89d241fd..2c096f3e 100644
--- a/src/Util.cc
+++ b/src/Util.cc
@@ -664,3 +664,12 @@ void Util::indexRange(int32_t& startIndex, int32_t& endIndex,
endIndex = _endIndex;
}
+string Util::getHomeDir()
+{
+ const char* p = getenv("HOME");
+ if(p) {
+ return p;
+ } else {
+ return "";
+ }
+}
diff --git a/src/Util.h b/src/Util.h
index fd1f1cd1..6127b41e 100644
--- a/src/Util.h
+++ b/src/Util.h
@@ -144,6 +144,8 @@ public:
static void indexRange(int32_t& startIndex, int32_t& endIndex,
int64_t offset,
int32_t srcLength, int32_t destLength);
+
+ static string getHomeDir();
};
#endif // _D_UTIL_H_
diff --git a/src/common.h b/src/common.h
index 91049cb5..fd0e9c70 100644
--- a/src/common.h
+++ b/src/common.h
@@ -70,6 +70,7 @@ public:
};
#include "SharedHandle.h"
+#include "SingletonHolder.h"
typedef deque<string> Strings;
typedef deque<int32_t> Integers;
diff --git a/src/main.cc b/src/main.cc
index b6533ed5..97d4fda5 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -48,6 +48,7 @@
#include "BitfieldManFactory.h"
#include "SimpleRandomizer.h"
#include "ConsoleFileAllocationMonitor.h"
+#include "Netrc.h"
#include <deque>
#include <algorithm>
#include <time.h>
@@ -342,8 +343,6 @@ int main(int argc, char* argv[]) {
op->put(PREF_MAX_TRIES, "5");
op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
- op->put(PREF_FTP_USER, "anonymous");
- op->put(PREF_FTP_PASSWD, "ARIA2USER@");
op->put(PREF_FTP_TYPE, V_BINARY);
op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
op->put(PREF_AUTO_SAVE_INTERVAL, "60");
@@ -357,6 +356,7 @@ int main(int argc, char* argv[]) {
op->put(PREF_ALLOW_OVERWRITE, V_FALSE);
op->put(PREF_REALTIME_CHUNK_CHECKSUM, V_TRUE);
op->put(PREF_CHECK_INTEGRITY, V_FALSE);
+ op->put(PREF_NETRC_PATH, Util::getHomeDir()+"/.netrc");
while(1) {
int optIndex = 0;
int lopt;
@@ -773,6 +773,10 @@ int main(int argc, char* argv[]) {
logger->info("%s %s", PACKAGE, PACKAGE_VERSION);
logger->info("Logging started.");
+ NetrcHandle netrc = new Netrc();
+ netrc->parse(op->get(PREF_NETRC_PATH));
+ NetrcSingletonHolder::instance(netrc);
+
Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
RequestInfo* firstReqInfo = 0;
diff --git a/src/prefs.h b/src/prefs.h
index 95e8c312..4846f80b 100644
--- a/src/prefs.h
+++ b/src/prefs.h
@@ -90,6 +90,8 @@
#define PREF_REALTIME_CHUNK_CHECKSUM "realtime_chunk_checksum"
// value: true | false
#define PREF_CHECK_INTEGRITY "check_integrity"
+// value: string that your file system recognizes as a file name.
+#define PREF_NETRC_PATH "netrc_path"
/**
* FTP related preferences