http-negotiate 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE +202 -0
- data/README.md +53 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/http-negotiate.gemspec +35 -0
- data/lib/http-negotiate.rb +1 -0
- data/lib/http/negotiate.rb +181 -0
- data/lib/http/negotiate/version.rb +5 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd99cf36cd404c834530adbd738b22a485280105cc5e1bc868515f70f08d9549
|
4
|
+
data.tar.gz: aad680c6a7a7f9d627232317b0ef880d801bc65a9dbbf73ff0a9ab2b40c7a005
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7dad74af3b4900b212d17519fc517d54e4d1b8c3b26d7fe527e35c4296a4c27ac1dc4e5776fdde817922e250190bcec1c0f59db9c3ece17fbee58d29686cc4e0
|
7
|
+
data.tar.gz: e628966125eba4f566a151bf91d77d637097977c71e46f9b0fc7d5c734ebdf70b6b39954e354bbfadd1ab7e5854e4464e54586c4d00bd3b4daa6f2d09219db26
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# HTTP::Negotiate
|
2
|
+
|
3
|
+
This is literally just a port of Gisle Aas's
|
4
|
+
[HTTP::Negotiate](https://metacpan.org/pod/HTTP::Negotiate) written in
|
5
|
+
Perl, with a couple tiny changes to the interface. The `negotiate`
|
6
|
+
method is accessible as either a class method or instance method, so
|
7
|
+
you can take your pick of interface.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'http/negotiate'
|
13
|
+
|
14
|
+
# access it as an ordinary function
|
15
|
+
HTTP::Negotiate.negotiate request, variants
|
16
|
+
|
17
|
+
# ...or include it as an instance method
|
18
|
+
class Foo
|
19
|
+
include HTTP::Negotiate
|
20
|
+
|
21
|
+
# you now have access to #negotiate
|
22
|
+
end
|
23
|
+
|
24
|
+
```
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'http-negotiate'
|
32
|
+
```
|
33
|
+
|
34
|
+
And then execute:
|
35
|
+
|
36
|
+
$ bundle install
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
|
40
|
+
$ gem install http-negotiate
|
41
|
+
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome at
|
46
|
+
[the Github repository](https://github.com/doriantaylor/rb-http-negotiate).
|
47
|
+
|
48
|
+
## Copyright & License
|
49
|
+
|
50
|
+
©2020 [Dorian Taylor](https://doriantaylor.com/)
|
51
|
+
|
52
|
+
This software is provided under
|
53
|
+
the [Apache License, 2.0](https://www.apache.org/licenses/LICENSE-2.0).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "http/negotiate"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- mode: enh-ruby -*-
|
2
|
+
require_relative 'lib/http/negotiate/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'http-negotiate'
|
6
|
+
spec.version = HTTP::Negotiate::VERSION
|
7
|
+
spec.authors = ['Dorian Taylor']
|
8
|
+
spec.email = ['code@doriantaylor.com']
|
9
|
+
spec.license = 'Apache-2.0'
|
10
|
+
spec.homepage = 'https://github.com/doriantaylor/rb-http-negotiate'
|
11
|
+
spec.summary = 'An implementation of Gisle Aas\'s HTTP::Negotiate'
|
12
|
+
|
13
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
14
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is
|
17
|
+
# released. The `git ls-files -z` loads the files in the RubyGem
|
18
|
+
# that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
30
|
+
|
31
|
+
# dev/test dependencies
|
32
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
33
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.9'
|
35
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'http/negotiate'
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require "http/negotiate/version"
|
2
|
+
|
3
|
+
# We are basically copying Gisle Aas' (Perl) HTTP::Negotiate here,
|
4
|
+
# with Ruby characteristics.
|
5
|
+
module HTTP::Negotiate
|
6
|
+
private
|
7
|
+
|
8
|
+
HEADERS = { type: '' }.merge(
|
9
|
+
%i[charset language encoding].map { |k| [k, ?_ + k.to_s.upcase] }.to_h)
|
10
|
+
|
11
|
+
public
|
12
|
+
|
13
|
+
# The variant mapping takes the following form:
|
14
|
+
# { variant => [weight, type, encoding, charset, language, size] }
|
15
|
+
#
|
16
|
+
# * the variant can be anything, including the actual variant
|
17
|
+
# * the weight is a number between 0 and 1 denoting the initial
|
18
|
+
# preference for the variant
|
19
|
+
# * type, encoding, charset, language are all strings containing
|
20
|
+
# their respective standardized tokens (note "encoding" is like
|
21
|
+
# `gzip`, not like `utf-8`: that's "charset")
|
22
|
+
# * size is the number of bytes
|
23
|
+
#
|
24
|
+
# Returns either the winning variant or all variants if requested,
|
25
|
+
# sorted by the algorithm, or nil or the empty array if none are
|
26
|
+
# selected.
|
27
|
+
#
|
28
|
+
# @param request [Hash,Rack::Request,#env] Anything roughly a hash of headers
|
29
|
+
# @param variants [Hash] the variants, described above
|
30
|
+
# @param all [false, true] whether to return
|
31
|
+
#
|
32
|
+
def negotiate request, variants, all: false
|
33
|
+
# pull accept headers
|
34
|
+
request = request.env if request.respond_to? :env
|
35
|
+
# this will ensure that the keys will match irrespective of
|
36
|
+
# whether it's passed in as actual http headers or a cgi-like env
|
37
|
+
request = request.transform_keys do |k|
|
38
|
+
if /^Accept(-.+)?$/i.match? k.to_s
|
39
|
+
'HTTP_' + k.to_s.upcase.tr(?-, ?_)
|
40
|
+
else
|
41
|
+
k
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# the working set
|
46
|
+
accept = {}
|
47
|
+
|
48
|
+
HEADERS.each do |k, h|
|
49
|
+
if hdr = request["HTTP_ACCEPT#{h}"]
|
50
|
+
defq = 1.0
|
51
|
+
# theoretically you can have quoted-string parameters but we don't care
|
52
|
+
hdr = hdr.dup.gsub(/\s+/, '')
|
53
|
+
accept[k] = hdr.split(/,+/).map do |c|
|
54
|
+
val, *params = c.split(/;+/)
|
55
|
+
params = params.map do |p|
|
56
|
+
k, v = p.split(/=+/, 2)
|
57
|
+
k = k.downcase.to_sym
|
58
|
+
v = v.to_f if v and k == :q
|
59
|
+
[k, v]
|
60
|
+
end.to_h
|
61
|
+
if params[:q]
|
62
|
+
params[:q] = 1.0 if params[:q] > 1.0
|
63
|
+
params[:q] = 0.0 if params[:q] < 0.0
|
64
|
+
else
|
65
|
+
params[:q] = defq
|
66
|
+
defq -= 0.0001
|
67
|
+
end
|
68
|
+
# none of the accept header contents are case sensitive
|
69
|
+
[val.downcase, params]
|
70
|
+
end.to_h
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# check if any of the variants specify a language, since this will
|
75
|
+
# affect the scoring
|
76
|
+
any_lang = variants.values.any? { |v| v[5] }
|
77
|
+
|
78
|
+
# chosen will be { variant => value }
|
79
|
+
scores = {}
|
80
|
+
variants.keys.each do |var|
|
81
|
+
qs, type, encoding, charset, language, size = variants[var]
|
82
|
+
# some defaults
|
83
|
+
qs ||= 1
|
84
|
+
type ||= ''
|
85
|
+
size ||= 0
|
86
|
+
|
87
|
+
# coerce encoding
|
88
|
+
encoding = encoding.to_s.strip.downcase if encoding
|
89
|
+
# coerce charset
|
90
|
+
charset = charset.to_s.strip.downcase if charset
|
91
|
+
# coerce language to canonical form
|
92
|
+
language = language.to_s.strip.downcase.tr_s '_-', '-' if language
|
93
|
+
|
94
|
+
# calculate encoding quality
|
95
|
+
qe = 1
|
96
|
+
if accept[:encoding] and encoding
|
97
|
+
qe = 0
|
98
|
+
qe = accept[:encoding][encoding][:q] if accept[:encoding][encoding]
|
99
|
+
qe = accept[:encoding][?*][:q] if accept[:encoding][?*] and
|
100
|
+
accept[:encoding][?*][:q] > qe
|
101
|
+
end
|
102
|
+
|
103
|
+
# calculate charset quality
|
104
|
+
qc = 1
|
105
|
+
if accept[:charset] and charset and charset != 'us-ascii'
|
106
|
+
qc = 0
|
107
|
+
qc = accept[:charset][charset][:q] if accept[:charset][charset]
|
108
|
+
qc = accept[:charset][?*][:q] if accept[:charset][?*] and
|
109
|
+
accept[:charset][?*][:q] > qc
|
110
|
+
end
|
111
|
+
|
112
|
+
# calculate the language quality
|
113
|
+
ql = 1
|
114
|
+
if accept[:language] and language
|
115
|
+
ql = 0.001 # initial value is very low but not zero
|
116
|
+
lang = language.split(/-+/)
|
117
|
+
(1..lang.length).to_a.reverse.each do |i|
|
118
|
+
test = lang.slice(0, i).join ?-
|
119
|
+
if accept[:language][test]
|
120
|
+
al = accept[:language][test][:q]
|
121
|
+
if al == 0
|
122
|
+
ql = 0
|
123
|
+
break
|
124
|
+
elsif al > ql
|
125
|
+
ql = al
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
# apparently there is no wildcard for accept-language?
|
130
|
+
elsif accept[:language] and any_lang
|
131
|
+
ql = 0.5
|
132
|
+
end
|
133
|
+
|
134
|
+
# calculate the type quality
|
135
|
+
qt = 1
|
136
|
+
if accept[:type] and type
|
137
|
+
type = type.to_s
|
138
|
+
qt = 0
|
139
|
+
at = {}
|
140
|
+
accept[:type].each do |k, v|
|
141
|
+
maj, min = k.split(/\//)
|
142
|
+
x = at[maj] ||= {}
|
143
|
+
x[min] = v
|
144
|
+
end
|
145
|
+
|
146
|
+
# XXX we do not actually try to do the params this time
|
147
|
+
mt, *params = type.split(/;+/)
|
148
|
+
maj, min = mt.split(/\/+/)
|
149
|
+
params = params.map { |p| p.split(/=+/, 2) }
|
150
|
+
|
151
|
+
# warn maj.inspect, min.inspect
|
152
|
+
|
153
|
+
# XXX match params at some point
|
154
|
+
if at[maj]
|
155
|
+
if at[maj][min]
|
156
|
+
qt = at[maj][min][:q]
|
157
|
+
elsif at[maj][?*]
|
158
|
+
qt = at[maj][?*][:q]
|
159
|
+
else
|
160
|
+
qt = 0.1
|
161
|
+
end
|
162
|
+
elsif at[?*]
|
163
|
+
# ???
|
164
|
+
qt = at[?*].fetch(?*, { q: 0.1 })[:q]
|
165
|
+
else
|
166
|
+
# ???
|
167
|
+
qt = 0.1
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
scores[var] = qs * qe * qc * ql * qt
|
173
|
+
end
|
174
|
+
|
175
|
+
chosen = scores.sort { |a, b| b.last <=> a.last }.map(&:first)
|
176
|
+
all ? chosen : chosen.first
|
177
|
+
end
|
178
|
+
|
179
|
+
extend self
|
180
|
+
end
|
181
|
+
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http-negotiate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dorian Taylor
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.9'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- code@doriantaylor.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- http-negotiate.gemspec
|
72
|
+
- lib/http-negotiate.rb
|
73
|
+
- lib/http/negotiate.rb
|
74
|
+
- lib/http/negotiate/version.rb
|
75
|
+
homepage: https://github.com/doriantaylor/rb-http-negotiate
|
76
|
+
licenses:
|
77
|
+
- Apache-2.0
|
78
|
+
metadata:
|
79
|
+
homepage_uri: https://github.com/doriantaylor/rb-http-negotiate
|
80
|
+
source_code_uri: https://github.com/doriantaylor/rb-http-negotiate
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.4.0
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.1.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: An implementation of Gisle Aas's HTTP::Negotiate
|
100
|
+
test_files: []
|