uwa 0.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/uwa/config.rb +1 -1
  2. data/lib/uwa/handler.rb +52 -47
  3. metadata +2 -2
@@ -1,6 +1,6 @@
1
1
  module UWA
2
2
  NAME = 'UWA'
3
- VERSION = '0.6'
3
+ VERSION = '0.7'
4
4
  DESC = 'Widget server for UWA specs by Netvibes (http://dev.netvibes.com/doc/universal_widget_api)'
5
5
  AUTHOR = 'Florent Solt'
6
6
  EMAIL = 'florent@solt.biz'
@@ -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
- UWA::Server.log(@request.params['REQUEST_METHOD'], @request.params['REQUEST_URI'])
36
- method = @request.params['PATH_INFO'].split('/')[1]
37
- UWA::Server.log(:method, method.inspect)
27
+ Thread.current[:uwa] = {
28
+ :request => req,
29
+ :response => res,
30
+ :tempfiles => [],
31
+ }
38
32
  parse_query
39
- UWA::Server.log(:query, @query.inspect)
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
- @tempfiles.each { |f| f.unlink }
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 <<(buf)
53
- @response.body.write(buf)
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
- @response.status = 200
60
- @response.header[Mongrel::Const::LAST_MODIFIED] = file_status.mtime.httpdate
61
- @response.header[Mongrel::Const::ETAG] = Mongrel::Const::ETAG_FORMAT %
62
- [file_status.mtime.to_i, file_status.size, file_status.ino]
63
- @response.header[Mongrel::Const::CONTENT_TYPE] = content_type unless content_type.nil?
64
- @response.header['Content-disposition'] = "attachment; filename=\"#{File.basename(file)}\""
65
- @response.send_status(file_status.size)
66
- @response.send_header
67
- @response.send_file(file)
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
- @response.start(404) do |head, out|
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
- @query = Mongrel::HttpRequest.query_parse(@request.params['QUERY_STRING'])
105
- if @request.params['REQUEST_METHOD'] =~ /post/i and @request.params['CONTENT_TYPE'] =~ MULTIPART_REGEXP
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 = @request.params['CONTENT_LENGTH'].to_i
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 = @request.body.read(boundary_size)
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 = @request.body.read(bufsize < content_length ? bufsize : content_length)
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
- @tempfiles << body
163
+ tempfiles << body
160
164
  else
161
165
  data = body
162
166
  end
163
- if @query[name].nil?
164
- @query[name] = data
167
+ if query[name].nil?
168
+ query[name] = data
165
169
  else
166
- if not @query[name].is_a? Array
167
- @query[name] = [@query[name]]
170
+ if not query[name].is_a? Array
171
+ query[name] = [query[name]]
168
172
  end
169
- @query[name] << data
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
- @response.start(200) do |head, out|
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://' + @request.params['HTTP_HOST'] + @request.params['SCRIPT_NAME']
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 = @request.params['PATH_INFO'].gsub(/\.\./, '').split('/')[2..-1].join($/)
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
- @response.header["Content-Type"] = "text/html"
239
- @response.status = 200
242
+ response.header["Content-Type"] = "text/html"
243
+ response.status = 200
240
244
  self.send(method)
241
- @response.finished
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
- @response.start(404) do |head, out|
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.6"
7
- date: 2007-04-04 00:00:00 +02:00
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