passenger 2.2.1 → 2.2.2

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 (216) hide show
  1. data/Rakefile +66 -79
  2. data/bin/passenger-install-nginx-module +1 -1
  3. data/bin/passenger-memory-stats +1 -1
  4. data/bin/passenger-spawn-server +8 -2
  5. data/doc/Users guide Apache.html +33 -49
  6. data/doc/Users guide Apache.txt +28 -27
  7. data/doc/Users guide Nginx.html +9 -19
  8. data/doc/Users guide Nginx.txt +8 -20
  9. data/doc/cxxapi/Bucket_8h-source.html +1 -1
  10. data/doc/cxxapi/Configuration_8h-source.html +297 -300
  11. data/doc/cxxapi/DirectoryMapper_8h-source.html +1 -1
  12. data/doc/cxxapi/Hooks_8h-source.html +1 -1
  13. data/doc/cxxapi/annotated.html +1 -1
  14. data/doc/cxxapi/classHooks-members.html +1 -1
  15. data/doc/cxxapi/classHooks.html +1 -1
  16. data/doc/cxxapi/classPassenger_1_1DirectoryMapper-members.html +1 -1
  17. data/doc/cxxapi/classPassenger_1_1DirectoryMapper.html +1 -1
  18. data/doc/cxxapi/classes.html +1 -1
  19. data/doc/cxxapi/definitions_8h-source.html +1 -1
  20. data/doc/cxxapi/files.html +1 -1
  21. data/doc/cxxapi/functions.html +1 -1
  22. data/doc/cxxapi/functions_func.html +1 -1
  23. data/doc/cxxapi/graph_legend.html +1 -1
  24. data/doc/cxxapi/group__Configuration.html +1 -23
  25. data/doc/cxxapi/group__Core.html +1 -1
  26. data/doc/cxxapi/group__Hooks.html +1 -1
  27. data/doc/cxxapi/group__Support.html +1 -1
  28. data/doc/cxxapi/main.html +1 -1
  29. data/doc/cxxapi/modules.html +1 -1
  30. data/doc/users_guide_snippets/rackup_specifications.txt +4 -2
  31. data/ext/apache2/Configuration.h +0 -3
  32. data/ext/apache2/Hooks.cpp +5 -3
  33. data/ext/common/ApplicationPoolServer.h +1 -0
  34. data/ext/common/ApplicationPoolServerExecutable.cpp +5 -2
  35. data/ext/common/SpawnManager.h +1 -0
  36. data/ext/common/Utils.cpp +22 -23
  37. data/ext/common/Utils.h +32 -21
  38. data/ext/common/Version.h +31 -0
  39. data/ext/nginx/ContentHandler.c +61 -8
  40. data/ext/nginx/HelperServer.cpp +3 -0
  41. data/ext/nginx/HttpStatusExtractor.h +185 -0
  42. data/ext/nginx/StaticContentHandler.c +18 -3
  43. data/ext/nginx/config +2 -1
  44. data/ext/nginx/ngx_http_passenger_module.c +21 -15
  45. data/ext/oxt/backtrace.cpp +4 -2
  46. data/ext/oxt/spin_lock.hpp +3 -3
  47. data/lib/phusion_passenger/abstract_request_handler.rb +5 -3
  48. data/lib/phusion_passenger/admin_tools/control_process.rb +6 -1
  49. data/lib/phusion_passenger/constants.rb +2 -2
  50. data/lib/phusion_passenger/rack/application_spawner.rb +2 -1
  51. data/lib/phusion_passenger/rack/request_handler.rb +45 -35
  52. data/lib/phusion_passenger/templates/nginx/config_snippets.txt.erb +1 -1
  53. data/lib/phusion_passenger/utils.rb +13 -3
  54. data/misc/rake/cplusplus.rb +7 -0
  55. data/test/ApplicationPoolServer_ApplicationPoolTest.cpp +2 -0
  56. data/test/ApplicationPoolTest.cpp +39 -62
  57. data/test/CxxTestMain.cpp +6 -6
  58. data/test/HttpStatusExtractorTest.cpp +17 -0
  59. data/test/StandardApplicationPoolTest.cpp +2 -0
  60. data/test/UtilsTest.cpp +17 -28
  61. data/test/ruby/abstract_request_handler_spec.rb +3 -7
  62. data/test/ruby/utils_spec.rb +18 -13
  63. data/test/ruby/wsgi/application_spawner_spec.rb +5 -9
  64. data/test/stub/railsapp/app/controllers/{bar_controller_1.rb → bar_controller.rb} +0 -0
  65. data/test/stub/railsapp/app/controllers/bar_controller_1.txt +5 -0
  66. data/test/stub/railsapp/app/controllers/{bar_controller_2.rb → bar_controller_2.txt} +0 -0
  67. data/test/support/Support.h +20 -0
  68. data/test/support/config.rb +13 -0
  69. data/vendor/README +4 -3
  70. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/COPYING +1 -1
  71. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/KNOWN-ISSUES +0 -0
  72. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/README +54 -7
  73. data/vendor/rack-1.0.0-git/Rakefile +164 -0
  74. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack.rb +7 -3
  75. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/adapter/camping.rb +0 -0
  76. data/vendor/rack-1.0.0-git/lib/rack/auth/abstract/handler.rb +37 -0
  77. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/auth/abstract/request.rb +0 -0
  78. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/auth/basic.rb +0 -0
  79. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/auth/digest/md5.rb +1 -1
  80. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/auth/digest/nonce.rb +0 -0
  81. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/auth/digest/params.rb +0 -0
  82. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/auth/digest/request.rb +2 -2
  83. data/vendor/rack-1.0.0-git/lib/rack/auth/openid.rb +480 -0
  84. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/builder.rb +1 -5
  85. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/cascade.rb +0 -0
  86. data/vendor/rack-1.0.0-git/lib/rack/chunked.rb +49 -0
  87. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/commonlogger.rb +0 -0
  88. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/conditionalget.rb +4 -0
  89. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/content_length.rb +7 -3
  90. data/vendor/rack-1.0.0-git/lib/rack/content_type.rb +23 -0
  91. data/vendor/rack-1.0.0-git/lib/rack/deflater.rb +96 -0
  92. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/directory.rb +5 -2
  93. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/file.rb +4 -1
  94. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler.rb +22 -1
  95. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/cgi.rb +7 -3
  96. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/evented_mongrel.rb +0 -0
  97. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/fastcgi.rb +26 -24
  98. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/lsws.rb +7 -4
  99. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/mongrel.rb +5 -3
  100. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/scgi.rb +5 -3
  101. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/swiftiplied_mongrel.rb +0 -0
  102. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/thin.rb +3 -0
  103. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/handler/webrick.rb +11 -5
  104. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/head.rb +0 -0
  105. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/lint.rb +138 -66
  106. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/lobster.rb +0 -0
  107. data/vendor/rack-1.0.0-git/lib/rack/lock.rb +16 -0
  108. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/methodoverride.rb +0 -0
  109. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/mime.rb +4 -4
  110. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/mock.rb +42 -5
  111. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/recursive.rb +0 -0
  112. data/vendor/rack-1.0.0-git/lib/rack/reloader.rb +106 -0
  113. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/request.rb +46 -10
  114. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/response.rb +15 -3
  115. data/vendor/rack-1.0.0-git/lib/rack/rewindable_input.rb +98 -0
  116. data/vendor/rack-1.0.0-git/lib/rack/session/abstract/id.rb +142 -0
  117. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/session/cookie.rb +2 -0
  118. data/vendor/rack-1.0.0-git/lib/rack/session/memcache.rb +109 -0
  119. data/vendor/rack-1.0.0-git/lib/rack/session/pool.rb +100 -0
  120. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/showexceptions.rb +2 -1
  121. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/showstatus.rb +1 -1
  122. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/static.rb +0 -0
  123. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/urlmap.rb +12 -5
  124. data/vendor/{rack-0.9.1 → rack-1.0.0-git}/lib/rack/utils.rb +212 -65
  125. metadata +71 -170
  126. data/doc/rdoc/classes/ConditionVariable.html +0 -194
  127. data/doc/rdoc/classes/Exception.html +0 -120
  128. data/doc/rdoc/classes/GC.html +0 -113
  129. data/doc/rdoc/classes/IO.html +0 -169
  130. data/doc/rdoc/classes/PhusionPassenger.html +0 -238
  131. data/doc/rdoc/classes/PhusionPassenger/AbstractInstaller.html +0 -153
  132. data/doc/rdoc/classes/PhusionPassenger/AbstractRequestHandler.html +0 -506
  133. data/doc/rdoc/classes/PhusionPassenger/AbstractServer.html +0 -692
  134. data/doc/rdoc/classes/PhusionPassenger/AbstractServer/ServerAlreadyStarted.html +0 -97
  135. data/doc/rdoc/classes/PhusionPassenger/AbstractServer/ServerError.html +0 -96
  136. data/doc/rdoc/classes/PhusionPassenger/AbstractServer/ServerNotStarted.html +0 -97
  137. data/doc/rdoc/classes/PhusionPassenger/AbstractServer/UnknownMessage.html +0 -96
  138. data/doc/rdoc/classes/PhusionPassenger/AbstractServerCollection.html +0 -598
  139. data/doc/rdoc/classes/PhusionPassenger/AdminTools.html +0 -140
  140. data/doc/rdoc/classes/PhusionPassenger/AdminTools/ControlProcess.html +0 -264
  141. data/doc/rdoc/classes/PhusionPassenger/AdminTools/ControlProcess/Instance.html +0 -138
  142. data/doc/rdoc/classes/PhusionPassenger/AppInitError.html +0 -154
  143. data/doc/rdoc/classes/PhusionPassenger/Application.html +0 -283
  144. data/doc/rdoc/classes/PhusionPassenger/ConsoleTextTemplate.html +0 -172
  145. data/doc/rdoc/classes/PhusionPassenger/FrameworkInitError.html +0 -145
  146. data/doc/rdoc/classes/PhusionPassenger/HTMLTemplate.html +0 -175
  147. data/doc/rdoc/classes/PhusionPassenger/InitializationError.html +0 -141
  148. data/doc/rdoc/classes/PhusionPassenger/InvalidPath.html +0 -92
  149. data/doc/rdoc/classes/PhusionPassenger/MessageChannel.html +0 -489
  150. data/doc/rdoc/classes/PhusionPassenger/NativeSupport.html +0 -350
  151. data/doc/rdoc/classes/PhusionPassenger/Rack.html +0 -91
  152. data/doc/rdoc/classes/PhusionPassenger/Rack/ApplicationSpawner.html +0 -185
  153. data/doc/rdoc/classes/PhusionPassenger/Rack/RequestHandler.html +0 -184
  154. data/doc/rdoc/classes/PhusionPassenger/Railz.html +0 -95
  155. data/doc/rdoc/classes/PhusionPassenger/Railz/ApplicationSpawner.html +0 -419
  156. data/doc/rdoc/classes/PhusionPassenger/Railz/ApplicationSpawner/Error.html +0 -98
  157. data/doc/rdoc/classes/PhusionPassenger/Railz/CGIFixed.html +0 -200
  158. data/doc/rdoc/classes/PhusionPassenger/Railz/FrameworkSpawner.html +0 -443
  159. data/doc/rdoc/classes/PhusionPassenger/Railz/FrameworkSpawner/Error.html +0 -98
  160. data/doc/rdoc/classes/PhusionPassenger/Railz/RequestHandler.html +0 -154
  161. data/doc/rdoc/classes/PhusionPassenger/SpawnManager.html +0 -402
  162. data/doc/rdoc/classes/PhusionPassenger/UnknownError.html +0 -125
  163. data/doc/rdoc/classes/PhusionPassenger/Utils.html +0 -694
  164. data/doc/rdoc/classes/PhusionPassenger/VersionNotFound.html +0 -140
  165. data/doc/rdoc/classes/PhusionPassenger/WSGI.html +0 -89
  166. data/doc/rdoc/classes/PhusionPassenger/WSGI/ApplicationSpawner.html +0 -188
  167. data/doc/rdoc/classes/PlatformInfo.html +0 -831
  168. data/doc/rdoc/classes/RakeExtensions.html +0 -197
  169. data/doc/rdoc/classes/Signal.html +0 -134
  170. data/doc/rdoc/created.rid +0 -1
  171. data/doc/rdoc/files/DEVELOPERS_TXT.html +0 -240
  172. data/doc/rdoc/files/README.html +0 -157
  173. data/doc/rdoc/files/ext/phusion_passenger/native_support_c.html +0 -92
  174. data/doc/rdoc/files/lib/phusion_passenger/abstract_installer_rb.html +0 -129
  175. data/doc/rdoc/files/lib/phusion_passenger/abstract_request_handler_rb.html +0 -131
  176. data/doc/rdoc/files/lib/phusion_passenger/abstract_server_collection_rb.html +0 -126
  177. data/doc/rdoc/files/lib/phusion_passenger/abstract_server_rb.html +0 -130
  178. data/doc/rdoc/files/lib/phusion_passenger/admin_tools/control_process_rb.html +0 -129
  179. data/doc/rdoc/files/lib/phusion_passenger/admin_tools_rb.html +0 -122
  180. data/doc/rdoc/files/lib/phusion_passenger/application_rb.html +0 -127
  181. data/doc/rdoc/files/lib/phusion_passenger/console_text_template_rb.html +0 -126
  182. data/doc/rdoc/files/lib/phusion_passenger/constants_rb.html +0 -122
  183. data/doc/rdoc/files/lib/phusion_passenger/dependencies_rb.html +0 -134
  184. data/doc/rdoc/files/lib/phusion_passenger/events_rb.html +0 -122
  185. data/doc/rdoc/files/lib/phusion_passenger/exceptions_rb.html +0 -122
  186. data/doc/rdoc/files/lib/phusion_passenger/html_template_rb.html +0 -126
  187. data/doc/rdoc/files/lib/phusion_passenger/message_channel_rb.html +0 -122
  188. data/doc/rdoc/files/lib/phusion_passenger/packaging_rb.html +0 -122
  189. data/doc/rdoc/files/lib/phusion_passenger/platform_info_rb.html +0 -127
  190. data/doc/rdoc/files/lib/phusion_passenger/rack/application_spawner_rb.html +0 -133
  191. data/doc/rdoc/files/lib/phusion_passenger/rack/request_handler_rb.html +0 -126
  192. data/doc/rdoc/files/lib/phusion_passenger/railz/application_spawner_rb.html +0 -143
  193. data/doc/rdoc/files/lib/phusion_passenger/railz/cgi_fixed_rb.html +0 -126
  194. data/doc/rdoc/files/lib/phusion_passenger/railz/framework_spawner_rb.html +0 -145
  195. data/doc/rdoc/files/lib/phusion_passenger/railz/request_handler_rb.html +0 -127
  196. data/doc/rdoc/files/lib/phusion_passenger/simple_benchmarking_rb.html +0 -122
  197. data/doc/rdoc/files/lib/phusion_passenger/spawn_manager_rb.html +0 -161
  198. data/doc/rdoc/files/lib/phusion_passenger/utils_rb.html +0 -175
  199. data/doc/rdoc/files/lib/phusion_passenger/wsgi/application_spawner_rb.html +0 -129
  200. data/doc/rdoc/files/misc/rake/extensions_rb.html +0 -130
  201. data/doc/rdoc/fr_class_index.html +0 -90
  202. data/doc/rdoc/fr_file_index.html +0 -76
  203. data/doc/rdoc/fr_method_index.html +0 -195
  204. data/doc/rdoc/index.html +0 -26
  205. data/doc/rdoc/rdoc-style.css +0 -187
  206. data/vendor/rack-0.9.1/AUTHORS +0 -8
  207. data/vendor/rack-0.9.1/ChangeLog +0 -1423
  208. data/vendor/rack-0.9.1/Rakefile +0 -188
  209. data/vendor/rack-0.9.1/SPEC +0 -129
  210. data/vendor/rack-0.9.1/lib/rack/auth/abstract/handler.rb +0 -28
  211. data/vendor/rack-0.9.1/lib/rack/auth/openid.rb +0 -438
  212. data/vendor/rack-0.9.1/lib/rack/deflater.rb +0 -87
  213. data/vendor/rack-0.9.1/lib/rack/reloader.rb +0 -64
  214. data/vendor/rack-0.9.1/lib/rack/session/abstract/id.rb +0 -153
  215. data/vendor/rack-0.9.1/lib/rack/session/memcache.rb +0 -97
  216. data/vendor/rack-0.9.1/lib/rack/session/pool.rb +0 -73
