Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add accept_language method to request to extract HTTP_ACCEPT_LANGUAGE #623

Merged
merged 2 commits into from

2 participants

@craic

HTTP_ACCEPT_LANGUAGE header (RFC 2616) allows browser to pass the user's language preferences to the server. This behaves effectively the same as HTTP_ACCEPT_ENCODING. The added accept_language method is based on accept_encoding. Likewise, the spec for the method is very similar to that for accept_encoding.

Moved duplicate code into utility method parse_http_accept_header as per raggi request - passes tests

@raggi
Owner

I'm not too concerned with the duplication here, but if you wouldn't mind extracting a utils method and removing the duplication, that would be appreciated.

@craic

OK - done that - updated the pull request

@raggi raggi merged commit b7e0bde into rack:master

1 check failed

Details default The Travis CI build failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 25, 2013
  1. @craic

    Add accept_language method to request to extract HTTP_ACCEPT_LANGUAGE…

    craic authored
    … header as per RFC 2616 + spec to test it
Commits on Dec 5, 2013
  1. @craic
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 9 deletions.
  1. +17 −9 lib/rack/request.rb
  2. +17 −0 test/spec_request.rb
View
26 lib/rack/request.rb
@@ -142,7 +142,7 @@ def put?; request_method == "PUT" end
# Checks the HTTP request method (or verb) to see if it was of type TRACE
def trace?; request_method == "TRACE" end
-
+
# Checks the HTTP request method (or verb) to see if it was of type UNLINK
def unlink?; request_method == "UNLINK" end
@@ -337,14 +337,11 @@ def fullpath
end
def accept_encoding
- @env["HTTP_ACCEPT_ENCODING"].to_s.split(/\s*,\s*/).map do |part|
- encoding, parameters = part.split(/\s*;\s*/, 2)
- quality = 1.0
- if parameters and /\Aq=([\d.]+)/ =~ parameters
- quality = $1.to_f
- end
- [encoding, quality]
- end
+ parse_http_accept_header(@env["HTTP_ACCEPT_ENCODING"])
+ end
+
+ def accept_language
+ parse_http_accept_header(@env["HTTP_ACCEPT_LANGUAGE"])
end
def trusted_proxy?(ip)
@@ -384,5 +381,16 @@ def parse_query(qs)
def parse_multipart(env)
Rack::Multipart.parse_multipart(env)
end
+
+ def parse_http_accept_header(header)
+ header.to_s.split(/\s*,\s*/).map do |part|
+ attribute, parameters = part.split(/\s*;\s*/, 2)
+ quality = 1.0
+ if parameters and /\Aq=([\d.]+)/ =~ parameters
+ quality = $1.to_f
+ end
+ [attribute, quality]
+ end
+ end
end
end
View
17 test/spec_request.rb
@@ -938,6 +938,23 @@
parser.call("gzip ; deflate").should.equal([["gzip", 1.0]])
end
+ should "parse Accept-Language correctly" do
+ parser = lambda do |x|
+ Rack::Request.new(Rack::MockRequest.env_for("", "HTTP_ACCEPT_LANGUAGE" => x)).accept_language
+ end
+
+ parser.call(nil).should.equal([])
+
+ parser.call("fr, en").should.equal([["fr", 1.0], ["en", 1.0]])
+ parser.call("").should.equal([])
+ parser.call("*").should.equal([["*", 1.0]])
+ parser.call("fr;q=0.5, en;q=1.0").should.equal([["fr", 0.5], ["en", 1.0]])
+ parser.call("fr;q=1.0, en; q=0.5, *;q=0").should.equal([["fr", 1.0], ["en", 0.5], ["*", 0] ])
+
+ parser.call("fr ; q=0.9").should.equal([["fr", 0.9]])
+ parser.call("fr").should.equal([["fr", 1.0]])
+ end
+
ip_app = lambda { |env|
request = Rack::Request.new(env)
response = Rack::Response.new
Something went wrong with that request. Please try again.