class HTTP::Cookie::Scanner

Constants

RE_BAD_CHAR

A pattern that matches the comma in a (typically date) value.

RE_NAME

A pattern that matches a cookie name or attribute name which may be empty, capturing trailing whitespace.

RE_WSP

Whitespace.

Public Class Methods

new(string, logger = nil) click to toggle source
Calls superclass method
# File lib/http/cookie/scanner.rb, line 18
def initialize(string, logger = nil)
  @logger = logger
  super(string)
end
quote(s) click to toggle source
# File lib/http/cookie/scanner.rb, line 24
def quote(s)
  return s unless s.match(RE_BAD_CHAR)
  '"' << s.gsub(/([\\"])/, "\\\\\\1") << '"'
end

Public Instance Methods

scan_dquoted() click to toggle source
# File lib/http/cookie/scanner.rb, line 34
def scan_dquoted
  ''.tap { |s|
    case
    when skip(/"/)
      break
    when skip(/\\/)
      s << getch
    when scan(/[^"\\]+/)
      s << matched
    end until eos?
  }
end
scan_name() click to toggle source
# File lib/http/cookie/scanner.rb, line 47
def scan_name
  scan(RE_NAME).tap { |s|
    s.rstrip! if s
  }
end
scan_name_value(comma_as_separator = false) click to toggle source
# File lib/http/cookie/scanner.rb, line 73
def scan_name_value(comma_as_separator = false)
  name = scan_name
  if skip(/\=/)
    value = scan_value(comma_as_separator)
  else
    scan_value(comma_as_separator)
    value = nil
  end
  [name, value]
end
scan_value(comma_as_separator = false) click to toggle source
# File lib/http/cookie/scanner.rb, line 53
def scan_value(comma_as_separator = false)
  ''.tap { |s|
    case
    when scan(/[^,;"]+/)
      s << matched
    when skip(/"/)
      # RFC 6265 2.2
      # A cookie-value may be DQUOTE'd.
      s << scan_dquoted
    when check(/;/)
      break
    when comma_as_separator && check(RE_COOKIE_COMMA)
      break
    else
      s << getch
    end until eos?
    s.rstrip!
  }
end
skip_wsp() click to toggle source
# File lib/http/cookie/scanner.rb, line 30
def skip_wsp
  skip(RE_WSP)
end

Private Instance Methods

tuple_to_time(day_of_month, month, year, time) click to toggle source
# File lib/http/cookie/scanner.rb, line 85
def tuple_to_time(day_of_month, month, year, time)
  Time.strptime(
    '%02d %s %04d %02d:%02d:%02d UTC' % [day_of_month, month, year, *time],
    '%d %b %Y %T %Z'
  ).tap { |date|
    date.day == day_of_month or return nil
  }
end