# :api: private
def valid?(uri)
# From the RFC (http://tools.ietf.org/html/rfc2965, see section 2)
#
# The term request-port refers to the port portion of the absoluteURI
# (http_URL) of the HTTP request line. If the absoluteURI has no
# explicit port, the request-port is the HTTP default, 80. The
# request-port of a cookie is the request-port of the request in which
# a Set-Cookie2 response header was returned to the user agent.
#
# Host names can be specified either as an IP address or a HDN string.
# Sometimes we compare one host name with another. (Such comparisons
# SHALL be case-insensitive.) Host A's name domain-matches host B's if
#
# * their host name strings string-compare equal; or
#
# * A is a HDN string and has the form NB, where N is a non-empty
# name string, B has the form .B', and B' is a HDN string. (So,
# x.y.com domain-matches .Y.com but not Y.com.)
#
# Note that domain-match is not a commutative operation: a.b.c.com
# domain-matches .c.com, but not the reverse.
#
# The reach R of a host name H is defined as follows:
# * If
# - H is the host domain name of a host; and,
# - H has the form A.B; and
# - A has no embedded (that is, interior) dots; and
# - B has at least one embedded dot, or B is the string "local".
# then the reach of H is .B.
# * Otherwise, the reach of H is H.
# For two strings that represent paths, P1 and P2, P1 path-matches P2
# if P2 is a prefix of P1 (including the case where P1 and P2 string-
# compare equal). Thus, the string /tec/waldo path-matches /tec.
# we simplify things here for now +intentionally+
host_matches = (uri.host == domain || uri.host =~ Regexp.new("#{Regexp.escape(domain)}$"))
path_matches = (uri.path =~ Regexp.new("^#{Regexp.escape(path)}"))
STDOUT.puts "[merb/test helpers] CookieJar#valid: host match is #{host_matches.inspect}, domain is #{domain.inspect} and uri.host is #{uri.host.inspect}" if ENV["DEBUG_DOMAINS"]
host_matches && path_matches
end