bbrowning-deltacloud-core 0.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/COPYING +502 -0
  2. data/Rakefile +85 -0
  3. data/bin/deltacloudd +88 -0
  4. data/config.ru +5 -0
  5. data/deltacloud.rb +14 -0
  6. data/lib/deltacloud/base_driver.rb +19 -0
  7. data/lib/deltacloud/base_driver/base_driver.rb +189 -0
  8. data/lib/deltacloud/base_driver/features.rb +159 -0
  9. data/lib/deltacloud/base_driver/mock_driver.rb +37 -0
  10. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +340 -0
  11. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +170 -0
  12. data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +45 -0
  13. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +322 -0
  14. data/lib/deltacloud/drivers/mock/mock_driver.rb +275 -0
  15. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +116 -0
  16. data/lib/deltacloud/drivers/opennebula/occi_client.rb +204 -0
  17. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +241 -0
  18. data/lib/deltacloud/drivers/rackspace/rackspace_client.rb +129 -0
  19. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +150 -0
  20. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +254 -0
  21. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +84 -0
  22. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +144 -0
  23. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +261 -0
  24. data/lib/deltacloud/hardware_profile.rb +153 -0
  25. data/lib/deltacloud/helpers.rb +5 -0
  26. data/lib/deltacloud/helpers/application_helper.rb +56 -0
  27. data/lib/deltacloud/helpers/conversion_helper.rb +38 -0
  28. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +35 -0
  29. data/lib/deltacloud/method_serializer.rb +84 -0
  30. data/lib/deltacloud/models/base_model.rb +58 -0
  31. data/lib/deltacloud/models/image.rb +26 -0
  32. data/lib/deltacloud/models/instance.rb +37 -0
  33. data/lib/deltacloud/models/instance_profile.rb +47 -0
  34. data/lib/deltacloud/models/realm.rb +25 -0
  35. data/lib/deltacloud/models/storage_snapshot.rb +26 -0
  36. data/lib/deltacloud/models/storage_volume.rb +27 -0
  37. data/lib/deltacloud/state_machine.rb +84 -0
  38. data/lib/deltacloud/validation.rb +70 -0
  39. data/lib/drivers.rb +38 -0
  40. data/lib/sinatra/accept_media_types.rb +128 -0
  41. data/lib/sinatra/lazy_auth.rb +56 -0
  42. data/lib/sinatra/rabbit.rb +272 -0
  43. data/lib/sinatra/respond_to.rb +269 -0
  44. data/lib/sinatra/static_assets.rb +83 -0
  45. data/lib/sinatra/url_for.rb +51 -0
  46. data/public/favicon.ico +0 -0
  47. data/public/images/grid.png +0 -0
  48. data/public/images/logo-wide.png +0 -0
  49. data/public/images/rails.png +0 -0
  50. data/public/images/topbar-bg.png +0 -0
  51. data/public/javascripts/application.js +32 -0
  52. data/public/javascripts/jquery-1.4.2.min.js +154 -0
  53. data/public/stylesheets/compiled/application.css +613 -0
  54. data/public/stylesheets/compiled/ie.css +31 -0
  55. data/public/stylesheets/compiled/print.css +27 -0
  56. data/public/stylesheets/compiled/screen.css +456 -0
  57. data/server.rb +342 -0
  58. data/support/fedora/deltacloudd +68 -0
  59. data/support/fedora/rubygem-deltacloud-core.spec +91 -0
  60. data/tests/deltacloud_test.rb +60 -0
  61. data/tests/images_test.rb +94 -0
  62. data/tests/instances_test.rb +136 -0
  63. data/tests/realms_test.rb +56 -0
  64. data/tests/storage_snapshots_test.rb +48 -0
  65. data/tests/storage_volumes_test.rb +48 -0
  66. data/torquebox-ec2-config.ru +8 -0
  67. data/views/accounts/index.html.haml +11 -0
  68. data/views/accounts/show.html.haml +30 -0
  69. data/views/api/show.html.haml +15 -0
  70. data/views/api/show.xml.haml +5 -0
  71. data/views/docs/collection.html.haml +37 -0
  72. data/views/docs/collection.xml.haml +14 -0
  73. data/views/docs/index.html.haml +15 -0
  74. data/views/docs/index.xml.haml +5 -0
  75. data/views/docs/operation.html.haml +31 -0
  76. data/views/docs/operation.xml.haml +10 -0
  77. data/views/errors/auth_exception.html.haml +8 -0
  78. data/views/errors/auth_exception.xml.haml +2 -0
  79. data/views/errors/backend_error.html.haml +17 -0
  80. data/views/errors/backend_error.xml.haml +8 -0
  81. data/views/errors/validation_failure.html.haml +11 -0
  82. data/views/errors/validation_failure.xml.haml +7 -0
  83. data/views/hardware_profiles/index.html.haml +25 -0
  84. data/views/hardware_profiles/index.xml.haml +4 -0
  85. data/views/hardware_profiles/show.html.haml +19 -0
  86. data/views/hardware_profiles/show.xml.haml +17 -0
  87. data/views/images/index.html.haml +30 -0
  88. data/views/images/index.xml.haml +7 -0
  89. data/views/images/show.html.haml +21 -0
  90. data/views/images/show.xml.haml +5 -0
  91. data/views/instance_states/show.gv.erb +45 -0
  92. data/views/instance_states/show.html.haml +31 -0
  93. data/views/instance_states/show.xml.haml +8 -0
  94. data/views/instances/index.html.haml +30 -0
  95. data/views/instances/index.xml.haml +23 -0
  96. data/views/instances/new.html.haml +55 -0
  97. data/views/instances/show.html.haml +43 -0
  98. data/views/instances/show.xml.haml +41 -0
  99. data/views/layout.html.haml +26 -0
  100. data/views/realms/index.html.haml +29 -0
  101. data/views/realms/index.xml.haml +12 -0
  102. data/views/realms/show.html.haml +15 -0
  103. data/views/realms/show.xml.haml +10 -0
  104. data/views/root/index.html.haml +4 -0
  105. data/views/storage_snapshots/index.html.haml +20 -0
  106. data/views/storage_snapshots/index.xml.haml +11 -0
  107. data/views/storage_snapshots/show.html.haml +14 -0
  108. data/views/storage_snapshots/show.xml.haml +9 -0
  109. data/views/storage_volumes/index.html.haml +21 -0
  110. data/views/storage_volumes/index.xml.haml +13 -0
  111. data/views/storage_volumes/show.html.haml +20 -0
  112. data/views/storage_volumes/show.xml.haml +13 -0
  113. metadata +361 -0
