Class: SeoCache::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/seo_cache/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
12
13
14
15
16
# File 'lib/seo_cache/middleware.rb', line 8

def initialize(app, options = {})
  @options              = options
  @extensions_to_ignore = SeoCache.extensions_to_ignore
  @crawler_user_agents  = SeoCache.crawler_user_agents

  @app = app

  @page_caching = PageCaching.new
end

Instance Method Details

#after_render(env, response, status = 200) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/seo_cache/middleware.rb', line 204

def after_render(env, response, status = 200)
  return unless response && SeoCache.cache_only_status.include?(status.to_i)

  env_request = Rack::Request.new(env)

  if SeoCache.log_missed_cache
    SeoCache.log("missed cache : #{env_request.path} (User agent: #{env_request.user_agent})")
  end

  @page_caching.cache(response, env_request.path, get_locale(env_request.params, env_request.host))
end

#before_render(env) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/seo_cache/middleware.rb', line 174

def before_render(env)
  # return nil unless @options[:before_render]
  # cached_render = @options[:before_render].call(env)

  env_request = Rack::Request.new(env)

  cached_render = @page_caching.get(env_request.path, get_locale(env_request.params, env_request.host))

  return nil unless cached_render

  if cached_render.is_a?(String)
    Rack::Response.new(cached_render, 200, 'Content-Type' => 'text/html; charset=utf-8')
  elsif cached_render.is_a?(Rack::Response)
    cached_render
  end
end

#build_api_url(env) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/seo_cache/middleware.rb', line 142

def build_api_url(env)
  new_env = env
  if env['CF-VISITOR']
    match = /"scheme":"(http|https)"/.match(env['CF-VISITOR'])
    (new_env['HTTPS'] = true) && (new_env['rack.url_scheme'] = 'https') && (new_env['SERVER_PORT'] = 443) if match && match[1] == 'https'
    (new_env['HTTPS'] = false) && (new_env['rack.url_scheme'] = 'http') && (new_env['SERVER_PORT'] = 80) if match && match[1] == 'http'
  end

  if env['X-FORWARDED-PROTO']
    (new_env['HTTPS'] = true) && (new_env['rack.url_scheme'] = 'https') && (new_env['SERVER_PORT'] = 443) if env['X-FORWARDED-PROTO'].split(',')[0] == 'https'
    (new_env['HTTPS'] = false) && (new_env['rack.url_scheme'] = 'http') && (new_env['SERVER_PORT'] = 80) if env['X-FORWARDED-PROTO'].split(',')[0] == 'http'
  end

  if SeoCache.protocol.present?
    (new_env['HTTPS'] = true) && (new_env['rack.url_scheme'] = 'https') && (new_env['SERVER_PORT'] = 443) if @options[:protocol] == 'https'
    (new_env['HTTPS'] = false) && (new_env['rack.url_scheme'] = 'http') && (new_env['SERVER_PORT'] = 80) if @options[:protocol] == 'http'
  end

  url           = Rack::Request.new(new_env).url
  prerender_url = SeoCache.prerender_service_url
  forward_slash = prerender_url[-1, 1] == '/' ? '' : '/'
  "#{prerender_url}#{forward_slash}#{url}"
end

#build_response_from_prerender(prerender_response) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/seo_cache/middleware.rb', line 166

def build_response_from_prerender(prerender_response)
  response = Rack::Response.new(prerender_response.body, prerender_response.code, prerender_response.header)

  # @options[:build_rack_response_from_prerender]&.call(response, prerendered_response)

  return response
end

#call(env) ⇒ Object



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
# File 'lib/seo_cache/middleware.rb', line 18

def call(env)
  @status, @headers, @response = @app.call(env)

  if prerender_page?(env, @status)
    cached_response = before_render(env)

    return cached_response.finish if cached_response.present?

    if SeoCache.prerender_service_url.present?
      prerender_response = prerender_service(env)
      if prerender_response
        response = build_response_from_prerender(prerender_response.body)
        after_render(env, prerender_response)

        return response.finish
      end
    else
      Thread.new do
        prerender_data = page_render(env)

        after_render(env, prerender_data, @status)
      end
    end
  elsif prerender_params?(env)
    env['seo_mode']           = true
    status, headers, response = @app.call(env)
    begin
      return [status, headers, [response.body]]
    rescue
      return [status, headers, [nil]]
    end
  end

  return [@status, @headers, @response]
end

#get_locale(params, host) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/seo_cache/middleware.rb', line 216

