Module: ResponseBank::BrotliSpliceSlot

Defined in:
lib/response_bank/brotli_splice_slot.rb

Defined Under Namespace

Classes: EncodedBody

Constant Summary collapse

INJECTOR_ENV_KEY =
'response_bank.html_metadata_injector'
METADATA_KEY =
'brotli_splice'
METADATA_VERSION =
1
CONTEXT_SUFFIX_LENGTH =

BrotliSplice reserves the last 2 bytes of every slot as a fixed "\r\n" context suffix, so a slot must be longer than that to hold any replaceable content -- BrotliSplice.encode raises unless the slot length exceeds it.

2

Class Method Summary collapse

Class Method Details

.encode_body(env, body, headers, compression_level:) ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
# File 'lib/response_bank/brotli_splice_slot.rb', line 16

def encode_body(env, body, headers, compression_level:)
  injector = env[INJECTOR_ENV_KEY]
  return unless injector
  return unless body && body != ''

  prepared = injector.prepare_response_bank_brotli_splice(body, headers)
  return unless prepared

  prepared_body = hash_fetch(prepared, :body)
  slots = hash_fetch(prepared, :slots)
  return unless prepared_body && slots && slots.length == 1

  slot = slots.first
  slot_name = hash_fetch(slot, :name).to_s
  html_offset = integer_value(hash_fetch(slot, :offset))
  html_length = integer_value(hash_fetch(slot, :length))
  return unless valid_html_slot?(prepared_body, html_offset, html_length)

  # Load the native gem only once we have real splice work to do. Keeping this
  # outside the begin/rescue below is deliberate: the rescue clause references
  # BrotliSplice::Error, so if the gem is missing, evaluating that clause would
  # raise NameError and mask the LoadError we want the caller to see.
  ensure_brotli_splice_loaded!

  begin
    result = BrotliSplice.encode(prepared_body, html_offset, html_length, quality: compression_level)

    EncodedBody.new(
      body: prepared_body,
      compressed_body: result[:data],
      metadata: (
        name: slot_name,
        compressed_offset: result[:secret_offset],
        replacement_length: result[:secret_length],
        html_placeholder_offset: html_offset,
        html_placeholder_length: html_length,
        context_suffix: result[:context_suffix],
      ),
    )
  rescue BrotliSplice::Error, ArgumentError => error
    ResponseBank.log("BrotliSplice encode skipped: #{error.class}")
    nil
  end
end

.metadata_for(name:, compressed_offset:, replacement_length:, html_placeholder_offset:, html_placeholder_length:, context_suffix:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/response_bank/brotli_splice_slot.rb', line 61

def (
  name:,
  compressed_offset:,
  replacement_length:,
  html_placeholder_offset:,
  html_placeholder_length:,
  context_suffix:
)
  {
    METADATA_KEY => {
      'version' => METADATA_VERSION,
      'slots' => [{
        'name' => name.to_s,
        'compressed_offset' => compressed_offset,
        'replacement_length' => replacement_length,
        'html_placeholder_offset' => html_placeholder_offset,
        'html_placeholder_length' => html_placeholder_length,
        'context_suffix' => context_suffix,
      }],
    },
  }
end

.metadata_slots(metadata) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/response_bank/brotli_splice_slot.rb', line 133

def ()
  return unless 

  brotli_splice = [METADATA_KEY]
  return unless brotli_splice && brotli_splice['version'] == METADATA_VERSION

  brotli_splice['slots']
end

.replace_compressed_secret(env, body, metadata) ⇒ Object



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
120
# File 'lib/response_bank/brotli_splice_slot.rb', line 84

def replace_compressed_secret(env, body, )
  injector = env[INJECTOR_ENV_KEY]
  slots = ()
  return body unless injector && slots && body && body != ''

  # See encode_body: load outside the begin/rescue so a missing gem surfaces as
  # LoadError rather than a masked NameError from the BrotliSplice::Error clause.
  ensure_brotli_splice_loaded!

  begin
    slots.reduce(body) do |current_body, slot|
      replacement = replacement_for_slot(injector, slot)

      unless valid_replacement?(replacement, slot)
        got = replacement ? replacement.bytesize : 'nil'
        ResponseBank.log(
          'BrotliSplice replace skipped: replacement length mismatch ' \
          "(got #{got}, want #{slot['replacement_length']})",
        )
        next current_body
      end

      offset = integer_value(slot['compressed_offset'])
      length = integer_value(slot['replacement_length'])

      unless valid_compressed_slot?(current_body, offset, length)
        ResponseBank.log('BrotliSplice replace skipped: compressed slot out of bounds')
        next current_body
      end

      BrotliSplice.replace(current_body, replacement, offset, length)
    end
  rescue BrotliSplice::Error, ArgumentError => error
    ResponseBank.log("BrotliSplice replace skipped: #{error.class}")
    body
  end
end

.replace_plain_body(env, body, metadata) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/response_bank/brotli_splice_slot.rb', line 122

def replace_plain_body(env, body, )
  injector = env[INJECTOR_ENV_KEY]
  slots = ()
  return body unless injector && slots && body && body != ''

  injector.replace_response_bank_brotli_splice_placeholders(body, slots)
rescue ArgumentError => error
  ResponseBank.log("BrotliSplice plain replacement skipped: #{error.class}")
  body
end