http-accept 1.7.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +19 -9
- data/.rspec +2 -3
- data/.travis.yml +20 -4
- data/Gemfile +0 -5
- data/README.md +5 -5
- data/Rakefile +1 -3
- data/http-accept.gemspec +4 -5
- data/lib/http/accept.rb +2 -2
- data/lib/http/accept/charsets.rb +3 -3
- data/lib/http/accept/content_type.rb +4 -2
- data/lib/http/accept/encodings.rb +3 -3
- data/lib/http/accept/languages.rb +2 -0
- data/lib/http/accept/media_types.rb +18 -11
- data/lib/http/accept/media_types/map.rb +2 -0
- data/lib/http/accept/parse_error.rb +2 -0
- data/lib/http/accept/quoted_string.rb +2 -0
- data/lib/http/accept/sort.rb +2 -0
- data/lib/http/accept/version.rb +3 -1
- metadata +21 -9
- data/.simplecov +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1b4a14e8a82f11fa25b5da2a13a3cf6ddd5e5ced9c02ba37950ff340390b3dc5
|
4
|
+
data.tar.gz: 57faa362d425e4ce3a0ee437874c235f2b16c98100adda81a315dadfc0740212
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4cf41a3713b243bc9d4a0cfba2e459709191072d60ba23f864b852ab9d5c2cd5e5a8fc5d80783c2d798e03270f98e8913e866adcaed84f98ff489dcbf3a3567
|
7
|
+
data.tar.gz: eda8944a035fbda557a09e6f394fcd6691664ba8a7fd4acf91b8c94b67874c9b2afab3c5935003b0000799c27d1b031afd85138b7af432cd678b56fdf8347731
|
data/.gitignore
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
.tags*
|
19
|
+
.rspec_status
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
language: ruby
|
2
|
-
sudo:
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
sudo: required
|
3
|
+
dist: xenial
|
4
|
+
cache: bundler
|
5
|
+
|
6
|
+
matrix:
|
7
|
+
include:
|
8
|
+
- rvm: 2.3
|
9
|
+
- rvm: 2.4
|
10
|
+
- rvm: 2.5
|
11
|
+
- rvm: 2.6
|
12
|
+
- rvm: 2.6
|
13
|
+
env: COVERAGE=PartialSummary,Coveralls
|
14
|
+
- rvm: truffleruby
|
15
|
+
- rvm: jruby-head
|
16
|
+
env: JRUBY_OPTS="--debug -X+O"
|
17
|
+
- rvm: ruby-head
|
18
|
+
allow_failures:
|
19
|
+
- rvm: truffleruby
|
20
|
+
- rvm: ruby-head
|
21
|
+
- rvm: jruby-head
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
Provides a robust set of parsers for dealing with HTTP `Accept`, `Accept-Language`, `Accept-Encoding`, `Accept-Charset` headers.
|
4
4
|
|
5
|
-
[![Build Status](https://secure.travis-ci.org/
|
6
|
-
[![Code Climate](https://codeclimate.com/github/
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/
|
5
|
+
[![Build Status](https://secure.travis-ci.org/socketry/http-accept.svg)](http://travis-ci.org/socketry/http-accept)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/socketry/http-accept.svg)](https://codeclimate.com/github/socketry/http-accept)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/socketry/http-accept/badge.svg)](https://coveralls.io/r/socketry/http-accept)
|
8
8
|
|
9
9
|
## Motivation
|
10
10
|
|
@@ -52,7 +52,7 @@ Normally, you'd want to match the media types against some set of available mime
|
|
52
52
|
```ruby
|
53
53
|
module ToJSON
|
54
54
|
def content_type
|
55
|
-
HTTP::Accept::ContentType.new("application
|
55
|
+
HTTP::Accept::ContentType.new("application", "json", charset: 'utf-8')
|
56
56
|
end
|
57
57
|
|
58
58
|
# Used for inserting into map.
|
@@ -115,7 +115,7 @@ The `desired_localizations` in the example above is a subset of `available_local
|
|
115
115
|
|
116
116
|
Released under the MIT license.
|
117
117
|
|
118
|
-
Copyright, 2016, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
118
|
+
Copyright, 2016, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
119
119
|
Copyright, 2016, by [Matthew Kerwin](http://kerwin.net.au).
|
120
120
|
|
121
121
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
data/Rakefile
CHANGED
data/http-accept.gemspec
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'http/accept/version'
|
1
|
+
|
2
|
+
require_relative 'lib/http/accept/version'
|
5
3
|
|
6
4
|
Gem::Specification.new do |spec|
|
7
5
|
spec.name = "http-accept"
|
@@ -17,7 +15,8 @@ Gem::Specification.new do |spec|
|
|
17
15
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
16
|
spec.require_paths = ["lib"]
|
19
17
|
|
20
|
-
spec.add_development_dependency "
|
18
|
+
spec.add_development_dependency "covered"
|
19
|
+
spec.add_development_dependency "bundler"
|
21
20
|
spec.add_development_dependency "rake", "~> 10.0"
|
22
21
|
spec.add_development_dependency "rspec", "~> 3.0"
|
23
22
|
end
|
data/lib/http/accept.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -18,8 +20,6 @@
|
|
18
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
21
|
# THE SOFTWARE.
|
20
22
|
|
21
|
-
require "http/accept/version"
|
22
|
-
|
23
23
|
require_relative 'accept/version'
|
24
24
|
|
25
25
|
# Accept: header
|
data/lib/http/accept/charsets.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright (C) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -65,9 +67,7 @@ module HTTP
|
|
65
67
|
|
66
68
|
# Parse the list of browser preferred charsets and return ordered by priority.
|
67
69
|
def self.browser_preferred_charsets(env)
|
68
|
-
if accept_charsets = env[HTTP_ACCEPT_CHARSET]
|
69
|
-
accept_charsets.strip!
|
70
|
-
|
70
|
+
if accept_charsets = env[HTTP_ACCEPT_CHARSET]&.strip
|
71
71
|
if accept_charsets.empty?
|
72
72
|
# https://tools.ietf.org/html/rfc7231#section-5.3.3 :
|
73
73
|
#
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -25,9 +27,9 @@ module HTTP
|
|
25
27
|
module Accept
|
26
28
|
# A content type is different from a media range, in that a content type should not have any wild cards.
|
27
29
|
class ContentType < MediaTypes::MediaRange
|
28
|
-
def initialize(
|
30
|
+
def initialize(type, subtype, parameters = {})
|
29
31
|
# We do some basic validation here:
|
30
|
-
raise ArgumentError.new("#{self.class} can not have wildcards: #{
|
32
|
+
raise ArgumentError.new("#{self.class} can not have wildcards: #{type}", "#{subtype}") if type.include?('*') || subtype.include?('*')
|
31
33
|
|
32
34
|
super
|
33
35
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright (C) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -72,9 +74,7 @@ module HTTP
|
|
72
74
|
# specified, the behaviour is the same as if `Accept-Encoding: identity` was provided
|
73
75
|
# (according to RFC).
|
74
76
|
def self.browser_preferred_content_codings(env)
|
75
|
-
if accept_content_codings = env[HTTP_ACCEPT_ENCODING]
|
76
|
-
accept_content_codings.strip!
|
77
|
-
|
77
|
+
if accept_content_codings = env[HTTP_ACCEPT_ENCODING]&.strip
|
78
78
|
if accept_content_codings.empty?
|
79
79
|
# "An Accept-Encoding header field with a combined field-value that is
|
80
80
|
# empty implies that the user agent does not want any content-coding in
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -31,11 +33,11 @@ module HTTP
|
|
31
33
|
# Parse and process the HTTP Accept: header.
|
32
34
|
module MediaTypes
|
33
35
|
# According to https://tools.ietf.org/html/rfc7231#section-5.3.2
|
34
|
-
MIME_TYPE = /(
|
36
|
+
MIME_TYPE = /(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})/
|
35
37
|
PARAMETER = /\s*;\s*(?<key>#{TOKEN})=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/
|
36
38
|
|
37
39
|
# A single entry in the Accept: header, which includes a mime type and associated parameters.
|
38
|
-
MediaRange = Struct.new(:
|
40
|
+
MediaRange = Struct.new(:type, :subtype, :parameters) do
|
39
41
|
def parameters_string
|
40
42
|
return '' if parameters == nil or parameters.empty?
|
41
43
|
|
@@ -52,8 +54,12 @@ module HTTP
|
|
52
54
|
end
|
53
55
|
end
|
54
56
|
|
57
|
+
def mime_type
|
58
|
+
"#{type}/#{subtype}"
|
59
|
+
end
|
60
|
+
|
55
61
|
def to_s
|
56
|
-
"#{
|
62
|
+
"#{type}/#{subtype}#{parameters_string}"
|
57
63
|
end
|
58
64
|
|
59
65
|
alias to_str to_s
|
@@ -62,8 +68,8 @@ module HTTP
|
|
62
68
|
parameters.fetch('q', 1.0).to_f
|
63
69
|
end
|
64
70
|
|
65
|
-
def split(
|
66
|
-
|
71
|
+
def split(*args)
|
72
|
+
return [type, subtype]
|
67
73
|
end
|
68
74
|
|
69
75
|
def self.parse_parameters(scanner, normalize_whitespace)
|
@@ -86,10 +92,13 @@ module HTTP
|
|
86
92
|
def self.parse(scanner, normalize_whitespace = true)
|
87
93
|
return to_enum(:parse, scanner, normalize_whitespace) unless block_given?
|
88
94
|
|
89
|
-
while
|
95
|
+
while scanner.scan(MIME_TYPE)
|
96
|
+
type = scanner[:type]
|
97
|
+
subtype = scanner[:subtype]
|
98
|
+
|
90
99
|
parameters = parse_parameters(scanner, normalize_whitespace)
|
91
100
|
|
92
|
-
yield self.new(
|
101
|
+
yield self.new(type, subtype, parameters)
|
93
102
|
|
94
103
|
# Are there more?
|
95
104
|
break unless scanner.scan(/\s*,\s*/)
|
@@ -108,13 +117,11 @@ module HTTP
|
|
108
117
|
end
|
109
118
|
|
110
119
|
HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
|
111
|
-
WILDCARD_MEDIA_RANGE = MediaRange.new("
|
120
|
+
WILDCARD_MEDIA_RANGE = MediaRange.new("*", "*", {}).freeze
|
112
121
|
|
113
122
|
# Parse the list of browser preferred content types and return ordered by priority. If no `Accept:` header is specified, the behaviour is the same as if `Accept: */*` was provided (according to RFC).
|
114
123
|
def self.browser_preferred_media_types(env)
|
115
|
-
if accept_content_types = env[HTTP_ACCEPT]
|
116
|
-
accept_content_types.strip!
|
117
|
-
|
124
|
+
if accept_content_types = env[HTTP_ACCEPT]&.strip
|
118
125
|
unless accept_content_types.empty?
|
119
126
|
return HTTP::Accept::MediaTypes.parse(accept_content_types)
|
120
127
|
end
|
data/lib/http/accept/sort.rb
CHANGED
data/lib/http/accept/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -20,6 +22,6 @@
|
|
20
22
|
|
21
23
|
module HTTP
|
22
24
|
module Accept
|
23
|
-
VERSION = "1.
|
25
|
+
VERSION = "2.1.0"
|
24
26
|
end
|
25
27
|
end
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-accept
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: covered
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- - "
|
31
|
+
- - ">="
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
33
|
+
version: '0'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- - "
|
38
|
+
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -61,7 +75,6 @@ extra_rdoc_files: []
|
|
61
75
|
files:
|
62
76
|
- ".gitignore"
|
63
77
|
- ".rspec"
|
64
|
-
- ".simplecov"
|
65
78
|
- ".travis.yml"
|
66
79
|
- Gemfile
|
67
80
|
- README.md
|
@@ -96,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
109
|
- !ruby/object:Gem::Version
|
97
110
|
version: '0'
|
98
111
|
requirements: []
|
99
|
-
|
100
|
-
rubygems_version: 2.6.10
|
112
|
+
rubygems_version: 3.0.4
|
101
113
|
signing_key:
|
102
114
|
specification_version: 4
|
103
115
|
summary: Parse Accept and Accept-Language HTTP headers.
|