/hphp/runtime/server/virtual-host.h

https://github.com/soitun/hiphop-php · C Header · 140 lines · 95 code · 22 blank · 23 comment · 1 complexity · c0135f3a558757339b6daef64dd9f35b MD5 · raw file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-present 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. #pragma once
  17. #include "hphp/util/hdf.h"
  18. #include "hphp/runtime/server/ip-block-map.h"
  19. #include "hphp/runtime/base/ini-setting.h"
  20. namespace HPHP {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. struct VirtualHost {
  23. static VirtualHost &GetDefault();
  24. static VirtualHost* Resolve(const std::string& host);
  25. static void SetCurrent(VirtualHost *vhost);
  26. static const VirtualHost *GetCurrent();
  27. static int64_t GetMaxPostSize();
  28. static int64_t GetLowestMaxPostSize();
  29. static int64_t GetUploadMaxFileSize();
  30. static void UpdateSerializationSizeLimit();
  31. static const std::vector<std::string> &GetAllowedDirectories();
  32. static void SortAllowedDirectories(std::vector<std::string>& dirs);
  33. static bool IsDefault(const IniSetting::Map &ini, const Hdf &vh,
  34. const std::string &ini_key = "");
  35. public:
  36. VirtualHost();
  37. VirtualHost(const IniSetting::Map& ini, const Hdf& vh,
  38. const std::string &ini_key = "");
  39. void init(const IniSetting::Map& ini, const Hdf& vh,
  40. const std::string &ini_key = "");
  41. void addAllowedDirectories(const std::vector<std::string>& dirs);
  42. int getRequestTimeoutSeconds(int defaultTimeout) const;
  43. int64_t getMaxPostSize() const;
  44. const std::string &getName() const { return m_name;}
  45. const std::string &getPathTranslation() const { return m_pathTranslation;}
  46. const std::string &getDocumentRoot() const { return m_documentRoot;}
  47. const std::map<std::string, std::string> &getServerVars() const {
  48. return m_serverVars;
  49. }
  50. std::string serverName(const std::string &host) const;
  51. bool valid() const { return !(m_prefix.empty() && !m_pattern); }
  52. bool match(const String &host) const;
  53. bool disabled() const { return m_disabled; }
  54. // whether to check (and serve) files that exist before applying rewrite rules
  55. bool checkExistenceBeforeRewrite() const {
  56. return m_checkExistenceBeforeRewrite;
  57. }
  58. // should we always decode the post data as if it were
  59. // application/x-www-form-urlencoded
  60. bool alwaysDecodePostData(const String& url) const;
  61. // url rewrite rules
  62. bool rewriteURL(const String& host, String &url,
  63. bool &qsa, int &redirect) const;
  64. // ip blocking rules
  65. bool isBlocking(const std::string &command, const std::string &ip) const;
  66. // query string filters
  67. bool hasLogFilter() const { return !m_queryStringFilters.empty();}
  68. std::string filterUrl(const std::string &url) const;
  69. private:
  70. struct RewriteCond {
  71. enum class Type {
  72. Request,
  73. Host
  74. };
  75. Type type;
  76. StringData* pattern;
  77. bool negate = false;
  78. };
  79. struct RewriteRule {
  80. StringData* pattern;
  81. std::string to;
  82. bool qsa = false; // whether to append original query string
  83. bool encode_backrefs = false;
  84. int redirect = 0; // redirect status code (301 or 302) or 0 for no redirect
  85. std::vector<RewriteCond> rewriteConds;
  86. };
  87. struct QueryStringFilter {
  88. std::string urlPattern; // matching URLs
  89. std::string namePattern; // matching parameter names
  90. std::string replaceWith; // what to replace with
  91. };
  92. struct VhostRuntimeOption {
  93. public:
  94. int requestTimeoutSeconds = -1;
  95. int64_t maxPostSize = -1;
  96. int64_t uploadMaxFileSize = -1;
  97. std::vector<std::string> allowedDirectories;
  98. int64_t serializationSizeLimit = StringData::MaxSize;
  99. };
  100. void initRuntimeOption(const IniSetting::Map& ini, const Hdf& overwrite);
  101. bool m_disabled = false;
  102. bool m_checkExistenceBeforeRewrite = true;
  103. bool m_alwaysDecodePostData = true;
  104. std::set<std::string, stdltistr> m_decodePostDataBlackList;
  105. std::string m_name;
  106. std::string m_prefix;
  107. StringData* m_pattern{nullptr};
  108. std::string m_serverName;
  109. std::map<std::string, std::string> m_serverVars;
  110. std::string m_pathTranslation;
  111. std::string m_documentRoot;
  112. std::vector<RewriteRule> m_rewriteRules;
  113. std::shared_ptr<IpBlockMap> m_ipBlocks;
  114. std::vector<QueryStringFilter> m_queryStringFilters;
  115. VhostRuntimeOption m_runtimeOption;
  116. };
  117. ///////////////////////////////////////////////////////////////////////////////
  118. }