Skip to content
This repository
Browse code

Merge pull request #43 from trustner/master

Fixes an issue when parsing headers with a wildcard in the language-range.
  • Loading branch information...
commit f2ba4e2ea1bfbaef279a399fc12b16997f8498f6 2 parents 2bd07da + d30bc0a
Douwe Maan DouweM authored
6 lib/http_accept_language/parser.rb
@@ -18,15 +18,17 @@ def user_preferred_languages
18 18 @user_preferred_languages ||= begin
19 19 header.to_s.gsub(/\s+/, '').split(',').map do |language|
20 20 locale, quality = language.split(';q=')
21   - raise ArgumentError, 'Not correctly formatted' unless locale =~ /^[a-z\-0-9]+$/i
  21 + raise ArgumentError, 'Not correctly formatted' unless locale =~ /^[a-z\-0-9]+|\*$/i
22 22
23 23 locale = locale.downcase.gsub(/-[a-z0-9]+$/i, &:upcase) # Uppercase territory
  24 + locale = nil if locale == '*' # Ignore wildcards
  25 +
24 26 quality = quality ? quality.to_f : 1.0
25 27
26 28 [locale, quality]
27 29 end.sort do |(_, left), (_, right)|
28 30 right <=> left
29   - end.map(&:first)
  31 + end.map(&:first).compact
30 32 rescue ArgumentError # Just rescue anything if the browser messed up badly.
31 33 []
32 34 end
5 spec/parser_spec.rb
@@ -44,6 +44,11 @@ def parser
44 44 parser.compatible_language_from([:"en-HK"]).should eq :"en-HK"
45 45 end
46 46
  47 + it "should accept and ignore wildcards" do
  48 + parser.header = 'en-US,en,*'
  49 + parser.compatible_language_from([:"en-US"]).should eq :"en-US"
  50 + end
  51 +
47 52 it "should sanitize available language names" do
48 53 parser.sanitize_available_locales(%w{en_UK-x3 en-US-x1 ja_JP-x2 pt-BR-x5 es-419-x4}).should eq ["en-UK", "en-US", "ja-JP", "pt-BR", "es-419"]
49 54 end

0 comments on commit f2ba4e2

Please sign in to comment.
Something went wrong with that request. Please try again.