|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2020-2023, by Samuel Williams. |
| 5 | +# Copyright, 2023, by Thomas Morgan. |
| 6 | + |
| 7 | +require_relative "split" |
| 8 | +require_relative "quoted_string" |
| 9 | +require_relative "../error" |
| 10 | + |
| 11 | +module Protocol |
| 12 | + module HTTP |
| 13 | + module Header |
| 14 | + # The `accept-content-type` header represents a list of content-types that the client can accept. |
| 15 | + class Accept < Array |
| 16 | + # Regular expression used to split values on commas, with optional surrounding whitespace, taking into account quoted strings. |
| 17 | + SPLIT = / |
| 18 | + (?: # Start non-capturing group |
| 19 | + "[^"\\]*" # Match quoted strings (no escaping of quotes within) |
| 20 | + | # OR |
| 21 | + [^,"]+ # Match non-quoted strings until a comma or quote |
| 22 | + )+ |
| 23 | + (?=,|\z) # Match until a comma or end of string |
| 24 | + /x |
| 25 | + |
| 26 | + ParseError = Class.new(Error) |
| 27 | + |
| 28 | + MEDIA_RANGE = /\A(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})(?<parameters>.*)\z/ |
| 29 | + |
| 30 | + PARAMETER = /\s*;\s*(?<key>#{TOKEN})=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/ |
| 31 | + |
| 32 | + # A single entry in the Accept: header, which includes a mime type and associated parameters. |
| 33 | + MediaRange = Struct.new(:type, :subtype, :parameters) do |
| 34 | + def initialize(type, subtype = '*', parameters = {}) |
| 35 | + super(type, subtype, parameters) |
| 36 | + end |
| 37 | + |
| 38 | + def <=> other |
| 39 | + other.quality_factor <=> self.quality_factor |
| 40 | + end |
| 41 | + |
| 42 | + def parameters_string |
| 43 | + return '' if parameters == nil or parameters.empty? |
| 44 | + |
| 45 | + parameters.collect do |key, value| |
| 46 | + "; #{key.to_s}=#{QuotedString.quote(value.to_s)}" |
| 47 | + end.join |
| 48 | + end |
| 49 | + |
| 50 | + def === other |
| 51 | + if other.is_a? self.class |
| 52 | + super |
| 53 | + else |
| 54 | + return self.mime_type === other |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def mime_type |
| 59 | + "#{type}/#{subtype}" |
| 60 | + end |
| 61 | + |
| 62 | + def to_s |
| 63 | + "#{type}/#{subtype}#{parameters_string}" |
| 64 | + end |
| 65 | + |
| 66 | + alias to_str to_s |
| 67 | + |
| 68 | + def quality_factor |
| 69 | + parameters.fetch('q', 1.0).to_f |
| 70 | + end |
| 71 | + |
| 72 | + def split(*args) |
| 73 | + return [type, subtype] |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def initialize(value = nil) |
| 78 | + if value |
| 79 | + super(value.scan(SPLIT).map(&:strip)) |
| 80 | + else |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + # Adds one or more comma-separated values to the header. |
| 85 | + # |
| 86 | + # The input string is split into distinct entries and appended to the array. |
| 87 | + # |
| 88 | + # @parameter value [String] the value or values to add, separated by commas. |
| 89 | + def << (value) |
| 90 | + self.concat(value.scan(SPLIT).map(&:strip)) |
| 91 | + end |
| 92 | + |
| 93 | + # Serializes the stored values into a comma-separated string. |
| 94 | + # |
| 95 | + # @returns [String] the serialized representation of the header values. |
| 96 | + def to_s |
| 97 | + join(",") |
| 98 | + end |
| 99 | + |
| 100 | + # Parse the `accept` header. |
| 101 | + # |
| 102 | + # @returns [Array(Charset)] the list of content types and their associated parameters. |
| 103 | + def media_ranges |
| 104 | + self.map do |value| |
| 105 | + self.parse_media_range(value) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + private |
| 110 | + |
| 111 | + def parse_media_range(value) |
| 112 | + if match = value.match(MEDIA_RANGE) |
| 113 | + type = match[:type] |
| 114 | + subtype = match[:subtype] |
| 115 | + parameters = {} |
| 116 | + |
| 117 | + match[:parameters].scan(PARAMETER) do |key, value, quoted_value| |
| 118 | + parameters[key] = quoted_value || value |
| 119 | + end |
| 120 | + |
| 121 | + return MediaRange.new(type, subtype, parameters) |
| 122 | + else |
| 123 | + raise ArgumentError, "Invalid media type: #{value.inspect}" |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments