Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikolai Weibull
committed
Dec 20, 2010
1 parent
319428d
commit 663d29c
Showing
6 changed files
with
344 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
class Lookout::Rack::Cookie | ||
include Comparable | ||
|
||
def initialize(header, uri = nil, default_host = Lookout::Rack::DefaultHost) | ||
@default_host = default_host | ||
uri ||= default_uri | ||
|
||
@cookie, options = header.split(/[;,] */n, 2) | ||
|
||
@name, @value = Rack::Utils.parse_query(@cookie, ';').to_a.first | ||
|
||
@options = Hash[Rack::Utils.parse_query(options, ';').map{ |k, v| [k.downcase, v] }] | ||
@options['domain'] ||= uri.host || default_host | ||
@options['path'] ||= uri.path.sub(%r{/[^/]*\Z}, '') | ||
end | ||
|
||
def empty? | ||
@value.nil? or @value.empty? | ||
end | ||
|
||
def valid?(uri) | ||
uri ||= default_uri | ||
uri.host ||= @default_host | ||
|
||
real_domain = domain.start_with?('.') ? domain[1..-1] : domain | ||
secure? uri and | ||
uri.host.downcase.end_with? real_domain.downcase and | ||
uri.path.start_with? path | ||
end | ||
|
||
def matches?(uri) | ||
valid? uri and not expired? | ||
end | ||
|
||
def eql?(other) | ||
self.class === other and self == other | ||
end | ||
|
||
def <=>(other) | ||
[name.downcase, path, domain.reverse] <=> | ||
[other.name.downcase, other.path, other.domain.reverse] | ||
end | ||
|
||
def hash | ||
name.downcase.hash ^ path.hash ^ domain.hash | ||
end | ||
|
||
def to_s | ||
@cookie | ||
end | ||
|
||
attr_reader :name | ||
|
||
protected | ||
|
||
def domain | ||
@options['domain'] | ||
end | ||
|
||
def path | ||
@options['path'].strip | ||
end | ||
|
||
private | ||
|
||
def default_uri | ||
URI('//%s/' % @default_host) | ||
end | ||
|
||
def secure?(uri) | ||
not @options.include? 'secure' or uri.scheme == 'https' | ||
end | ||
|
||
def expired? | ||
@options['expires'] and Time.parse(@options['expires']) < Time.now | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
class Lookout::Rack::Cookies | ||
def initialize(default_host = Lookout::Rack::DefaultHost) | ||
@default_host = default_host | ||
@cookies = {} | ||
end | ||
|
||
def merge!(headers, uri = nil) | ||
headers.split("\n").reject{ |c| c.empty? }.each do |header| | ||
cookie = Lookout::Rack::Cookie.new(header, uri, @default_host) | ||
@cookies[cookie] = cookie if cookie.valid? uri | ||
end | ||
self | ||
end | ||
|
||
def for(uri) | ||
@cookies.values.select{ |c| c.matches? uri }.sort. | ||
reduce({}){ |h, c| h[c.name] = c; h }.values.join(';') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.