From 384ef111b90b7bcb04b8ff477eca7218a99cb98d Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 18 Aug 2011 21:22:48 +0900 Subject: [PATCH] Added --rpc-allow-origin-all option. This option adds Access-Control-Allow-Origin header field with value '*' to the RPC response. --- src/HttpServer.cc | 3 +++ src/HttpServer.h | 6 ++++++ src/HttpServerCommand.cc | 3 +++ src/OptionHandlerFactory.cc | 9 +++++++++ src/prefs.cc | 2 ++ src/prefs.h | 2 ++ src/usage_text.h | 3 +++ 7 files changed, 28 insertions(+) diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 31893c95..9d3eaaa7 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -165,6 +165,9 @@ void HttpServer::feedResponse(const std::string& status, strappend(header, "Content-Length: ", util::uitos(text.size()), "\r\n", "Expires: ", httpDate, "\r\n", "Cache-Control: no-cache\r\n"); + if(!allowOrigin_.empty()) { + strappend(header, "Access-Control-Allow-Origin: ", allowOrigin_, "\r\n"); + } if(supportsGZip()) { header += "Content-Encoding: gzip\r\n"; } diff --git a/src/HttpServer.h b/src/HttpServer.h index 2f8d6329..c8dbc8c2 100644 --- a/src/HttpServer.h +++ b/src/HttpServer.h @@ -68,6 +68,7 @@ private: std::string password_; bool acceptsPersistentConnection_; bool acceptsGZip_; + std::string allowOrigin_; public: HttpServer(const SharedHandle& socket, DownloadEngine* e); @@ -123,6 +124,11 @@ public: { return socketRecvBuffer_; } + + void setAllowOrigin(const std::string& allowOrigin) + { + allowOrigin_ = allowOrigin; + } }; } // namespace aria2 diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 8306ad15..4dd43a7c 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -66,6 +66,9 @@ HttpServerCommand::HttpServerCommand e_->addSocketForReadCheck(socket_, this); httpServer_->setUsernamePassword(e_->getOption()->get(PREF_RPC_USER), e_->getOption()->get(PREF_RPC_PASSWD)); + if(e_->getOption()->getAsBool(PREF_RPC_ALLOW_ORIGIN_ALL)) { + httpServer_->setAllowOrigin("*"); + } #ifdef HAVE_ZLIB httpServer_->enableGZip(); #else // !HAVE_ZLIB diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index ae2db34a..87fd87b5 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -595,6 +595,15 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers() op->addTag(TAG_ADVANCED); handlers.push_back(op); } + { + SharedHandle op(new BooleanOptionHandler + (PREF_RPC_ALLOW_ORIGIN_ALL, + TEXT_RPC_ALLOW_ORIGIN_ALL, + A2_V_FALSE, + OptionHandler::OPT_ARG)); + op->addTag(TAG_RPC); + handlers.push_back(op); + } { SharedHandle op(new BooleanOptionHandler (PREF_RPC_LISTEN_ALL, diff --git a/src/prefs.cc b/src/prefs.cc index f8407198..ad7583c0 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -169,6 +169,8 @@ const std::string PREF_RPC_MAX_REQUEST_SIZE("rpc-max-request-size"); // value: true | false const std::string PREF_RPC_LISTEN_ALL("rpc-listen-all"); // value: true | false +const std::string PREF_RPC_ALLOW_ORIGIN_ALL("rpc-allow-origin-all"); +// value: true | false const std::string PREF_ENABLE_XML_RPC("enable-xml-rpc"); // value: 1*digit const std::string PREF_XML_RPC_LISTEN_PORT("xml-rpc-listen-port"); diff --git a/src/prefs.h b/src/prefs.h index 18ab9cb5..373ac695 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -172,6 +172,8 @@ extern const std::string PREF_RPC_MAX_REQUEST_SIZE; // value: true | false extern const std::string PREF_RPC_LISTEN_ALL; // value: true | false +extern const std::string PREF_RPC_ALLOW_ORIGIN_ALL; +// value: true | false extern const std::string PREF_ENABLE_XML_RPC; // value: 1*digit extern const std::string PREF_XML_RPC_LISTEN_PORT; diff --git a/src/usage_text.h b/src/usage_text.h index 7e5d1316..c505b124 100644 --- a/src/usage_text.h +++ b/src/usage_text.h @@ -816,3 +816,6 @@ #define TEXT_PAUSE \ _(" --pause[=true|false] Pause download after added. This option is\n" \ " effective only when --enable-rpc=true is given.") +#define TEXT_RPC_ALLOW_ORIGIN_ALL \ + _(" --rpc-allow-origin-all[=true|false] Add Access-Control-Allow-Origin header\n" \ + " field with value '*' to the RPC response.")