File: route.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (101 lines) | stat: -rw-r--r-- 2,213 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class Puppet::Network::HTTP::Route
  MethodNotAllowedHandler = lambda do |req, res|
    raise Puppet::Network::HTTP::Error::HTTPMethodNotAllowedError.new("method #{req.method} not allowed for route #{req.path}", Puppet::Network::HTTP::Issues::UNSUPPORTED_METHOD)
  end

  NO_HANDLERS = [MethodNotAllowedHandler]

  attr_reader :path_matcher

  def self.path(path_matcher)
    new(path_matcher)
  end

  def initialize(path_matcher)
    @path_matcher = path_matcher
    @method_handlers = {
      :GET => NO_HANDLERS,
      :HEAD => NO_HANDLERS,
      :OPTIONS => NO_HANDLERS,
      :POST => NO_HANDLERS,
      :PUT => NO_HANDLERS,
      :DELETE => NO_HANDLERS
    }
    @chained = []
  end

  def get(*handlers)
    @method_handlers[:GET] = handlers
    return self
  end

  def head(*handlers)
    @method_handlers[:HEAD] = handlers
    return self
  end

  def options(*handlers)
    @method_handlers[:OPTIONS] = handlers
    return self
  end

  def post(*handlers)
    @method_handlers[:POST] = handlers
    return self
  end

  def put(*handlers)
    @method_handlers[:PUT] = handlers
    return self
  end

  def delete(*handlers)
    @method_handlers[:DELETE] = handlers
    return self
  end

  def any(*handlers)
    @method_handlers.each do |method, registered_handlers|
      @method_handlers[method] = handlers
    end
    return self
  end

  def chain(*routes)
    @chained = routes
    self
  end

  def matches?(request)
    Puppet.debug { "Evaluating match for #{self.inspect}" }
    if match(request.routing_path)
      return true
    else
      Puppet.debug { "Did not match path (#{request.routing_path.inspect})" }
    end
    return false
  end

  def process(request, response)
    handlers = @method_handlers[request.method.upcase.intern] || NO_HANDLERS
    handlers.each do |handler|
      handler.call(request, response)
    end

    subrequest = request.route_into(match(request.routing_path).to_s)
    chained_route = @chained.find { |route| route.matches?(subrequest) }
    if chained_route
      chained_route.process(subrequest, response)
    end
  end

  def inspect
    "Route #{@path_matcher.inspect}"
  end

  private

  def match(path)
    @path_matcher.match(path)
  end
end