From 386ede04006eedce2eefad29661443119487c10f Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 5 Apr 2018 12:54:14 +0200 Subject: [PATCH] Add special middleware for handling of Rails 5.1 WOPI errors Closes SCI-2266. --- app/middlewares/wopi_method_override.rb | 19 +++++++++++++++++++ config/application.rb | 4 ++++ 2 files changed, 23 insertions(+) create mode 100644 app/middlewares/wopi_method_override.rb diff --git a/app/middlewares/wopi_method_override.rb b/app/middlewares/wopi_method_override.rb new file mode 100644 index 000000000..e30a5c75b --- /dev/null +++ b/app/middlewares/wopi_method_override.rb @@ -0,0 +1,19 @@ +# When WOPI performs calls onto sciNote WOPI subdomain REST endpoints +# Rack::MethodOverride MUST be omitted because it crashes the requests +# due to trying to parse body of the requests +class WopiMethodOverride + def initialize(app) + @app = app + end + + def call(env) + app = @app + + unless WopiSubdomain.matches?(ActionDispatch::Request.new(env)) + # Use the wrapped Rack::MethodOverride middleware + app = Rack::MethodOverride.new(@app) + end + + app.call(env) + end +end \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 129ba8e4c..4442591d6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,6 +15,10 @@ module Scinote # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. + # Swap the Rack::MethodOverride with a wrapped middleware for WOPI handling + require_relative '../app/middlewares/wopi_method_override' + config.middleware.swap Rack::MethodOverride, WopiMethodOverride + # Load all model concerns, including subfolders config.autoload_paths += Dir["#{Rails.root}/app/models/concerns/**/*.rb"]