more consistent use/avoidance of HeaderHash
[rainbows.git] / lib / rainbows / server_token.rb
blob0ee87ac73bab39902bae22cde47c34a021ab9cf6
1 # -*- encoding: binary -*-
2 module Rainbows
4 # An optional middleware to proudly display your usage of \Rainbows! in
5 # the "Server:" response header.  This means you can help tell the world
6 # you're using \Rainbows! and spread fun and joy all over the Internet!
8 #    ------ in your config.ru ------
9 #    require 'rainbows/server_token'
10 #    require 'rack/lobster'
11 #    use Rainbows::ServerToken
12 #    run Rack::Lobster.new
14 # If you're nervous about the exact version of \Rainbows! you're running,
15 # then you can actually specify anything you want:
17 #    use Rainbows::ServerToken, "netcat 1.0"
20 class ServerToken < Struct.new(:app, :token)
22   # :stopdoc:
23   #
24   # Freeze constants as they're slightly faster when setting hashes
25   SERVER = "Server".freeze
27   def initialize(app, token = Const::RACK_DEFAULTS['SERVER_SOFTWARE'])
28     super
29   end
31   def call(env)
32     status, headers, body = app.call(env)
33     headers = Rack::Utils::HeaderHash.new(headers) unless Hash === headers
34     headers[SERVER] = token
35     [ status, headers, body ]
36   end
37   # :startdoc:
38 end
39 end