Skip to content

Commit

Permalink
Merge pull request #318 from httprb/improve/header-name-allocation
Browse files Browse the repository at this point in the history
Reduce amount of objects upon headers normalization
  • Loading branch information
ixti committed Mar 16, 2016
2 parents 438e86a + e5f1012 commit 1f04062
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Headers
include Enumerable

# Matches HTTP header names when in "Canonical-Http-Format"
CANONICAL_HEADER = /^[A-Z][a-z]*(-[A-Z][a-z]*)*$/
CANONICAL_NAME_RE = /^[A-Z][a-z]*(?:-[A-Z][a-z]*)*$/

# Matches valid header field name according to RFC.
# @see http://tools.ietf.org/html/rfc7230#section-3.2
HEADER_NAME_RE = /^[A-Za-z0-9!#\$%&'*+\-.^_`|~]+$/
COMPLIANT_NAME_RE = /^[A-Za-z0-9!#\$%&'*+\-.^_`|~]+$/

# Class constructor.
def initialize
Expand Down Expand Up @@ -191,10 +191,11 @@ def coerce(object)
# match {HEADER_NAME_RE}
# @return [String] canonical HTTP header name
def normalize_header(name)
normalized = name[CANONICAL_HEADER]
normalized ||= name.split(/[\-_]/).map(&:capitalize).join("-")
return name if name =~ CANONICAL_NAME_RE

return normalized if normalized =~ HEADER_NAME_RE
normalized = name.split(/[\-_]/).each(&:capitalize!).join("-")

return normalized if normalized =~ COMPLIANT_NAME_RE

fail InvalidHeaderNameError, "Invalid HTTP header field name: #{name.inspect}"
end
Expand Down

0 comments on commit 1f04062

Please sign in to comment.