./lib/dolt/sinatra/actions.rb in dolt-0.9.0 vs ./lib/dolt/sinatra/actions.rb in dolt-0.10.0
- old
+ new
@@ -14,10 +14,11 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
require "json"
+require "time"
module Dolt
module Sinatra
module Actions
# Built-in redirect seems to not work with Sinatra::Async, it throws
@@ -27,11 +28,11 @@
response["Location"] = url
body ""
end
def error(error, repo, ref)
- response["Content-Type"] = "text/html"
+ add_headers(response)
body(renderer.render(:"500", {
:error => error,
:repository_slug => repo,
:ref => ref
}))
@@ -58,16 +59,16 @@
:content_type => "text/plain",
:template_options => { :layout => nil }
})
end
- def blob(repo, ref, path, options = { :template => :blob, :content_type => "text/html" })
+ def blob(repo, ref, path, options = { :template => :blob })
actions.blob(repo, ref, path) do |err, data|
next error(err, repo, ref) if !err.nil?
blob = data[:blob]
next redirect(tree_url(repo, ref, path)) if blob.class.to_s !~ /\bBlob/
- response["Content-Type"] = options[:content_type]
+ add_headers(response, options.merge(:ref => ref))
tpl_options = options[:template_options] || {}
body(renderer.render(options[:template], data, tpl_options))
end
end
@@ -75,11 +76,11 @@
actions.tree(repo, ref, path) do |err, data|
begin
next error(err, repo, ref) if !err.nil?
tree = data[:tree]
next redirect(blob_url(repo, ref, path)) if tree.class.to_s !~ /\bTree/
- response["Content-Type"] = "text/html"
+ add_headers(response, :ref => ref)
body(renderer.render(:tree, data))
rescue Exception => err
error(err, repo, ref)
end
end
@@ -87,49 +88,66 @@
def tree_entry(repo, ref, path)
actions.tree_entry(repo, ref, path) do |err, data|
begin
next error(err, repo, ref) if !err.nil?
- response["Content-Type"] = "text/html"
+ add_headers(response, :ref => ref)
body(renderer.render(data.key?(:tree) ? :tree : :blob, data))
rescue Exception => err
error(err, repo, ref)
end
end
end
def blame(repo, ref, path)
actions.blame(repo, ref, path) do |err, data|
next error(err, repo, ref) if !err.nil?
- response["Content-Type"] = "text/html"
+ add_headers(response, :ref => ref)
body(renderer.render(:blame, data))
end
end
def history(repo, ref, path, count)
actions.history(repo, ref, path, count) do |err, data|
next error(err, repo, ref) if !err.nil?
- response["Content-Type"] = "text/html"
+ add_headers(response, :ref => ref)
body(renderer.render(:commits, data))
end
end
def refs(repo)
actions.refs(repo) do |err, data|
next error(err, repo, ref) if !err.nil?
- response["Content-Type"] = "application/json"
+ add_headers(response, :content_type => "application/json")
body(renderer.render(:refs, data, :layout => nil))
end
end
def tree_history(repo, ref, path, count = 1)
actions.tree_history(repo, ref, path, count) do |err, data|
- if !err.nil?
+ begin
+ if !err.nil?
+ error(err, repo, ref)
+ else
+ add_headers(response, :content_type => "application/json", :ref => ref)
+ body(renderer.render(:tree_history, data, :layout => nil))
+ end
+ rescue Exception => err
error(err, repo, ref)
- else
- response["Content-Type"] = "application/json"
- body(renderer.render(:tree_history, data, :layout => nil))
end
+ end
+ end
+
+ private
+ def add_headers(response, headers = {})
+ default_ct = "text/html; charset=utf-8"
+ response["Content-Type"] = headers[:content_type] || default_ct
+ response["X-UA-Compatible"] = "IE=edge"
+
+ if headers[:ref] && headers[:ref].length == 40
+ response["Cache-Control"] = "max-age=315360000, public"
+ year = 60*60*24*365
+ response["Expires"] = (Time.now + year).httpdate
end
end
end
end
end