actionpack 2.2.3 → 2.3.2

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

Potentially problematic release.


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

Files changed (264) hide show
  1. data/CHANGELOG +433 -375
  2. data/MIT-LICENSE +1 -1
  3. data/README +21 -75
  4. data/Rakefile +1 -1
  5. data/lib/action_controller.rb +80 -43
  6. data/lib/action_controller/assertions/model_assertions.rb +1 -0
  7. data/lib/action_controller/assertions/response_assertions.rb +43 -16
  8. data/lib/action_controller/assertions/routing_assertions.rb +1 -1
  9. data/lib/action_controller/assertions/selector_assertions.rb +17 -12
  10. data/lib/action_controller/assertions/tag_assertions.rb +1 -4
  11. data/lib/action_controller/base.rb +153 -82
  12. data/lib/action_controller/benchmarking.rb +9 -9
  13. data/lib/action_controller/caching.rb +9 -11
  14. data/lib/action_controller/caching/actions.rb +11 -18
  15. data/lib/action_controller/caching/fragments.rb +28 -20
  16. data/lib/action_controller/caching/pages.rb +13 -15
  17. data/lib/action_controller/caching/sweeping.rb +2 -2
  18. data/lib/action_controller/cgi_ext.rb +0 -1
  19. data/lib/action_controller/cgi_ext/cookie.rb +2 -0
  20. data/lib/action_controller/cgi_process.rb +54 -162
  21. data/lib/action_controller/cookies.rb +13 -25
  22. data/lib/action_controller/dispatcher.rb +43 -122
  23. data/lib/action_controller/failsafe.rb +52 -0
  24. data/lib/action_controller/flash.rb +38 -47
  25. data/lib/action_controller/helpers.rb +13 -9
  26. data/lib/action_controller/http_authentication.rb +203 -23
  27. data/lib/action_controller/integration.rb +126 -70
  28. data/lib/action_controller/layout.rb +36 -39
  29. data/lib/action_controller/middleware_stack.rb +119 -0
  30. data/lib/action_controller/middlewares.rb +13 -0
  31. data/lib/action_controller/mime_responds.rb +19 -4
  32. data/lib/action_controller/mime_type.rb +8 -0
  33. data/lib/action_controller/params_parser.rb +71 -0
  34. data/lib/action_controller/performance_test.rb +0 -1
  35. data/lib/action_controller/polymorphic_routes.rb +36 -30
  36. data/lib/action_controller/reloader.rb +14 -0
  37. data/lib/action_controller/request.rb +107 -499
  38. data/lib/action_controller/request_forgery_protection.rb +7 -39
  39. data/lib/action_controller/rescue.rb +55 -35
  40. data/lib/action_controller/resources.rb +34 -31
  41. data/lib/action_controller/response.rb +99 -57
  42. data/lib/action_controller/rewindable_input.rb +28 -0
  43. data/lib/action_controller/routing.rb +7 -7
  44. data/lib/action_controller/routing/builder.rb +4 -1
  45. data/lib/action_controller/routing/optimisations.rb +1 -1
  46. data/lib/action_controller/routing/recognition_optimisation.rb +1 -2
  47. data/lib/action_controller/routing/route.rb +15 -5
  48. data/lib/action_controller/routing/route_set.rb +82 -35
  49. data/lib/action_controller/routing/segments.rb +35 -0
  50. data/lib/action_controller/session/abstract_store.rb +181 -0
  51. data/lib/action_controller/session/cookie_store.rb +197 -175
  52. data/lib/action_controller/session/mem_cache_store.rb +36 -83
  53. data/lib/action_controller/session_management.rb +26 -134
  54. data/lib/action_controller/streaming.rb +24 -7
  55. data/lib/action_controller/templates/rescues/diagnostics.erb +2 -2
  56. data/lib/action_controller/templates/rescues/template_error.erb +2 -2
  57. data/lib/action_controller/test_case.rb +87 -30
  58. data/lib/action_controller/test_process.rb +145 -104
  59. data/lib/action_controller/uploaded_file.rb +44 -0
  60. data/lib/action_controller/url_rewriter.rb +3 -6
  61. data/lib/action_controller/vendor/html-scanner.rb +16 -0
  62. data/lib/action_controller/vendor/html-scanner/html/selector.rb +1 -1
  63. data/lib/action_controller/vendor/rack-1.0/rack.rb +89 -0
  64. data/lib/action_controller/vendor/rack-1.0/rack/adapter/camping.rb +22 -0
  65. data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/handler.rb +37 -0
  66. data/lib/action_controller/vendor/rack-1.0/rack/auth/abstract/request.rb +37 -0
  67. data/lib/action_controller/vendor/rack-1.0/rack/auth/basic.rb +58 -0
  68. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/md5.rb +124 -0
  69. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/nonce.rb +51 -0
  70. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/params.rb +55 -0
  71. data/lib/action_controller/vendor/rack-1.0/rack/auth/digest/request.rb +40 -0
  72. data/lib/action_controller/vendor/rack-1.0/rack/auth/openid.rb +480 -0
  73. data/lib/action_controller/vendor/rack-1.0/rack/builder.rb +63 -0
  74. data/lib/action_controller/vendor/rack-1.0/rack/cascade.rb +36 -0
  75. data/lib/action_controller/vendor/rack-1.0/rack/chunked.rb +49 -0
  76. data/lib/action_controller/vendor/rack-1.0/rack/commonlogger.rb +61 -0
  77. data/lib/action_controller/vendor/rack-1.0/rack/conditionalget.rb +45 -0
  78. data/lib/action_controller/vendor/rack-1.0/rack/content_length.rb +29 -0
  79. data/lib/action_controller/vendor/rack-1.0/rack/content_type.rb +23 -0
  80. data/lib/action_controller/vendor/rack-1.0/rack/deflater.rb +85 -0
  81. data/lib/action_controller/vendor/rack-1.0/rack/directory.rb +153 -0
  82. data/lib/action_controller/vendor/rack-1.0/rack/file.rb +88 -0
  83. data/lib/action_controller/vendor/rack-1.0/rack/handler.rb +48 -0
  84. data/lib/action_controller/vendor/rack-1.0/rack/handler/cgi.rb +61 -0
  85. data/lib/action_controller/vendor/rack-1.0/rack/handler/evented_mongrel.rb +8 -0
  86. data/lib/action_controller/vendor/rack-1.0/rack/handler/fastcgi.rb +89 -0
  87. data/lib/action_controller/vendor/rack-1.0/rack/handler/lsws.rb +55 -0
  88. data/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb +84 -0
  89. data/lib/action_controller/vendor/rack-1.0/rack/handler/scgi.rb +59 -0
  90. data/lib/action_controller/vendor/rack-1.0/rack/handler/swiftiplied_mongrel.rb +8 -0
  91. data/lib/action_controller/vendor/rack-1.0/rack/handler/thin.rb +18 -0
  92. data/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb +67 -0
  93. data/lib/action_controller/vendor/rack-1.0/rack/head.rb +19 -0
  94. data/lib/action_controller/vendor/rack-1.0/rack/lint.rb +462 -0
  95. data/lib/action_controller/vendor/rack-1.0/rack/lobster.rb +65 -0
  96. data/lib/action_controller/vendor/rack-1.0/rack/lock.rb +16 -0
  97. data/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb +27 -0
  98. data/lib/action_controller/vendor/rack-1.0/rack/mime.rb +204 -0
  99. data/lib/action_controller/vendor/rack-1.0/rack/mock.rb +160 -0
  100. data/lib/action_controller/vendor/rack-1.0/rack/recursive.rb +57 -0
  101. data/lib/action_controller/vendor/rack-1.0/rack/reloader.rb +64 -0
  102. data/lib/action_controller/vendor/rack-1.0/rack/request.rb +241 -0
  103. data/lib/action_controller/vendor/rack-1.0/rack/response.rb +179 -0
  104. data/lib/action_controller/vendor/rack-1.0/rack/session/abstract/id.rb +142 -0
  105. data/lib/action_controller/vendor/rack-1.0/rack/session/cookie.rb +91 -0
  106. data/lib/action_controller/vendor/rack-1.0/rack/session/memcache.rb +109 -0
  107. data/lib/action_controller/vendor/rack-1.0/rack/session/pool.rb +100 -0
  108. data/lib/action_controller/vendor/rack-1.0/rack/showexceptions.rb +349 -0
  109. data/lib/action_controller/vendor/rack-1.0/rack/showstatus.rb +106 -0
  110. data/lib/action_controller/vendor/rack-1.0/rack/static.rb +38 -0
  111. data/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb +55 -0
  112. data/lib/action_controller/vendor/rack-1.0/rack/utils.rb +392 -0
  113. data/lib/action_controller/verification.rb +1 -1
  114. data/lib/action_pack.rb +1 -1
  115. data/lib/action_pack/version.rb +2 -2
  116. data/lib/action_view.rb +22 -17
  117. data/lib/action_view/base.rb +53 -79
  118. data/lib/action_view/erb/util.rb +38 -0
  119. data/lib/action_view/helpers.rb +24 -5
  120. data/lib/action_view/helpers/active_record_helper.rb +2 -2
  121. data/lib/action_view/helpers/asset_tag_helper.rb +81 -50
  122. data/lib/action_view/helpers/atom_feed_helper.rb +1 -1
  123. data/lib/action_view/helpers/benchmark_helper.rb +26 -5
  124. data/lib/action_view/helpers/date_helper.rb +82 -7
  125. data/lib/action_view/helpers/form_helper.rb +295 -64
  126. data/lib/action_view/helpers/form_options_helper.rb +160 -18
  127. data/lib/action_view/helpers/form_tag_helper.rb +2 -2
  128. data/lib/action_view/helpers/number_helper.rb +31 -18
  129. data/lib/action_view/helpers/prototype_helper.rb +2 -12
  130. data/lib/action_view/helpers/sanitize_helper.rb +0 -10
  131. data/lib/action_view/helpers/scriptaculous_helper.rb +1 -0
  132. data/lib/action_view/helpers/tag_helper.rb +3 -4
  133. data/lib/action_view/helpers/text_helper.rb +99 -122
  134. data/lib/action_view/helpers/translation_helper.rb +19 -1
  135. data/lib/action_view/helpers/url_helper.rb +25 -2
  136. data/lib/action_view/inline_template.rb +1 -1
  137. data/lib/action_view/locale/en.yml +19 -1
  138. data/lib/action_view/partials.rb +46 -9
  139. data/lib/action_view/paths.rb +28 -84
  140. data/lib/action_view/reloadable_template.rb +117 -0
  141. data/lib/action_view/renderable.rb +28 -35
  142. data/lib/action_view/renderable_partial.rb +3 -4
  143. data/lib/action_view/template.rb +172 -31
  144. data/lib/action_view/template_error.rb +8 -9
  145. data/lib/action_view/template_handler.rb +1 -1
  146. data/lib/action_view/template_handlers.rb +9 -6
  147. data/lib/action_view/template_handlers/erb.rb +2 -39
  148. data/lib/action_view/template_handlers/rjs.rb +1 -0
  149. data/lib/action_view/test_case.rb +27 -1
  150. data/test/abstract_unit.rb +23 -17
  151. data/test/active_record_unit.rb +5 -4
  152. data/test/activerecord/active_record_store_test.rb +139 -106
  153. data/test/activerecord/render_partial_with_record_identification_test.rb +5 -21
  154. data/test/controller/action_pack_assertions_test.rb +25 -23
  155. data/test/controller/addresses_render_test.rb +3 -6
  156. data/test/controller/assert_select_test.rb +83 -70
  157. data/test/controller/base_test.rb +11 -13
  158. data/test/controller/benchmark_test.rb +3 -3
  159. data/test/controller/caching_test.rb +34 -24
  160. data/test/controller/capture_test.rb +3 -6
  161. data/test/controller/content_type_test.rb +3 -6
  162. data/test/controller/cookie_test.rb +31 -66
  163. data/test/controller/deprecation/deprecated_base_methods_test.rb +9 -11
  164. data/test/controller/dispatcher_test.rb +23 -28
  165. data/test/controller/fake_models.rb +8 -0
  166. data/test/controller/filters_test.rb +6 -2
  167. data/test/controller/flash_test.rb +2 -6
  168. data/test/controller/helper_test.rb +15 -1
  169. data/test/controller/html-scanner/document_test.rb +1 -1
  170. data/test/controller/html-scanner/sanitizer_test.rb +1 -1
  171. data/test/controller/http_basic_authentication_test.rb +88 -0
  172. data/test/controller/http_digest_authentication_test.rb +178 -0
  173. data/test/controller/integration_test.rb +56 -52
  174. data/test/controller/layout_test.rb +46 -44
  175. data/test/controller/middleware_stack_test.rb +90 -0
  176. data/test/controller/mime_responds_test.rb +7 -11
  177. data/test/controller/mime_type_test.rb +9 -0
  178. data/test/controller/polymorphic_routes_test.rb +235 -151
  179. data/test/controller/rack_test.rb +52 -81
  180. data/test/controller/redirect_test.rb +6 -14
  181. data/test/controller/render_test.rb +273 -60
  182. data/test/controller/request/json_params_parsing_test.rb +45 -0
  183. data/test/controller/request/multipart_params_parsing_test.rb +223 -0
  184. data/test/controller/request/query_string_parsing_test.rb +120 -0
  185. data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
  186. data/test/controller/request/xml_params_parsing_test.rb +88 -0
  187. data/test/controller/request_forgery_protection_test.rb +17 -98
  188. data/test/controller/request_test.rb +45 -530
  189. data/test/controller/rescue_test.rb +45 -22
  190. data/test/controller/resources_test.rb +112 -37
  191. data/test/controller/routing_test.rb +1442 -1384
  192. data/test/controller/selector_test.rb +3 -3
  193. data/test/controller/send_file_test.rb +30 -3
  194. data/test/controller/session/cookie_store_test.rb +169 -240
  195. data/test/controller/session/mem_cache_store_test.rb +94 -148
  196. data/test/controller/session/test_session_test.rb +58 -0
  197. data/test/controller/test_test.rb +32 -13
  198. data/test/controller/url_rewriter_test.rb +54 -4
  199. data/test/controller/verification_test.rb +1 -1
  200. data/test/controller/view_paths_test.rb +15 -15
  201. data/test/controller/webservice_test.rb +178 -147
  202. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  203. data/test/fixtures/layout_tests/alt/layouts/alt.rhtml +0 -0
  204. data/test/fixtures/layouts/default_html.html.erb +1 -0
  205. data/test/fixtures/layouts/xhr.html.erb +2 -0
  206. data/test/fixtures/multipart/empty +10 -0
  207. data/test/fixtures/multipart/hello.txt +1 -0
  208. data/test/fixtures/multipart/none +9 -0
  209. data/test/fixtures/public/500.da.html +1 -0
  210. data/test/fixtures/quiz/questions/_question.html.erb +1 -0
  211. data/test/fixtures/replies.yml +1 -1
  212. data/test/fixtures/test/_one.html.erb +1 -0
  213. data/test/fixtures/test/_two.html.erb +1 -0
  214. data/test/fixtures/test/dont_pick_me +1 -0
  215. data/test/fixtures/test/hello.builder +1 -1
  216. data/test/fixtures/test/hello_world.da.html.erb +1 -0
  217. data/test/fixtures/test/hello_world.erb~ +1 -0
  218. data/test/fixtures/test/hello_world.pt-BR.html.erb +1 -0
  219. data/test/fixtures/test/malformed/malformed.en.html.erb~ +1 -0
  220. data/test/fixtures/test/malformed/malformed.erb~ +1 -0
  221. data/test/fixtures/test/malformed/malformed.html.erb~ +1 -0
  222. data/test/fixtures/test/render_explicit_html_template.js.rjs +1 -0
  223. data/test/fixtures/test/render_implicit_html_template.js.rjs +1 -0
  224. data/test/fixtures/test/render_implicit_html_template_from_xhr_request.da.html.erb +1 -0
  225. data/test/fixtures/test/render_implicit_html_template_from_xhr_request.html.erb +1 -0
  226. data/test/fixtures/test/render_implicit_js_template_without_layout.js.erb +1 -0
  227. data/test/fixtures/test/utf8.html.erb +2 -0
  228. data/test/template/active_record_helper_i18n_test.rb +31 -33
  229. data/test/template/active_record_helper_test.rb +34 -0
  230. data/test/template/asset_tag_helper_test.rb +52 -14
  231. data/test/template/atom_feed_helper_test.rb +3 -5
  232. data/test/template/benchmark_helper_test.rb +50 -24
  233. data/test/template/compiled_templates_test.rb +177 -33
  234. data/test/template/date_helper_i18n_test.rb +88 -81
  235. data/test/template/date_helper_test.rb +427 -43
  236. data/test/template/form_helper_test.rb +243 -44
  237. data/test/template/form_options_helper_test.rb +631 -565
  238. data/test/template/form_tag_helper_test.rb +9 -2
  239. data/test/template/javascript_helper_test.rb +0 -5
  240. data/test/template/number_helper_i18n_test.rb +60 -48
  241. data/test/template/number_helper_test.rb +1 -0
  242. data/test/template/render_test.rb +117 -35
  243. data/test/template/test_test.rb +4 -6
  244. data/test/template/text_helper_test.rb +129 -50
  245. data/test/template/translation_helper_test.rb +23 -19
  246. data/test/template/url_helper_test.rb +35 -2
  247. data/test/view/test_case_test.rb +8 -0
  248. metadata +197 -23
  249. data/lib/action_controller/assertions.rb +0 -69
  250. data/lib/action_controller/caching/sql_cache.rb +0 -18
  251. data/lib/action_controller/cgi_ext/session.rb +0 -53
  252. data/lib/action_controller/components.rb +0 -169
  253. data/lib/action_controller/rack_process.rb +0 -297
  254. data/lib/action_controller/request_profiler.rb +0 -169
  255. data/lib/action_controller/session/active_record_store.rb +0 -340
  256. data/lib/action_controller/session/drb_server.rb +0 -32
  257. data/lib/action_controller/session/drb_store.rb +0 -35
  258. data/test/controller/cgi_test.rb +0 -269
  259. data/test/controller/components_test.rb +0 -156
  260. data/test/controller/http_authentication_test.rb +0 -54
  261. data/test/controller/integration_upload_test.rb +0 -43
  262. data/test/controller/session_fixation_test.rb +0 -89
  263. data/test/controller/session_management_test.rb +0 -178
  264. data/test/fixtures/test/hello_world.js +0 -1
