-
Notifications
You must be signed in to change notification settings - Fork 29
/
middleware.rb
136 lines (116 loc) · 3.46 KB
/
middleware.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'rack/request'
require 'builder'
module RackSessionAccess
class Middleware
# Initialize RackSessionAccess middleware
#
# @param app a rack application
# @param options
#
# Options:
# * :key - rack session key
def initialize(app, options = {})
@app = app
@key = options[:key] || 'rack.session'
@routing = [
[ 'GET', RackSessionAccess.path, :show ],
[ 'GET', RackSessionAccess.edit_path, :edit ],
[ 'PUT', RackSessionAccess.path, :update ]
]
end
def call(env)
return render(500) do |xml|
xml.h2("#{@key} env is not initialized")
end unless env[@key]
request = ::Rack::Request.new(env)
if action = dispatch_action(request)
send(action, request)
else
@app.call(env)
end
end
protected
# List session data
def show(request)
# call inspect because session can be lazy loaded
request.env[@key].inspect
render do |xml|
xml.h2 "Rack session data"
xml.ul do |xml|
request.env[@key].each do |k,v|
xml.li("#{k.inspect} : #{v.inspect}")
end
end
xml.p do |xml|
xml.a("Edit", :href => action_path(:edit))
end
end
end
# Render form for submit new session data
def edit(request)
render do |xml|
xml.h2 "Update rack session"
xml.p "Put marshalized and encoded with base64 ruby hash into the form"
xml.form({
:action => action_path(:update),
:method => 'post',
:enctype => 'application/x-www-form-urlencoded'
}) do |xml|
xml.input(:type => 'hidden', :name =>'_method', :value => 'put')
xml.textarea(:cols => 40, :rows => 10, :name => 'data') {}
xml.p do |xml|
xml.input(:type => 'submit', :value => "Update")
end
end
end
end
# Update session data
def update(request)
begin
data = request.params['data']
hash = RackSessionAccess.decode(data)
hash.each { |k, v| request.env[@key][k] = v }
rescue => e
return render(400) do |xml|
xml.h2("Bad data #{data.inspect}: #{e.message} ")
end
end
redirect_to action_path(:show)
end
private
# Dispatch action from request
def dispatch_action(request)
method = request_method(request)
path = request.path
route = @routing.detect { |r| r[0] == method && r[1] == path }
route[2] if route
end
# Return HTTP method, detect emulated method with _method param
def request_method(request)
return request.request_method if request.request_method != 'POST'
return request.params['_method'].upcase if request.params['_method']
request.request_method
end
# @return path for given action name
def action_path(action)
@routing.detect { |r| r[2] == action }[1]
end
# @return redirect response to specified url
def redirect_to(url)
render(302, {"Location" => url}) do |xml|
xml.a "You are being redirected", :href => url
end
end
# @return html response
def render(code = 200, headers = {})
headers["Content-Type"] ||= "text/html"
builder = Builder::XmlMarkup.new(:indent => 2)
builder.html do |xml|
xml.body do |xml|
yield xml
end
end
[ code, headers, [builder.target!] ]
end
end
end