Browse Source

Added --rpc-allow-origin-all option.

This option adds Access-Control-Allow-Origin header field with value
'*' to the RPC response.
Tatsuhiro Tsujikawa 14 năm trước cách đây
mục cha
commit
384ef111b9
7 tập tin đã thay đổi với 28 bổ sung0 xóa
  1. 3 0
      src/HttpServer.cc
  2. 6 0
      src/HttpServer.h
  3. 3 0
      src/HttpServerCommand.cc
  4. 9 0
      src/OptionHandlerFactory.cc
  5. 2 0
      src/prefs.cc
  6. 2 0
      src/prefs.h
  7. 3 0
      src/usage_text.h

+ 3 - 0
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";
   }

+ 6 - 0
src/HttpServer.h

@@ -68,6 +68,7 @@ private:
   std::string password_;
   bool acceptsPersistentConnection_;
   bool acceptsGZip_;
+  std::string allowOrigin_;
 public:
   HttpServer(const SharedHandle<SocketCore>& socket, DownloadEngine* e);
 
@@ -123,6 +124,11 @@ public:
   {
     return socketRecvBuffer_;
   }
+
+  void setAllowOrigin(const std::string& allowOrigin)
+  {
+    allowOrigin_ = allowOrigin;
+  }
 };
 
 } // namespace aria2

+ 3 - 0
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

+ 9 - 0
src/OptionHandlerFactory.cc

@@ -595,6 +595,15 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
     op->addTag(TAG_ADVANCED);
     handlers.push_back(op);
   }
+  {
+    SharedHandle<OptionHandler> 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<OptionHandler> op(new BooleanOptionHandler
                                    (PREF_RPC_LISTEN_ALL,

+ 2 - 0
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");

+ 2 - 0
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;

+ 3 - 0
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.")