Skip to content

Commit

Permalink
Minor performance improvements in header_matchers plugin
Browse files Browse the repository at this point in the history
Avoid string allocation in :header matcher using tr!.

Use Regexp#match instead of String#=~ in :user_agent matcher,
and use match.captures in instead of $~[1..-1], which should
save an array allocation (even if not, it's more understandable).
  • Loading branch information
jeremyevans committed Dec 14, 2024
1 parent 311cfad commit fe7c699
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/roda/plugins/header_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def match_accept(mimetype)

# Match if the given uppercase key is present inside the environment.
def match_header(key)
key = key.upcase.tr("-","_")
key = key.upcase
key.tr!("-","_")
unless key == "CONTENT_TYPE" || key == "CONTENT_LENGTH"
key = "HTTP_#{key}"
end
Expand All @@ -75,8 +76,8 @@ def match_host(hostname)
# Match the submitted user agent to the given pattern, capturing any
# regexp match groups.
def match_user_agent(pattern)
if (user_agent = @env["HTTP_USER_AGENT"]) && user_agent.to_s =~ pattern
@captures.concat($~[1..-1])
if (user_agent = @env["HTTP_USER_AGENT"]) && (match = pattern.match(user_agent))
@captures.concat(match.captures)
end
end
end
Expand Down

0 comments on commit fe7c699

Please sign in to comment.