http_cookie_monster 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +28 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +125 -0
- data/Rakefile +2 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/cookie_monster.gemspec +32 -0
- data/lib/cookie_monster.rb +127 -0
- data/lib/cookie_monster/version.rb +3 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5cca29bec6ea05bf5be5c79496d3a42843bc55c8
|
4
|
+
data.tar.gz: 86cc6b181e01cb4db5e2fb2721b2a4890f8206b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b8ccafd95c834ca2fe40f49e478a2ca2836c64d147699a066e9a8d7bb515483568825ac2c299f8f77f858030e3975e377c4ab49cc5d8053973c5896ab3f193d
|
7
|
+
data.tar.gz: 232f4860d4622ee89925ca3d3ccbb8222207db2c13e9679e96460d8f773819d04b7e76bee991f5f2d03dd30c1ed7ccfa87be5d729ef9d918bff1e2c06f7935bd
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
http_cookie_monster (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.0)
|
10
|
+
method_source (0.8.2)
|
11
|
+
pry (0.10.3)
|
12
|
+
coderay (~> 1.1.0)
|
13
|
+
method_source (~> 0.8.1)
|
14
|
+
slop (~> 3.4)
|
15
|
+
rake (10.4.2)
|
16
|
+
slop (3.6.0)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
bundler (~> 1.12)
|
23
|
+
http_cookie_monster!
|
24
|
+
pry
|
25
|
+
rake (~> 10.0)
|
26
|
+
|
27
|
+
BUNDLED WITH
|
28
|
+
1.12.5
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Marc Jakobs
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Marc Jakobs
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# CookieMonster
|
2
|
+
|
3
|
+
CookieMonster is a Ruby library to work with HTTP Cookies. It allows to parse cookies from a HTTP header into an object and to convert the object back to a cookie string or response header.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'http_cookie_monster'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install http_cookie_monster
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
|
25
|
+
# include the gem into your code
|
26
|
+
#
|
27
|
+
require 'cookie_monster'
|
28
|
+
|
29
|
+
# a new cookie-monster object
|
30
|
+
#
|
31
|
+
cookie = CookieMonster.new
|
32
|
+
|
33
|
+
# a new cookie-monster object with initial attributes
|
34
|
+
#
|
35
|
+
cookie = CookieMonster.new({ :name => 'cookiename', :value => 'cookievalue', :httponly => true })
|
36
|
+
|
37
|
+
# a new cookie-monster object from a raw cookie HTTP request header
|
38
|
+
#
|
39
|
+
cookie = CookieMonster.new 'Cookie: name=value; domain=.domain.com; path=/; httponly'
|
40
|
+
|
41
|
+
# a new cookie-monster object from a cookie string
|
42
|
+
#
|
43
|
+
cookie = CookieMonster.new 'name=value; domain=.domain.com; path=/; httponly; secure'
|
44
|
+
|
45
|
+
#
|
46
|
+
# please note:
|
47
|
+
#
|
48
|
+
# name and value will be automatically URL decoded when parsed
|
49
|
+
# and automatically URL encoded when being set
|
50
|
+
#
|
51
|
+
|
52
|
+
# the following cookie attributes can be set
|
53
|
+
#
|
54
|
+
cookie.name = 'myShinyCookie' # the name will be URL encoded
|
55
|
+
cookie.value = 'test|123|zyx' # the value will be URL encoded as well
|
56
|
+
cookie.path = '/'
|
57
|
+
cookie.domain = '.my.domain.com'
|
58
|
+
cookie.expires = Time.now + 7*86400 # must be a Time object
|
59
|
+
cookie.httponly = true
|
60
|
+
cookie.secure = true
|
61
|
+
|
62
|
+
# the attributes can be accessed like this
|
63
|
+
#
|
64
|
+
puts cookie.name
|
65
|
+
puts cookie.value
|
66
|
+
puts cookie.path
|
67
|
+
puts cookie.domain
|
68
|
+
puts cookie.expires
|
69
|
+
puts cookie.httponly
|
70
|
+
puts cookie.secure
|
71
|
+
|
72
|
+
# there are some helper methods available
|
73
|
+
#
|
74
|
+
|
75
|
+
# is_valid? does some basic checks: is the cookie name set and is the expiry a Time object (if it's defined)
|
76
|
+
if cookie.is_valid?
|
77
|
+
puts ":-)"
|
78
|
+
else
|
79
|
+
puts ":-("
|
80
|
+
end
|
81
|
+
|
82
|
+
# runs the validation and throws exceptions
|
83
|
+
#
|
84
|
+
cookie.validate!
|
85
|
+
|
86
|
+
# sets the cookie expiry
|
87
|
+
#
|
88
|
+
cookie.expires_in_seconds(60) # in 60 seconds
|
89
|
+
cookie.expires_in_seconds(3600) # in 1 hour
|
90
|
+
cookie.expires_in_seconds(86400) # in 1 day
|
91
|
+
cookie.expires_in_seconds(-60) # expires the cookie
|
92
|
+
|
93
|
+
# expire cookies
|
94
|
+
#
|
95
|
+
cookie.expire! # sets the expire date to a date in the past
|
96
|
+
cookie.delete! # removes the cookie value
|
97
|
+
|
98
|
+
# export the cookie object - runs validate! and throws a RuntimeError exception if the cookie is invalid!
|
99
|
+
#
|
100
|
+
cookie.to_s # convert to a string
|
101
|
+
cookie.to_header # convert to a HTTP response header
|
102
|
+
|
103
|
+
# ... so this would be better
|
104
|
+
#
|
105
|
+
begin
|
106
|
+
puts cookie.to_header
|
107
|
+
rescue => ex
|
108
|
+
puts "Failed to generate cookie: #{ex}"
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
## Development
|
113
|
+
|
114
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hirschnase/CookieMonster.
|
121
|
+
|
122
|
+
## License
|
123
|
+
|
124
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
125
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cookie_monster/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "http_cookie_monster"
|
8
|
+
spec.version = CookieMonster::VERSION
|
9
|
+
spec.authors = ["Marc Jakobs"]
|
10
|
+
spec.email = ["marc@moc.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby Class to work on HTTP Cookies}
|
13
|
+
spec.description = %q{A simple helper Class to parse / generate HTTP Cookies}
|
14
|
+
spec.homepage = "https://github.com/hirschnase/CookieMonster"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'cookie_monster/version'
|
2
|
+
require 'time' # always need more time! :)
|
3
|
+
require 'uri' # url encoding/decoding
|
4
|
+
|
5
|
+
class CookieMonster
|
6
|
+
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :value
|
9
|
+
attr_accessor :domain
|
10
|
+
attr_accessor :path
|
11
|
+
attr_accessor :expires
|
12
|
+
attr_accessor :httponly
|
13
|
+
attr_accessor :secure
|
14
|
+
|
15
|
+
# init - a raw cookie can be passed for parsing. a hash with attributes can be handed in as well.
|
16
|
+
#
|
17
|
+
def initialize(args=nil)
|
18
|
+
from_header(args) if args.is_a? String
|
19
|
+
if args.is_a? Hash
|
20
|
+
args.each do |k,v|
|
21
|
+
_set([k,v])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# helper method to mass asign attributes
|
28
|
+
#
|
29
|
+
def _set(kv)
|
30
|
+
case kv.first.to_sym
|
31
|
+
when :name
|
32
|
+
@name = kv.last
|
33
|
+
when :value
|
34
|
+
@value = kv.last
|
35
|
+
when :path
|
36
|
+
@path = kv.last
|
37
|
+
when :domain
|
38
|
+
@domain = kv.last
|
39
|
+
when :expires
|
40
|
+
@expires = Time.parse(kv.last.gsub('GMT', 'UTC'))
|
41
|
+
when :httponly
|
42
|
+
@httponly = true
|
43
|
+
when :secure
|
44
|
+
@secure = true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# parse a cookie header string
|
49
|
+
#
|
50
|
+
def from_header(header)
|
51
|
+
parse(header.gsub(/^set-cookie:\s*/i, '').gsub(/^cookie:\s*/i, ''))
|
52
|
+
end
|
53
|
+
|
54
|
+
# parse a raw cookie string
|
55
|
+
#
|
56
|
+
def parse(raw_cookie)
|
57
|
+
cookie = raw_cookie.split(/\;\s*/)
|
58
|
+
|
59
|
+
kv = cookie.shift
|
60
|
+
@name = URI.unescape(kv.split('=').first)
|
61
|
+
@value = URI.unescape(kv.split('=').last)
|
62
|
+
|
63
|
+
cookie.each do |kv|
|
64
|
+
_set(kv.split('='))
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
# convert the cookie to a string
|
70
|
+
#
|
71
|
+
def to_s
|
72
|
+
validate!
|
73
|
+
URI.escape(name.to_s) + '=' + (value ? URI.escape(value.to_s) : '') +
|
74
|
+
(domain ? '; domain='+domain : '') +
|
75
|
+
(path ? '; path='+path : '') +
|
76
|
+
(expires ? '; expires='+expires.to_time.utc.strftime('%a, %d-%b-%Y %T GMT') : '') +
|
77
|
+
(httponly ? '; httponly' : '') +
|
78
|
+
(secure ? '; secure' : '')
|
79
|
+
end
|
80
|
+
|
81
|
+
# convert the cookie to a HTTP response header
|
82
|
+
#
|
83
|
+
def to_header
|
84
|
+
validate!
|
85
|
+
"Set-Cookie: " + to_s
|
86
|
+
end
|
87
|
+
|
88
|
+
# sets the cookie expiry in seconds from now
|
89
|
+
#
|
90
|
+
def expires_in_seconds(secs)
|
91
|
+
@expires = Time.now.utc + secs;
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
# expires the cookie by settings its expire date to yesterday - browsers will then delete the cookie from their jar
|
96
|
+
#
|
97
|
+
def expire!
|
98
|
+
@expires = Time.now.utc - 86400;
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
# deletes the cookie by removing it's value - browsers will then delete the cookie from their jar
|
103
|
+
#
|
104
|
+
def delete!
|
105
|
+
@value = ''
|
106
|
+
self
|
107
|
+
end
|
108
|
+
|
109
|
+
# runs some simple validation against the cookie - throws exceptions!
|
110
|
+
#
|
111
|
+
def validate!
|
112
|
+
raise 'Cookie name must be defined' unless name
|
113
|
+
raise 'Cookie expires is not an instance of the Time class' if expires and ! expires.is_a? Time
|
114
|
+
end
|
115
|
+
|
116
|
+
# validates the cookie - returns true or false
|
117
|
+
#
|
118
|
+
def is_valid?
|
119
|
+
begin
|
120
|
+
validate!
|
121
|
+
true
|
122
|
+
rescue => ex
|
123
|
+
false
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_cookie_monster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc Jakobs
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: A simple helper Class to parse / generate HTTP Cookies
|
42
|
+
email:
|
43
|
+
- marc@moc.io
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- cookie_monster.gemspec
|
57
|
+
- lib/cookie_monster.rb
|
58
|
+
- lib/cookie_monster/version.rb
|
59
|
+
homepage: https://github.com/hirschnase/CookieMonster
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.14.1
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: A Ruby Class to work on HTTP Cookies
|
83
|
+
test_files: []
|