module Roda::RodaPlugins::Caching::RequestMethods

  1. lib/roda/plugins/caching.rb

Methods

Public Instance

  1. etag
  2. last_modified

Public Instance methods

etag(value, opts=(no_positional_opts = true; OPTS), weak: opts[:weak], new_resource: opts.fetch(:new_resource){post?}, **kw)

Set the response entity tag using the ETag header.

The value argument is an identifier that uniquely identifies the current version of the resource. Keyword Arguments:

weak

Use a weak cache validator (a strong cache validator is the default)

new_resource

Whether this etag should match an etag of * (true for POST, false otherwise)

When the current request includes an If-None-Match header with a matching etag, immediately returns a response with a 304 or 412 status, depending on the request method.

When the current request includes an If-Match header with an etag that doesn’t match, or where the weak argument was used, immediately returns a response with a 412 status.

[show source]
    # File lib/roda/plugins/caching.rb
118 def etag(value, opts=(no_positional_opts = true; OPTS), weak: opts[:weak], new_resource: opts.fetch(:new_resource){post?}, **kw)
119   RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
120   
121   res = response
122   e = env
123   res[RodaResponseHeaders::ETAG] = etag = "#{'W/' if weak}\"#{value}\""
124   status = res.status
125 
126   if (!status || (status >= 200 && status < 300) || status == 304)
127     if etag_matches?(e['HTTP_IF_NONE_MATCH'], etag, new_resource, weak: true)
128       res.status = (request_method =~ /\AGET|HEAD|OPTIONS|TRACE\z/i ? 304 : 412)
129       halt
130     end
131 
132     if ifm = e['HTTP_IF_MATCH']
133       if weak || !etag_matches?(ifm, etag, new_resource)
134         res.status = 412
135         halt
136       end
137     end
138   end
139 end
last_modified(time)

Set the last modified time of the resource using the Last-Modified header. The time argument should be a Time instance.

If the current request includes an If-Unmodified-Since header that is before than the time specified, immediately returns a response with a 412 status.

If the current request does not include an If-None-Match header and includes an If-Modified-Since header that is equal or later than the time specified, immediately returns a response with a 304 status.

[show source]
    # File lib/roda/plugins/caching.rb
 85 def last_modified(time)
 86   return unless time
 87   res = response
 88   e = env
 89   res[RodaResponseHeaders::LAST_MODIFIED] = time.httpdate
 90   status = res.status
 91 
 92   if (!status || (status >= 200 && status < 300) || status == 412) && (ius = time_from_header(e['HTTP_IF_UNMODIFIED_SINCE'])) && ius < time.to_i
 93     res.status = 412
 94     halt
 95   end
 96 
 97   if !e['HTTP_IF_NONE_MATCH'] && (!status || status == 200) && (ims = time_from_header(e['HTTP_IF_MODIFIED_SINCE'])) && ims >= time.to_i
 98     res.status = 304
 99     halt
100   end
101 end