@@ -0,0 +1,56 @@
1
+ require 'sinatra/base'
2
+
3
+ # Lazy Basic HTTP authentication. Authentication is only forced when the
4
+ # credentials are actually needed.
5
+ module Sinatra
6
+ module LazyAuth
7
+ class LazyCredentials
8
+ def initialize(app)
9
+ @app = app
10
+ @provided = false
11
+ end
12
+
13
+ def user
14
+ credentials!
15
+ @user
16
+ end
17
+
18
+ def password
19
+ credentials!
20
+ @password
21
+ end
22
+
23
+ def provided?
24
+ @provided
25
+ end
26
+
27
+ private
28
+ def credentials!
29
+ unless provided?
30
+ auth = Rack::Auth::Basic::Request.new(@app.request.env)
31
+ unless auth.provided? && auth.basic? && auth.credentials
32
+ @app.authorize!
33
+ end
34
+ @user = auth.credentials[0]
35
+ @password = auth.credentials[1]
36
+ @provided = true
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ def authorize!
43
+ r = "#{DRIVER}-deltacloud@#{HOSTNAME}"
44
+ response['WWW-Authenticate'] = %(Basic realm="#{r}")
45
+ throw(:halt, [401, "Not authorized\n"])
46
+ end
47
+
48
+ # Request the current user's credentials. Actual credentials are only
49
+ # requested when an attempt is made to get the user name or password
50
+ def credentials
51
+ LazyCredentials.new(self)
52
+ end
53
+ end
54
+
55
+ helpers LazyAuth
56
+ end
@@ -0,0 +1,272 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/url_for'
3
+ require 'deltacloud/validation'
4
+
5
+ module Sinatra
6
+
7
+ module Rabbit
8
+
9
+ class DuplicateParamException < Exception; end
10
+ class DuplicateOperationException < Exception; end
11
+ class DuplicateCollectionException < Exception; end
12
+
13
+ class Operation
14
+ attr_reader :name, :method
15
+
16
+ include ::Deltacloud::Validation
17
+
18
+ STANDARD = {
19
+ :index => { :method => :get, :member => false },
20
+ :show => { :method => :get, :member => true },
21
+ :create => { :method => :post, :member => false },
22
+ :update => { :method => :put, :member => true },
23
+ :destroy => { :method => :delete, :member => true }
24
+ }
25
+
26
+ def initialize(coll, name, opts, &block)
27
+ @name = name.to_sym
28
+ opts = STANDARD[@name].merge(opts) if standard?
29
+ @collection = coll
30
+ raise "No method for operation #{name}" unless opts[:method]
31
+ @method = opts[:method].to_sym
32
+ @member = opts[:member]
33
+ @description = ""
34
+ instance_eval(&block) if block_given?
35
+ generate_documentation
36
+ end
37
+
38
+ def standard?
39
+ STANDARD.keys.include?(name)
40
+ end
41
+
42
+ def description(text="")
43
+ return @description if text.blank?
44
+ @description = text
45
+ end
46
+
47
+ def generate_documentation
48
+ coll, oper = @collection, self
49
+ ::Sinatra::Application.get("/api/docs/#{@collection.name}/#{@name}") do
50
+ @collection, @operation = coll, oper
51
+ respond_to do |format|
52
+ format.html { haml :'docs/operation' }
53
+ format.xml { haml :'docs/operation' }
54
+ end
55
+ end
56
+ end
57
+
58
+ def control(&block)
59
+ op = self
60
+ @control = Proc.new do
61
+ op.validate(params)
62
+ instance_eval(&block)
63
+ end
64
+ end
65
+
66
+ def prefix
67
+ # FIXME: Make the /api prefix configurable
68
+ "/api"
69
+ end
70
+
71
+ def path(args = {})
72
+ l_prefix = args[:prefix] ? args[:prefix] : prefix
73
+ if @member
74
+ if standard?
75
+ "#{l_prefix}/#{@collection.name}/:id"
76
+ else
77
+ "#{l_prefix}/#{@collection.name}/:id/#{name}"
78
+ end
79
+ else
80
+ "#{l_prefix}/#{@collection.name}"
81
+ end
82
+ end
83
+
84
+ def generate
85
+ ::Sinatra::Application.send(@method, path, {}, &@control)
86
+ # Set up some Rails-like URL helpers
87
+ if name == :index
88
+ gen_route "#{@collection.name}_url"
89
+ elsif name == :show
90
+ gen_route "#{@collection.name.to_s.singularize}_url"
91
+ else
92
+ gen_route "#{name}_#{@collection.name.to_s.singularize}_url"
93
+ end
94
+ end
95
+
96
+ private
97
+ def gen_route(name)
98
+ route_url = path
99
+ if @member
100
+ ::Sinatra::Application.send(:define_method, name) do |id, *args|
101
+ url = query_url(route_url, args[0])
102
+ url_for url.gsub(/:id/, id.to_s), :full
103
+ end
104
+ else
105
+ ::Sinatra::Application.send(:define_method, name) do |*args|
106
+ url = query_url(route_url, args[0])
107
+ url_for url, :full
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ class Collection
114
+ attr_reader :name, :operations
115
+
116
+ def initialize(name, &block)
117
+ @name = name
118
+ @description = ""
119
+ @operations = {}
120
+ instance_eval(&block) if block_given?
121
+ generate_documentation
122
+ end
123
+
124
+ # Set/Return description for collection
125
+ # If first parameter is not present, full description will be
126
+ # returned.
127
+ def description(text='')
128
+ return @description if text.blank?
129
+ @description = text
130
+ end
131
+
132
+ def generate_documentation
133
+ coll, oper, features = self, @operations, driver.features(name)
134
+ ::Sinatra::Application.get("/api/docs/#{@name}") do
135
+ @collection, @operations, @features = coll, oper, features
136
+ respond_to do |format|
137
+ format.html { haml :'docs/collection' }
138
+ format.xml { haml :'docs/collection' }
139
+ end
140
+ end
141
+ end
142
+
143
+ # Add a new operation for this collection. For the standard REST
144
+ # operations :index, :show, :update, and :destroy, we already know
145
+ # what method to use and whether this is an operation on the URL for
146
+ # individual elements or for the whole collection.
147
+ #
148
+ # For non-standard operations, options must be passed:
149
+ # :method : one of the HTTP methods
150
+ # :member : whether this is an operation on the collection or an
151
+ # individual element (FIXME: custom operations on the
152
+ # collection will use a nonsensical URL) The URL for the
153
+ # operation is the element URL with the name of the operation
154
+ # appended
155
+ #
156
+ # This also defines a helper method like show_instance_url that returns
157
+ # the URL to this operation (in request context)
158
+ def operation(name, opts = {}, &block)
159
+ raise DuplicateOperationException if @operations[name]
160
+ @operations[name] = Operation.new(self, name, opts, &block)
161
+ end
162
+
163
+ def generate
164
+ operations.values.each { |op| op.generate }
165
+ app = ::Sinatra::Application
166
+ collname = name # Work around Ruby's weird scoping/capture
167
+ app.send(:define_method, "#{name.to_s.singularize}_url") do |id|
168
+ url_for "/api/#{collname}/#{id}", :full
169
+ end
170
+
171
+ if index_op = operations[:index]
172
+ app.send(:define_method, "#{name}_url") do
173
+ url_for index_op.path.gsub(/\/\?$/,''), :full
174
+ end
175
+ end
176
+ end
177
+
178
+ def add_feature_params(features)
179
+ features.each do |f|
180
+ f.operations.each do |fop|
181
+ if cop = operations[fop.name]
182
+ fop.params.each_key do |k|
183
+ if cop.params.has_key?(k)
184
+ raise DuplicateParamException, "Parameter '#{k}' for operation #{fop.name} defined by collection #{@name} and by feature #{f.name}"
185
+ else
186
+ cop.params[k] = fop.params[k]
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ def collections
196
+ @collections ||= {}
197
+ end
198
+
199
+ # Create a new collection. NAME should be the pluralized name of the
200
+ # collection.
201
+ #
202
+ # Adds a helper method #{name}_url which returns the URL to the :index
203
+ # operation on this collection.
204
+ def collection(name, &block)
205
+ raise DuplicateCollectionException if collections[name]
206
+ collections[name] = Collection.new(name, &block)
207
+ collections[name].add_feature_params(driver.features(name))
208
+ collections[name].generate
209
+ end
210
+
211
+ # Generate a root route for API docs
212
+ get '/api/docs\/?' do
213
+ respond_to do |format|
214
+ format.html { haml :'docs/index' }
215
+ format.xml { haml :'docs/index' }
216
+ end
217
+ end
218
+
219
+ end
220
+
221
+ module RabbitHelper
222
+ def query_url(url, params)
223
+ return url if params.nil? || params.empty?
224
+ url + "?#{URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))}"
225
+ end
226
+
227
+ def entry_points
228
+ collections.values.inject([]) do |m, coll|
229
+ url = url_for coll.operations[:index].path, :full
230
+ m << [ coll.name, url ]
231
+ end
232
+ end
233
+ end
234
+
235
+ register Rabbit
236
+ helpers RabbitHelper
237
+ end
238
+
239
+ class String
240
+ # Rails defines this for a number of other classes, including Object
241
+ # see activesupport/lib/active_support/core_ext/object/blank.rb
242
+ def blank?
243
+ self !~ /\S/
244
+ end
245
+
246
+ # Title case.
247
+ #
248
+ # "this is a string".titlecase
249
+ # => "This Is A String"
250
+ #
251
+ # CREDIT: Eliazar Parra
252
+ # Copied from facets
253
+ def titlecase
254
+ gsub(/\b\w/){ $`[-1,1] == "'" ? $& : $&.upcase }
255
+ end
256
+
257
+ def pluralize
258
+ self + "s"
259
+ end
260
+
261
+ def singularize
262
+ self.gsub(/s$/, '')
263
+ end
264
+
265
+ def underscore
266
+ gsub(/::/, '/').
267
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
268
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
269
+ tr("-", "_").
270
+ downcase
271
+ end
272
+ end
@@ -0,0 +1,269 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/accept_media_types'
3
+
4
+ # Accept header parsing was looked at but deemed
5
+ # too much of an irregularity to deal with. Problems with the header
6
+ # differences from IE, Firefox, Safari, and every other UA causes
7
+ # problems with the expected output. The general expected behavior
8
+ # would be serve html when no extension provided, but most UAs say
9
+ # they will accept application/xml with out a quality indicator, meaning
10
+ # you'd get the xml block served insead. Just plain retarded, use the
11
+ # extension and you'll never be suprised.
12
+
13
+ module Sinatra
14
+ module RespondTo
15
+ class UnhandledFormat < Sinatra::NotFound; end
16
+ class MissingTemplate < Sinatra::NotFound
17
+ def code; 500 end
18
+ end
19
+
20
+ TEXT_MIME_TYPES = [:txt, :html, :js, :json, :xml, :rss, :atom, :css, :asm, :c, :cc, :conf,
21
+ :csv, :cxx, :diff, :dtd, :f, :f77, :f90, :for, :gemspec, :h, :hh, :htm,
22
+ :log, :mathml, :mml, :p, :pas, :pl, :pm, :py, :rake, :rb, :rdf, :rtf, :ru,
23
+ :s, :sgm, :sgml, :sh, :svg, :svgz, :text, :wsdl, :xhtml, :xsl, :xslt, :yaml,
24
+ :yml, :ics, :png]
25
+
26
+ def self.registered(app)
27
+ app.helpers RespondTo::Helpers
28
+
29
+ app.set :default_charset, 'utf-8'
30
+ app.set :default_content, :html
31
+ app.set :assume_xhr_is_js, true
32
+
33
+ # We remove the trailing extension so routes
34
+ # don't have to be of the style
35
+ #
36
+ # get '/resouce.:format'
37
+ #
38
+ # They can instead be of the style
39
+ #
40
+ # get '/resource'
41
+ #
42
+ # and the format will automatically be available in <tt>format</tt>
43
+ app.before do
44
+ # Let through sinatra image urls in development
45
+ next if self.class.development? && request.path_info =~ %r{/__sinatra__/.*?.png}
46
+
47
+ unless options.static? && options.public? && (request.get? || request.head?) && static_file?(request.path_info)
48
+ rpi = request.path_info.sub(%r{\.([^\./]+)$}, '')
49
+
50
+ if (not $1) or ($1 and TEXT_MIME_TYPES.include?($1.to_sym))
51
+ request.path_info, ext = rpi, nil
52
+ if ! $1.nil?
53
+ ext = $1
54
+ elsif env['HTTP_ACCEPT'].nil? || env['HTTP_ACCEPT'].empty?
55
+ ext = options.default_content
56
+ end
57
+ if ext
58
+ @mime_types = [ Helpers::mime_type(ext) ]
59
+ format ext
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ app.configure :development do |dev|
66
+ dev.error UnhandledFormat do
67
+ content_type :html, :charset => 'utf-8'
68
+
69
+ (<<-HTML).gsub(/^ {10}/, '')
70
+ <!DOCTYPE html>
71
+ <html>
72
+ <head>
73
+ <style type="text/css">
74
+ body { text-align:center;font-family:helvetica,arial;font-size:22px;
75
+ color:#888;margin:20px}
76
+ #c {margin:0 auto;width:500px;text-align:left}
77
+ </style>
78
+ </head>
79
+ <body>
80
+ <h2>Sinatra doesn't know this ditty.</h2>
81
+ <img src='/__sinatra__/404.png'>
82
+ <div id="c">
83
+ Try this:
84
+ <pre>#{request.request_method.downcase} '#{request.path_info}' do\n respond_to do |wants|\n wants.#{format} { "Hello World" }\n end\nend</pre>
85
+ </div>
86
+ </body>
87
+ </html>
88
+ HTML
89
+ end
90
+
91
+ dev.error MissingTemplate do
92
+ content_type :html, :charset => 'utf-8'
93
+ response.status = request.env['sinatra.error'].code
94
+
95
+ engine = request.env['sinatra.error'].message.split('.').last
96
+ engine = 'haml' unless ['haml', 'builder', 'erb'].include? engine
97
+
98
+ path = File.basename(request.path_info)
99
+ path = "root" if path.nil? || path.empty?
100
+
101
+ format = engine == 'builder' ? 'xml' : 'html'
102
+
103
+ layout = case engine
104
+ when 'haml' then "!!!\n%html\n %body= yield"
105
+ when 'erb' then "<html>\n <body>\n <%= yield %>\n </body>\n</html>"
106
+ when 'builder' then ::Sinatra::VERSION =~ /^1.0/ ? "xml << yield" : "builder do |xml|\n xml << yield\nend"
107
+ end
108
+
109
+ layout = "<small>app.#{format}.#{engine}</small>\n<pre>#{escape_html(layout)}</pre>"
110
+
111
+ (<<-HTML).gsub(/^ {10}/, '')
112
+ <!DOCTYPE html>
113
+ <html>
114
+ <head>
115
+ <style type="text/css">
116
+ body { text-align:center;font-family:helvetica,arial;font-size:22px;
117
+ color:#888;margin:20px}
118
+ #c {margin:0 auto;width:500px;text-align:left;}
119
+ small {float:right;clear:both;}
120
+ pre {clear:both;}
121
+ </style>
122
+ </head>
123
+ <body>
124
+ <h2>Sinatra can't find #{request.env['sinatra.error'].message}</h2>
125
+ <img src='/__sinatra__/500.png'>
126
+ <div id="c">
127
+ Try this:<br />
128
+ #{layout}
129
+ <small>#{path}.#{format}.#{engine}</small>
130
+ <pre>Hello World!</pre>
131
+ <small>application.rb</small>
132
+ <pre>#{request.request_method.downcase} '#{request.path_info}' do\n respond_to do |wants|\n wants.#{engine == 'builder' ? 'xml' : 'html'} { #{engine} :#{path}#{",\n#{' '*32}layout => :app" if layout} }\n end\nend</pre>
133
+ </div>
134
+ </body>
135
+ </html>
136
+ HTML
137
+ end
138
+
139
+ end
140
+
141
+ app.class_eval do
142
+ private
143
+ def accept_list
144
+ @mime_types || Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT'] || '')
145
+ end
146
+
147
+ # Changes in 1.0 Sinatra reuse render for layout so we store
148
+ # the original value to tell us if this is an automatic attempt
149
+ # to do a layout call. If it is, it might fail with Errno::ENOENT
150
+ # and we want to pass that back to sinatra since it isn't a MissingTemplate
151
+ # error
152
+ def render_with_format(*args, &block)
153
+ assumed_layout = args[1] == :layout
154
+ args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
155
+ render_without_format *args, &block
156
+ rescue Errno::ENOENT => e
157
+ raise MissingTemplate, "#{args[1]}.#{args[0]}" unless assumed_layout
158
+ raise e
159
+ end
160
+ alias_method :render_without_format, :render
161
+ alias_method :render, :render_with_format
162
+
163
+ if ::Sinatra::VERSION =~ /^0\.9/
164
+ def lookup_layout_with_format(*args)
165
+ args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
166
+ lookup_layout_without_format *args
167
+ end
168
+ alias_method :lookup_layout_without_format, :lookup_layout
169
+ alias_method :lookup_layout, :lookup_layout_with_format
170
+ end
171
+ end
172
+ end
173
+
174
+ module Helpers
175
+ # Patch the content_type function to remember the set type
176
+ # This helps cut down on time in the format helper so it
177
+ # doesn't have to do a reverse lookup on the header
178
+ def self.included(klass)
179
+ klass.class_eval do
180
+ def content_type_with_save(*args)
181
+ content_type_without_save *args
182
+ @_format = args.first.to_sym
183
+ response['Content-Type']
184
+ end
185
+ alias_method :content_type_without_save, :content_type
186
+ alias_method :content_type, :content_type_with_save
187
+ end if ::Sinatra::VERSION =~ /^1.0/
188
+ end
189
+
190
+ def self.mime_type(sym)
191
+ ::Sinatra::Base.respond_to?(:mime_type) && ::Sinatra::Base.mime_type(sym) || ::Sinatra::Base.media_type(sym)
192
+ end
193
+
194
+ def format(val=nil)
195
+ unless val.nil?
196
+ mime_type = ::Sinatra::RespondTo::Helpers.mime_type(val)
197
+ fail "Unknown media type #{val}\nTry registering the extension with a mime type" if mime_type.nil?
198
+
199
+ @_format = val.to_sym
200
+ response['Content-Type'].sub!(/^[^;]+/, mime_type)
201
+ charset options.default_charset if Sinatra::RespondTo::TEXT_MIME_TYPES.include?(format) and format!=:png
202
+ end
203
+
204
+ @_format
205
+ end
206
+
207
+ # This is mostly just a helper so request.path_info isn't changed when
208
+ # serving files from the public directory
209
+ def static_file?(path)
210
+ public_dir = File.expand_path(options.public)
211
+ path = File.expand_path(File.join(public_dir, unescape(path)))
212
+
213
+ path[0, public_dir.length] == public_dir && File.file?(path)
214
+ end
215
+
216
+ def charset(val=nil)
217
+ fail "Content-Type must be set in order to specify a charset" if response['Content-Type'].nil?
218
+
219
+ if response['Content-Type'] =~ /charset=[^;]+/
220
+ response['Content-Type'].sub!(/charset=[^;]+/, (val == '' && '') || "charset=#{val}")
221
+ else
222
+ response['Content-Type'] += ";charset=#{val}"
223
+ end unless val.nil?
224
+
225
+ response['Content-Type'][/charset=([^;]+)/, 1]
226
+ end
227
+
228
+ def respond_to(&block)
229
+ wants = Format.new
230
+ yield wants
231
+ fmt, type, handler = match_accept_type(accept_list, wants)
232
+ raise UnhandledFormat if fmt.nil?
233
+ format fmt
234
+ handler.nil? ? nil : handler.call
235
+ end
236
+
237
+ def match_accept_type(mime_types, format)
238
+ selected = []
239
+ accepted_types = mime_types.map {|type| Regexp.escape(type).gsub(/\\\*/,'.*') }
240
+ # Fix for Chrome based browsers which returns XML when 'xhtml' is requested.
241
+ if env['HTTP_USER_AGENT'] =~ /Chrome/ and accepted_types.size>1
242
+ accepted_types[0], accepted_types[1] = accepted_types[1], accepted_types[0]
243
+ if accepted_types[0].eql?('application/xhtml\\+xml')
244
+ accepted_types[0] = 'text/html'
245
+ end
246
+ end
247
+ accepted_types.each do |at|
248
+ format.each do |fmt, ht, handler|
249
+ (selected = [fmt, ht, handler]) and break if ht.match(at)
250
+ end
251
+ break unless selected.empty?
252
+ end
253
+ selected
254
+ end
255
+
256
+ # NOTE Array instead of hash because order matters (wildcard type
257
+ # matches first handler)
258
+ class Format < Array #:nodoc:
259
+ def method_missing(format, *args, &handler)
260
+ mt = Sinatra::RespondTo::Helpers.mime_type(format)
261
+ if mt.nil?
262
+ Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{format}\nTry registering the extension with a mime type")
263
+ end
264
+ self << [format.to_s, mt, handler]
265
+ end
266
+ end
267
+ end
268
+ end
269
+ end