Class: ResponseBank::Middleware
- Inherits:
-
Object
- Object
- ResponseBank::Middleware
- Defined in:
- lib/response_bank/middleware.rb
Constant Summary collapse
- CACHEABLE_HEADERS =
Limit the cached headers TODO: Make this lowercase/case-insentitive as per rfc2616 ยง4.2
["Location", "Content-Type", "ETag", "Content-Encoding", "Last-Modified", "Cache-Control", "Expires", "Link", "Surrogate-Keys", "Cache-Tags", "Speculation-Rules"].freeze
- 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.
14 15 16 17 |
# File 'lib/response_bank/middleware.rb', line 14 def initialize(app, brotli_splice_injector = nil) @app = app @brotli_splice_injector = brotli_splice_injector end |
Instance Method Details
#call(env) ⇒ Object
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 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/response_bank/middleware.rb', line 19 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 = @app.call(env) if env['cacheable.cache'] if [200, 404, 301, 304].include?(status) headers['ETag'] = %{"#{env['cacheable.key']}"} end if [200, 404, 301].include?(status) && env['cacheable.miss'] # Flatten down the result so that it can be stored to memcached. if body.is_a?(String) body_string = body else body_string = +"" body.each { |part| body_string << part } end body_compressed = nil = nil if body_string && body_string != "" headers['Content-Encoding'] = content_encoding env["cacheable.compression_level"] = ResponseBank.compression_level_for_request(env, headers) time = ResponseBank.measure do encoded_body = if content_encoding == 'br' ResponseBank::BrotliSpliceSlot.encode_body( env, body_string, headers, compression_level: env["cacheable.compression_level"], ) end if encoded_body body_string = encoded_body.body body_compressed = encoded_body.compressed_body = encoded_body. else body_compressed = ResponseBank.compress( body_string, content_encoding, compression_level: env["cacheable.compression_level"], ) end end ResponseBank.log("Compression time: #{time}ms") env["cacheable.compression_time"] = time end cached_headers = headers.slice(*CACHEABLE_HEADERS) # Store result cache_data = [status, cached_headers, body_compressed, , env["cacheable.compression_level"]] cache_data << if ResponseBank.write_to_cache(env['cacheable.key']) do payload = MessagePack.dump(cache_data) ResponseBank.write_to_backing_cache_store( env, env['cacheable.unversioned-key'], payload, expires_in: env['cacheable.versioned-cache-expiry'], ) end # since we had to generate the compressed version already we may # as well serve it if the client wants it if body_compressed if env['HTTP_ACCEPT_ENCODING'].to_s.include?(content_encoding) if content_encoding == 'br' body = [ResponseBank::BrotliSpliceSlot.replace_compressed_secret(env, body_compressed, )] else body = [body_compressed] end else # Remove content-encoding header for response with compressed content headers.delete('Content-Encoding') body = [ResponseBank::BrotliSpliceSlot.replace_plain_body(env, body_string, )] if 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 |