Module: Landline::Util::ParserCommon

Defined in:
lib/landline/util/parseutils.rb

Overview

Module for all things related to parsing HTTP and related syntax.

Constant Summary collapse

RFC1123_DATE =

#strftime parameter to return a correct RFC 1123 date.

"%a, %d %b %Y %H:%M:%S GMT"

Class Method Summary collapse

Class Method Details

.make_value(input, opts, sep = ";") ⇒ String

Construct a parametrized header value. Does some input sanitization during construction

Parameters:

  • input (String)
  • opts (Hash)

Returns:

  • (String)

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/landline/util/parseutils.rb', line 79

def self.make_value(input, opts, sep = ";")
  output = input
  unless input.match? HeaderRegexp::PRINTABLE
    raise Landline::ParsingError, "input is not ascii printable"
  end
  
  opts.each do |key, value|
    check_param(key, value)
    newparam = if [String, Integer].include? value.class
                 "#{sep} #{key}=#{value}"
               elsif value
                 "#{sep} #{key}"
               end
    output += newparam if newparam
  end
  output
end

.parse_value(input, sep: ";", unquote: false, regexp: nil) ⇒ Array(String, Hash)

Parse parametrized header values. This method will try the best attempt at decoding parameters. However, it does no decoding on the first argument.

Parameters:

  • input (String)
  • sep (String, Regexp) (defaults to: ";")
  • unquote (Boolean) (defaults to: false)

    interpret params as possibly quoted

  • regexp (Regexp, nil) (defaults to: nil)

    override param matching regexp

Returns:

  • (Array(String, Hash))


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/landline/util/parseutils.rb', line 50

def self.parse_value(input, sep: ";", unquote: false, regexp: nil)
  parts = input.split(sep).map { |x| URI.decode_uri_component(x).strip }
  base = parts.shift
  opts = parts.map do |raw|
    key, value = raw.match(if regexp
                             regexp
                           elsif unquote
                             HeaderRegexp::PARAM_QUOTED
                           else
                             HeaderRegexp::PARAM
                           end).to_a[1..]
    value = case value
            when "" then true
            when /\A".*"\z/ then value.undump
            else value
            end
    [key, value]
  end.to_h
  [base, opts]
end