aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal/streaming.rb
blob: b9bd49f670979602b3118a30d9d523a3b257a7e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'active_support/core_ext/file/path'
require 'rack/chunked'

module ActionController #:nodoc:
  # Methods for sending streaming templates back to the client.
  module Streaming
    extend ActiveSupport::Concern

    include AbstractController::Rendering
    attr_internal :stream

    module ClassMethods
      # Render streaming templates. It accepts :only, :except, :if and :unless as options
      # to specify when to stream, as in ActionController filters.
      def stream(options={})
        if defined?(Fiber)
          before_filter :_stream_filter, options
        else
          raise "You cannot use streaming if Fiber is not available."
        end
      end
    end

    protected

    # Mark following render calls as streaming.
    def _stream_filter #:nodoc:
      self.stream = true
    end

    # Consider the stream option when normalazing options.
    def _normalize_options(options) #:nodoc:
      super
      options[:stream] = self.stream unless options.key?(:stream)
    end

    # Set proper cache control and transfer encoding when streaming
    def _process_options(options) #:nodoc:
      super
      if options[:stream]
        if env["HTTP_VERSION"] == "HTTP/1.0"
          options.delete(:stream)
        else
          headers["Cache-Control"] ||= "no-cache"
          headers["Transfer-Encoding"] = "chunked"
          headers.delete("Content-Length")
        end
      end
    end

    # Call render_to_body if we are streaming instead of usual +render+.
    def _render_template(options) #:nodoc:
      if options.delete(:stream)
        Rack::Chunked::Body.new view_context.render_body(options)
      else
        super
      end
    end
  end
end