From a2019b683b8b79c19e031c1dd407aca575289f28 Mon Sep 17 00:00:00 2001 From: Jim van Musscher Date: Sun, 11 Oct 2015 17:16:09 +0200 Subject: [PATCH 1/3] Create an error response body outside render_error. Related to issue #144. --- lib/webmachine/decision/flow.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/webmachine/decision/flow.rb b/lib/webmachine/decision/flow.rb index 103e23e6..8c56faaf 100644 --- a/lib/webmachine/decision/flow.rb +++ b/lib/webmachine/decision/flow.rb @@ -351,7 +351,23 @@ def l5 # POST? def l7 - request.post? ? :m7 : 404 + if request.post? + :m7 + else + unless response.body + title, message = t(["errors.404.title", "errors.404.message"], + { :method => request.method, + :error => response.error}) + response.body = t("errors.standard_body", + {:title => title, + :message => message, + :version => Webmachine::SERVER_STRING}) + response.headers[CONTENT_TYPE] = TEXT_HTML + end + + encode_body + 404 + end end # If-Modified-Since exists? From 15de5371b3c04da11221bc4b81cb501ea268f4fe Mon Sep 17 00:00:00 2001 From: Jim van Musscher Date: Sun, 11 Oct 2015 22:34:47 +0200 Subject: [PATCH 2/3] Don't encode the response body in l7. --- lib/webmachine/decision/flow.rb | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/lib/webmachine/decision/flow.rb b/lib/webmachine/decision/flow.rb index 8c56faaf..103e23e6 100644 --- a/lib/webmachine/decision/flow.rb +++ b/lib/webmachine/decision/flow.rb @@ -351,23 +351,7 @@ def l5 # POST? def l7 - if request.post? - :m7 - else - unless response.body - title, message = t(["errors.404.title", "errors.404.message"], - { :method => request.method, - :error => response.error}) - response.body = t("errors.standard_body", - {:title => title, - :message => message, - :version => Webmachine::SERVER_STRING}) - response.headers[CONTENT_TYPE] = TEXT_HTML - end - - encode_body - 404 - end + request.post? ? :m7 : 404 end # If-Modified-Since exists? From 8eb13fda2441facadf09dec81ab27714f162bc50 Mon Sep 17 00:00:00 2001 From: Jim van Musscher Date: Sun, 11 Oct 2015 22:38:47 +0200 Subject: [PATCH 3/3] Delete Content-Encoding header in render_error. It is not possible to correctly encode the response in render_error. So it is better to delete the Content-Encoding header here. --- lib/webmachine/errors.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/webmachine/errors.rb b/lib/webmachine/errors.rb index e25ac53f..413bbef9 100644 --- a/lib/webmachine/errors.rb +++ b/lib/webmachine/errors.rb @@ -16,6 +16,9 @@ module Webmachine # the response body def self.render_error(code, req, res, options={}) res.code = code + # The response cannot be encoded in render_error so the Content-Encoding + # header is removed here. + res.headers.delete CONTENT_ENCODING unless res.body title, message = t(["errors.#{code}.title", "errors.#{code}.message"], { :method => req.method,