-
Notifications
You must be signed in to change notification settings - Fork 21.9k
add ability to set per param encoding #40457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,11 @@ def inherited(klass) # :nodoc: | |
end | ||
|
||
def setup_param_encode # :nodoc: | ||
@_parameter_encodings = {} | ||
@_parameter_encodings = Hash.new { |h, k| h[k] = {} } | ||
end | ||
|
||
def binary_params_for?(action) # :nodoc: | ||
@_parameter_encodings[action.to_s] | ||
def custom_encoding_for(action, param) # :nodoc: | ||
@_parameter_encodings[action.to_s][param.to_s] | ||
end | ||
|
||
# Specify that a given action's parameters should all be encoded as | ||
|
@@ -44,7 +44,35 @@ def binary_params_for?(action) # :nodoc: | |
# encoded as ASCII-8BIT. This is useful in the case where an application | ||
# must handle data but encoding of the data is unknown, like file system data. | ||
def skip_parameter_encoding(action) | ||
@_parameter_encodings[action.to_s] = true | ||
@_parameter_encodings[action.to_s] = Hash.new { Encoding::ASCII_8BIT } | ||
end | ||
|
||
# Specify the encoding for a a parameter on an action | ||
# ASCII-8BIT (it "skips" the encoding default of UTF-8). | ||
# | ||
# For example, a controller would use it like this: | ||
# | ||
# class RepositoryController < ActionController::Base | ||
# param_encoding :show, :file_path, Encoding::ASCII_8BIT | ||
# | ||
# def show | ||
# @repo = Repository.find_by_filesystem_path params[:file_path] | ||
# | ||
# # `repo_name` is guaranteed to be UTF-8, but was ASCII-8BIT, so | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is confusing. If it is |
||
# # tag it as such | ||
# @repo_name = params[:repo_name].force_encoding 'UTF-8' | ||
# end | ||
# | ||
# def index | ||
# @repositories = Repository.all | ||
# end | ||
# end | ||
# | ||
# The file_path parameter on the show action would be encoded as ASCII-8BIT. | ||
# This is useful in the case where an application must handle data | ||
# but encoding of the data is unknown, like file system data. | ||
def param_encoding(action, param, encoding) | ||
@_parameter_encodings[action.to_s][param.to_s] = encoding | ||
end | ||
end | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ def self.check_param_encoding(params) | |
end | ||
|
||
def self.set_binary_encoding(request, params, controller, action) | ||
BinaryParamEncoder.encode(request, params, controller, action) | ||
CustomParamEncoder.encode(request, params, controller, action) | ||
end | ||
|
||
class ParamEncoder # :nodoc: | ||
|
@@ -78,23 +78,25 @@ def self.handle_array(params) | |
end | ||
end | ||
|
||
class BinaryParamEncoder # :nodoc: | ||
class CustomParamEncoder # :nodoc: | ||
def self.encode(request, params, controller, action) | ||
return params unless controller && controller.valid_encoding? | ||
|
||
if binary_params_for?(request, controller, action) | ||
ActionDispatch::Request::Utils.each_param_value(params.except(:controller, :action)) do |param| | ||
param.force_encoding ::Encoding::ASCII_8BIT | ||
params.except(:controller, :action).each do |key, value| | ||
# next if key == :controller || key == :action | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this comment here? |
||
ActionDispatch::Request::Utils.each_param_value(value) do |param| | ||
if desired_encoding = custom_encoding_for(request, controller, action, key) | ||
param.force_encoding(desired_encoding) | ||
end | ||
end | ||
end | ||
|
||
params | ||
end | ||
|
||
def self.binary_params_for?(request, controller, action) | ||
request.controller_class_for(controller).binary_params_for?(action) | ||
def self.custom_encoding_for(request, controller, action, param) | ||
request.controller_class_for(controller).custom_encoding_for(action, param) | ||
rescue MissingController | ||
false | ||
nil | ||
end | ||
end | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also confusing. It seems it is not related with the previous sentence.