Class: ResponseBank::Middleware
- Inherits:
-
Object
- Object
- ResponseBank::Middleware
- Defined in:
- lib/response_bank/middleware.rb
Constant Summary collapse
- CACHEABLE_HEADERS =
ResponseBank::CACHEABLE_HEADERS
- CACHEABLE_STATUSES =
ResponseBank::CACHEABLE_STATUSES
- REQUESTED_WITH =
"HTTP_X_REQUESTED_WITH"- ACCEPT =
"HTTP_ACCEPT"- USER_AGENT =
"HTTP_USER_AGENT"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, brotli_splice_injector = nil) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, brotli_splice_injector = nil) ⇒ Middleware
Returns a new instance of Middleware.
16 17 18 19 |
# File 'lib/response_bank/middleware.rb', line 16 def initialize(app, brotli_splice_injector = nil) @app = app @brotli_splice_injector = brotli_splice_injector end |
Instance Method Details
#call(env) ⇒ Object
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/response_bank/middleware.rb', line 21 def call(env) env['cacheable.cache'] = false install_brotli_splice_injector(env) content_encoding = env['response_bank.server_cache_encoding'] = ResponseBank.check_encoding(env) status, headers, body = begin @app.call(env) rescue StandardError ResponseBank::DeferredStore.from_env(env)&.abort raise end deferred_store = ResponseBank::DeferredStore.from_env(env) ResponseBank::DeferredStore.arm_from_middleware(env, status: status, headers: headers) if env['cacheable.cache'] if [200, 404, 301, 304].include?(status) && !deferred_store headers['ETag'] = %{"#{env['cacheable.key']}"} end if CACHEABLE_STATUSES.include?(status) && env['cacheable.miss'] && !deferred_store body_string = CacheWriter.flatten(body) stored = nil begin stored = CacheWriter.store( env, status: status, headers: headers, body: body_string, timestamp: -> { }, content_encoding: content_encoding, ) if stored.compressed_body if env['HTTP_ACCEPT_ENCODING'].to_s.include?(content_encoding) headers['Content-Encoding'] = content_encoding if content_encoding == 'br' body = [ResponseBank::BrotliSpliceSlot.replace_compressed_secret(env, stored.compressed_body, stored.)] else body = [stored.compressed_body] end else headers.delete('Content-Encoding') body = [ResponseBank::BrotliSpliceSlot.replace_plain_body(env, stored.body, stored.)] if stored. end end rescue => exception headers.delete('Content-Encoding') if stored&.compressed_body ResponseBank.log("Failed to write to cache: #{exception.class} - #{exception.}") if env['response_bank.on_exception'] begin env['response_bank.on_exception'].call(exception) rescue => handler_exception ResponseBank.log("Exception handler failed: #{handler_exception.class} - #{handler_exception.}") end end end end # Add X-Cache header miss = env['cacheable.miss'] x_cache = miss ? 'miss' : 'hit' x_cache += ", #{env['cacheable.store']}" unless miss headers['X-Cache'] = x_cache end [status, headers, body] end |