Ability to optionally disable powered-by header (#449)

This commit is contained in:
Blacksmoke16 2018-05-09 03:19:59 -04:00 committed by Serdar Dogruyol
parent 73045bb138
commit f3f7e319ae
4 changed files with 16 additions and 1 deletions

View file

@ -17,6 +17,10 @@ describe "Config" do
config.env.should eq "production" config.env.should eq "production"
end end
it "sets default powered_by_header to true" do
Kemal.config.powered_by_header.should be_true
end
it "sets host binding" do it "sets host binding" do
config = Kemal.config config = Kemal.config
config.host_binding = "127.0.0.1" config.host_binding = "127.0.0.1"

View file

@ -19,4 +19,14 @@ describe "Kemal::InitHandler" do
Kemal::InitHandler::INSTANCE.call(context) Kemal::InitHandler::INSTANCE.call(context)
context.response.headers["X-Powered-By"].should eq "Kemal" context.response.headers["X-Powered-By"].should eq "Kemal"
end end
it "does not initialize context with X-Powered-By: Kemal if disabled" do
Kemal.config.powered_by_header = false
request = HTTP::Request.new("GET", "/")
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
Kemal::InitHandler::INSTANCE.call(context)
context.response.headers["X-Powered-By"]?.should be_nil
end
end end

View file

@ -22,6 +22,7 @@ module Kemal
property always_rescue, server : HTTP::Server?, extra_options, shutdown_message property always_rescue, server : HTTP::Server?, extra_options, shutdown_message
property serve_static : (Bool | Hash(String, Bool)) property serve_static : (Bool | Hash(String, Bool))
property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)? property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)?
property powered_by_header : Bool = true
def initialize def initialize
@host_binding = "0.0.0.0" @host_binding = "0.0.0.0"

View file

@ -7,7 +7,7 @@ module Kemal
INSTANCE = new INSTANCE = new
def call(context : HTTP::Server::Context) def call(context : HTTP::Server::Context)
context.response.headers.add "X-Powered-By", "Kemal" context.response.headers.add "X-Powered-By", "Kemal" if Kemal.config.powered_by_header
context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type")
call_next context call_next context
end end