qoobaa-rack 1.0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. data/COPYING +18 -0
  2. data/KNOWN-ISSUES +18 -0
  3. data/RDOX +0 -0
  4. data/README +353 -0
  5. data/Rakefile +164 -0
  6. data/SPEC +164 -0
  7. data/bin/rackup +176 -0
  8. data/contrib/rack_logo.svg +111 -0
  9. data/example/lobster.ru +4 -0
  10. data/example/protectedlobster.rb +14 -0
  11. data/example/protectedlobster.ru +8 -0
  12. data/lib/rack/adapter/camping.rb +22 -0
  13. data/lib/rack/auth/abstract/handler.rb +37 -0
  14. data/lib/rack/auth/abstract/request.rb +37 -0
  15. data/lib/rack/auth/basic.rb +58 -0
  16. data/lib/rack/auth/digest/md5.rb +124 -0
  17. data/lib/rack/auth/digest/nonce.rb +51 -0
  18. data/lib/rack/auth/digest/params.rb +55 -0
  19. data/lib/rack/auth/digest/request.rb +40 -0
  20. data/lib/rack/auth/openid.rb +487 -0
  21. data/lib/rack/builder.rb +63 -0
  22. data/lib/rack/cascade.rb +41 -0
  23. data/lib/rack/chunked.rb +49 -0
  24. data/lib/rack/commonlogger.rb +52 -0
  25. data/lib/rack/conditionalget.rb +47 -0
  26. data/lib/rack/content_length.rb +29 -0
  27. data/lib/rack/content_type.rb +23 -0
  28. data/lib/rack/deflater.rb +96 -0
  29. data/lib/rack/directory.rb +153 -0
  30. data/lib/rack/file.rb +88 -0
  31. data/lib/rack/handler/cgi.rb +61 -0
  32. data/lib/rack/handler/evented_mongrel.rb +8 -0
  33. data/lib/rack/handler/fastcgi.rb +88 -0
  34. data/lib/rack/handler/lsws.rb +60 -0
  35. data/lib/rack/handler/mongrel.rb +87 -0
  36. data/lib/rack/handler/scgi.rb +62 -0
  37. data/lib/rack/handler/swiftiplied_mongrel.rb +8 -0
  38. data/lib/rack/handler/thin.rb +18 -0
  39. data/lib/rack/handler/webrick.rb +71 -0
  40. data/lib/rack/handler.rb +69 -0
  41. data/lib/rack/head.rb +19 -0
  42. data/lib/rack/lint.rb +546 -0
  43. data/lib/rack/lobster.rb +65 -0
  44. data/lib/rack/lock.rb +16 -0
  45. data/lib/rack/methodoverride.rb +27 -0
  46. data/lib/rack/mime.rb +204 -0
  47. data/lib/rack/mock.rb +187 -0
  48. data/lib/rack/recursive.rb +57 -0
  49. data/lib/rack/reloader.rb +107 -0
  50. data/lib/rack/request.rb +248 -0
  51. data/lib/rack/response.rb +183 -0
  52. data/lib/rack/rewindable_input.rb +100 -0
  53. data/lib/rack/session/abstract/id.rb +142 -0
  54. data/lib/rack/session/cookie.rb +91 -0
  55. data/lib/rack/session/memcache.rb +109 -0
  56. data/lib/rack/session/pool.rb +100 -0
  57. data/lib/rack/showexceptions.rb +349 -0
  58. data/lib/rack/showstatus.rb +106 -0
  59. data/lib/rack/static.rb +38 -0
  60. data/lib/rack/urlmap.rb +55 -0
  61. data/lib/rack/utils.rb +528 -0
  62. data/lib/rack.rb +90 -0
  63. data/rack.gemspec +60 -0
  64. data/test/cgi/lighttpd.conf +20 -0
  65. data/test/cgi/test +9 -0
  66. data/test/cgi/test.fcgi +8 -0
  67. data/test/cgi/test.ru +7 -0
  68. data/test/multipart/binary +0 -0
  69. data/test/multipart/empty +10 -0
  70. data/test/multipart/file1.txt +1 -0
  71. data/test/multipart/ie +6 -0
  72. data/test/multipart/nested +10 -0
  73. data/test/multipart/none +9 -0
  74. data/test/multipart/text +10 -0
  75. data/test/spec_rack_auth_basic.rb +73 -0
  76. data/test/spec_rack_auth_digest.rb +226 -0
  77. data/test/spec_rack_auth_openid.rb +84 -0
  78. data/test/spec_rack_builder.rb +84 -0
  79. data/test/spec_rack_camping.rb +51 -0
  80. data/test/spec_rack_cascade.rb +48 -0
  81. data/test/spec_rack_cgi.rb +89 -0
  82. data/test/spec_rack_chunked.rb +62 -0
  83. data/test/spec_rack_commonlogger.rb +61 -0
  84. data/test/spec_rack_conditionalget.rb +41 -0
  85. data/test/spec_rack_content_length.rb +43 -0
  86. data/test/spec_rack_content_type.rb +30 -0
  87. data/test/spec_rack_deflater.rb +127 -0
  88. data/test/spec_rack_directory.rb +61 -0
  89. data/test/spec_rack_fastcgi.rb +89 -0
  90. data/test/spec_rack_file.rb +75 -0
  91. data/test/spec_rack_handler.rb +43 -0
  92. data/test/spec_rack_head.rb +30 -0
  93. data/test/spec_rack_lint.rb +521 -0
  94. data/test/spec_rack_lobster.rb +45 -0
  95. data/test/spec_rack_lock.rb +38 -0
  96. data/test/spec_rack_methodoverride.rb +60 -0
  97. data/test/spec_rack_mock.rb +243 -0
  98. data/test/spec_rack_mongrel.rb +189 -0
  99. data/test/spec_rack_recursive.rb +77 -0
  100. data/test/spec_rack_request.rb +504 -0
  101. data/test/spec_rack_response.rb +218 -0
  102. data/test/spec_rack_rewindable_input.rb +118 -0
  103. data/test/spec_rack_session_cookie.rb +82 -0
  104. data/test/spec_rack_session_memcache.rb +250 -0
  105. data/test/spec_rack_session_pool.rb +172 -0
  106. data/test/spec_rack_showexceptions.rb +21 -0
  107. data/test/spec_rack_showstatus.rb +72 -0
  108. data/test/spec_rack_static.rb +37 -0
  109. data/test/spec_rack_thin.rb +91 -0
  110. data/test/spec_rack_urlmap.rb +185 -0
  111. data/test/spec_rack_utils.rb +467 -0
  112. data/test/spec_rack_webrick.rb +130 -0
  113. data/test/testrequest.rb +57 -0
  114. data/test/unregistered_handler/rack/handler/unregistered.rb +7 -0
  115. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +7 -0
  116. metadata +276 -0
