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