Skip to content

Commit

Permalink
Avoid 2nd degree polynomial regexp in MediaType
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot authored and tenderlove committed Feb 21, 2024
1 parent 6245768 commit d9c163a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/rack/media_type.rb
Expand Up @@ -4,7 +4,7 @@ module Rack
# Rack::MediaType parse media type and parameters out of content_type string

class MediaType
SPLIT_PATTERN = %r{\s*[;,]\s*}
SPLIT_PATTERN = /[;,]/

class << self
# The media type (type/subtype) portion of the CONTENT_TYPE header
Expand All @@ -15,7 +15,11 @@ class << self
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
def type(content_type)
return nil unless content_type
content_type.split(SPLIT_PATTERN, 2).first.tap &:downcase!
if type = content_type.split(SPLIT_PATTERN, 2).first
type.rstrip!
type.downcase!
type
end
end

# The media type parameters provided in CONTENT_TYPE as a Hash, or
Expand All @@ -27,9 +31,10 @@ def params(content_type)
return {} if content_type.nil?

content_type.split(SPLIT_PATTERN)[1..-1].each_with_object({}) do |s, hsh|
s.strip!
k, v = s.split('=', 2)

hsh[k.tap(&:downcase!)] = strip_doublequotes(v)
k.downcase!
hsh[k] = strip_doublequotes(v)
end
end

Expand Down

0 comments on commit d9c163a

Please sign in to comment.