uwa 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/uwa/config.rb +1 -1
- data/lib/uwa/handler.rb +52 -47
- metadata +2 -2
data/lib/uwa/config.rb
CHANGED
data/lib/uwa/handler.rb
CHANGED
@@ -20,53 +20,49 @@ class Handler < Mongrel::HttpHandler
|
|
20
20
|
@base = nil
|
21
21
|
@cache = {:css => nil, :script => nil}
|
22
22
|
@autoRefresh = nil
|
23
|
-
|
24
|
-
@query = nil
|
25
|
-
@out = nil
|
26
|
-
@response = nil
|
27
|
-
@request = nil
|
28
23
|
end
|
29
24
|
|
30
25
|
def process(req,res)
|
31
|
-
@request = req
|
32
|
-
@response = res
|
33
|
-
@tempfiles = []
|
34
26
|
begin
|
35
|
-
|
36
|
-
|
37
|
-
|
27
|
+
Thread.current[:uwa] = {
|
28
|
+
:request => req,
|
29
|
+
:response => res,
|
30
|
+
:tempfiles => [],
|
31
|
+
}
|
38
32
|
parse_query
|
39
|
-
UWA::Server.log(
|
33
|
+
UWA::Server.log(req.params['REQUEST_METHOD'], req.params['REQUEST_URI'])
|
34
|
+
method = req.params['PATH_INFO'].split('/')[1]
|
35
|
+
UWA::Server.log(:method, method.inspect)
|
36
|
+
UWA::Server.log(:query, query.inspect)
|
40
37
|
if method.nil?
|
41
38
|
index
|
42
39
|
else
|
43
40
|
ajax(method.to_sym)
|
44
41
|
end
|
45
42
|
ensure
|
46
|
-
|
43
|
+
tempfiles.each { |f| f.unlink }
|
47
44
|
end
|
45
|
+
GC.start # Force garbage collect
|
48
46
|
end
|
49
47
|
|
50
48
|
protected
|
51
49
|
|
52
|
-
def
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
def send_file(file, content_type = 'application/octet-stream')
|
50
|
+
def send_file(file, content_type)
|
51
|
+
content_type ||= 'application/octet-stream'
|
57
52
|
if File.exists?(file)
|
58
53
|
file_status = File.stat(file)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
54
|
+
response.status = 200
|
55
|
+
response.header['Last-Modified'] = file_status.mtime.httpdate
|
56
|
+
response.header['ETag'] = '%x-%x-%x' % [file_status.mtime.to_i, file_status.size, file_status.ino]
|
57
|
+
response.header['Content-Type'] = content_type unless content_type.nil?
|
58
|
+
response.header['Content-Disposition'] = "attachment; filename=\"#{File.basename(file)}\""
|
59
|
+
# response.header['Cache-Control'] = 'no-cache, must-revalidate';
|
60
|
+
# response.header['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
|
61
|
+
response.send_status(file_status.size)
|
62
|
+
response.send_header
|
63
|
+
response.send_file(file)
|
68
64
|
else
|
69
|
-
|
65
|
+
response.start(404) do |head, out|
|
70
66
|
out.write("File #{file} not found")
|
71
67
|
end
|
72
68
|
end
|
@@ -90,6 +86,14 @@ class Handler < Mongrel::HttpHandler
|
|
90
86
|
xml << 'Loading...'
|
91
87
|
end
|
92
88
|
|
89
|
+
# Helper methods
|
90
|
+
|
91
|
+
def <<(buf); Thread.current[:uwa][:response].body.write(buf); end
|
92
|
+
def request; Thread.current[:uwa][:request]; end
|
93
|
+
def response; Thread.current[:uwa][:response]; end
|
94
|
+
def query; Thread.current[:uwa][:query]; end
|
95
|
+
def tempfiles; Thread.current[:uwa][:tempfiles]; end
|
96
|
+
|
93
97
|
private
|
94
98
|
|
95
99
|
MULTIPART_REGEXP = /\Amultipart\/form-data.*boundary=\"?([^\";,]+)/n.freeze
|
@@ -101,15 +105,15 @@ class Handler < Mongrel::HttpHandler
|
|
101
105
|
|
102
106
|
# Inspired bu controller_mixin.rb from the Merb project
|
103
107
|
def parse_query
|
104
|
-
|
105
|
-
if
|
108
|
+
Thread.current[:uwa][:query] = Mongrel::HttpRequest.query_parse(request.params['QUERY_STRING'])
|
109
|
+
if request.params['REQUEST_METHOD'] =~ /post/i and request.params['CONTENT_TYPE'] =~ MULTIPART_REGEXP
|
106
110
|
boundary = "--#{$1}"
|
107
111
|
buf = ""
|
108
|
-
content_length =
|
112
|
+
content_length = request.params['CONTENT_LENGTH'].to_i
|
109
113
|
boundary_size = boundary.size + EOL.size
|
110
114
|
bufsize = 16384
|
111
115
|
content_length -= boundary_size
|
112
|
-
status =
|
116
|
+
status = request.body.read(boundary_size)
|
113
117
|
raise EOFError, "bad content body" unless status == boundary + EOL
|
114
118
|
rx = /(?:#{EOL})?#{Regexp.quote(boundary)}(#{EOL}|--)/
|
115
119
|
|
@@ -139,7 +143,7 @@ class Handler < Mongrel::HttpHandler
|
|
139
143
|
body << buf.slice!(0, buf.size - (boundary_size+4))
|
140
144
|
end
|
141
145
|
|
142
|
-
c =
|
146
|
+
c = request.body.read(bufsize < content_length ? bufsize : content_length)
|
143
147
|
raise EOFError, "bad content body" if c.nil? || c.empty?
|
144
148
|
buf << c
|
145
149
|
content_length -= c.size
|
@@ -156,17 +160,17 @@ class Handler < Mongrel::HttpHandler
|
|
156
160
|
if filename
|
157
161
|
body.rewind
|
158
162
|
data = {'filename' => filename, 'type' => content_type, 'tempfile' => body}
|
159
|
-
|
163
|
+
tempfiles << body
|
160
164
|
else
|
161
165
|
data = body
|
162
166
|
end
|
163
|
-
if
|
164
|
-
|
167
|
+
if query[name].nil?
|
168
|
+
query[name] = data
|
165
169
|
else
|
166
|
-
if not
|
167
|
-
|
170
|
+
if not query[name].is_a? Array
|
171
|
+
query[name] = [query[name]]
|
168
172
|
end
|
169
|
-
|
173
|
+
query[name] << data
|
170
174
|
end
|
171
175
|
break if buf.empty? || content_length == -1
|
172
176
|
end
|
@@ -174,7 +178,7 @@ class Handler < Mongrel::HttpHandler
|
|
174
178
|
end
|
175
179
|
|
176
180
|
def index
|
177
|
-
|
181
|
+
response.start(200) do |head, out|
|
178
182
|
head["Content-Type"] = "text/html"
|
179
183
|
xml = Builder::XmlMarkup.new(:indent => 4, :target => out)
|
180
184
|
xml.instruct!
|
@@ -204,7 +208,7 @@ class Handler < Mongrel::HttpHandler
|
|
204
208
|
xml.link(:rel => :icon, :type => 'image/png', :href => @icon)
|
205
209
|
css.each { |c| xml.style("\n" + c, :type => 'text/css') }
|
206
210
|
xml.script(:type => 'text/javascript') do |xml|
|
207
|
-
url = 'http://' +
|
211
|
+
url = 'http://' + request.params['HTTP_HOST'] + request.params['SCRIPT_NAME']
|
208
212
|
xml << "widget.remoteUrl = '#{url}/'\n"
|
209
213
|
['Text', 'Json', 'Xml', 'Feed'].each do |m|
|
210
214
|
xml << "widget.remote#{m} = function(uri, cb) {
|
@@ -230,21 +234,22 @@ class Handler < Mongrel::HttpHandler
|
|
230
234
|
|
231
235
|
def ajax(method)
|
232
236
|
if method == :resource or method == :resources
|
233
|
-
file =
|
237
|
+
file = request.params['PATH_INFO'].gsub(/\.\./, '').split('/')[2..-1].join($/)
|
234
238
|
file = File.join(@base, 'resources', file)
|
235
|
-
send_file(file)
|
239
|
+
send_file(file, nil)
|
236
240
|
elsif self.respond_to?(method)
|
237
241
|
begin
|
238
|
-
|
239
|
-
|
242
|
+
response.header["Content-Type"] = "text/html"
|
243
|
+
response.status = 200
|
240
244
|
self.send(method)
|
241
|
-
|
245
|
+
response.finished
|
242
246
|
rescue Exception
|
243
247
|
UWA::Server.log(:error, "#{$!.message} <#{$!.class.name}>")
|
244
248
|
UWA::Server.log(:error, $!.backtrace)
|
245
249
|
end
|
246
250
|
else
|
247
|
-
|
251
|
+
UWA::Server.log(:warning, "Method #{method.inspect} does not exists")
|
252
|
+
response.start(404) do |head, out|
|
248
253
|
out.write("Method #{method.inspect} not found")
|
249
254
|
end
|
250
255
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: uwa
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "0.
|
7
|
-
date: 2007-04-
|
6
|
+
version: "0.7"
|
7
|
+
date: 2007-04-12 00:00:00 +02:00
|
8
8
|
summary: Widget server for UWA specs by Netvibes (http://dev.netvibes.com/doc/universal_widget_api)
|
9
9
|
require_paths:
|
10
10
|
- lib
|