omg-actionpack 8.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +129 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +57 -0
- data/lib/abstract_controller/asset_paths.rb +14 -0
- data/lib/abstract_controller/base.rb +299 -0
- data/lib/abstract_controller/caching/fragments.rb +149 -0
- data/lib/abstract_controller/caching.rb +68 -0
- data/lib/abstract_controller/callbacks.rb +265 -0
- data/lib/abstract_controller/collector.rb +44 -0
- data/lib/abstract_controller/deprecator.rb +9 -0
- data/lib/abstract_controller/error.rb +8 -0
- data/lib/abstract_controller/helpers.rb +243 -0
- data/lib/abstract_controller/logger.rb +16 -0
- data/lib/abstract_controller/railties/routes_helpers.rb +25 -0
- data/lib/abstract_controller/rendering.rb +126 -0
- data/lib/abstract_controller/translation.rb +42 -0
- data/lib/abstract_controller/url_for.rb +37 -0
- data/lib/abstract_controller.rb +36 -0
- data/lib/action_controller/api/api_rendering.rb +18 -0
- data/lib/action_controller/api.rb +155 -0
- data/lib/action_controller/base.rb +332 -0
- data/lib/action_controller/caching.rb +49 -0
- data/lib/action_controller/deprecator.rb +9 -0
- data/lib/action_controller/form_builder.rb +55 -0
- data/lib/action_controller/log_subscriber.rb +96 -0
- data/lib/action_controller/metal/allow_browser.rb +123 -0
- data/lib/action_controller/metal/basic_implicit_render.rb +17 -0
- data/lib/action_controller/metal/conditional_get.rb +341 -0
- data/lib/action_controller/metal/content_security_policy.rb +86 -0
- data/lib/action_controller/metal/cookies.rb +20 -0
- data/lib/action_controller/metal/data_streaming.rb +154 -0
- data/lib/action_controller/metal/default_headers.rb +21 -0
- data/lib/action_controller/metal/etag_with_flash.rb +22 -0
- data/lib/action_controller/metal/etag_with_template_digest.rb +59 -0
- data/lib/action_controller/metal/exceptions.rb +106 -0
- data/lib/action_controller/metal/flash.rb +67 -0
- data/lib/action_controller/metal/head.rb +67 -0
- data/lib/action_controller/metal/helpers.rb +129 -0
- data/lib/action_controller/metal/http_authentication.rb +565 -0
- data/lib/action_controller/metal/implicit_render.rb +67 -0
- data/lib/action_controller/metal/instrumentation.rb +120 -0
- data/lib/action_controller/metal/live.rb +398 -0
- data/lib/action_controller/metal/logging.rb +22 -0
- data/lib/action_controller/metal/mime_responds.rb +337 -0
- data/lib/action_controller/metal/parameter_encoding.rb +84 -0
- data/lib/action_controller/metal/params_wrapper.rb +312 -0
- data/lib/action_controller/metal/permissions_policy.rb +38 -0
- data/lib/action_controller/metal/rate_limiting.rb +62 -0
- data/lib/action_controller/metal/redirecting.rb +251 -0
- data/lib/action_controller/metal/renderers.rb +181 -0
- data/lib/action_controller/metal/rendering.rb +260 -0
- data/lib/action_controller/metal/request_forgery_protection.rb +667 -0
- data/lib/action_controller/metal/rescue.rb +33 -0
- data/lib/action_controller/metal/streaming.rb +183 -0
- data/lib/action_controller/metal/strong_parameters.rb +1546 -0
- data/lib/action_controller/metal/testing.rb +25 -0
- data/lib/action_controller/metal/url_for.rb +65 -0
- data/lib/action_controller/metal.rb +339 -0
- data/lib/action_controller/railtie.rb +149 -0
- data/lib/action_controller/railties/helpers.rb +26 -0
- data/lib/action_controller/renderer.rb +161 -0
- data/lib/action_controller/template_assertions.rb +13 -0
- data/lib/action_controller/test_case.rb +691 -0
- data/lib/action_controller.rb +80 -0
- data/lib/action_dispatch/constants.rb +34 -0
- data/lib/action_dispatch/deprecator.rb +9 -0
- data/lib/action_dispatch/http/cache.rb +249 -0
- data/lib/action_dispatch/http/content_disposition.rb +47 -0
- data/lib/action_dispatch/http/content_security_policy.rb +365 -0
- data/lib/action_dispatch/http/filter_parameters.rb +80 -0
- data/lib/action_dispatch/http/filter_redirect.rb +50 -0
- data/lib/action_dispatch/http/headers.rb +134 -0
- data/lib/action_dispatch/http/mime_negotiation.rb +187 -0
- data/lib/action_dispatch/http/mime_type.rb +389 -0
- data/lib/action_dispatch/http/mime_types.rb +54 -0
- data/lib/action_dispatch/http/parameters.rb +119 -0
- data/lib/action_dispatch/http/permissions_policy.rb +189 -0
- data/lib/action_dispatch/http/rack_cache.rb +67 -0
- data/lib/action_dispatch/http/request.rb +498 -0
- data/lib/action_dispatch/http/response.rb +556 -0
- data/lib/action_dispatch/http/upload.rb +107 -0
- data/lib/action_dispatch/http/url.rb +344 -0
- data/lib/action_dispatch/journey/formatter.rb +226 -0
- data/lib/action_dispatch/journey/gtg/builder.rb +149 -0
- data/lib/action_dispatch/journey/gtg/simulator.rb +50 -0
- data/lib/action_dispatch/journey/gtg/transition_table.rb +217 -0
- data/lib/action_dispatch/journey/nfa/dot.rb +27 -0
- data/lib/action_dispatch/journey/nodes/node.rb +208 -0
- data/lib/action_dispatch/journey/parser.rb +103 -0
- data/lib/action_dispatch/journey/path/pattern.rb +209 -0
- data/lib/action_dispatch/journey/route.rb +189 -0
- data/lib/action_dispatch/journey/router/utils.rb +105 -0
- data/lib/action_dispatch/journey/router.rb +151 -0
- data/lib/action_dispatch/journey/routes.rb +82 -0
- data/lib/action_dispatch/journey/scanner.rb +70 -0
- data/lib/action_dispatch/journey/visitors.rb +267 -0
- data/lib/action_dispatch/journey/visualizer/fsm.css +30 -0
- data/lib/action_dispatch/journey/visualizer/fsm.js +159 -0
- data/lib/action_dispatch/journey/visualizer/index.html.erb +52 -0
- data/lib/action_dispatch/journey.rb +7 -0
- data/lib/action_dispatch/log_subscriber.rb +25 -0
- data/lib/action_dispatch/middleware/actionable_exceptions.rb +46 -0
- data/lib/action_dispatch/middleware/assume_ssl.rb +27 -0
- data/lib/action_dispatch/middleware/callbacks.rb +38 -0
- data/lib/action_dispatch/middleware/cookies.rb +719 -0
- data/lib/action_dispatch/middleware/debug_exceptions.rb +206 -0
- data/lib/action_dispatch/middleware/debug_locks.rb +129 -0
- data/lib/action_dispatch/middleware/debug_view.rb +73 -0
- data/lib/action_dispatch/middleware/exception_wrapper.rb +350 -0
- data/lib/action_dispatch/middleware/executor.rb +32 -0
- data/lib/action_dispatch/middleware/flash.rb +318 -0
- data/lib/action_dispatch/middleware/host_authorization.rb +171 -0
- data/lib/action_dispatch/middleware/public_exceptions.rb +64 -0
- data/lib/action_dispatch/middleware/reloader.rb +16 -0
- data/lib/action_dispatch/middleware/remote_ip.rb +199 -0
- data/lib/action_dispatch/middleware/request_id.rb +50 -0
- data/lib/action_dispatch/middleware/server_timing.rb +78 -0
- data/lib/action_dispatch/middleware/session/abstract_store.rb +112 -0
- data/lib/action_dispatch/middleware/session/cache_store.rb +66 -0
- data/lib/action_dispatch/middleware/session/cookie_store.rb +129 -0
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +34 -0
- data/lib/action_dispatch/middleware/show_exceptions.rb +88 -0
- data/lib/action_dispatch/middleware/ssl.rb +180 -0
- data/lib/action_dispatch/middleware/stack.rb +194 -0
- data/lib/action_dispatch/middleware/static.rb +192 -0
- data/lib/action_dispatch/middleware/templates/rescues/_actions.html.erb +13 -0
- data/lib/action_dispatch/middleware/templates/rescues/_actions.text.erb +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb +22 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +17 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +36 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +62 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb +12 -0
- data/lib/action_dispatch/middleware/templates/rescues/blocked_host.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +35 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +24 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +16 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +284 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +23 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +32 -0
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +6 -0
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +19 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +232 -0
- data/lib/action_dispatch/railtie.rb +77 -0
- data/lib/action_dispatch/request/session.rb +283 -0
- data/lib/action_dispatch/request/utils.rb +109 -0
- data/lib/action_dispatch/routing/endpoint.rb +19 -0
- data/lib/action_dispatch/routing/inspector.rb +323 -0
- data/lib/action_dispatch/routing/mapper.rb +2372 -0
- data/lib/action_dispatch/routing/polymorphic_routes.rb +363 -0
- data/lib/action_dispatch/routing/redirection.rb +218 -0
- data/lib/action_dispatch/routing/route_set.rb +958 -0
- data/lib/action_dispatch/routing/routes_proxy.rb +66 -0
- data/lib/action_dispatch/routing/url_for.rb +244 -0
- data/lib/action_dispatch/routing.rb +262 -0
- data/lib/action_dispatch/system_test_case.rb +206 -0
- data/lib/action_dispatch/system_testing/browser.rb +75 -0
- data/lib/action_dispatch/system_testing/driver.rb +85 -0
- data/lib/action_dispatch/system_testing/server.rb +33 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +164 -0
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +23 -0
- data/lib/action_dispatch/testing/assertion_response.rb +48 -0
- data/lib/action_dispatch/testing/assertions/response.rb +114 -0
- data/lib/action_dispatch/testing/assertions/routing.rb +343 -0
- data/lib/action_dispatch/testing/assertions.rb +25 -0
- data/lib/action_dispatch/testing/integration.rb +694 -0
- data/lib/action_dispatch/testing/request_encoder.rb +60 -0
- data/lib/action_dispatch/testing/test_helpers/page_dump_helper.rb +35 -0
- data/lib/action_dispatch/testing/test_process.rb +57 -0
- data/lib/action_dispatch/testing/test_request.rb +73 -0
- data/lib/action_dispatch/testing/test_response.rb +58 -0
- data/lib/action_dispatch.rb +147 -0
- data/lib/action_pack/gem_version.rb +19 -0
- data/lib/action_pack/version.rb +12 -0
- data/lib/action_pack.rb +27 -0
- metadata +375 -0
@@ -0,0 +1,183 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :markup: markdown
|
4
|
+
|
5
|
+
module ActionController # :nodoc:
|
6
|
+
# # Action Controller Streaming
|
7
|
+
#
|
8
|
+
# Allows views to be streamed back to the client as they are rendered.
|
9
|
+
#
|
10
|
+
# By default, Rails renders views by first rendering the template and then the
|
11
|
+
# layout. The response is sent to the client after the whole template is
|
12
|
+
# rendered, all queries are made, and the layout is processed.
|
13
|
+
#
|
14
|
+
# Streaming inverts the rendering flow by rendering the layout first and
|
15
|
+
# subsequently each part of the layout as they are processed. This allows the
|
16
|
+
# header of the HTML (which is usually in the layout) to be streamed back to
|
17
|
+
# client very quickly, enabling JavaScripts and stylesheets to be loaded earlier
|
18
|
+
# than usual.
|
19
|
+
#
|
20
|
+
# Several Rack middlewares may not work and you need to be careful when
|
21
|
+
# streaming. This is covered in more detail below, see the Streaming@Middlewares
|
22
|
+
# section.
|
23
|
+
#
|
24
|
+
# Streaming can be added to a given template easily, all you need to do is to
|
25
|
+
# pass the `:stream` option to `render`.
|
26
|
+
#
|
27
|
+
# class PostsController
|
28
|
+
# def index
|
29
|
+
# @posts = Post.all
|
30
|
+
# render stream: true
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# ## When to use streaming
|
35
|
+
#
|
36
|
+
# Streaming may be considered to be overkill for lightweight actions like `new`
|
37
|
+
# or `edit`. The real benefit of streaming is on expensive actions that, for
|
38
|
+
# example, do a lot of queries on the database.
|
39
|
+
#
|
40
|
+
# In such actions, you want to delay queries execution as much as you can. For
|
41
|
+
# example, imagine the following `dashboard` action:
|
42
|
+
#
|
43
|
+
# def dashboard
|
44
|
+
# @posts = Post.all
|
45
|
+
# @pages = Page.all
|
46
|
+
# @articles = Article.all
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# Most of the queries here are happening in the controller. In order to benefit
|
50
|
+
# from streaming you would want to rewrite it as:
|
51
|
+
#
|
52
|
+
# def dashboard
|
53
|
+
# # Allow lazy execution of the queries
|
54
|
+
# @posts = Post.all
|
55
|
+
# @pages = Page.all
|
56
|
+
# @articles = Article.all
|
57
|
+
# render stream: true
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# Notice that `:stream` only works with templates. Rendering `:json` or `:xml`
|
61
|
+
# with `:stream` won't work.
|
62
|
+
#
|
63
|
+
# ## Communication between layout and template
|
64
|
+
#
|
65
|
+
# When streaming, rendering happens top-down instead of inside-out. Rails starts
|
66
|
+
# with the layout, and the template is rendered later, when its `yield` is
|
67
|
+
# reached.
|
68
|
+
#
|
69
|
+
# This means that, if your application currently relies on instance variables
|
70
|
+
# set in the template to be used in the layout, they won't work once you move to
|
71
|
+
# streaming. The proper way to communicate between layout and template,
|
72
|
+
# regardless of whether you use streaming or not, is by using `content_for`,
|
73
|
+
# `provide`, and `yield`.
|
74
|
+
#
|
75
|
+
# Take a simple example where the layout expects the template to tell which
|
76
|
+
# title to use:
|
77
|
+
#
|
78
|
+
# <html>
|
79
|
+
# <head><title><%= yield :title %></title></head>
|
80
|
+
# <body><%= yield %></body>
|
81
|
+
# </html>
|
82
|
+
#
|
83
|
+
# You would use `content_for` in your template to specify the title:
|
84
|
+
#
|
85
|
+
# <%= content_for :title, "Main" %>
|
86
|
+
# Hello
|
87
|
+
#
|
88
|
+
# And the final result would be:
|
89
|
+
#
|
90
|
+
# <html>
|
91
|
+
# <head><title>Main</title></head>
|
92
|
+
# <body>Hello</body>
|
93
|
+
# </html>
|
94
|
+
#
|
95
|
+
# However, if `content_for` is called several times, the final result would have
|
96
|
+
# all calls concatenated. For instance, if we have the following template:
|
97
|
+
#
|
98
|
+
# <%= content_for :title, "Main" %>
|
99
|
+
# Hello
|
100
|
+
# <%= content_for :title, " page" %>
|
101
|
+
#
|
102
|
+
# The final result would be:
|
103
|
+
#
|
104
|
+
# <html>
|
105
|
+
# <head><title>Main page</title></head>
|
106
|
+
# <body>Hello</body>
|
107
|
+
# </html>
|
108
|
+
#
|
109
|
+
# This means that, if you have `yield :title` in your layout and you want to use
|
110
|
+
# streaming, you would have to render the whole template (and eventually trigger
|
111
|
+
# all queries) before streaming the title and all assets, which defeats the
|
112
|
+
# purpose of streaming. Alternatively, you can use a helper called `provide`
|
113
|
+
# that does the same as `content_for` but tells the layout to stop searching for
|
114
|
+
# other entries and continue rendering.
|
115
|
+
#
|
116
|
+
# For instance, the template above using `provide` would be:
|
117
|
+
#
|
118
|
+
# <%= provide :title, "Main" %>
|
119
|
+
# Hello
|
120
|
+
# <%= content_for :title, " page" %>
|
121
|
+
#
|
122
|
+
# Resulting in:
|
123
|
+
#
|
124
|
+
# <html>
|
125
|
+
# <head><title>Main</title></head>
|
126
|
+
# <body>Hello</body>
|
127
|
+
# </html>
|
128
|
+
#
|
129
|
+
# That said, when streaming, you need to properly check your templates and
|
130
|
+
# choose when to use `provide` and `content_for`.
|
131
|
+
#
|
132
|
+
# See also ActionView::Helpers::CaptureHelper for more information.
|
133
|
+
#
|
134
|
+
# ## Headers, cookies, session, and flash
|
135
|
+
#
|
136
|
+
# When streaming, the HTTP headers are sent to the client right before it
|
137
|
+
# renders the first line. This means that, modifying headers, cookies, session
|
138
|
+
# or flash after the template starts rendering will not propagate to the client.
|
139
|
+
#
|
140
|
+
# ## Middlewares
|
141
|
+
#
|
142
|
+
# Middlewares that need to manipulate the body won't work with streaming. You
|
143
|
+
# should disable those middlewares whenever streaming in development or
|
144
|
+
# production. For instance, `Rack::Bug` won't work when streaming as it needs to
|
145
|
+
# inject contents in the HTML body.
|
146
|
+
#
|
147
|
+
# Also `Rack::Cache` won't work with streaming as it does not support streaming
|
148
|
+
# bodies yet. Whenever streaming `Cache-Control` is automatically set to
|
149
|
+
# "no-cache".
|
150
|
+
#
|
151
|
+
# ## Errors
|
152
|
+
#
|
153
|
+
# When it comes to streaming, exceptions get a bit more complicated. This
|
154
|
+
# happens because part of the template was already rendered and streamed to the
|
155
|
+
# client, making it impossible to render a whole exception page.
|
156
|
+
#
|
157
|
+
# Currently, when an exception happens in development or production, Rails will
|
158
|
+
# automatically stream to the client:
|
159
|
+
#
|
160
|
+
# "><script>window.location = "/500.html"</script></html>
|
161
|
+
#
|
162
|
+
# The first two characters (`">`) are required in case the exception happens
|
163
|
+
# while rendering attributes for a given tag. You can check the real cause for
|
164
|
+
# the exception in your logger.
|
165
|
+
#
|
166
|
+
# ## Web server support
|
167
|
+
#
|
168
|
+
# Rack 3+ compatible servers all support streaming.
|
169
|
+
module Streaming
|
170
|
+
private
|
171
|
+
# Call render_body if we are streaming instead of usual `render`.
|
172
|
+
def _render_template(options)
|
173
|
+
if options.delete(:stream)
|
174
|
+
# It shoudn't be necessary to set this.
|
175
|
+
headers["cache-control"] ||= "no-cache"
|
176
|
+
|
177
|
+
view_renderer.render_body(view_context, options)
|
178
|
+
else
|
179
|
+
super
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|