Skip to content
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

Reduce amount of objects upon headers normalization #318

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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