From 4830158dc833358beecd7f435c58d1d7760ce3ce Mon Sep 17 00:00:00 2001 From: Dan Bernier Date: Fri, 26 Jul 2024 17:58:34 -0400 Subject: [PATCH] Rewrite Puma::Acme::Middleware w/o Sinatra --- lib/puma/acme/middleware.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/puma/acme/middleware.rb b/lib/puma/acme/middleware.rb index 71204a9..d49ba83 100644 --- a/lib/puma/acme/middleware.rb +++ b/lib/puma/acme/middleware.rb @@ -3,16 +3,18 @@ module Puma module Acme # ACME challenge response handler for HTTP-01 challenges. - class Middleware < Sinatra::Base + class Middleware def initialize(app, manager:) @app = app @manager = manager + end - super(app) + def call(env) + dup._call(env) end - get '/.well-known/acme-challenge/:token' do - if (token = params[:token]).nil? + def _call(env) + if (token = token(env)).nil? return @app.call(env) end @@ -20,8 +22,18 @@ def initialize(app, manager:) return @app.call(env) end - content_type 'text/plain' - answer.value + [200, {'content-type' => 'text/plain;charset=utf-8'}, [answer.value]] + end + + private + + def token(env) + path = ::Rack::Request.new(env).path_info + + prefix = '/.well-known/acme-challenge/' + if path.start_with?(prefix) + path[prefix.size..-1] + end end end end