File: processing_skip_analyzer.rb

package info (click to toggle)
ruby-rack-livereload 0.3.17%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 216 kB
  • sloc: javascript: 973; ruby: 470; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download
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
require 'rack/livereload'

module Rack
  class LiveReload
    class ProcessingSkipAnalyzer
      BAD_USER_AGENTS = [ %r{MSIE} ]

      def self.skip_processing?(result, env, options)
        new(result, env, options).skip_processing?
      end

      def initialize(result, env, options)
        @env, @options = env, options

        @status, @headers, @body = result
      end

      def skip_processing?
        !html? || chunked? || inline? || ignored? || bad_browser? || !get?
      end

      def chunked?
        @headers['Transfer-Encoding'] == 'chunked'
      end

      def inline?
        @headers['Content-Disposition'] =~ %r{^inline}
      end

      def ignored?
        path = @env['QUERY_STRING'].empty? ? @env['PATH_INFO'] : "#{@env['PATH_INFO']}?#{@env['QUERY_STRING']}"
        @options[:ignore] and @options[:ignore].any? { |filter| path[filter] }
      end

      def bad_browser?
        BAD_USER_AGENTS.any? { |pattern| @env['HTTP_USER_AGENT'] =~ pattern }
      end

      def html?
        @headers['Content-Type'] =~ %r{text/html}
      end

      def get?
        @env['REQUEST_METHOD'] == 'GET'
      end
    end
  end
end