data/lib/rack/mime.rb ADDED
@@ -0,0 +1,204 @@
1
+ module Rack
2
+ module Mime
3
+ # Returns String with mime type if found, otherwise use +fallback+.
4
+ # +ext+ should be filename extension in the '.ext' format that
5
+ # File.extname(file) returns.
6
+ # +fallback+ may be any object
7
+ #
8
+ # Also see the documentation for MIME_TYPES
9
+ #
10
+ # Usage:
11
+ # Rack::Mime.mime_type('.foo')
12
+ #
13
+ # This is a shortcut for:
14
+ # Rack::Mime::MIME_TYPES.fetch('.foo', 'application/octet-stream')
15
+
16
+ def mime_type(ext, fallback='application/octet-stream')
17
+ MIME_TYPES.fetch(ext.to_s.downcase, fallback)
18
+ end
19
+ module_function :mime_type
20
+
21
+ # List of most common mime-types, selected various sources
22
+ # according to their usefulness in a webserving scope for Ruby
23
+ # users.
24
+ #
25
+ # To amend this list with your local mime.types list you can use:
26
+ #
27
+ # require 'webrick/httputils'
28
+ # list = WEBrick::HTTPUtils.load_mime_types('/etc/mime.types')
29
+ # Rack::Mime::MIME_TYPES.merge!(list)
30
+ #
31
+ # To add the list mongrel provides, use:
32
+ #
33
+ # require 'mongrel/handlers'
34
+ # Rack::Mime::MIME_TYPES.merge!(Mongrel::DirHandler::MIME_TYPES)
35
+
36
+ MIME_TYPES = {
37
+ ".3gp" => "video/3gpp",
38
+ ".a" => "application/octet-stream",
39
+ ".ai" => "application/postscript",
40
+ ".aif" => "audio/x-aiff",
41
+ ".aiff" => "audio/x-aiff",
42
+ ".asc" => "application/pgp-signature",
43
+ ".asf" => "video/x-ms-asf",
44
+ ".asm" => "text/x-asm",
45
+ ".asx" => "video/x-ms-asf",
46
+ ".atom" => "application/atom+xml",
47
+ ".au" => "audio/basic",
48
+ ".avi" => "video/x-msvideo",
49
+ ".bat" => "application/x-msdownload",
50
+ ".bin" => "application/octet-stream",
51
+ ".bmp" => "image/bmp",
52
+ ".bz2" => "application/x-bzip2",
53
+ ".c" => "text/x-c",
54
+ ".cab" => "application/vnd.ms-cab-compressed",
55
+ ".cc" => "text/x-c",
56
+ ".chm" => "application/vnd.ms-htmlhelp",
57
+ ".class" => "application/octet-stream",
58
+ ".com" => "application/x-msdownload",
59
+ ".conf" => "text/plain",
60
+ ".cpp" => "text/x-c",
61
+ ".crt" => "application/x-x509-ca-cert",
62
+ ".css" => "text/css",
63
+ ".csv" => "text/csv",
64
+ ".cxx" => "text/x-c",
65
+ ".deb" => "application/x-debian-package",
66
+ ".der" => "application/x-x509-ca-cert",
67
+ ".diff" => "text/x-diff",
68
+ ".djv" => "image/vnd.djvu",
69
+ ".djvu" => "image/vnd.djvu",
70
+ ".dll" => "application/x-msdownload",
71
+ ".dmg" => "application/octet-stream",
72
+ ".doc" => "application/msword",
73
+ ".dot" => "application/msword",
74
+ ".dtd" => "application/xml-dtd",
75
+ ".dvi" => "application/x-dvi",
76
+ ".ear" => "application/java-archive",
77
+ ".eml" => "message/rfc822",
78
+ ".eps" => "application/postscript",
79
+ ".exe" => "application/x-msdownload",
80
+ ".f" => "text/x-fortran",
81
+ ".f77" => "text/x-fortran",
82
+ ".f90" => "text/x-fortran",
83
+ ".flv" => "video/x-flv",
84
+ ".for" => "text/x-fortran",
85
+ ".gem" => "application/octet-stream",
86
+ ".gemspec" => "text/x-script.ruby",
87
+ ".gif" => "image/gif",
88
+ ".gz" => "application/x-gzip",
89
+ ".h" => "text/x-c",
90
+ ".hh" => "text/x-c",
91
+ ".htm" => "text/html",
92
+ ".html" => "text/html",
93
+ ".ico" => "image/vnd.microsoft.icon",
94
+ ".ics" => "text/calendar",
95
+ ".ifb" => "text/calendar",
96
+ ".iso" => "application/octet-stream",
97
+ ".jar" => "application/java-archive",
98
+ ".java" => "text/x-java-source",
99
+ ".jnlp" => "application/x-java-jnlp-file",
100
+ ".jpeg" => "image/jpeg",
101
+ ".jpg" => "image/jpeg",
102
+ ".js" => "application/javascript",
103
+ ".json" => "application/json",
104
+ ".log" => "text/plain",
105
+ ".m3u" => "audio/x-mpegurl",
106
+ ".m4v" => "video/mp4",
107
+ ".man" => "text/troff",
108
+ ".mathml" => "application/mathml+xml",
109
+ ".mbox" => "application/mbox",
110
+ ".mdoc" => "text/troff",
111
+ ".me" => "text/troff",
112
+ ".mid" => "audio/midi",
113
+ ".midi" => "audio/midi",
114
+ ".mime" => "message/rfc822",
115
+ ".mml" => "application/mathml+xml",
116
+ ".mng" => "video/x-mng",
117
+ ".mov" => "video/quicktime",
118
+ ".mp3" => "audio/mpeg",
119
+ ".mp4" => "video/mp4",
120
+ ".mp4v" => "video/mp4",
121
+ ".mpeg" => "video/mpeg",
122
+ ".mpg" => "video/mpeg",
123
+ ".ms" => "text/troff",
124
+ ".msi" => "application/x-msdownload",
125
+ ".odp" => "application/vnd.oasis.opendocument.presentation",
126
+ ".ods" => "application/vnd.oasis.opendocument.spreadsheet",
127
+ ".odt" => "application/vnd.oasis.opendocument.text",
128
+ ".ogg" => "application/ogg",
129
+ ".p" => "text/x-pascal",
130
+ ".pas" => "text/x-pascal",
131
+ ".pbm" => "image/x-portable-bitmap",
132
+ ".pdf" => "application/pdf",
133
+ ".pem" => "application/x-x509-ca-cert",
134
+ ".pgm" => "image/x-portable-graymap",
135
+ ".pgp" => "application/pgp-encrypted",
136
+ ".pkg" => "application/octet-stream",
137
+ ".pl" => "text/x-script.perl",
138
+ ".pm" => "text/x-script.perl-module",
139
+ ".png" => "image/png",
140
+ ".pnm" => "image/x-portable-anymap",
141
+ ".ppm" => "image/x-portable-pixmap",
142
+ ".pps" => "application/vnd.ms-powerpoint",
143
+ ".ppt" => "application/vnd.ms-powerpoint",
144
+ ".ps" => "application/postscript",
145
+ ".psd" => "image/vnd.adobe.photoshop",
146
+ ".py" => "text/x-script.python",
147
+ ".qt" => "video/quicktime",
148
+ ".ra" => "audio/x-pn-realaudio",
149
+ ".rake" => "text/x-script.ruby",
150
+ ".ram" => "audio/x-pn-realaudio",
151
+ ".rar" => "application/x-rar-compressed",
152
+ ".rb" => "text/x-script.ruby",
153
+ ".rdf" => "application/rdf+xml",
154
+ ".roff" => "text/troff",
155
+ ".rpm" => "application/x-redhat-package-manager",
156
+ ".rss" => "application/rss+xml",
157
+ ".rtf" => "application/rtf",
158
+ ".ru" => "text/x-script.ruby",
159
+ ".s" => "text/x-asm",
160
+ ".sgm" => "text/sgml",
161
+ ".sgml" => "text/sgml",
162
+ ".sh" => "application/x-sh",
163
+ ".sig" => "application/pgp-signature",
164
+ ".snd" => "audio/basic",
165
+ ".so" => "application/octet-stream",
166
+ ".svg" => "image/svg+xml",
167
+ ".svgz" => "image/svg+xml",
168
+ ".swf" => "application/x-shockwave-flash",
169
+ ".t" => "text/troff",
170
+ ".tar" => "application/x-tar",
171
+ ".tbz" => "application/x-bzip-compressed-tar",
172
+ ".tcl" => "application/x-tcl",
173
+ ".tex" => "application/x-tex",
174
+ ".texi" => "application/x-texinfo",
175
+ ".texinfo" => "application/x-texinfo",
176
+ ".text" => "text/plain",
177
+ ".tif" => "image/tiff",
178
+ ".tiff" => "image/tiff",
179
+ ".torrent" => "application/x-bittorrent",
180
+ ".tr" => "text/troff",
181
+ ".txt" => "text/plain",
182
+ ".vcf" => "text/x-vcard",
183
+ ".vcs" => "text/x-vcalendar",
184
+ ".vrml" => "model/vrml",
185
+ ".war" => "application/java-archive",
186
+ ".wav" => "audio/x-wav",
187
+ ".wma" => "audio/x-ms-wma",
188
+ ".wmv" => "video/x-ms-wmv",
189
+ ".wmx" => "video/x-ms-wmx",
190
+ ".wrl" => "model/vrml",
191
+ ".wsdl" => "application/wsdl+xml",
192
+ ".xbm" => "image/x-xbitmap",
193
+ ".xhtml" => "application/xhtml+xml",
194
+ ".xls" => "application/vnd.ms-excel",
195
+ ".xml" => "application/xml",
196
+ ".xpm" => "image/x-xpixmap",
197
+ ".xsl" => "application/xml",
198
+ ".xslt" => "application/xslt+xml",
199
+ ".yaml" => "text/yaml",
200
+ ".yml" => "text/yaml",
201
+ ".zip" => "application/zip",
202
+ }
203
+ end
204
+ end
data/lib/rack/mock.rb ADDED
@@ -0,0 +1,187 @@
1
+ require 'uri'
2
+ require 'stringio'
3
+ require 'rack/lint'
4
+ require 'rack/utils'
5
+ require 'rack/response'
6
+
7
+ module Rack
8
+ # Rack::MockRequest helps testing your Rack application without
9
+ # actually using HTTP.
10
+ #
11
+ # After performing a request on a URL with get/post/put/delete, it
12
+ # returns a MockResponse with useful helper methods for effective
13
+ # testing.
14
+ #
15
+ # You can pass a hash with additional configuration to the
16
+ # get/post/put/delete.
17
+ # <tt>:input</tt>:: A String or IO-like to be used as rack.input.
18
+ # <tt>:fatal</tt>:: Raise a FatalWarning if the app writes to rack.errors.
19
+ # <tt>:lint</tt>:: If true, wrap the application in a Rack::Lint.
20
+
21
+ class MockRequest
22
+ class FatalWarning < RuntimeError
23
+ end
24
+
25
+ class FatalWarner
26
+ def puts(warning)
27
+ raise FatalWarning, warning
28
+ end
29
+
30
+ def write(warning)
31
+ raise FatalWarning, warning
32
+ end
33
+
34
+ def flush
35
+ end
36
+
37
+ def string
38
+ ""
39
+ end
40
+ end
41
+
42
+ DEFAULT_ENV = {
43
+ "rack.version" => [1,0],
44
+ "rack.input" => StringIO.new,
45
+ "rack.errors" => StringIO.new,
46
+ "rack.multithread" => true,
47
+ "rack.multiprocess" => true,
48
+ "rack.run_once" => false,
49
+ }
50
+
51
+ def initialize(app)
52
+ @app = app
53
+ end
54
+
55
+ def get(uri, opts={}) request("GET", uri, opts) end
56
+ def post(uri, opts={}) request("POST", uri, opts) end
57
+ def put(uri, opts={}) request("PUT", uri, opts) end
58
+ def delete(uri, opts={}) request("DELETE", uri, opts) end
59
+
60
+ def request(method="GET", uri="", opts={})
61
+ env = self.class.env_for(uri, opts.merge(:method => method))
62
+
63
+ if opts[:lint]
64
+ app = Rack::Lint.new(@app)
65
+ else
66
+ app = @app
67
+ end
68
+
69
+ errors = env["rack.errors"]
70
+ MockResponse.new(*(app.call(env) + [errors]))
71
+ end
72
+
73
+ # Return the Rack environment used for a request to +uri+.
74
+ def self.env_for(uri="", opts={})
75
+ uri = URI(uri)
76
+ uri.path = "/#{uri.path}" unless uri.path[0] == ?/
77
+
78
+ env = DEFAULT_ENV.dup
79
+
80
+ env["REQUEST_METHOD"] = opts[:method] ? opts[:method].to_s.upcase : "GET"
81
+ env["SERVER_NAME"] = uri.host || "example.org"
82
+ env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
83
+ env["QUERY_STRING"] = uri.query.to_s
84
+ env["PATH_INFO"] = (!uri.path || uri.path.empty?) ? "/" : uri.path
85
+ env["rack.url_scheme"] = uri.scheme || "http"
86
+ env["HTTPS"] = env["rack.url_scheme"] == "https" ? "on" : "off"
87
+
88
+ env["SCRIPT_NAME"] = opts[:script_name] || ""
89
+
90
+ if opts[:fatal]
91
+ env["rack.errors"] = FatalWarner.new
92
+ else
93
+ env["rack.errors"] = StringIO.new
94
+ end
95
+
96
+ if params = opts[:params]
97
+ if env["REQUEST_METHOD"] == "GET"
98
+ params = Utils.parse_nested_query(params) if params.is_a?(String)
99
+ params.update(Utils.parse_nested_query(env["QUERY_STRING"]))
100
+ env["QUERY_STRING"] = Utils.build_nested_query(params)
101
+ elsif !opts.has_key?(:input)
102
+ opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
103
+ if params.is_a?(Hash)
104
+ if data = Utils::Multipart.build_multipart(params)
105
+ opts[:input] = data
106
+ opts["CONTENT_LENGTH"] ||= data.length.to_s
107
+ opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}"
108
+ else
109
+ opts[:input] = Utils.build_nested_query(params)
110
+ end
111
+ else
112
+ opts[:input] = params
113
+ end
114
+ end
115
+ end
116
+
117
+ opts[:input] ||= ""
118
+ if String === opts[:input]
119
+ rack_input = StringIO.new(opts[:input])
120
+ else
121
+ rack_input = opts[:input]
122
+ end
123
+
124
+ rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
125
+ env['rack.input'] = rack_input
126
+
127
+ env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s
128
+
129
+ opts.each { |field, value|
130
+ env[field] = value if String === field
131
+ }
132
+
133
+ env
134
+ end
135
+ end
136
+
137
+ # Rack::MockResponse provides useful helpers for testing your apps.
138
+ # Usually, you don't create the MockResponse on your own, but use
139
+ # MockRequest.
140
+
141
+ class MockResponse
142
+ def initialize(status, headers, body, errors=StringIO.new(""))
143
+ @status = status.to_i
144
+
145
+ @original_headers = headers
146
+ @headers = Rack::Utils::HeaderHash.new
147
+ headers.each { |field, values|
148
+ @headers[field] = values
149
+ @headers[field] = "" if values.empty?
150
+ }
151
+
152
+ @body = ""
153
+ body.each { |part| @body << part }
154
+
155
+ @errors = errors.string if errors.respond_to?(:string)
156
+ end
157
+
158
+ # Status
159
+ attr_reader :status
160
+
161
+ # Headers
162
+ attr_reader :headers, :original_headers
163
+
164
+ def [](field)
165
+ headers[field]
166
+ end
167
+
168
+
169
+ # Body
170
+ attr_reader :body
171
+
172
+ def =~(other)
173
+ @body =~ other
174
+ end
175
+
176
+ def match(other)
177
+ @body.match other
178
+ end
179
+
180
+
181
+ # Errors
182
+ attr_accessor :errors
183
+
184
+
185
+ include Response::Helpers
186
+ end
187
+ end
@@ -0,0 +1,57 @@
1
+ require 'uri'
2
+
3
+ module Rack
4
+ # Rack::ForwardRequest gets caught by Rack::Recursive and redirects
5
+ # the current request to the app at +url+.
6
+ #
7
+ # raise ForwardRequest.new("/not-found")
8
+ #
9
+
10
+ class ForwardRequest < Exception
11
+ attr_reader :url, :env
12
+
13
+ def initialize(url, env={})
14
+ @url = URI(url)
15
+ @env = env
16
+
17
+ @env["PATH_INFO"] = @url.path
18
+ @env["QUERY_STRING"] = @url.query if @url.query
19
+ @env["HTTP_HOST"] = @url.host if @url.host
20
+ @env["HTTP_PORT"] = @url.port if @url.port
21
+ @env["rack.url_scheme"] = @url.scheme if @url.scheme
22
+
23
+ super "forwarding to #{url}"
24
+ end
25
+ end
26
+
27
+ # Rack::Recursive allows applications called down the chain to
28
+ # include data from other applications (by using
29
+ # <tt>rack['rack.recursive.include'][...]</tt> or raise a
30
+ # ForwardRequest to redirect internally.
31
+
32
+ class Recursive
33
+ def initialize(app)
34
+ @app = app
35
+ end
36
+
37
+ def call(env)
38
+ @script_name = env["SCRIPT_NAME"]
39
+ @app.call(env.merge('rack.recursive.include' => method(:include)))
40
+ rescue ForwardRequest => req
41
+ call(env.merge(req.env))
42
+ end
43
+
44
+ def include(env, path)
45
+ unless path.index(@script_name) == 0 && (path[@script_name.size] == ?/ ||
46
+ path[@script_name.size].nil?)
47
+ raise ArgumentError, "can only include below #{@script_name}, not #{path}"
48
+ end
49
+
50
+ env = env.merge("PATH_INFO" => path, "SCRIPT_NAME" => @script_name,
51
+ "REQUEST_METHOD" => "GET",
52
+ "CONTENT_LENGTH" => "0", "CONTENT_TYPE" => "",
53
+ "rack.input" => StringIO.new(""))
54
+ @app.call(env)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,107 @@
1
+ # Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
2
+ # Rack::Reloader is subject to the terms of an MIT-style license.
3
+ # See COPYING or http://www.opensource.org/licenses/mit-license.php.
4
+
5
+ require 'pathname'
6
+
7
+ module Rack
8
+
9
+ # High performant source reloader
10
+ #
11
+ # This class acts as Rack middleware.
12
+ #
13
+ # What makes it especially suited for use in a production environment is that
14
+ # any file will only be checked once and there will only be made one system
15
+ # call stat(2).
16
+ #
17
+ # Please note that this will not reload files in the background, it does so
18
+ # only when actively called.
19
+ #
20
+ # It is performing a check/reload cycle at the start of every request, but
21
+ # also respects a cool down time, during which nothing will be done.
22
+ class Reloader
23
+ def initialize(app, cooldown = 10, backend = Stat)
24
+ @app = app
25
+ @cooldown = cooldown
26
+ @last = (Time.now - cooldown)
27
+ @cache = {}
28
+ @mtimes = {}
29
+
30
+ extend backend
31
+ end
32
+
33
+ def call(env)
34
+ if @cooldown and Time.now > @last + @cooldown
35
+ if Thread.list.size > 1
36
+ Thread.exclusive{ reload! }
37
+ else
38
+ reload!
39
+ end
40
+
41
+ @last = Time.now
42
+ end
43
+
44
+ @app.call(env)
45
+ end
46
+
47
+ def reload!(stderr = $stderr)
48
+ rotation do |file, mtime|
49
+ previous_mtime = @mtimes[file] ||= mtime
50
+ safe_load(file, mtime, stderr) if mtime > previous_mtime
51
+ end
52
+ end
53
+
54
+ # A safe Kernel::load, issuing the hooks depending on the results
55
+ def safe_load(file, mtime, stderr = $stderr)
56
+ load(file)
57
+ stderr.puts "#{self.class}: reloaded `#{file}'"
58
+ file
59
+ rescue LoadError, SyntaxError => ex
60
+ stderr.puts ex
61
+ ensure
62
+ @mtimes[file] = mtime
63
+ end
64
+
65
+ module Stat
66
+ def rotation
67
+ files = [$0, *$LOADED_FEATURES].uniq
68
+ paths = ['./', *$LOAD_PATH].uniq
69
+
70
+ files.map{|file|
71
+ next if file =~ /\.(so|bundle)$/ # cannot reload compiled files
72
+
73
+ found, stat = figure_path(file, paths)
74
+ next unless found && stat && mtime = stat.mtime
75
+
76
+ @cache[file] = found
77
+
78
+ yield(found, mtime)
79
+ }.compact
80
+ end
81
+
82
+ # Takes a relative or absolute +file+ name, a couple possible +paths+ that
83
+ # the +file+ might reside in. Returns the full path and File::Stat for the
84
+ # path.
85
+ def figure_path(file, paths)
86
+ found = @cache[file]
87
+ found = file if !found and Pathname.new(file).absolute?
88
+ found, stat = safe_stat(found)
89
+ return found, stat if found
90
+
91
+ paths.find do |possible_path|
92
+ path = ::File.join(possible_path, file)
93
+ found, stat = safe_stat(path)
94
+ return ::File.expand_path(found), stat if found
95
+ end
96
+ end
97
+
98
+ def safe_stat(file)
99
+ return unless file
100
+ stat = ::File.stat(file)
101
+ return file, stat if stat.file?
102
+ rescue Errno::ENOENT, Errno::ENOTDIR
103
+ @cache.delete(file) and false
104
+ end
105
+ end
106
+ end
107
+ end