@@ -1,87 +0,0 @@
1
- require "zlib"
2
- require "stringio"
3
- require "time" # for Time.httpdate
4
-
5
- module Rack
6
-
7
- class Deflater
8
- def initialize(app)
9
- @app = app
10
- end
11
-
12
- def call(env)
13
- status, headers, body = @app.call(env)
14
- headers = Utils::HeaderHash.new(headers)
15
-
16
- # Skip compressing empty entity body responses and responses with
17
- # no-transform set.
18
- if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
19
- headers['Cache-Control'].to_s =~ /\bno-transform\b/
20
- return [status, headers, body]
21
- end
22
-
23
- request = Request.new(env)
24
-
25
- encoding = Utils.select_best_encoding(%w(gzip deflate identity),
26
- request.accept_encoding)
27
-
28
- # Set the Vary HTTP header.
29
- vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
30
- unless vary.include?("*") || vary.include?("Accept-Encoding")
31
- headers["Vary"] = vary.push("Accept-Encoding").join(",")
32
- end
33
-
34
- case encoding
35
- when "gzip"
36
- mtime = if headers.key?("Last-Modified")
37
- Time.httpdate(headers["Last-Modified"])
38
- else
39
- Time.now
40
- end
41
- [status,
42
- headers.merge("Content-Encoding" => "gzip"),
43
- self.class.gzip(body, mtime)]
44
- when "deflate"
45
- [status,
46
- headers.merge("Content-Encoding" => "deflate"),
47
- self.class.deflate(body)]
48
- when "identity"
49
- [status, headers, body]
50
- when nil
51
- message = ["An acceptable encoding for the requested resource #{request.fullpath} could not be found."]
52
- [406, {"Content-Type" => "text/plain"}, message]
53
- end
54
- end
55
-
56
- def self.gzip(body, mtime)
57
- io = StringIO.new
58
- gzip = Zlib::GzipWriter.new(io)
59
- gzip.mtime = mtime
60
-
61
- # TODO: Add streaming
62
- body.each { |part| gzip << part }
63
-
64
- gzip.close
65
- return io.string
66
- end
67
-
68
- DEFLATE_ARGS = [
69
- Zlib::DEFAULT_COMPRESSION,
70
- # drop the zlib header which causes both Safari and IE to choke
71
- -Zlib::MAX_WBITS,
72
- Zlib::DEF_MEM_LEVEL,
73
- Zlib::DEFAULT_STRATEGY
74
- ]
75
-
76
- # Loosely based on Mongrel's Deflate handler
77
- def self.deflate(body)
78
- deflater = Zlib::Deflate.new(*DEFLATE_ARGS)
79
-
80
- # TODO: Add streaming
81
- body.each { |part| deflater << part }
82
-
83
- return deflater.finish
84
- end
85
- end
86
-
87
- end
@@ -1,64 +0,0 @@
1
- require 'thread'
2
-
3
- module Rack
4
- # Rack::Reloader checks on every request, but at most every +secs+
5
- # seconds, if a file loaded changed, and reloads it, logging to
6
- # rack.errors.
7
- #
8
- # It is recommended you use ShowExceptions to catch SyntaxErrors etc.
9
-
10
- class Reloader
11
- def initialize(app, secs=10)
12
- @app = app
13
- @secs = secs # reload every @secs seconds max
14
- @last = Time.now
15
- end
16
-
17
- def call(env)
18
- if Time.now > @last + @secs
19
- Thread.exclusive {
20
- reload!(env['rack.errors'])
21
- @last = Time.now
22
- }
23
- end
24
-
25
- @app.call(env)
26
- end
27
-
28
- def reload!(stderr=STDERR)
29
- need_reload = $LOADED_FEATURES.find_all { |loaded|
30
- begin
31
- if loaded =~ /\A[.\/]/ # absolute filename or 1.9
32
- abs = loaded
33
- else
34
- abs = $LOAD_PATH.map { |path| ::File.join(path, loaded) }.
35
- find { |file| ::File.exist? file }
36
- end
37
-
38
- if abs
39
- ::File.mtime(abs) > @last - @secs rescue false
40
- else
41
- false
42
- end
43
- end
44
- }
45
-
46
- need_reload.each { |l|
47
- $LOADED_FEATURES.delete l
48
- }
49
-
50
- need_reload.each { |to_load|
51
- begin
52
- if require to_load
53
- stderr.puts "#{self.class}: reloaded `#{to_load}'"
54
- end
55
- rescue LoadError, SyntaxError => e
56
- raise e # Possibly ShowExceptions
57
- end
58
- }
59
-
60
- stderr.flush
61
- need_reload
62
- end
63
- end
64
- end
@@ -1,153 +0,0 @@
1
- # AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
2
- # bugrep: Andreas Zehnder
3
-
4
- require 'rack/utils'
5
- require 'time'
6
-
7
- module Rack
8
- module Session
9
- module Abstract
10
- # ID sets up a basic framework for implementing an id based sessioning
11
- # service. Cookies sent to the client for maintaining sessions will only
12
- # contain an id reference. Only #get_session and #set_session should
13
- # need to be overwritten.
14
- #
15
- # All parameters are optional.
16
- # * :key determines the name of the cookie, by default it is
17
- # 'rack.session'
18
- # * :domain and :path set the related cookie values, by default
19
- # domain is nil, and the path is '/'.
20
- # * :expire_after is the number of seconds in which the session
21
- # cookie will expire. By default it is set not to provide any
22
- # expiry time.
23
- class ID
24
- attr_reader :key
25
- DEFAULT_OPTIONS = {
26
- :key => 'rack.session',
27
- :path => '/',
28
- :domain => nil,
29
- :expire_after => nil,
30
- :secure => false,
31
- :httponly => true,
32
- :sidbits => 128
33
- }
34
-
35
- def initialize(app, options={})
36
- @default_options = self.class::DEFAULT_OPTIONS.merge(options)
37
- @key = @default_options[:key]
38
- @default_context = context app
39
- end
40
-
41
- def call(env)
42
- @default_context.call(env)
43
- end
44
-
45
- def context(app)
46
- Rack::Utils::Context.new self, app do |env|
47
- load_session env
48
- response = app.call(env)
49
- commit_session env, response
50
- response
51
- end
52
- end
53
-
54
- private
55
-
56
- # Generate a new session id using Ruby #rand. The size of the
57
- # session id is controlled by the :sidbits option.
58
- # Monkey patch this to use custom methods for session id generation.
59
- def generate_sid
60
- "%0#{@default_options[:sidbits] / 4}x" %
61
- rand(2**@default_options[:sidbits] - 1)
62
- end
63
-
64
- # Extracts the session id from provided cookies and passes it and the
65
- # environment to #get_session. It then sets the resulting session into
66
- # 'rack.session', and places options and session metadata into
67
- # 'rack.session.options'.
68
- def load_session(env)
69
- sid = (env['HTTP_COOKIE']||'')[/#{@key}=([^,;]+)/,1]
70
- sid, session = get_session(env, sid)
71
- unless session.is_a?(Hash)
72
- puts 'Session: '+sid.inspect+"\n"+session.inspect if $DEBUG
73
- raise TypeError, 'Session not a Hash'
74
- end
75
-
76
- options = @default_options.
77
- merge({ :id => sid, :by => self, :at => Time.now })
78
-
79
- env['rack.session'] = session
80
- env['rack.session.options'] = options
81
-
82
- return true
83
- end
84
-
85
- # Acquires the session from the environment and the session id from
86
- # the session options and passes them to #set_session. It then
87
- # proceeds to set a cookie up in the response with the session's id.
88
- def commit_session(env, response)
89
- unless response.is_a?(Array)
90
- puts 'Response: '+response.inspect if $DEBUG
91
- raise ArgumentError, 'Response is not an array.'
92
- end
93
-
94
- options = env['rack.session.options']
95
- unless options.is_a?(Hash)
96
- puts 'Options: '+options.inspect if $DEBUG
97
- raise TypeError, 'Options not a Hash'
98
- end
99
-
100
- sid, time, z = options.values_at(:id, :at, :by)
101
- unless self == z
102
- warn "#{self} not managing this session."
103
- return
104
- end
105
-
106
- unless env['rack.session'].is_a?(Hash)
107
- warn 'Session: '+sid.inspect+"\n"+session.inspect if $DEBUG
108
- raise TypeError, 'Session not a Hash'
109
- end
110
-
111
- unless set_session(env, sid)
112
- warn "Session not saved." if $DEBUG
113
- warn "#{env['rack.session'].inspect} has been lost."if $DEBUG
114
- return false
115
- end
116
-
117
- cookie = Utils.escape(@key)+'='+Utils.escape(sid)
118
- cookie<< "; domain=#{options[:domain]}" if options[:domain]
119
- cookie<< "; path=#{options[:path]}" if options[:path]
120
- if options[:expire_after]
121
- expiry = time + options[:expire_after]
122
- cookie<< "; expires=#{expiry.httpdate}"
123
- end
124
- cookie<< "; Secure" if options[:secure]
125
- cookie<< "; HttpOnly" if options[:httponly]
126
-
127
- case a = (h = response[1])['Set-Cookie']
128
- when Array then a << cookie
129
- when String then h['Set-Cookie'] = [a, cookie]
130
- when nil then h['Set-Cookie'] = cookie
131
- end
132
-
133
- return true
134
- end
135
-
136
- # Should return [session_id, session]. All thread safety and session
137
- # retrival proceedures should occur here.
138
- # If nil is provided as the session id, generation of a new valid id
139
- # should occur within.
140
- def get_session(env, sid)
141
- raise '#get_session needs to be implemented.'
142
- end
143
-
144
- # All thread safety and session storage proceedures should occur here.
145
- # Should return true or false dependant on whether or not the session
146
- # was saved or not.
147
- def set_session(env, sid)
148
- raise '#set_session needs to be implemented.'
149
- end
150
- end
151
- end
152
- end
153
- end
@@ -1,97 +0,0 @@
1
- # AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
2
-
3
- require 'rack/session/abstract/id'
4
- require 'memcache'
5
-
6
- module Rack
7
- module Session
8
- # Rack::Session::Memcache provides simple cookie based session management.
9
- # Session data is stored in memcached. The corresponding session key is
10
- # maintained in the cookie.
11
- # You may treat Session::Memcache as you would Session::Pool with the
12
- # following caveats.
13
- #
14
- # * Setting :expire_after to 0 would note to the Memcache server to hang
15
- # onto the session data until it would drop it according to it's own
16
- # specifications. However, the cookie sent to the client would expire
17
- # immediately.
18
- #
19
- # Note that memcache does drop data before it may be listed to expire. For
20
- # a full description of behaviour, please see memcache's documentation.
21
-
22
- class Memcache < Abstract::ID
23
- attr_reader :mutex, :pool
24
- DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge({
25
- :namespace => 'rack:session',
26
- :memcache_server => 'localhost:11211'
27
- })
28
-
29
- def initialize(app, options={})
30
- super
31
- @pool = MemCache.new @default_options[:memcache_server], @default_options
32
- unless @pool.servers.any?{|s|s.alive?}
33
- raise "#{self} unable to find server during initialization."
34
- end
35
- @mutex = Mutex.new
36
- end
37
-
38
- private
39
-
40
- def get_session(env, sid)
41
- session = sid && @pool.get(sid)
42
- unless session and session.is_a?(Hash)
43
- session = {}
44
- lc = 0
45
- @mutex.synchronize do
46
- begin
47
- raise RuntimeError, 'Unique id finding looping excessively' if (lc+=1) > 1000
48
- sid = generate_sid
49
- ret = @pool.add(sid, session)
50
- end until /^STORED/ =~ ret
51
- end
52
- end
53
- class << session
54
- @deleted = []
55
- def delete key
56
- (@deleted||=[]) << key
57
- super
58
- end
59
- end
60
- [sid, session]
61
- rescue MemCache::MemCacheError, Errno::ECONNREFUSED # MemCache server cannot be contacted
62
- warn "#{self} is unable to find server."
63
- warn $!.inspect
64
- return [ nil, {} ]
65
- end
66
-
67
- def set_session(env, sid)
68
- session = env['rack.session']
69
- options = env['rack.session.options']
70
- expiry = options[:expire_after] || 0
71
- o, s = @mutex.synchronize do
72
- old_session = @pool.get(sid)
73
- unless old_session.is_a?(Hash)
74
- warn 'Session not properly initialized.' if $DEBUG
75
- old_session = {}
76
- @pool.add sid, old_session, expiry
77
- end
78
- session.instance_eval do
79
- @deleted.each{|k| old_session.delete(k) } if defined? @deleted
80
- end
81
- @pool.set sid, old_session.merge(session), expiry
82
- [old_session, session]
83
- end
84
- s.each do |k,v|
85
- next unless o.has_key?(k) and v != o[k]
86
- warn "session value assignment collision at #{k.inspect}:"+
87
- "\n\t#{o[k].inspect}\n\t#{v.inspect}"
88
- end if $DEBUG and env['rack.multithread']
89
- return true
90
- rescue MemCache::MemCacheError, Errno::ECONNREFUSED # MemCache server cannot be contacted
91
- warn "#{self} is unable to find server."
92
- warn $!.inspect
93
- return false
94
- end
95
- end
96
- end
97
- end
@@ -1,73 +0,0 @@
1
- # AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
2
- # THANKS:
3
- # apeiros, for session id generation, expiry setup, and threadiness
4
- # sergio, threadiness and bugreps
5
-
6
- require 'rack/session/abstract/id'
7
- require 'thread'
8
-
9
- module Rack
10
- module Session
11
- # Rack::Session::Pool provides simple cookie based session management.
12
- # Session data is stored in a hash held by @pool.
13
- # In the context of a multithreaded environment, sessions being
14
- # committed to the pool is done in a merging manner.
15
- #
16
- # Example:
17
- # myapp = MyRackApp.new
18
- # sessioned = Rack::Session::Pool.new(myapp,
19
- # :key => 'rack.session',
20
- # :domain => 'foo.com',
21
- # :path => '/',
22
- # :expire_after => 2592000
23
- # )
24
- # Rack::Handler::WEBrick.run sessioned
25
-
26
- class Pool < Abstract::ID
27
- attr_reader :mutex, :pool
28
- DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.dup
29
-
30
- def initialize(app, options={})
31
- super
32
- @pool = Hash.new
33
- @mutex = Mutex.new
34
- end
35
-
36
- private
37
-
38
- def get_session(env, sid)
39
- session = @mutex.synchronize do
40
- unless sess = @pool[sid] and ((expires = sess[:expire_at]).nil? or expires > Time.now)
41
- @pool.delete_if{|k,v| expiry = v[:expire_at] and expiry < Time.now }
42
- begin
43
- sid = generate_sid
44
- end while @pool.has_key?(sid)
45
- end
46
- @pool[sid] ||= {}
47
- end
48
- [sid, session]
49
- end
50
-
51
- def set_session(env, sid)
52
- options = env['rack.session.options']
53
- expiry = options[:expire_after] && options[:at]+options[:expire_after]
54
- @mutex.synchronize do
55
- old_session = @pool[sid]
56
- old_session[:expire_at] = expiry if expiry
57
- session = old_session.merge(env['rack.session'])
58
- @pool[sid] = session
59
- session.each do |k,v|
60
- next unless old_session.has_key?(k) and v != old_session[k]
61
- warn "session value assignment collision at #{k}: #{old_session[k]} <- #{v}"
62
- end if $DEBUG and env['rack.multithread']
63
- end
64
- return true
65
- rescue
66
- warn "#{self} is unable to find server."
67
- warn "#{env['rack.session'].inspect} has been lost."
68
- warn $!.inspect
69
- return false
70
- end
71
- end
72
- end
73
- end