php_session 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -12
- data/lib/php_session.rb +23 -35
- data/lib/php_session/version.rb +1 -1
- data/spec/php_session_spec.rb +20 -67
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16cf738d413f25164e6bee9698592671dd1cc6af
|
4
|
+
data.tar.gz: f32cf9dd832e3c66f1c7755ae0c2581c890a5d2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b93e0e6d6a2e334e4a3bc0ed1e1054c0486021f4bf2bf761db49c8685cf2b0030dca809e4e76602d4717e4708f1c60b830ef7446b65a4345e17510223335f5d6
|
7
|
+
data.tar.gz: 6fbe2cc80a4ea1977b6dae82f5a1d68ad2516f15439b6d82a97f2b8c46e8824c614ff01ce0922dd61d692630db5b14977419e246370adc023da2da9ccb594a5f
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# PHPSession
|
2
2
|
[](https://travis-ci.org/Shinpeim/ruby_php_session)
|
3
3
|
[](https://codeclimate.com/github/Shinpeim/ruby_php_session)
|
4
|
+
|
4
5
|
## Description
|
5
6
|
PHPSession is a php session file reader/writer. Multibyte string and exclusive control are supported.
|
6
7
|
|
@@ -72,21 +73,16 @@ Or install it yourself as:
|
|
72
73
|
# :encoding_option => {}
|
73
74
|
session = PHPSession.new(session_file_dir, option)
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
data = session.load(session_id)
|
76
|
+
# load session data from file with lock
|
77
|
+
data = session.load(session_id)
|
78
78
|
|
79
|
-
|
79
|
+
data.is_a? Hash # => true
|
80
80
|
|
81
|
-
|
82
|
-
|
81
|
+
# save session to a file with lock
|
82
|
+
session.commit(session_id, data)
|
83
83
|
|
84
|
-
|
85
|
-
|
86
|
-
ensure
|
87
|
-
# please ensure that the lock is released and the file is closed.
|
88
|
-
session.ensure_file_closed
|
89
|
-
end
|
84
|
+
# delete session
|
85
|
+
session.destroy(session_id)
|
90
86
|
|
91
87
|
## Contributing
|
92
88
|
|
data/lib/php_session.rb
CHANGED
@@ -14,60 +14,48 @@ class PHPSession
|
|
14
14
|
}
|
15
15
|
@option = default_option.merge(option)
|
16
16
|
@session_dir = File.expand_path(session_dir)
|
17
|
-
|
18
|
-
@file = nil
|
19
17
|
end
|
20
18
|
|
21
19
|
def load(session_id)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
raise PHPSession::Errors, "can't obtain lock of session file"
|
20
|
+
with_lock(file_path(session_id)) do |f|
|
21
|
+
# set internal_encoding to nil to avoid encoding conversion
|
22
|
+
f.set_encoding(@option[:external_encoding], nil)
|
23
|
+
Decoder.decode(f.read, @option[:internal_encoding], @option[:encoding_option]) || {}
|
27
24
|
end
|
28
|
-
|
29
|
-
# set internal_encoding to nil to avoid encoding conversion
|
30
|
-
@file.set_encoding(@option[:external_encoding], nil)
|
31
|
-
data = Decoder.decode(@file.read, @option[:internal_encoding], @option[:encoding_option]) || {}
|
32
|
-
@file.rewind
|
33
|
-
data
|
34
25
|
end
|
35
26
|
|
36
|
-
def
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def destroy
|
41
|
-
if @file && ! @file.closed?
|
42
|
-
@file.truncate(0)
|
43
|
-
end
|
44
|
-
ensure_file_closed
|
45
|
-
File.delete(file_path)
|
27
|
+
def destroy(session_id)
|
28
|
+
File.delete(file_path(session_id))
|
46
29
|
rescue Errno::ENOENT => e
|
47
30
|
# file already deleted
|
48
31
|
end
|
49
32
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
ensure_file_closed
|
55
|
-
end
|
56
|
-
|
57
|
-
def ensure_file_closed
|
58
|
-
if @file && ! @file.closed?
|
59
|
-
@file.close
|
33
|
+
def save(session_id, data)
|
34
|
+
with_lock(file_path(session_id)) do |f|
|
35
|
+
f.truncate(0)
|
36
|
+
f.write(Encoder.encode(data, @option[:external_encoding], @option[:encoding_option]))
|
60
37
|
end
|
61
38
|
end
|
62
39
|
|
63
40
|
private
|
64
41
|
|
42
|
+
def with_lock(file_path)
|
43
|
+
File.open(file_path, File::CREAT|File::RDWR) do |f|
|
44
|
+
unless f.flock(File::LOCK_EX)
|
45
|
+
raise PHPSession::Errors, "can't obtain lock of session file"
|
46
|
+
end
|
47
|
+
yield(f)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
65
51
|
def set_session_id(session_id)
|
66
52
|
@session_id = session_id
|
67
53
|
raise Errors::SecurityError, "directory traversal detected" unless file_path.index(@session_dir) == 0
|
68
54
|
end
|
69
55
|
|
70
|
-
def file_path
|
71
|
-
File.expand_path(File.join(@session_dir, "sess_#{
|
56
|
+
def file_path(session_id)
|
57
|
+
path = File.expand_path(File.join(@session_dir, "sess_#{session_id}"))
|
58
|
+
raise Errors::SecurityError, "directory traversal detected" unless path.index(@session_dir) == 0
|
59
|
+
path
|
72
60
|
end
|
73
61
|
end
|
data/lib/php_session/version.rb
CHANGED
data/spec/php_session_spec.rb
CHANGED
@@ -2,79 +2,48 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe PHPSession do
|
5
|
-
describe "loaded?" do
|
6
|
-
before do
|
7
|
-
@session_file = create_dummy_session_file('key|s:1:"a";')
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should return true when file is loaded" do
|
11
|
-
session = PHPSession.new(@session_file[:dir_name])
|
12
|
-
session.load(@session_file[:session_id])
|
13
|
-
expect(session.loaded?).to be_true
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should return false when file is loaded" do
|
17
|
-
session = PHPSession.new(@session_file[:dir_name])
|
18
|
-
expect(session.loaded?).to be_false
|
19
|
-
end
|
20
|
-
|
21
|
-
after do
|
22
|
-
File.delete(@session_file[:file_path])
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
5
|
describe "load" do
|
27
|
-
|
6
|
+
context "when session file encoding is utf8" do
|
28
7
|
before do
|
29
8
|
@session_file = create_dummy_session_file('key|s:13:"テスト🍺";')
|
30
9
|
end
|
10
|
+
|
31
11
|
it "should be able to load file with internal:nil, external:utf8" do
|
32
12
|
option = {
|
33
13
|
:internal_encoding => nil,
|
34
14
|
:external_encoding => "UTF-8",
|
35
15
|
}
|
36
16
|
session = PHPSession.new(@session_file[:dir_name], option)
|
37
|
-
|
38
|
-
|
39
|
-
expect(data).to eq({"key" => "テスト🍺"})
|
40
|
-
ensure
|
41
|
-
session.ensure_file_closed
|
42
|
-
end
|
17
|
+
data = session.load(@session_file[:session_id])
|
18
|
+
expect(data).to eq({"key" => "テスト🍺"})
|
43
19
|
end
|
20
|
+
|
44
21
|
it "should be able to load file with internal:utf8, external:utf8" do
|
45
22
|
option = {
|
46
23
|
:internal_encoding => "UTF-8",
|
47
24
|
:external_encoding => "UTF-8",
|
48
25
|
}
|
49
26
|
session = PHPSession.new(@session_file[:dir_name], option)
|
50
|
-
|
51
|
-
|
52
|
-
expect(data).to eq({"key" => "テスト🍺"})
|
53
|
-
ensure
|
54
|
-
session.ensure_file_closed
|
55
|
-
end
|
27
|
+
data = session.load(@session_file[:session_id])
|
28
|
+
expect(data).to eq({"key" => "テスト🍺"})
|
56
29
|
end
|
30
|
+
|
57
31
|
it "should return euc-jp string with internal:euc-jp, exterenal:utf8" do
|
58
32
|
option = {
|
59
33
|
:internal_encoding => "EUC-JP",
|
60
34
|
:external_encoding => "UTF-8",
|
61
|
-
:encoding_option => {
|
62
|
-
:undef => :replace
|
63
|
-
}
|
35
|
+
:encoding_option => {:undef => :replace}
|
64
36
|
}
|
65
37
|
session = PHPSession.new(@session_file[:dir_name], option)
|
66
|
-
|
67
|
-
|
68
|
-
expect(data).to eq({"key" => "テスト🍺".encode("EUC-JP", {:undef => :replace})})
|
69
|
-
ensure
|
70
|
-
session.ensure_file_closed
|
71
|
-
end
|
38
|
+
data = session.load(@session_file[:session_id])
|
39
|
+
expect(data).to eq({"key" => "テスト🍺".encode("EUC-JP", {:undef => :replace})})
|
72
40
|
end
|
73
41
|
|
74
42
|
after do
|
75
43
|
File.delete(@session_file[:file_path])
|
76
44
|
end
|
77
45
|
end
|
46
|
+
|
78
47
|
context "when session file exists" do
|
79
48
|
before do
|
80
49
|
@session_file = create_dummy_session_file('key|s:1:"a";')
|
@@ -82,12 +51,8 @@ describe PHPSession do
|
|
82
51
|
|
83
52
|
it "should return session data" do
|
84
53
|
session = PHPSession.new(@session_file[:dir_name])
|
85
|
-
|
86
|
-
|
87
|
-
expect(data).to eq({"key" => "a"})
|
88
|
-
ensure
|
89
|
-
session.ensure_file_closed
|
90
|
-
end
|
54
|
+
data = session.load(@session_file[:session_id])
|
55
|
+
expect(data).to eq({"key" => "a"})
|
91
56
|
end
|
92
57
|
|
93
58
|
after do
|
@@ -98,17 +63,13 @@ describe PHPSession do
|
|
98
63
|
context "when session file dosen't exist" do
|
99
64
|
it "should return new session data" do
|
100
65
|
session = PHPSession.new(Dir.tmpdir)
|
101
|
-
|
102
|
-
|
103
|
-
expect(data).to eq({})
|
104
|
-
ensure
|
105
|
-
session.ensure_file_closed
|
106
|
-
end
|
66
|
+
data = session.load("session_id")
|
67
|
+
expect(data).to eq({})
|
107
68
|
end
|
108
69
|
end
|
109
70
|
end
|
110
71
|
|
111
|
-
describe "#
|
72
|
+
describe "#save" do
|
112
73
|
before do
|
113
74
|
@session_file = create_dummy_session_file('key|s:1:"a";')
|
114
75
|
end
|
@@ -122,20 +83,13 @@ describe PHPSession do
|
|
122
83
|
session = PHPSession.new(@session_file[:dir_name], option)
|
123
84
|
data = session.load(@session_file[:session_id])
|
124
85
|
data["key"] = "テスト🍣"
|
125
|
-
session.
|
86
|
+
session.save(@session_file[:session_id], data)
|
126
87
|
|
127
88
|
# read in bytesequence mode to avoid encoding conversion
|
128
89
|
byte_sequence = IO.read(@session_file[:file_path], File.size(@session_file[:file_path]))
|
129
90
|
expect(byte_sequence.force_encoding('EUC-JP')).to eq('key|s:7:"テスト?";'.encode("EUC-JP"))
|
130
91
|
end
|
131
92
|
|
132
|
-
it "should save session data even if session_file is not loaded" do
|
133
|
-
session = PHPSession.new(@session_file[:dir_name])
|
134
|
-
expect {
|
135
|
-
session.commit({:hoge => "nyan"})
|
136
|
-
}.not_to raise_error
|
137
|
-
end
|
138
|
-
|
139
93
|
after do
|
140
94
|
File.delete(@session_file[:file_path])
|
141
95
|
end
|
@@ -145,12 +99,11 @@ describe PHPSession do
|
|
145
99
|
context "when session file exists and loaded" do
|
146
100
|
before do
|
147
101
|
@session_file = create_dummy_session_file('key|s:1:"a";')
|
148
|
-
@session = PHPSession.new(@session_file[:dir_name])
|
149
|
-
@session.load(@session_file[:session_id])
|
150
102
|
end
|
151
103
|
|
152
104
|
it "should delete session file" do
|
153
|
-
|
105
|
+
session = PHPSession.new(@session_file[:dir_name])
|
106
|
+
session.destroy(@session_file[:session_id])
|
154
107
|
expect(File.exists?(@session_file[:file_path])).to eq(false)
|
155
108
|
end
|
156
109
|
end
|