mirror of
https://gitea.invidious.io/iv-org/shard-athena-negotiation.git
synced 2024-08-15 00:53:23 +00:00
Compare commits
No commits in common. "master" and "v0.1.5" have entirely different histories.
5 changed files with 14 additions and 14 deletions
|
@ -24,12 +24,12 @@ Each negotiator exposes two methods: [ANG::AbstractNegotiator#best][] and [ANG::
|
||||||
negotiator = ANG.negotiator
|
negotiator = ANG.negotiator
|
||||||
|
|
||||||
accept_header = "text/html, application/xhtml+xml, application/xml;q=0.9"
|
accept_header = "text/html, application/xhtml+xml, application/xml;q=0.9"
|
||||||
priorities = ["text/html; charset=utf-8", "application/json", "application/xml;q=0.5"]
|
priorities = ["text/html; charset=UTF-8", "application/json", "application/xml;q=0.5"]
|
||||||
|
|
||||||
accept = negotiator.best(accept_header, priorities).not_nil!
|
accept = negotiator.best(accept_header, priorities).not_nil!
|
||||||
|
|
||||||
accept.media_range # => "text/html"
|
accept.media_range # => "text/html"
|
||||||
accept.parameters # => {"charset" => "utf-8"}
|
accept.parameters # => {"charset" => "UTF-8"}
|
||||||
```
|
```
|
||||||
|
|
||||||
The [ANG::Negotiator][] type returns an [ANG::Accept][], or `nil` if negotiating the best media type has failed.
|
The [ANG::Negotiator][] type returns an [ANG::Accept][], or `nil` if negotiating the best media type has failed.
|
||||||
|
@ -39,7 +39,7 @@ The [ANG::Negotiator][] type returns an [ANG::Accept][], or `nil` if negotiating
|
||||||
```crystal
|
```crystal
|
||||||
negotiator = ANG.charset_negotiator
|
negotiator = ANG.charset_negotiator
|
||||||
|
|
||||||
accept_header = "ISO-8859-1, utf-8; q=0.9"
|
accept_header = "ISO-8859-1, UTF-8; q=0.9"
|
||||||
priorities = ["iso-8859-1;q=0.3", "utf-8;q=0.9", "utf-16;q=1.0"]
|
priorities = ["iso-8859-1;q=0.3", "utf-8;q=0.9", "utf-16;q=1.0"]
|
||||||
|
|
||||||
accept = negotiator.best(accept_header, priorities).not_nil!
|
accept = negotiator.best(accept_header, priorities).not_nil!
|
||||||
|
|
|
@ -71,9 +71,9 @@ struct NegotiatorTest < NegotiatorTestCase
|
||||||
{"image/png, text/plain, audio/ogg", {"baz/asdf"}, nil},
|
{"image/png, text/plain, audio/ogg", {"baz/asdf"}, nil},
|
||||||
{"image/png, text/plain, audio/ogg", {"audio/ogg"}, {"audio/ogg", nil}},
|
{"image/png, text/plain, audio/ogg", {"audio/ogg"}, {"audio/ogg", nil}},
|
||||||
{"image/png, text/plain, audio/ogg", {"YO/SuP"}, nil},
|
{"image/png, text/plain, audio/ogg", {"YO/SuP"}, nil},
|
||||||
{"text/html; charset=utf-8, application/pdf", {"text/html; charset=utf-8"}, {"text/html", {"charset" => "utf-8"}}},
|
{"text/html; charset=UTF-8, application/pdf", {"text/html; charset=UTF-8"}, {"text/html", {"charset" => "UTF-8"}}},
|
||||||
{"text/html; charset=utf-8, application/pdf", {"text/html"}, nil},
|
{"text/html; charset=UTF-8, application/pdf", {"text/html"}, nil},
|
||||||
{"text/html, application/pdf", {"text/html; charset=utf-8"}, {"text/html", {"charset" => "utf-8"}}},
|
{"text/html, application/pdf", {"text/html; charset=UTF-8"}, {"text/html", {"charset" => "UTF-8"}}},
|
||||||
|
|
||||||
# PHP"s PEAR HTTP2 assertions I took from the other lib.
|
# PHP"s PEAR HTTP2 assertions I took from the other lib.
|
||||||
{php_pear_header, {"image/gif", "image/png", "application/xhtml+xml", "application/xml", "text/html", "image/jpeg", "text/plain"}, {"image/png", nil}},
|
{php_pear_header, {"image/gif", "image/png", "application/xhtml+xml", "application/xml", "text/html", "image/jpeg", "text/plain"}, {"image/png", nil}},
|
||||||
|
|
|
@ -26,9 +26,9 @@ abstract class Athena::Negotiation::AbstractNegotiator(HeaderType)
|
||||||
raise ex if strict
|
raise ex if strict
|
||||||
end
|
end
|
||||||
|
|
||||||
accepted_priorities = priorities.map { |p| HeaderType.new p }
|
accepted_priorties = priorities.map { |p| HeaderType.new p }
|
||||||
|
|
||||||
matches = self.find_matches accepted_headers, accepted_priorities
|
matches = self.find_matches accepted_headers, accepted_priorties
|
||||||
|
|
||||||
specific_matches = matches.reduce({} of Int32 => ANG::AcceptMatch) do |acc, match|
|
specific_matches = matches.reduce({} of Int32 => ANG::AcceptMatch) do |acc, match|
|
||||||
ANG::AcceptMatch.reduce acc, match
|
ANG::AcceptMatch.reduce acc, match
|
||||||
|
@ -38,7 +38,7 @@ abstract class Athena::Negotiation::AbstractNegotiator(HeaderType)
|
||||||
|
|
||||||
match = specific_matches.shift?
|
match = specific_matches.shift?
|
||||||
|
|
||||||
match.nil? ? nil : accepted_priorities[match.index]
|
match.nil? ? nil : accepted_priorties[match.index]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns an array of `HeaderType` that the provided *header* allows, ordered so that the `#best` match is first.
|
# Returns an array of `HeaderType` that the provided *header* allows, ordered so that the `#best` match is first.
|
||||||
|
|
|
@ -3,11 +3,11 @@ require "./base_accept"
|
||||||
# Represents an [Accept](https://tools.ietf.org/html/rfc7231#section-5.3.2) header media type.
|
# Represents an [Accept](https://tools.ietf.org/html/rfc7231#section-5.3.2) header media type.
|
||||||
#
|
#
|
||||||
# ```
|
# ```
|
||||||
# accept = ANG::Accept.new "application/json; q = 0.75; charset = utf-8"
|
# accept = ANG::Accept.new "application/json; q = 0.75; charset = UTF-8"
|
||||||
#
|
#
|
||||||
# accept.header # => "application/json; q = 0.75; charset = utf-8"
|
# accept.header # => "application/json; q = 0.75; charset = UTF-8"
|
||||||
# accept.normalized_header # => "application/json; charset=utf-8"
|
# accept.normalized_header # => "application/json; charset=UTF-8"
|
||||||
# accept.parameters # => {"charset" => "utf-8"}
|
# accept.parameters # => {"charset" => "UTF-8"}
|
||||||
# accept.quality # => 0.75
|
# accept.quality # => 0.75
|
||||||
# accept.type # => "application"
|
# accept.type # => "application"
|
||||||
# accept.sub_type # => "json"
|
# accept.sub_type # => "json"
|
||||||
|
|
|
@ -10,7 +10,7 @@ abstract struct Athena::Negotiation::BaseAccept
|
||||||
getter normalized_header : String
|
getter normalized_header : String
|
||||||
|
|
||||||
# Returns any extension parameters included in the header `self` represents.
|
# Returns any extension parameters included in the header `self` represents.
|
||||||
# E.x. `charset=utf-8` or `version=2`.
|
# E.x. `charset=UTF-8` or `version=2`.
|
||||||
getter parameters : Hash(String, String) = Hash(String, String).new
|
getter parameters : Hash(String, String) = Hash(String, String).new
|
||||||
|
|
||||||
# Returns the [quality value](https://tools.ietf.org/html/rfc7231#section-5.3.1) of the header `self` represents.
|
# Returns the [quality value](https://tools.ietf.org/html/rfc7231#section-5.3.1) of the header `self` represents.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue