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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/landline/util/parseutils.rb', line 85

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/landline/util/parseutils.rb', line 54

def self.parse_value(input, sep: ";", unquote: false, regexp: nil)
  parts = input.split(sep).map do |x|
    URI.decode_www_form_component(x).strip
  end
  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