From a7678abe925e0d1862db1471c4f55b2618defeaa Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Wed, 20 Dec 2023 22:35:27 +1100 Subject: [PATCH] Fix HEAD response header for brid.gy --- Gemfile | 1 + Gemfile.lock | 3 +++ app/action.rb | 4 ++++ config.ru | 9 ++++++++- config/routes.rb | 2 +- lib/adamantium/middleware/header_fix.rb | 24 ++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 lib/adamantium/middleware/header_fix.rb diff --git a/Gemfile b/Gemfile index 3d8af69..ae86204 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem "matrix" gem "rack-session" gem "rack-rewrite" +gem "rack-contrib" gem "ruby-readability", require: "readability" gem "down" diff --git a/Gemfile.lock b/Gemfile.lock index 4590591..616f202 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -331,6 +331,8 @@ GEM que (2.3.0) racc (1.7.3) rack (2.2.8) + rack-contrib (2.4.0) + rack (< 4) rack-rewrite (1.5.1) rack-session (1.0.1) rack (< 3) @@ -524,6 +526,7 @@ DEPENDENCIES pg puma que + rack-contrib rack-rewrite rack-session rack-test diff --git a/app/action.rb b/app/action.rb index a4139d7..5af057a 100644 --- a/app/action.rb +++ b/app/action.rb @@ -22,6 +22,10 @@ module Adamantium handle_exception ROM::TupleCountMismatchError => :not_found handle_exception StandardError => :handle_error + config.default_headers = { + "Content-Type" => "text/html" + } + def cache(key:, content:) cacher.call(key: key, content: content, expiry: TimeMath.min.advance(Time.now, +10)) end diff --git a/config.ru b/config.ru index 2699500..f1ef051 100644 --- a/config.ru +++ b/config.ru @@ -15,7 +15,14 @@ use Rack::Session::Cookie, require "rack/rewrite" use Rack::Rewrite do # remove trailing slashes - # r302 %r{(/.*)/(\?.*)?$}, "$1$2" + r302 %r{(/.*)/(\?.*)?$}, "$1$2" +end + +require "adamantium/middleware/header_fix" +use Adamantium::Middleware::HeaderFix do |headers, env| + if env["REQUEST_METHOD"] == "HEAD" + headers['Content-Type'] = "text/html" + end end run Hanami.app diff --git a/config/routes.rb b/config/routes.rb index 675d2bc..ece24ec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,7 @@ module Adamantium slice :micropub, at: "/micropub" - get "/", to: "site.home" + root to: "site.home" get "/post/top_tracks/:slug", to: "posts.top_tracks" get "/post/:slug", to: "posts.show" get "/posts", to: "posts.index" diff --git a/lib/adamantium/middleware/header_fix.rb b/lib/adamantium/middleware/header_fix.rb new file mode 100644 index 0000000..1df873f --- /dev/null +++ b/lib/adamantium/middleware/header_fix.rb @@ -0,0 +1,24 @@ +require "rack" +require "rack/contrib" + +module Adamantium + module Middleware + class HeaderFix + HEADERS_KLASS = Rack.release < "3" ? Rack::Utils::HeaderHash : Rack::Headers + private_constant :HEADERS_KLASS + + def initialize(app, &block) + @app = app + @block = block + end + + def call(env) + response = @app.call(env) + headers = HEADERS_KLASS.new.merge(response[1]) + @block.call(headers, env) + response[1] = headers + response + end + end + end +end \ No newline at end of file