passenger 3.9.2.beta → 4.0.0.rc4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of passenger might be problematic. Click here for more details.

Files changed (159) hide show
  1. data/.travis.yml +3 -0
  2. data/NEWS +77 -7
  3. data/README.md +3 -11
  4. data/bin/passenger-install-apache2-module +24 -20
  5. data/bin/passenger-install-nginx-module +25 -23
  6. data/build/agents.rb +11 -0
  7. data/build/apache2.rb +9 -5
  8. data/build/basics.rb +37 -30
  9. data/build/common_library.rb +4 -1
  10. data/build/cplusplus_support.rb +5 -5
  11. data/build/cxx_tests.rb +28 -8
  12. data/build/integration_tests.rb +6 -3
  13. data/build/nginx.rb +3 -3
  14. data/build/packaging.rb +95 -57
  15. data/build/ruby_extension.rb +34 -21
  16. data/build/ruby_tests.rb +4 -2
  17. data/build/test_basics.rb +1 -1
  18. data/dev/run_travis.sh +36 -1
  19. data/doc/Users guide Apache.html +425 -308
  20. data/doc/Users guide Apache.idmap.txt +78 -70
  21. data/doc/Users guide Apache.index.sqlite3 +0 -0
  22. data/doc/Users guide Apache.txt +33 -92
  23. data/doc/Users guide Nginx.html +519 -220
  24. data/doc/Users guide Nginx.idmap.txt +78 -60
  25. data/doc/Users guide Nginx.txt +115 -26
  26. data/doc/Users guide Standalone.html +8 -2
  27. data/doc/users_guide_snippets/analysis_and_system_maintenance.txt +1 -7
  28. data/doc/users_guide_snippets/installation.txt +167 -22
  29. data/doc/users_guide_snippets/rackup_specifications.txt +4 -0
  30. data/doc/users_guide_snippets/since_version.txt +1 -0
  31. data/doc/users_guide_snippets/support_information.txt +3 -7
  32. data/doc/users_guide_snippets/tips.txt +0 -24
  33. data/ext/apache2/Configuration.cpp +11 -33
  34. data/ext/apache2/Configuration.hpp +3 -18
  35. data/ext/apache2/DirectoryMapper.h +20 -70
  36. data/ext/apache2/Hooks.cpp +2 -2
  37. data/ext/common/AgentsStarter.cpp +0 -2
  38. data/ext/common/AgentsStarter.h +0 -1
  39. data/ext/common/AgentsStarter.hpp +1 -3
  40. data/ext/common/ApplicationPool2/AppTypes.cpp +74 -0
  41. data/ext/common/ApplicationPool2/AppTypes.h +202 -0
  42. data/ext/common/ApplicationPool2/Common.h +12 -10
  43. data/ext/common/ApplicationPool2/DirectSpawner.h +256 -0
  44. data/ext/common/ApplicationPool2/DummySpawner.h +90 -0
  45. data/ext/common/ApplicationPool2/Group.h +311 -94
  46. data/ext/common/ApplicationPool2/Implementation.cpp +405 -145
  47. data/ext/common/ApplicationPool2/Options.h +24 -26
  48. data/ext/common/ApplicationPool2/PipeWatcher.h +20 -13
  49. data/ext/common/ApplicationPool2/Pool.h +326 -183
  50. data/ext/common/ApplicationPool2/Process.h +205 -55
  51. data/ext/common/ApplicationPool2/README.md +1 -1
  52. data/ext/common/ApplicationPool2/Session.h +21 -10
  53. data/ext/common/ApplicationPool2/SmartSpawner.h +801 -0
  54. data/ext/common/ApplicationPool2/Spawner.h +141 -1149
  55. data/ext/common/ApplicationPool2/SpawnerFactory.h +132 -0
  56. data/ext/common/ApplicationPool2/SuperGroup.h +146 -223
  57. data/ext/common/Constants.h +4 -2
  58. data/ext/common/Exceptions.h +23 -1
  59. data/ext/common/Logging.cpp +17 -6
  60. data/ext/common/Logging.h +37 -7
  61. data/ext/common/ResourceLocator.h +1 -1
  62. data/ext/common/Utils.cpp +49 -1
  63. data/ext/common/Utils.h +13 -4
  64. data/ext/common/{AnsiColorConstants.h → Utils/AnsiColorConstants.h} +0 -0
  65. data/ext/common/{BCrypt.cpp → Utils/BCrypt.cpp} +0 -0
  66. data/ext/common/{BCrypt.h → Utils/BCrypt.h} +0 -0
  67. data/ext/common/{Blowfish.c → Utils/Blowfish.c} +0 -0
  68. data/ext/common/{Blowfish.h → Utils/Blowfish.h} +0 -0
  69. data/ext/common/Utils/CachedFileStat.hpp +27 -25
  70. data/ext/common/Utils/Curl.h +184 -0
  71. data/ext/common/{HttpConstants.h → Utils/HttpConstants.h} +3 -0
  72. data/ext/common/Utils/IOUtils.cpp +6 -2
  73. data/ext/common/{IniFile.h → Utils/IniFile.h} +0 -0
  74. data/ext/common/Utils/LargeFiles.cpp +30 -0
  75. data/ext/common/Utils/LargeFiles.h +40 -0
  76. data/ext/common/Utils/StrIntUtils.cpp +72 -8
  77. data/ext/common/Utils/StrIntUtils.h +24 -2
  78. data/ext/common/Utils/StringMap.h +12 -2
  79. data/ext/common/Utils/VariantMap.h +51 -2
  80. data/ext/common/Utils/jsoncpp.cpp +1 -1
  81. data/ext/common/agents/Base.cpp +147 -11
  82. data/ext/common/agents/HelperAgent/AgentOptions.h +14 -6
  83. data/ext/common/agents/HelperAgent/Main.cpp +79 -19
  84. data/ext/common/agents/HelperAgent/RequestHandler.h +36 -16
  85. data/ext/common/agents/LoggingAgent/LoggingServer.h +3 -5
  86. data/ext/common/agents/LoggingAgent/Main.cpp +2 -4
  87. data/ext/common/agents/LoggingAgent/RemoteSender.h +18 -24
  88. data/ext/common/agents/SpawnPreparer.cpp +7 -0
  89. data/ext/common/agents/Watchdog/Main.cpp +96 -38
  90. data/ext/nginx/Configuration.c +26 -22
  91. data/ext/nginx/Configuration.h +4 -2
  92. data/ext/nginx/ContentHandler.c +23 -52
  93. data/ext/nginx/ContentHandler.h +5 -11
  94. data/ext/nginx/config +10 -3
  95. data/ext/nginx/ngx_http_passenger_module.c +21 -6
  96. data/ext/nginx/ngx_http_passenger_module.h +4 -1
  97. data/ext/oxt/dynamic_thread_group.hpp +9 -1
  98. data/ext/oxt/system_calls.cpp +2 -2
  99. data/ext/ruby/extconf.rb +2 -1
  100. data/helper-scripts/backtrace-sanitizer.rb +2 -0
  101. data/helper-scripts/wsgi-loader.py +54 -21
  102. data/lib/phusion_passenger.rb +5 -3
  103. data/lib/phusion_passenger/abstract_installer.rb +18 -41
  104. data/lib/phusion_passenger/admin_tools/memory_stats.rb +2 -2
  105. data/lib/phusion_passenger/admin_tools/server_instance.rb +2 -2
  106. data/lib/phusion_passenger/common_library.rb +23 -3
  107. data/lib/phusion_passenger/debug_logging.rb +10 -3
  108. data/lib/phusion_passenger/packaging.rb +1 -0
  109. data/lib/phusion_passenger/platform_info.rb +113 -115
  110. data/lib/phusion_passenger/platform_info/compiler.rb +224 -134
  111. data/lib/phusion_passenger/platform_info/cxx_portability.rb +143 -0
  112. data/lib/phusion_passenger/platform_info/depcheck.rb +371 -0
  113. data/lib/phusion_passenger/platform_info/depcheck_specs/apache2.rb +124 -0
  114. data/lib/phusion_passenger/platform_info/depcheck_specs/compiler_toolchain.rb +97 -0
  115. data/lib/phusion_passenger/platform_info/depcheck_specs/gems.rb +39 -0
  116. data/lib/phusion_passenger/platform_info/depcheck_specs/libs.rb +118 -0
  117. data/lib/phusion_passenger/platform_info/depcheck_specs/ruby.rb +137 -0
  118. data/lib/phusion_passenger/platform_info/depcheck_specs/utilities.rb +15 -0
  119. data/lib/phusion_passenger/platform_info/operating_system.rb +6 -5
  120. data/lib/phusion_passenger/platform_info/ruby.rb +45 -34
  121. data/lib/phusion_passenger/request_handler.rb +35 -22
  122. data/lib/phusion_passenger/request_handler/thread_handler.rb +5 -6
  123. data/lib/phusion_passenger/ruby_core_enhancements.rb +7 -1
  124. data/lib/phusion_passenger/standalone/runtime_installer.rb +43 -34
  125. data/lib/phusion_passenger/utils/robust_interruption.rb +34 -18
  126. data/passenger.gemspec +25 -0
  127. data/resources/templates/standalone/config.erb +3 -1
  128. data/test/config.json.travis +2 -2
  129. data/test/cxx/ApplicationPool2/DirectSpawnerTest.cpp +37 -5
  130. data/test/cxx/ApplicationPool2/PoolTest.cpp +143 -50
  131. data/test/cxx/ApplicationPool2/ProcessTest.cpp +8 -0
  132. data/test/cxx/ApplicationPool2/SmartSpawnerTest.cpp +28 -17
  133. data/test/cxx/ApplicationPool2/SpawnerTestCases.cpp +31 -26
  134. data/test/cxx/RequestHandlerTest.cpp +17 -1
  135. data/test/cxx/UtilsTest.cpp +84 -10
  136. data/test/integration_tests/apache2_tests.rb +49 -163
  137. data/test/integration_tests/hello_world_wsgi_spec.rb +2 -2
  138. data/test/integration_tests/mycook_spec.rb +1 -1
  139. data/test/integration_tests/nginx_tests.rb +37 -19
  140. data/test/ruby/request_handler_spec.rb +1 -0
  141. data/test/ruby/spec_helper.rb +52 -1
  142. data/test/stub/nginx/nginx.conf.erb +2 -0
  143. data/test/stub/rack/start.rb +5 -0
  144. data/test/stub/rails3.0/Gemfile.lock +30 -30
  145. data/test/stub/rails3.1/Gemfile +1 -1
  146. data/test/stub/rails3.1/Gemfile.lock +3 -3
  147. data/test/stub/rails3.2/Gemfile +1 -1
  148. data/test/stub/rails3.2/Gemfile.lock +4 -4
  149. data/test/stub/rails_apps/2.3/mycook/app/controllers/welcome_controller.rb +1 -1
  150. data/test/stub/rails_apps/2.3/mycook/app/helpers/recipes_helper.rb +2 -0
  151. data/test/stub/rails_apps/2.3/mycook/app/helpers/test_helper.rb +2 -0
  152. data/test/stub/rails_apps/2.3/mycook/app/helpers/uploads_helper.rb +2 -0
  153. data/test/stub/rails_apps/2.3/mycook/app/helpers/welcome_helper.rb +2 -0
  154. data/test/support/nginx_controller.rb +2 -1
  155. metadata +160 -156
  156. data/build/gempackagetask.rb +0 -99
  157. data/build/packagetask.rb +0 -186
  158. data/ext/common/StringListCreator.h +0 -83
  159. data/lib/phusion_passenger/dependencies.rb +0 -657
