Skip to content

Commit

Permalink
Per-class cache keys for cached query/body parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 14, 2023
1 parent 337c70d commit d1424e8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/rack/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,27 @@ def parseable_data?
end

private def cache_for(key, validity_key, current_value)
if get_header(validity_key).equal?(current_value)
if has_header?(key)
value = get_header(key)
if value.is_a?(Exception)
raise value.class, value.message, cause: value.cause
else
return value
end
end
end

value = yield(current_value)

set_header(validity_key, current_value)
set_header(key, value)
rescue => error
set_header(key, error)
raise
end

private def class_cache_for(key, validity_key, current_value)
if get_header(validity_key).equal?(current_value)
if cache = get_header(key)
if cache.key?(self.class)
Expand Down Expand Up @@ -513,7 +534,7 @@ def parseable_data?

# Returns the data received in the query string.
def GET
cache_for(RACK_REQUEST_QUERY_HASH, RACK_REQUEST_QUERY_STRING, query_string) do
class_cache_for(RACK_REQUEST_QUERY_HASH, RACK_REQUEST_QUERY_STRING, query_string) do
expand_params(query_param_list)
end
end
Expand All @@ -530,7 +551,7 @@ def query_param_list
# This method support both application/x-www-form-urlencoded and
# multipart/form-data.
def POST
cache_for(RACK_REQUEST_FORM_HASH, RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)) do
class_cache_for(RACK_REQUEST_FORM_HASH, RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)) do
expand_params(body_param_list)
end
end
Expand All @@ -547,6 +568,9 @@ def body_param_list
# form_vars.sub!(/\0\z/, '') # performance replacement:
form_vars.slice!(-1) if form_vars.end_with?("\0")

# Removing this line breaks Rail test "test_filters_rack_request_form_vars"!
set_header(RACK_REQUEST_FORM_VARS, form_vars)

form_pairs = split_query(form_vars, '&')
end
else
Expand Down

0 comments on commit d1424e8

Please sign in to comment.