@@ -0,0 +1,63 @@
1
+ module Rack
2
+ # Rack::Builder implements a small DSL to iteratively construct Rack
3
+ # applications.
4
+ #
5
+ # Example:
6
+ #
7
+ # app = Rack::Builder.new {
8
+ # use Rack::CommonLogger
9
+ # use Rack::ShowExceptions
10
+ # map "/lobster" do
11
+ # use Rack::Lint
12
+ # run Rack::Lobster.new
13
+ # end
14
+ # }
15
+ #
16
+ # Or
17
+ #
18
+ # app = Rack::Builder.app do
19
+ # use Rack::CommonLogger
20
+ # lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
21
+ # end
22
+ #
23
+ # +use+ adds a middleware to the stack, +run+ dispatches to an application.
24
+ # You can use +map+ to construct a Rack::URLMap in a convenient way.
25
+
26
+ class Builder
27
+ def initialize(&block)
28
+ @ins = []
29
+ instance_eval(&block) if block_given?
30
+ end
31
+
32
+ def self.app(&block)
33
+ self.new(&block).to_app
34
+ end
35
+
36
+ def use(middleware, *args, &block)
37
+ @ins << lambda { |app| middleware.new(app, *args, &block) }
38
+ end
39
+
40
+ def run(app)
41
+ @ins << app #lambda { |nothing| app }
42
+ end
43
+
44
+ def map(path, &block)
45
+ if @ins.last.kind_of? Hash
46
+ @ins.last[path] = self.class.new(&block).to_app
47
+ else
48
+ @ins << {}
49
+ map(path, &block)
50
+ end
51
+ end
52
+
53
+ def to_app
54
+ @ins[-1] = Rack::URLMap.new(@ins.last) if Hash === @ins.last
55
+ inner_app = @ins.last
56
+ @ins[0...-1].reverse.inject(inner_app) { |a, e| e.call(a) }
57
+ end
58
+
59
+ def call(env)
60
+ to_app.call(env)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ module Rack
2
+ # Rack::Cascade tries an request on several apps, and returns the
3
+ # first response that is not 404 (or in a list of configurable
4
+ # status codes).
5
+
6
+ class Cascade
7
+ attr_reader :apps
8
+
9
+ def initialize(apps, catch=404)
10
+ @apps = apps
11
+ @catch = [*catch]
12
+ end
13
+
14
+ def call(env)
15
+ status = headers = body = nil
16
+ raise ArgumentError, "empty cascade" if @apps.empty?
17
+ @apps.each { |app|
18
+ begin
19
+ status, headers, body = app.call(env)
20
+ break unless @catch.include?(status.to_i)
21
+ end
22
+ }
23
+ [status, headers, body]
24
+ end
25
+
26
+ def add app
27
+ @apps << app
28
+ end
29
+
30
+ def include? app
31
+ @apps.include? app
32
+ end
33
+
34
+ alias_method :<<, :add
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+
5
+ # Middleware that applies chunked transfer encoding to response bodies
6
+ # when the response does not include a Content-Length header.
7
+ class Chunked
8
+ include Rack::Utils
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ status, headers, body = @app.call(env)
16
+ headers = HeaderHash.new(headers)
17
+
18
+ if env['HTTP_VERSION'] == 'HTTP/1.0' ||
19
+ STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
20
+ headers['Content-Length'] ||
21
+ headers['Transfer-Encoding']
22
+ [status, headers.to_hash, body]
23
+ else
24
+ dup.chunk(status, headers, body)
25
+ end
26
+ end
27
+
28
+ def chunk(status, headers, body)
29
+ @body = body
30
+ headers.delete('Content-Length')
31
+ headers['Transfer-Encoding'] = 'chunked'
32
+ [status, headers.to_hash, self]
33
+ end
34
+
35
+ def each
36
+ term = "\r\n"
37
+ @body.each do |chunk|
38
+ size = bytesize(chunk)
39
+ next if size == 0
40
+ yield [size.to_s(16), term, chunk, term].join
41
+ end
42
+ yield ["0", term, "", term].join
43
+ end
44
+
45
+ def close
46
+ @body.close if @body.respond_to?(:close)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,61 @@
1
+ module Rack
2
+ # Rack::CommonLogger forwards every request to an +app+ given, and
3
+ # logs a line in the Apache common log format to the +logger+, or
4
+ # rack.errors by default.
5
+
6
+ class CommonLogger
7
+ def initialize(app, logger=nil)
8
+ @app = app
9
+ @logger = logger
10
+ end
11
+
12
+ def call(env)
13
+ dup._call(env)
14
+ end
15
+
16
+ def _call(env)
17
+ @env = env
18
+ @logger ||= self
19
+ @time = Time.now
20
+ @status, @header, @body = @app.call(env)
21
+ [@status, @header, self]
22
+ end
23
+
24
+ def close
25
+ @body.close if @body.respond_to? :close
26
+ end
27
+
28
+ # By default, log to rack.errors.
29
+ def <<(str)
30
+ @env["rack.errors"].write(str)
31
+ @env["rack.errors"].flush
32
+ end
33
+
34
+ def each
35
+ length = 0
36
+ @body.each { |part|
37
+ length += part.size
38
+ yield part
39
+ }
40
+
41
+ @now = Time.now
42
+
43
+ # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
44
+ # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
45
+ # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
46
+ @logger << %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n} %
47
+ [
48
+ @env['HTTP_X_FORWARDED_FOR'] || @env["REMOTE_ADDR"] || "-",
49
+ @env["REMOTE_USER"] || "-",
50
+ @now.strftime("%d/%b/%Y %H:%M:%S"),
51
+ @env["REQUEST_METHOD"],
52
+ @env["PATH_INFO"],
53
+ @env["QUERY_STRING"].empty? ? "" : "?"+@env["QUERY_STRING"],
54
+ @env["HTTP_VERSION"],
55
+ @status.to_s[0..3],
56
+ (length.zero? ? "-" : length.to_s),
57
+ @now - @time
58
+ ]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,45 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+
5
+ # Middleware that enables conditional GET using If-None-Match and
6
+ # If-Modified-Since. The application should set either or both of the
7
+ # Last-Modified or Etag response headers according to RFC 2616. When
8
+ # either of the conditions is met, the response body is set to be zero
9
+ # length and the response status is set to 304 Not Modified.
10
+ #
11
+ # Applications that defer response body generation until the body's each
12
+ # message is received will avoid response body generation completely when
13
+ # a conditional GET matches.
14
+ #
15
+ # Adapted from Michael Klishin's Merb implementation:
16
+ # http://github.com/wycats/merb-core/tree/master/lib/merb-core/rack/middleware/conditional_get.rb
17
+ class ConditionalGet
18
+ def initialize(app)
19
+ @app = app
20
+ end
21
+
22
+ def call(env)
23
+ return @app.call(env) unless %w[GET HEAD].include?(env['REQUEST_METHOD'])
24
+
25
+ status, headers, body = @app.call(env)
26
+ headers = Utils::HeaderHash.new(headers)
27
+ if etag_matches?(env, headers) || modified_since?(env, headers)
28
+ status = 304
29
+ body = []
30
+ end
31
+ [status, headers, body]
32
+ end
33
+
34
+ private
35
+ def etag_matches?(env, headers)
36
+ etag = headers['Etag'] and etag == env['HTTP_IF_NONE_MATCH']
37
+ end
38
+
39
+ def modified_since?(env, headers)
40
+ last_modified = headers['Last-Modified'] and
41
+ last_modified == env['HTTP_IF_MODIFIED_SINCE']
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,29 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+ # Sets the Content-Length header on responses with fixed-length bodies.
5
+ class ContentLength
6
+ include Rack::Utils
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ status, headers, body = @app.call(env)
14
+ headers = HeaderHash.new(headers)
15
+
16
+ if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
17
+ !headers['Content-Length'] &&
18
+ !headers['Transfer-Encoding'] &&
19
+ (body.respond_to?(:to_ary) || body.respond_to?(:to_str))
20
+
21
+ body = [body] if body.respond_to?(:to_str) # rack 0.4 compat
22
+ length = body.to_ary.inject(0) { |len, part| len + bytesize(part) }
23
+ headers['Content-Length'] = length.to_s
24
+ end
25
+
26
+ [status, headers, body]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+
5
+ # Sets the Content-Type header on responses which don't have one.
6
+ #
7
+ # Builder Usage:
8
+ # use Rack::ContentType, "text/plain"
9
+ #
10
+ # When no content type argument is provided, "text/html" is assumed.
11
+ class ContentType
12
+ def initialize(app, content_type = "text/html")
13
+ @app, @content_type = app, content_type
14
+ end
15
+
16
+ def call(env)
17
+ status, headers, body = @app.call(env)
18
+ headers = Utils::HeaderHash.new(headers)
19
+ headers['Content-Type'] ||= @content_type
20
+ [status, headers.to_hash, body]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,85 @@
1
+ require "zlib"
2
+ require "stringio"
3
+ require "time" # for Time.httpdate
4
+ require 'rack/utils'
5
+
6
+ module Rack
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 = headers.key?("Last-Modified") ?
37
+ Time.httpdate(headers["Last-Modified"]) : Time.now
38
+ body = self.class.gzip(body, mtime)
39
+ size = Rack::Utils.bytesize(body)
40
+ headers = headers.merge("Content-Encoding" => "gzip", "Content-Length" => size.to_s)
41
+ [status, headers, [body]]
42
+ when "deflate"
43
+ body = self.class.deflate(body)
44
+ size = Rack::Utils.bytesize(body)
45
+ headers = headers.merge("Content-Encoding" => "deflate", "Content-Length" => size.to_s)
46
+ [status, headers, [body]]
47
+ when "identity"
48
+ [status, headers, body]
49
+ when nil
50
+ message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
51
+ [406, {"Content-Type" => "text/plain", "Content-Length" => message.length.to_s}, [message]]
52
+ end
53
+ end
54
+
55
+ def self.gzip(body, mtime)
56
+ io = StringIO.new
57
+ gzip = Zlib::GzipWriter.new(io)
58
+ gzip.mtime = mtime
59
+
60
+ # TODO: Add streaming
61
+ body.each { |part| gzip << part }
62
+
63
+ gzip.close
64
+ return io.string
65
+ end
66
+
67
+ DEFLATE_ARGS = [
68
+ Zlib::DEFAULT_COMPRESSION,
69
+ # drop the zlib header which causes both Safari and IE to choke
70
+ -Zlib::MAX_WBITS,
71
+ Zlib::DEF_MEM_LEVEL,
72
+ Zlib::DEFAULT_STRATEGY
73
+ ]
74
+
75
+ # Loosely based on Mongrel's Deflate handler
76
+ def self.deflate(body)
77
+ deflater = Zlib::Deflate.new(*DEFLATE_ARGS)
78
+
79
+ # TODO: Add streaming
80
+ body.each { |part| deflater << part }
81
+
82
+ return deflater.finish
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,153 @@
1
+ require 'time'
2
+ require 'rack/utils'
3
+ require 'rack/mime'
4
+
5
+ module Rack
6
+ # Rack::Directory serves entries below the +root+ given, according to the
7
+ # path info of the Rack request. If a directory is found, the file's contents
8
+ # will be presented in an html based index. If a file is found, the env will
9
+ # be passed to the specified +app+.
10
+ #
11
+ # If +app+ is not specified, a Rack::File of the same +root+ will be used.
12
+
13
+ class Directory
14
+ DIR_FILE = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>"
15
+ DIR_PAGE = <<-PAGE
16
+ <html><head>
17
+ <title>%s</title>
18
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
19
+ <style type='text/css'>
20
+ table { width:100%%; }
21
+ .name { text-align:left; }
22
+ .size, .mtime { text-align:right; }
23
+ .type { width:11em; }
24
+ .mtime { width:15em; }
25
+ </style>
26
+ </head><body>
27
+ <h1>%s</h1>
28
+ <hr />
29
+ <table>
30
+ <tr>
31
+ <th class='name'>Name</th>
32
+ <th class='size'>Size</th>
33
+ <th class='type'>Type</th>
34
+ <th class='mtime'>Last Modified</th>
35
+ </tr>
36
+ %s
37
+ </table>
38
+ <hr />
39
+ </body></html>
40
+ PAGE
41
+
42
+ attr_reader :files
43
+ attr_accessor :root, :path
44
+
45
+ def initialize(root, app=nil)
46
+ @root = F.expand_path(root)
47
+ @app = app || Rack::File.new(@root)
48
+ end
49
+
50
+ def call(env)
51
+ dup._call(env)
52
+ end
53
+
54
+ F = ::File
55
+
56
+ def _call(env)
57
+ @env = env
58
+ @script_name = env['SCRIPT_NAME']
59
+ @path_info = Utils.unescape(env['PATH_INFO'])
60
+
61
+ if forbidden = check_forbidden
62
+ forbidden
63
+ else
64
+ @path = F.join(@root, @path_info)
65
+ list_path
66
+ end
67
+ end
68
+
69
+ def check_forbidden
70
+ return unless @path_info.include? ".."
71
+
72
+ body = "Forbidden\n"
73
+ size = Rack::Utils.bytesize(body)
74
+ return [403, {"Content-Type" => "text/plain","Content-Length" => size.to_s}, [body]]
75
+ end
76
+
77
+ def list_directory
78
+ @files = [['../','Parent Directory','','','']]
79
+ glob = F.join(@path, '*')
80
+
81
+ Dir[glob].sort.each do |node|
82
+ stat = stat(node)
83
+ next unless stat
84
+ basename = F.basename(node)
85
+ ext = F.extname(node)
86
+
87
+ url = F.join(@script_name, @path_info, basename)
88
+ size = stat.size
89
+ type = stat.directory? ? 'directory' : Mime.mime_type(ext)
90
+ size = stat.directory? ? '-' : filesize_format(size)
91
+ mtime = stat.mtime.httpdate
92
+ url << '/' if stat.directory?
93
+ basename << '/' if stat.directory?
94
+
95
+ @files << [ url, basename, size, type, mtime ]
96
+ end
97
+
98
+ return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
99
+ end
100
+
101
+ def stat(node, max = 10)
102
+ F.stat(node)
103
+ rescue Errno::ENOENT, Errno::ELOOP
104
+ return nil
105
+ end
106
+
107
+ # TODO: add correct response if not readable, not sure if 404 is the best
108
+ # option
109
+ def list_path
110
+ @stat = F.stat(@path)
111
+
112
+ if @stat.readable?
113
+ return @app.call(@env) if @stat.file?
114
+ return list_directory if @stat.directory?
115
+ else
116
+ raise Errno::ENOENT, 'No such file or directory'
117
+ end
118
+
119
+ rescue Errno::ENOENT, Errno::ELOOP
120
+ return entity_not_found
121
+ end
122
+
123
+ def entity_not_found
124
+ body = "Entity not found: #{@path_info}\n"
125
+ size = Rack::Utils.bytesize(body)
126
+ return [404, {"Content-Type" => "text/plain", "Content-Length" => size.to_s}, [body]]
127
+ end
128
+
129
+ def each
130
+ show_path = @path.sub(/^#{@root}/,'')
131
+ files = @files.map{|f| DIR_FILE % f }*"\n"
132
+ page = DIR_PAGE % [ show_path, show_path , files ]
133
+ page.each_line{|l| yield l }
134
+ end
135
+
136
+ # Stolen from Ramaze
137
+
138
+ FILESIZE_FORMAT = [
139
+ ['%.1fT', 1 << 40],
140
+ ['%.1fG', 1 << 30],
141
+ ['%.1fM', 1 << 20],
142
+ ['%.1fK', 1 << 10],
143
+ ]
144
+
145
+ def filesize_format(int)
146
+ FILESIZE_FORMAT.each do |format, size|
147
+ return format % (int.to_f / size) if int >= size
148
+ end
149
+
150
+ int.to_s + 'B'
151
+ end
152
+ end
153
+ end