def get_locale(params, host)
  if SeoCache.locale_as_first_directory
    SeoCache.locale_method ? SeoCache.locale_method.call(params, host) : I18n.locale
  else
    nil
  end
end

#page_render(env) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/seo_cache/middleware.rb', line 191

def page_render(env)
  # Add key parameter to url
  request = Rack::Request.new(env)
  url     = if request.query_string.present? || request.url.include?('?')
              request.url + '&'
            else
              request.url + '?'
            end
  url     += "#{SeoCache.prerender_url_param}=true"

  return PageRender.new.get(url)
end

#prerender_page?(env, status) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/seo_cache/middleware.rb', line 65

def prerender_page?(env, status)
  user_agent                   = env['HTTP_USER_AGENT']
  buffer_agent                 = env['HTTP_X_BUFFERBOT']
  is_requesting_prerender_page = false

  return false unless user_agent

  return false if status.to_i > 299

  return false if env['REQUEST_METHOD'] != 'GET'

  request      = Rack::Request.new(env)
  query_params = Rack::Utils.parse_query(request.query_string)

  # Check if user connected
  return false if !SeoCache.cache_with_user_connected && (env['warden'].respond_to?(:user) && env['warden'].user.present?)

  # If it is the generated page...don't prerender
  return false if query_params.has_key?(SeoCache.prerender_url_param)

  # if it is a bot and host doesn't contain these domains...don't prerender
  return false if SeoCache.whitelist_hosts.present? && SeoCache.whitelist_hosts.none? { |host| request.host.include?(host) }

  # if it is a bot and urls contain these params...don't prerender
  return false if SeoCache.blacklist_params.present? && SeoCache.blacklist_params.any? { |param| query_params.has_key?(param) }

  # if it is a bot and is requesting a resource...don't prerender
  return false if @extensions_to_ignore.any? { |extension| request.fullpath.include?(extension) }

  # if it is a bot and not requesting a resource and is not whitelisted...don't prerender
  return false if SeoCache.whitelist_urls.present? && SeoCache.whitelist_urls.all? { |whitelisted| !Regexp.new(whitelisted).match(request.fullpath) }

  # if it is a bot and not requesting a resource and is blacklisted(url or referer)...don't prerender
  blacklisted_url = SeoCache.blacklist_urls.present? && SeoCache.blacklist_urls.any? do |blacklisted|
    regex = Regexp.new(blacklisted)

    blacklisted_url     = regex.match(request.fullpath)
    blacklisted_referer = request.referer ? regex.match(request.referer) : false

    blacklisted_url || blacklisted_referer
  end
  return false if blacklisted_url

  is_requesting_prerender_page = true if Rack::Utils.parse_query(request.query_string).has_key?('_escaped_fragment_') || Rack::Utils.parse_query(request.query_string).has_key?(SeoCache.force_cache_url_param)

  # if it is a bot...show prerendered page
  is_requesting_prerender_page = true if @crawler_user_agents.any? { |crawler_user_agent| user_agent.downcase.include?(crawler_user_agent.downcase) }

  # if it is BufferBot...show prerendered page
  is_requesting_prerender_page = true if buffer_agent

  SeoCache.log('force cache : ' + request.path) if Rack::Utils.parse_query(request.query_string).has_key?(SeoCache.force_cache_url_param) && SeoCache.log_missed_cache

  return is_requesting_prerender_page
end

#prerender_params?(env) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
# File 'lib/seo_cache/middleware.rb', line 54

def prerender_params?(env)
  return false if env['REQUEST_METHOD'] != 'GET'

  request      = Rack::Request.new(env)
  query_params = Rack::Utils.parse_query(request.query_string)

  return false if @extensions_to_ignore.any? { |extension| request.fullpath.include? extension }

  return true if query_params.has_key?(SeoCache.prerender_url_param) || query_params.has_key?(SeoCache.force_cache_url_param)
end

#prerender_service(env) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/seo_cache/middleware.rb', line 121

def prerender_service(env)
  url          = URI.parse(build_api_url(env))
  headers      = {
    'User-Agent'      => env['HTTP_USER_AGENT'],
    'Accept-Encoding' => 'gzip'
  }
  req          = Net::HTTP::Get.new(url.request_uri, headers)
  http         = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true if url.scheme == 'https'
  response     = http.request(req)
  if response['Content-Encoding'] == 'gzip'
    response.body              = ActiveSupport::Gzip.decompress(response.body)
    response['Content-Length'] = response.body.length
    response.delete('Content-Encoding')
  end

  return response
rescue StandardError => error
  SeoCache.log_error(error.message)
end