2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
17 #include "hphp/runtime/base/config.h"
19 #include <boost/algorithm/string.hpp>
20 #include <boost/algorithm/string/erase.hpp>
21 #include <boost/algorithm/string/predicate.hpp>
22 #include <boost/filesystem.hpp>
25 #include "hphp/runtime/base/ini-setting.h"
26 #include "hphp/runtime/base/array-iterator.h"
27 #include "hphp/util/logger.h"
30 ///////////////////////////////////////////////////////////////////////////////
33 Config::IniName(const Hdf
& config
, bool /*prepend_hhvm*/ /* = true */) {
34 return Config::IniName(config
.getFullPath());
37 std::string
Config::IniName(const std::string
& config
,
38 bool prepend_hhvm
/* = true */) {
44 for (auto& c
: config
) {
45 // This is the first or last character
46 if (idx
== 0 || idx
== config
.length() - 1) {
48 } else if (!isalpha(c
)) {
49 // Any . or _ or numeral is just output with no special behavior
52 if (isupper(c
) && isupper(config
[idx
- 1 ]) && islower(config
[idx
+ 1])) {
53 // Handle something like "SSLPort", and c = "P", which will then put
54 // the underscore between the "L" and "P"
57 } else if (islower(c
) && isupper(config
[idx
+ 1])) {
58 // Handle something like "PathDebug", and c = "h", which will then put
59 // the underscore between the "h" and "D"
63 // Otherwise we just output as lower
70 // The HHIRLICM runtime option is all capitals, so separation
71 // cannot be determined. Special case it.
72 boost::replace_first(out
, "hhirlicm", "hhir_licm");
73 // The HHVM ini option becomes the standard PHP option.
74 boost::replace_first(out
,
75 "hhvm.server.upload.max_file_uploads",
77 // Make sure IPv6 or IPv4 are handled correctly
78 boost::replace_first(out
, "_i_pv", "_ipv");
79 boost::replace_first(out
, ".i_pv", ".ipv");
80 // urls are special too. Let's not have "ur_ls"
81 boost::replace_first(out
, "_ur_ls", "_urls");
82 boost::replace_first(out
, ".ur_ls", ".urls");
83 // No use of Eval in our ini strings
84 boost::replace_first(out
, ".eval.", ".");
85 boost::replace_first(out
, ".my_sql.", ".mysql.");
86 boost::replace_first(out
, ".enable_hip_hop_syntax", ".force_hh");
88 // Fix "XDebug" turning into "x_debug".
89 boost::replace_first(out
, "hhvm.debugger.x_debug_", "xdebug.");
94 void Config::ParseIniString(const std::string
&iniStr
, IniSettingMap
&ini
,
95 const bool constants_only
/* = false */ ) {
96 Config::SetParsedIni(ini
, iniStr
, "", constants_only
, true);
99 void Config::ParseHdfString(const std::string
&hdfStr
, Hdf
&hdf
) {
100 hdf
.fromString(hdfStr
.c_str());
103 void Config::ParseConfigFile(const std::string
&filename
, IniSettingMap
&ini
,
104 Hdf
&hdf
, const bool is_system
/* = true */) {
105 // We don't allow a filename of just ".ini"
106 if (boost::ends_with(filename
, ".ini") && filename
.length() > 4) {
107 Config::ParseIniFile(filename
, ini
, false, is_system
);
109 // For now, assume anything else is an hdf file
110 // TODO(#5151773): Have a non-invasive warning if HDF file does not end
112 Config::ParseHdfFile(filename
, hdf
);
116 void Config::ParseIniFile(const std::string
&filename
,
117 const bool is_system
/* = true */) {
118 IniSettingMap ini
= IniSettingMap();
119 Config::ParseIniFile(filename
, ini
, false, is_system
);
122 void Config::ParseIniFile(const std::string
&filename
, IniSettingMap
&ini
,
123 const bool constants_only
/* = false */,
124 const bool is_system
/* = true */ ) {
125 std::ifstream
ifs(filename
);
126 std::string
str((std::istreambuf_iterator
<char>(ifs
)),
127 std::istreambuf_iterator
<char>());
128 std::string with_includes
;
129 Config::ReplaceIncludesWithIni(filename
, str
, with_includes
);
130 Config::SetParsedIni(ini
, with_includes
, filename
, constants_only
,
134 void Config::ReplaceIncludesWithIni(const std::string
& original_ini_filename
,
135 const std::string
& iniStr
,
136 std::string
& with_includes
) {
137 std::istringstream
iss(iniStr
);
139 while (std::getline(iss
, line
)) {
142 // ##includefoo barbaz"myconfig.ini" how weird is that
143 // Anything that is not a syntactically correct #include "file" after
144 // this pre-processing, will be treated as an ini comment and processed
145 // as such in the ini parser
146 auto pos
= line
.find_first_not_of(" ");
147 if (pos
== std::string::npos
||
148 line
.compare(pos
, strlen("#include"), "#include") != 0) {
149 // treat as normal ini line, including comment that doesn't start with
151 with_includes
+= line
+ "\n";
154 pos
+= strlen("#include");
155 auto start
= line
.find_first_not_of(" ", pos
);
156 auto end
= line
.find_last_not_of(" ");
157 if ((start
== std::string::npos
|| line
[start
] != '"') ||
158 (end
== start
|| line
[end
] != '"')) {
159 with_includes
+= line
+ "\n"; // treat as normal comment
162 std::string file
= line
.substr(start
+ 1, end
- start
- 1);
163 const std::string logger_file
= file
;
164 boost::filesystem::path
p(file
);
165 if (!p
.is_absolute()) {
166 boost::filesystem::path
opath(original_ini_filename
);
167 p
= opath
.parent_path()/p
;
169 if (boost::filesystem::exists(p
)) {
170 std::ifstream
ifs(p
.string());
171 const std::string
contents((std::istreambuf_iterator
<char>(ifs
)),
172 std::istreambuf_iterator
<char>());
173 Config::ReplaceIncludesWithIni(p
.string(), contents
, with_includes
);
175 Logger::Warning("ini include file %s not found", logger_file
.c_str());
180 void Config::ParseHdfFile(const std::string
&filename
, Hdf
&hdf
) {
181 hdf
.append(filename
);
184 void Config::SetParsedIni(IniSettingMap
&ini
, const std::string confStr
,
185 const std::string
&filename
, bool constants_only
,
187 // if we are setting constants, we must be setting system settings
188 if (constants_only
) {
191 auto parsed_ini
= IniSetting::FromStringAsMap(confStr
, filename
);
192 for (ArrayIter
iter(parsed_ini
.toArray()); iter
; ++iter
) {
193 // most likely a string, but just make sure that we are dealing
194 // with something that can be converted to a string
195 assert(iter
.first().isScalar());
196 ini
.set(iter
.first().toString(), iter
.second());
197 if (constants_only
) {
198 IniSetting::FillInConstant(iter
.first().toString().toCppString(),
200 } else if (is_system
) {
201 IniSetting::SetSystem(iter
.first().toString().toCppString(),
207 // This method must return a char* which is owned by the IniSettingMap
208 // to avoid issues with the lifetime of the char*
209 const char* Config::Get(const IniSettingMap
&ini
, const Hdf
& config
,
210 const std::string
& name
/* = "" */,
211 const char *defValue
/* = nullptr */,
212 const bool prepend_hhvm
/* = true */) {
213 auto ini_name
= IniName(name
, prepend_hhvm
);
214 Hdf hdf
= name
!= "" ? config
[name
] : config
;
215 auto value
= ini_iterate(ini
, ini_name
);
216 if (value
.isString()) {
217 // See generic Get##METHOD below for why we are doing this
218 // Note that value is a string, so value.toString() is not
220 const char* ini_ret
= value
.toString().data();
221 const char* hdf_ret
= hdf
.configGet(ini_ret
);
222 if (hdf_ret
!= ini_ret
) {
224 IniSetting::SetSystem(ini_name
, ini_ret
);
228 return hdf
.configGet(defValue
);
231 template<class T
> static T
variant_init(T v
) {
234 static int64_t variant_init(uint32_t v
) {
238 #define CONFIG_BODY(T, METHOD) \
239 T Config::Get##METHOD(const IniSetting::Map &ini, const Hdf& config, \
240 const std::string &name /* = "" */, \
241 const T defValue /* = 0ish */, \
242 const bool prepend_hhvm /* = true */) { \
243 auto ini_name = IniName(name, prepend_hhvm); \
244 /* If we don't pass a name, then we just use the raw config as-is. */ \
245 /* This could happen when we are at a known leaf of a config node. */ \
246 Hdf hdf = name != "" ? config[name] : config; \
247 auto value = ini_iterate(ini, ini_name); \
248 if (value.isString()) { \
249 T ini_ret, hdf_ret; \
250 ini_on_update(value.toString(), ini_ret); \
251 /* I don't care what the ini_ret was if it isn't equal to what */ \
252 /* is returned back from from an HDF get call, which it will be */ \
253 /* if the call just passes back ini_ret because either they are */ \
254 /* the same or the hdf option associated with this name does */ \
255 /* not exist.... REMEMBER HDF WINS OVER INI UNTIL WE WIPE HDF */ \
256 hdf_ret = hdf.configGet##METHOD(ini_ret); \
257 if (hdf_ret != ini_ret) { \
259 IniSetting::SetSystem(ini_name, variant_init(ini_ret)); \
263 /* If there is a value associated with this setting in the hdf config */ \
264 /* then return it; otherwise the defValue will be returned as it is */ \
265 /* assigned to the return value for this call when nothing exists */ \
266 return hdf.configGet##METHOD(defValue); \
268 void Config::Bind(T& loc, const IniSetting::Map &ini, const Hdf& config, \
269 const std::string& name /* = "" */, \
270 const T defValue /* = 0ish */, \
271 const bool prepend_hhvm /* = true */) { \
272 loc = Get##METHOD(ini, config, name, defValue, prepend_hhvm); \
273 IniSetting::Bind(IniSetting::CORE, IniSetting::PHP_INI_SYSTEM, \
274 IniName(name, prepend_hhvm), &loc); \
277 CONFIG_BODY(bool, Bool
)
278 CONFIG_BODY(char, Byte
)
279 CONFIG_BODY(unsigned char, UByte
)
280 CONFIG_BODY(int16_t, Int16
)
281 CONFIG_BODY(uint16_t, UInt16
)
282 CONFIG_BODY(int32_t, Int32
)
283 CONFIG_BODY(uint32_t, UInt32
)
284 CONFIG_BODY(int64_t, Int64
)
285 CONFIG_BODY(uint64_t, UInt64
)
286 CONFIG_BODY(double, Double
)
287 CONFIG_BODY(std::string
, String
)
289 #define CONTAINER_CONFIG_BODY(T, METHOD) \
290 T Config::Get##METHOD(const IniSetting::Map& ini, const Hdf& config, \
291 const std::string& name /* = "" */, \
292 const T& defValue /* = T() */, \
293 const bool prepend_hhvm /* = true */) { \
294 auto ini_name = IniName(name, prepend_hhvm); \
295 Hdf hdf = name != "" ? config[name] : config; \
296 T ini_ret, hdf_ret; \
297 auto value = ini_iterate(ini, ini_name); \
298 if (value.isArray() || value.isObject()) { \
299 ini_on_update(value.toVariant(), ini_ret); \
300 /** Make sure that even if we have an ini value, that if we also **/ \
301 /** have an hdf value, that it maintains its edge as beating out **/ \
303 if (hdf.exists() && !hdf.isEmpty()) { \
304 hdf.configGet(hdf_ret); \
305 if (hdf_ret != ini_ret) { \
307 IniSetting::SetSystem(ini_name, ini_get(ini_ret)); \
312 if (hdf.exists() && !hdf.isEmpty()) { \
313 hdf.configGet(hdf_ret); \
318 void Config::Bind(T& loc, const IniSetting::Map& ini, const Hdf& config, \
319 const std::string& name /* = "" */, \
320 const T& defValue /* = T() */, \
321 const bool prepend_hhvm /* = true */) { \
322 loc = Get##METHOD(ini, config, name, defValue, prepend_hhvm); \
323 IniSetting::Bind(IniSetting::CORE, IniSetting::PHP_INI_SYSTEM, \
324 IniName(name, prepend_hhvm), &loc); \
327 CONTAINER_CONFIG_BODY(ConfigVector
, Vector
)
328 CONTAINER_CONFIG_BODY(ConfigMap
, Map
)
329 CONTAINER_CONFIG_BODY(ConfigMapC
, MapC
)
330 CONTAINER_CONFIG_BODY(ConfigSet
, Set
)
331 CONTAINER_CONFIG_BODY(ConfigSetC
, SetC
)
332 CONTAINER_CONFIG_BODY(ConfigFlatSet
, FlatSet
)
333 CONTAINER_CONFIG_BODY(ConfigIMap
, IMap
)
335 static HackStrictOption
GetHackStrictOption(const IniSettingMap
& ini
,
337 const std::string
& name
/* = "" */,
340 auto val
= Config::GetString(ini
, config
, name
);
345 return HackStrictOption::WARN
;
348 ini_on_update(val
, ret
);
349 return ret
? HackStrictOption::ON
: HackStrictOption::OFF
;
352 void Config::Bind(HackStrictOption
& loc
, const IniSettingMap
& ini
,
353 const Hdf
& config
, const std::string
& name
/* = "" */,
354 HackStrictOption def
) {
355 // Currently this doens't bind to ini_get since it is hard to thread through
357 loc
= GetHackStrictOption(ini
, config
, name
, def
);
360 // No `ini` binding yet. Hdf still takes precedence but will be removed
361 // once we have made all options ini-aware. All new settings should
362 // use the ini path of this method (i.e., pass a bogus Hdf or keep it null)
363 void Config::Iterate(std::function
<void (const IniSettingMap
&,
365 const std::string
&)> cb
,
366 const IniSettingMap
&ini
, const Hdf
& config
,
367 const std::string
&name
,
368 const bool prepend_hhvm
/* = true */) {
369 Hdf hdf
= name
.empty() ? config
: config
[name
];
370 if (hdf
.exists() && !hdf
.isEmpty()) {
371 for (Hdf c
= hdf
.firstChild(); c
.exists(); c
= c
.next()) {
372 cb(IniSetting::Map::object
, c
, "");
376 auto ini_value
= name
.empty() ? ini
:
377 ini_iterate(ini
, IniName(name
, prepend_hhvm
));
378 if (ini_value
.isArray()) {
379 for (ArrayIter
iter(ini_value
.toArray()); iter
; ++iter
) {
380 cb(iter
.second(), empty
, iter
.first().toString().toCppString());