@@ -0,0 +1,184 @@
1
+ /*
2
+ * Phusion Passenger - https://www.phusionpassenger.com/
3
+ * Copyright (c) 2013 Phusion
4
+ *
5
+ * "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+ */
25
+ #ifndef _PASSENGER_UTILS_CURL_H_
26
+ #define _PASSENGER_UTILS_CURL_H_
27
+
28
+ /**
29
+ * Utilities for setting libcurl proxy information. Proxy information is contained
30
+ * in a user-supplied string in the form of:
31
+ *
32
+ * protocol://[username:password@]host[:port][?option1,option2]
33
+ *
34
+ * The address may also be `none`, which indicates that proxy usage should be
35
+ * explicitly disabled even when environment variables such as "http_proxy" etc
36
+ * are set.
37
+ *
38
+ * You are supposed to prepare a CurlProxyInfo object with prepareCurlProxy().
39
+ * Keep this object alive as long as you're using the CURL handle.
40
+ * Then, call setCurlProxy() to set the proxy information on the CURL handle.
41
+ *
42
+ * prepareCurlProxy() throws ArgumentException upon encountering an invalid
43
+ * proxy address.
44
+ *
45
+ * If the address is an empty string, prepareCurlProxy() and setCurlProxy()
46
+ * don't do anything.
47
+ */
48
+
49
+ #include <string>
50
+ #include <cstring>
51
+ #include <curl/curl.h>
52
+ #include <boost/foreach.hpp>
53
+ #include <Exceptions.h>
54
+ #include <Utils.h>
55
+ #include <Utils/StrIntUtils.h>
56
+
57
+ namespace Passenger {
58
+
59
+ using namespace std;
60
+ using namespace boost;
61
+
62
+ struct CurlProxyInfo {
63
+ string hostAndPort;
64
+ string credentials;
65
+ long type;
66
+ bool none;
67
+ bool httpTunnel;
68
+
69
+ CurlProxyInfo()
70
+ : type(0),
71
+ none(false),
72
+ httpTunnel(false)
73
+ { }
74
+ };
75
+
76
+ inline const CurlProxyInfo
77
+ prepareCurlProxy(const string &address) {
78
+ if (address.empty()) {
79
+ return CurlProxyInfo();
80
+ } else if (address == "none") {
81
+ CurlProxyInfo result;
82
+ result.none = true;
83
+ return result;
84
+ } else {
85
+ CurlProxyInfo result;
86
+ size_t protocolLen;
87
+ vector<string> options;
88
+ string::size_type pos;
89
+
90
+ if (startsWith(address, "http://")) {
91
+ protocolLen = strlen("http://");
92
+ result.type = CURLPROXY_HTTP;
93
+ } else if (startsWith(address, "socks5://")) {
94
+ protocolLen = strlen("socks5://");
95
+ result.type = CURLPROXY_SOCKS5;
96
+ } else if (startsWith(address, "socks4://")) {
97
+ protocolLen = strlen("socks4://");
98
+ #if LIBCURL_VERSION_NUM >= 0x070A00
99
+ result.type = CURLPROXY_SOCKS4;
100
+ #else
101
+ throw ArgumentException("Socks4 proxies are not supported because "
102
+ "libcurl doesn't support it. Please upgrade libcurl to version "
103
+ "7.10 or higher.");
104
+ #endif
105
+ } else if (startsWith(address, "socks4a://")) {
106
+ protocolLen = strlen("socks4a://");
107
+ #if LIBCURL_VERSION_NUM >= 0x071200
108
+ result.type = CURLPROXY_SOCKS4A;
109
+ #else
110
+ throw ArgumentException("Socks4A proxies are not supported because "
111
+ "libcurl doesn't support it. Please upgrade libcurl to version "
112
+ "7.18.0 or higher.");
113
+ #endif
114
+ } else if (startsWith(address, "socks5h://")) {
115
+ protocolLen = strlen("socks5h://");
116
+ #if LIBCURL_VERSION_NUM >= 0x071200
117
+ result.type = CURLPROXY_SOCKS5_HOSTNAME;
118
+ #else
119
+ throw ArgumentException("Socks5 proxies (with proxy DNS resolving) are "
120
+ "not supported because libcurl doesn't support it. Please upgrade "
121
+ "libcurl to version 7.18.0 or higher.");
122
+ #endif
123
+ } else if (address.find("://") == string::npos) {
124
+ throw ArgumentException("Invalid proxy address: no protocol specified.");
125
+ } else {
126
+ throw ArgumentException("Only 'http' and 'socks5' proxies are supported.");
127
+ }
128
+
129
+ result.hostAndPort = address.substr(protocolLen);
130
+
131
+ // Extract options.
132
+ pos = result.hostAndPort.find("?");
133
+ if (pos != string::npos) {
134
+ string optionsString = result.hostAndPort.substr(pos + 1);
135
+ result.hostAndPort.erase(pos);
136
+ split(optionsString, ',', options);
137
+ }
138
+
139
+ // Extract authentication credentials.
140
+ pos = result.hostAndPort.find("@");
141
+ if (pos != string::npos) {
142
+ result.credentials = result.hostAndPort.substr(0, pos);
143
+ result.hostAndPort.erase(0, pos + 1);
144
+ }
145
+
146
+ if (result.hostAndPort.empty()) {
147
+ throw ArgumentException("No proxy host name given.");
148
+ }
149
+
150
+ foreach (const string option, options) {
151
+ if (option == "tunnel") {
152
+ if (result.type == CURLPROXY_HTTP) {
153
+ result.httpTunnel = true;
154
+ } else {
155
+ throw ArgumentException("The 'tunnel' option is only supported for HTTP proxies.");
156
+ }
157
+ } else {
158
+ throw ArgumentException("Invalid proxy address option '" + option + "'.");
159
+ }
160
+ }
161
+
162
+ return result;
163
+ }
164
+ }
165
+
166
+ inline void
167
+ setCurlProxy(CURL *curl, const CurlProxyInfo &proxyInfo) {
168
+ if (proxyInfo.hostAndPort.empty()) {
169
+ return;
170
+ } else if (proxyInfo.none) {
171
+ curl_easy_setopt(curl, CURLOPT_PROXY, "");
172
+ } else {
173
+ curl_easy_setopt(curl, CURLOPT_PROXY, proxyInfo.hostAndPort.c_str());
174
+ curl_easy_setopt(curl, CURLOPT_PROXYTYPE, proxyInfo.type);
175
+ curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyInfo.credentials.c_str());
176
+ if (proxyInfo.httpTunnel) {
177
+ curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1);
178
+ }
179
+ }
180
+ }
181
+
182
+ } // namespace Passenger
183
+
184
+ #endif /* _PASSENGER_UTILS_CURL_H_ */
@@ -131,6 +131,9 @@ getStatusCodeAndReasonPhrase(int statusCode) {
131
131
  return "429 Too Many Requests";
132
132
  case 431:
133
133
  return "431 Request Header Fields Too Large";
134
+ case 444:
135
+ // Nginx specific, used for timeouts.
136
+ return "444 No Response";
134
137
  case 449:
135
138
  return "449 Retry With";
136
139
  case 450:
@@ -1148,8 +1148,12 @@ readAll(int fd) {
1148
1148
  if (ret == 0) {
1149
1149
  break;
1150
1150
  } else if (ret == -1) {
1151
- int e = errno;
1152
- throw SystemException("Cannot read from file descriptor", e);
1151
+ if (errno == ECONNRESET) {
1152
+ break;
1153
+ } else {
1154
+ int e = errno;
1155
+ throw SystemException("Cannot read from file descriptor", e);
1156
+ }
1153
1157
  } else {
1154
1158
  result.append(buf, ret);
1155
1159
  }
@@ -0,0 +1,30 @@
1
+ #define _FILE_OFFSET_BITS 64
2
+ #define _LARGE_FILES 1
3
+ #define _LARGEFILE_SOURCE
4
+ #define _LARGEFILE64_SOURCE
5
+
6
+ #include <Utils/LargeFiles.h>
7
+ #include <stdlib.h>
8
+
9
+ namespace Passenger {
10
+
11
+ FILE *
12
+ lfs_fopen(const char *filename, const char *mode) {
13
+ return fopen(filename, mode);
14
+ }
15
+
16
+ FILE *
17
+ lfs_fdopen(int filedes, const char *mode) {
18
+ return fdopen(filedes, mode);
19
+ }
20
+
21
+ int
22
+ lfs_mkstemp(char *templ) {
23
+ #ifdef __linux__
24
+ return mkstemp64(templ);
25
+ #else
26
+ return mkstemp(templ);
27
+ #endif
28
+ }
29
+
30
+ } // namespace Passenger
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Phusion Passenger - https://www.phusionpassenger.com/
3
+ * Copyright (c) 2013 Phusion
4
+ *
5
+ * "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+ */
25
+ #ifndef _PASSENGER_UTILS_LARGE_FILES_H_
26
+ #define _PASSENGER_UTILS_LARGE_FILES_H_
27
+
28
+ #include <stdio.h>
29
+
30
+ /* Allows access to Large File Support APIs, if the OS supports them. */
31
+
32
+ namespace Passenger {
33
+
34
+ FILE *lfs_fopen(const char *filename, const char *mode);
35
+ FILE *lfs_fdopen(int filedes, const char *mode);
36
+ int lfs_mkstemp(char *templ);
37
+
38
+ } // namespace Passenger
39
+
40
+ #endif /* _PASSENGER_UTILS_LARGE_FILES_H_ */
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * Phusion Passenger - https://www.phusionpassenger.com/
3
- * Copyright (c) 2010, 2011, 2012 Phusion
3
+ * Copyright (c) 2010-2013 Phusion
4
4
  *
5
5
  * "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
6
6
  *
@@ -59,16 +59,18 @@ startsWith(const StaticString &str, const StaticString &substr) {
59
59
  }
60
60
 
61
61
  template<typename OutputString>
62
- void
62
+ static void
63
63
  _split(const StaticString &str, char sep, vector<OutputString> &output) {
64
- string::size_type start, pos;
65
- start = 0;
66
64
  output.clear();
67
- while ((pos = str.find(sep, start)) != string::npos) {
68
- output.push_back(str.substr(start, pos - start));
69
- start = pos + 1;
65
+ if (!str.empty()) {
66
+ string::size_type start, pos;
67
+ start = 0;
68
+ while ((pos = str.find(sep, start)) != string::npos) {
69
+ output.push_back(str.substr(start, pos - start));
70
+ start = pos + 1;
71
+ }
72
+ output.push_back(str.substr(start));
70
73
  }
71
- output.push_back(str.substr(start));
72
74
  }
73
75
 
74
76
  void
@@ -81,6 +83,33 @@ split(const StaticString &str, char sep, vector<StaticString> &output) {
81
83
  _split(str, sep, output);
82
84
  }
83
85
 
86
+ template<typename OutputString>
87
+ static void
88
+ _splitIncludeSep(const StaticString &str, char sep, vector<OutputString> &output) {
89
+ output.clear();
90
+ if (!str.empty()) {
91
+ string::size_type start, pos;
92
+ start = 0;
93
+ while ((pos = str.find(sep, start)) != string::npos) {
94
+ output.push_back(str.substr(start, pos - start + 1));
95
+ start = pos + 1;
96
+ }
97
+ if (start != str.size()) {
98
+ output.push_back(str.substr(start));
99
+ }
100
+ }
101
+ }
102
+
103
+ void
104
+ splitIncludeSep(const StaticString &str, char sep, vector<string> &output) {
105
+ _splitIncludeSep(str, sep, output);
106
+ }
107
+
108
+ void
109
+ splitIncludeSep(const StaticString &str, char sep, vector<StaticString> &output) {
110
+ _splitIncludeSep(str, sep, output);
111
+ }
112
+
84
113
  string
85
114
  replaceString(const string &str, const string &toFind, const string &replaceWith) {
86
115
  string::size_type pos = str.find(toFind);
@@ -92,6 +121,19 @@ replaceString(const string &str, const string &toFind, const string &replaceWith
92
121
  }
93
122
  }
94
123
 
124
+ string
125
+ strip(const StaticString &str) {
126
+ const char *data = str.data();
127
+ const char *end = str.data() + str.size();
128
+ while (data < end && (*data == ' ' || *data == '\n' || *data == '\t')) {
129
+ data++;
130
+ }
131
+ while (end > data && (end[-1] == ' ' || end[-1] == '\n' || end[-1] == '\t')) {
132
+ end--;
133
+ }
134
+ return string(data, end - data);
135
+ }
136
+
95
137
  string
96
138
  toString(const vector<string> &vec) {
97
139
  vector<StaticString> vec2;
@@ -333,6 +375,28 @@ atol(const string &s) {
333
375
  return ::atol(s.c_str());
334
376
  }
335
377
 
378
+ bool
379
+ constantTimeCompare(const StaticString &a, const StaticString &b) {
380
+ // http://blog.jasonmooberry.com/2010/10/constant-time-string-comparison/
381
+ // See also ActiveSupport::MessageVerifier#secure_compare.
382
+ if (a.size() != b.size()) {
383
+ return false;
384
+ } else {
385
+ const char *x = a.data();
386
+ const char *y = b.data();
387
+ const char *end = a.data() + a.size();
388
+ int result = 0;
389
+
390
+ while (x < end) {
391
+ result |= *x ^ *y;
392
+ x++;
393
+ y++;
394
+ }
395
+
396
+ return result == 0;
397
+ }
398
+ }
399
+
336
400
  string
337
401
  distanceOfTimeInWords(time_t fromTime, time_t toTime) {
338
402
  time_t seconds;
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * Phusion Passenger - https://www.phusionpassenger.com/
3
- * Copyright (c) 2010, 2011, 2012 Phusion
3
+ * Copyright (c) 2010-2013 Phusion
4
4
  *
5
5
  * "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
6
6
  *
@@ -67,7 +67,8 @@ string fillInMiddle(unsigned int max, const string &prefix, const string &middle
67
67
  bool startsWith(const StaticString &str, const StaticString &substr);
68
68
 
69
69
  /**
70
- * Split the given string using the given separator.
70
+ * Split the given string using the given separator. Excludes the
71
+ * separator from the output.
71
72
  *
72
73
  * @param str The string to split.
73
74
  * @param sep The separator to use.
@@ -76,12 +77,28 @@ bool startsWith(const StaticString &str, const StaticString &substr);
76
77
  void split(const StaticString &str, char sep, vector<string> &output);
77
78
  void split(const StaticString &str, char sep, vector<StaticString> &output);
78
79
 
80
+ /**
81
+ * Split the given string using the given separator. Includes the
82
+ * separator in the output, at the end of an item.
83
+ *
84
+ * @param str The string to split.
85
+ * @param sep The separator to use.
86
+ * @param output The vector to write the output to.
87
+ */
88
+ void splitIncludeSep(const StaticString &str, char sep, vector<string> &output);
89
+ void splitIncludeSep(const StaticString &str, char sep, vector<StaticString> &output);
90
+
79
91
  /**
80
92
  * Look for 'toFind' inside 'str', replace it with 'replaceWith' and return the result.
81
93
  * Only the first occurence of 'toFind' is replaced.
82
94
  */
83
95
  string replaceString(const string &str, const string &toFind, const string &replaceWith);
84
96
 
97
+ /**
98
+ * Strips leading and trailing whitespaces.
99
+ */
100
+ string strip(const StaticString &str);
101
+
85
102
  /**
86
103
  * Convert anything to a string.
87
104
  */
@@ -225,6 +242,11 @@ roundUp(IntegerType number, IntegerType multiple) {
225
242
  return (number + multiple - 1) / multiple * multiple;
226
243
  }
227
244
 
245
+ /**
246
+ * Compare two strings using a constant time algorithm to avoid timing attacks.
247
+ */
248
+ bool constantTimeCompare(const StaticString &a, const StaticString &b);
249
+
228
250
  string distanceOfTimeInWords(time_t fromTime, time_t toTime = 0);
229
251
 
230
252
  /**