PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/server/satellite-server.cpp

https://gitlab.com/Blueprint-Marketing/hhvm
C++ | 228 lines | 182 code | 21 blank | 25 comment | 19 complexity | a6aaa0a4a8b95cb61dc9736ed97c725f MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "hphp/runtime/server/satellite-server.h"
  17. #include "hphp/runtime/server/http-request-handler.h"
  18. #include "hphp/runtime/server/rpc-request-handler.h"
  19. #include "hphp/runtime/server/virtual-host.h"
  20. #include "hphp/runtime/base/runtime-option.h"
  21. #include "hphp/runtime/base/preg.h"
  22. #include "hphp/runtime/base/config.h"
  23. #include "hphp/util/text-util.h"
  24. #include <folly/Memory.h>
  25. using folly::make_unique;
  26. using std::set;
  27. namespace HPHP {
  28. ///////////////////////////////////////////////////////////////////////////////
  29. std::set<std::string> SatelliteServerInfo::InternalURLs;
  30. int SatelliteServerInfo::DanglingServerPort = 0;
  31. SatelliteServerInfo::SatelliteServerInfo(const IniSetting::Map& ini, Hdf hdf) {
  32. m_name = hdf.getName();
  33. m_port = Config::GetUInt16(ini, hdf, "Port", 0, false);
  34. m_threadCount = Config::GetInt32(ini, hdf, "ThreadCount", 5, false);
  35. m_maxRequest = Config::GetInt32(ini, hdf, "MaxRequest", 500, false);
  36. m_maxDuration = Config::GetInt32(ini, hdf, "MaxDuration", 120, false);
  37. m_timeoutSeconds = std::chrono::seconds(
  38. Config::GetInt32(ini, hdf, "TimeoutSeconds",
  39. RuntimeOption::RequestTimeoutSeconds, false));
  40. m_reqInitFunc = Config::GetString(ini, hdf, "RequestInitFunction", "", false);
  41. m_reqInitDoc = Config::GetString(ini, hdf, "RequestInitDocument", "", false);
  42. m_password = Config::GetString(ini, hdf, "Password", "", false);
  43. m_passwords = Config::GetSet(ini, hdf, "Passwords", m_passwords, false);
  44. m_alwaysReset = Config::GetBool(ini, hdf, "AlwaysReset", false, false);
  45. m_functions = Config::GetSet(ini, hdf, "Functions", m_functions, false);
  46. std::string type = Config::GetString(ini, hdf, "Type", "", false);
  47. if (type == "InternalPageServer") {
  48. m_type = SatelliteServer::Type::KindOfInternalPageServer;
  49. std::vector<std::string> urls;
  50. urls = Config::GetVector(ini, hdf, "URLs", urls, false);
  51. for (unsigned int i = 0; i < urls.size(); i++) {
  52. m_urls.insert(format_pattern(urls[i], true));
  53. }
  54. if (Config::GetBool(ini, hdf, "BlockMainServer", true, false)) {
  55. InternalURLs.insert(m_urls.begin(), m_urls.end());
  56. }
  57. } else if (type == "DanglingPageServer") {
  58. m_type = SatelliteServer::Type::KindOfDanglingPageServer;
  59. DanglingServerPort = m_port;
  60. } else if (type == "RPCServer") {
  61. m_type = SatelliteServer::Type::KindOfRPCServer;
  62. } else {
  63. m_type = SatelliteServer::Type::Unknown;
  64. }
  65. }
  66. bool SatelliteServerInfo::checkMainURL(const std::string& path) {
  67. String url(path.c_str(), path.size(), CopyString);
  68. for (auto iter = SatelliteServerInfo::InternalURLs.begin();
  69. iter != SatelliteServerInfo::InternalURLs.end(); ++iter) {
  70. Variant ret = preg_match
  71. (String(iter->c_str(), iter->size(), CopyString), url);
  72. if (ret.toInt64() > 0) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // InternalPageServer: Server + allowed URL checking
  80. class InternalPageServer : public SatelliteServer {
  81. public:
  82. explicit InternalPageServer(std::shared_ptr<SatelliteServerInfo> info)
  83. : m_allowedURLs(info->getURLs()) {
  84. m_server = ServerFactoryRegistry::createServer
  85. (RuntimeOption::ServerType, RuntimeOption::ServerIP, info->getPort(),
  86. info->getThreadCount());
  87. m_server->setRequestHandlerFactory<HttpRequestHandler>(
  88. info->getTimeoutSeconds().count());
  89. m_server->setUrlChecker(std::bind(&InternalPageServer::checkURL, this,
  90. std::placeholders::_1));
  91. }
  92. virtual void start() {
  93. m_server->start();
  94. }
  95. virtual void stop() {
  96. m_server->stop();
  97. m_server->waitForEnd();
  98. }
  99. virtual int getActiveWorker() {
  100. return m_server->getActiveWorker();
  101. }
  102. virtual int getQueuedJobs() {
  103. return m_server->getQueuedJobs();
  104. }
  105. private:
  106. bool checkURL(const std::string &path) const {
  107. String url(path.c_str(), path.size(), CopyString);
  108. for (const auto &allowed : m_allowedURLs) {
  109. Variant ret = preg_match
  110. (String(allowed.c_str(), allowed.size(), CopyString), url);
  111. if (ret.toInt64() > 0) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. ServerPtr m_server;
  118. std::set<std::string> m_allowedURLs;
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. // DanglingPageServer: same as Server
  122. class DanglingPageServer : public SatelliteServer {
  123. public:
  124. explicit DanglingPageServer(std::shared_ptr<SatelliteServerInfo> info) {
  125. m_server = ServerFactoryRegistry::createServer
  126. (RuntimeOption::ServerType, RuntimeOption::ServerIP, info->getPort(),
  127. info->getThreadCount());
  128. m_server->setRequestHandlerFactory<HttpRequestHandler>(
  129. info->getTimeoutSeconds().count());
  130. }
  131. virtual void start() {
  132. m_server->start();
  133. }
  134. virtual void stop() {
  135. m_server->stop();
  136. m_server->waitForEnd();
  137. }
  138. virtual int getActiveWorker() {
  139. return m_server->getActiveWorker();
  140. }
  141. virtual int getQueuedJobs() {
  142. return m_server->getQueuedJobs();
  143. }
  144. private:
  145. ServerPtr m_server;
  146. };
  147. ///////////////////////////////////////////////////////////////////////////////
  148. // RPCServer: Server + RPCRequestHandler
  149. class RPCServer : public SatelliteServer {
  150. public:
  151. explicit RPCServer(std::shared_ptr<SatelliteServerInfo> info) {
  152. m_server = ServerFactoryRegistry::createServer
  153. (RuntimeOption::ServerType, RuntimeOption::ServerIP, info->getPort(),
  154. info->getThreadCount());
  155. m_server->setRequestHandlerFactory([info] {
  156. auto handler = make_unique<RPCRequestHandler>(
  157. info->getTimeoutSeconds().count(), true);
  158. handler->setServerInfo(info);
  159. return handler;
  160. });
  161. }
  162. virtual void start() {
  163. m_server->start();
  164. }
  165. virtual void stop() {
  166. m_server->stop();
  167. m_server->waitForEnd();
  168. }
  169. virtual int getActiveWorker() {
  170. return m_server->getActiveWorker();
  171. }
  172. virtual int getQueuedJobs() {
  173. return m_server->getQueuedJobs();
  174. }
  175. private:
  176. ServerPtr m_server;
  177. };
  178. ///////////////////////////////////////////////////////////////////////////////
  179. // SatelliteServer
  180. std::unique_ptr<SatelliteServer>
  181. SatelliteServer::Create(std::shared_ptr<SatelliteServerInfo> info) {
  182. std::unique_ptr<SatelliteServer> satellite;
  183. if (info->getPort()) {
  184. switch (info->getType()) {
  185. case Type::KindOfInternalPageServer:
  186. satellite.reset(new InternalPageServer(info));
  187. break;
  188. case Type::KindOfDanglingPageServer:
  189. satellite.reset(new DanglingPageServer(info));
  190. break;
  191. case Type::KindOfRPCServer:
  192. satellite.reset(new RPCServer(info));
  193. break;
  194. case Type::KindOfXboxServer:
  195. satellite.reset(new RPCServer(info));
  196. break;
  197. default:
  198. assert(false);
  199. }
  200. if (satellite) {
  201. satellite->setName(info->getName());
  202. }
  203. }
  204. return satellite;
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. }