summary refs log tree commit
diff options
context:
space:
mode:
authorStephen Best <bestie@gmail.com>2013-08-10 16:24:33 +0100
committerJames Tucker <jftucker@gmail.com>2014-07-13 14:02:28 -0700
commit2ab24cf3590fc1fa6cf551841ef97edf2cdf1ebf (patch)
tree745ea505865d7d2e1b077dd44eff43340ab97dee
parent71c69113f269f04207157bee6c197b5675f3df61 (diff)
downloadrack-2ab24cf3590fc1fa6cf551841ef97edf2cdf1ebf.tar.gz
ShowException only serves HTML Accept header contains text/html
Rather than be concerned with whether a request is an asynchronous browser
request or not it is better to simply consider the Accept header and only serve
HTML to clients that specifically ask for it.

This way you will not find your pure JSON API application splitting out HTML
error messages to your console when using curl :)
-rw-r--r--lib/rack/showexceptions.rb12
-rw-r--r--test/spec_showexceptions.rb12
2 files changed, 12 insertions, 12 deletions
diff --git a/lib/rack/showexceptions.rb b/lib/rack/showexceptions.rb
index c91ca07c..39499d5e 100644
--- a/lib/rack/showexceptions.rb
+++ b/lib/rack/showexceptions.rb
@@ -28,12 +28,12 @@ module Rack
       env["rack.errors"].puts(exception_string)
       env["rack.errors"].flush
 
-      if prefers_plain_text?(env)
-        content_type = "text/plain"
-        body = [exception_string]
-      else
+      if accepts_html?(env)
         content_type = "text/html"
         body = pretty(env, e)
+      else
+        content_type = "text/plain"
+        body = [exception_string]
       end
 
       [500,
@@ -42,8 +42,8 @@ module Rack
        body]
     end
 
-    def prefers_plain_text?(env)
-      env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest" && (!env["HTTP_ACCEPT"] || !env["HTTP_ACCEPT"].include?("text/html"))
+    def accepts_html?(env)
+      env["HTTP_ACCEPT"] && env["HTTP_ACCEPT"].include?("text/html")
     end
 
     def dump_exception(exception)
diff --git a/test/spec_showexceptions.rb b/test/spec_showexceptions.rb
index bdd5ce5b..c5e2cb02 100644
--- a/test/spec_showexceptions.rb
+++ b/test/spec_showexceptions.rb
@@ -16,7 +16,7 @@ describe Rack::ShowExceptions do
     ))
 
     lambda{
-      res = req.get("/")
+      res = req.get("/", "HTTP_ACCEPT" => "text/html")
     }.should.not.raise
 
     res.should.be.a.server_error
@@ -26,7 +26,7 @@ describe Rack::ShowExceptions do
     res.should =~ /ShowExceptions/
   end
 
-  it "responds with plain text on AJAX requests accepting anything but HTML" do
+  it "responds with plain text to requests not specifically accepting HTML" do
     res = nil
 
     req = Rack::MockRequest.new(
@@ -35,7 +35,7 @@ describe Rack::ShowExceptions do
     ))
 
     lambda{
-      res = req.get("/", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest")
+      res = req.get( "/", "HTTP_ACCECPT" => "*/*")
     }.should.not.raise
 
     res.should.be.a.server_error
@@ -47,7 +47,7 @@ describe Rack::ShowExceptions do
     res.body.should.include __FILE__
   end
 
-  it "responds with HTML on AJAX requests accepting HTML" do
+  it "responds with HTML to requests specifically accepting HTML" do
     res = nil
 
     req = Rack::MockRequest.new(
@@ -56,7 +56,7 @@ describe Rack::ShowExceptions do
     ))
 
     lambda{
-      res = req.get("/", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", "HTTP_ACCEPT" => "text/html")
+      res = req.get("/", "HTTP_ACCEPT" => "text/html")
     }.should.not.raise
 
     res.should.be.a.server_error
@@ -79,7 +79,7 @@ describe Rack::ShowExceptions do
     )
 
     lambda{
-      res = req.get("/")
+      res = req.get("/", "HTTP_ACCEPT" => "text/html")
     }.should.not.raise
 
     res.should.be.a.server_error