kivanio (owner)

Fork Of

gist: 207984 by postmodern A Rack middleware app to sp...

Revisions

gist: 227931 Download_button fork
public
Public Clone URL: git://gist.github.com/227931.git
Embed All Files: show embed
lie_server.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Rack
  #
  # The LieServer is a simple Rack middleware app which allows one to spoof
  # the +Server+ header in responses for every request, requests to certain
  # sub-directories or paths which match a regular expression.
  #
  # Be deceitful to would be attackers, tell them your running IIS 3.0.
  #
  # MIT License - Hal Brodigan (postmodern.mod3 at gmail.com)
  #
  class LieServer
 
    #
    # Initializes the lie server.
    #
    # @param [#call] app
    # The Rack app to lie for.
    #
    # @param [Hash{Regexp,String => String}] options
    # Additional lie options.
    #
    # @example
    # use Rack::LieServer, '/' => 'IIS 3.0'
    #
    # @example
    # use Rack::LieServer, /\.asp$/ => 'Apache',
    # '/' => 'Nginx'
    #
    def initialize(app,options={})
      @app = app
 
      patterns = []
      paths = {}
 
      options.each do |pattern,lie|
        if pattern.kind_of?(Regexp)
          patterns << [pattern, lie]
        else
          paths[pattern] = lie
        end
      end
 
      @routes = patterns + paths.sort.reverse
    end
 
    def call(env)
      code, headers, body = @app.call(env)
      path = env['PATH_INFO']
 
      pattern, lie = @routes.find do |pattern,lie|
        if pattern.kind_of?(Regexp)
          path =~ pattern
        else
          path[0,pattern.length] == pattern
        end
      end
 
      headers['Server'] = lie if lie
 
      [code, headers, body]
    end
 
  end
end