Skip to content

Improved handling of invalid accept headers.#2226

Merged
ioquatix merged 4 commits intomainfrom
parse_http_accept_header-invalid-input
Jul 2, 2024
Merged

Improved handling of invalid accept headers.#2226
ioquatix merged 4 commits intomainfrom
parse_http_accept_header-invalid-input

Conversation

@ioquatix
Copy link
Copy Markdown
Member

@ioquatix ioquatix commented Jul 2, 2024

Possible fix for #2225.

A few thoughts:

  • How bad is split(/\s*,\s*/) on recent versions of Ruby? Could we return to using it in the future if the performance isn't bad?
  • filter_map is only available in Ruby 2.7+ - I know we decided to support old Rubies for as long as possible and I don't think we should change that right now, but I just wanted to call out that there is (probably) a slight loss of performance due to backwards compatibility.

@ioquatix ioquatix requested a review from jeremyevans July 2, 2024 13:35
@ioquatix ioquatix changed the title Add more test cases. Improved handling of invalid accept headers. Jul 2, 2024
Copy link
Copy Markdown
Contributor

@jeremyevans jeremyevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in recent Ruby versions, this new approach is much faster than the previous approach. If you can use strings instead of regexps, it's usually faster to use strings.

Comment thread lib/rack/request.rb Outdated
end
[attribute, quality]
end
end.compact
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use a temporary variable and map! and compact! to avoid unnecessary array allocations.

Comment thread lib/rack/request.rb Outdated
[attribute, quality]
end

parts.compact
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parts.compact!; parts to save another array allocation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this but intended to do that. Thanks for calling it out.

@ioquatix ioquatix force-pushed the parse_http_accept_header-invalid-input branch from 17a0b30 to 3e44b93 Compare July 2, 2024 15:23
@ioquatix ioquatix merged commit 7222c0a into main Jul 2, 2024
@ioquatix ioquatix deleted the parse_http_accept_header-invalid-input branch July 2, 2024 15:28
ioquatix added a commit that referenced this pull request Jul 2, 2024
@ioquatix ioquatix added this to the v3.1.6 milestone Jul 2, 2024
@ioquatix ioquatix self-assigned this Jul 2, 2024
@ioquatix
Copy link
Copy Markdown
Member Author

ioquatix commented Jul 2, 2024

Thanks @jeremyevans for your quick and excellent reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants