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
|
# File 'lib/esi_for_rack.rb', line 32
def call(env)
@lookup ||= [Lookup::PassThrough.new(@app, env), Lookup::Http.new(@app, env)]
request = Rack::Request.new(env)
result = @app.call(env)
response = Rack::Response.new(result[2], result[0], result[1])
if response['Content-Type'] =~ /text\/html/ && (body = EsiForRack.response_body_to_str(result.last)) && body.index('<esi:')
user_agent_hash = {}
begin
user_agent = SOC::SpyVsSpy.new(env['HTTP_USER_AGENT'] || '-')
user_agent_hash['browser'] = case user_agent.browser
when 'Firefox' then 'MOZILLA'
when 'MSIE' then 'MSIE'
else; 'OTHER'
end
user_agent_hash['version'] = [user_agent.version.major, user_agent.version.minor].compact.join('.')
user_agent_hash['os'] = if user_agent.os.windows?
'WIN'
elsif user_agent.os.osx?
'MAC'
else
'OTHER'
end
rescue
end
binding = {
:HTTP_ACCEPT_LANGUAGE => Set.new((env['HTTP_ACCEPT_LANGUAGE'] || '').split(',').map{|l| l.gsub(/q=[0-9]\.[0-9]{1,3}/, '').gsub(';','').strip}),
:HTTP_COOKIE => request.cookies,
:HTTP_HOST => request.host,
:HTTP_REFERER => request.referer,
:HTTP_USER_AGENT => user_agent_hash,
:QUERY_STRING => request.GET
}
context = Node::Context.new(binding, @lookup)
parsed_body = context.parse(body).to_s
response.['Content-Length'] = parsed_body.size.to_s
[response.status, response., [parsed_body]]
else
result
end
end
|