Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-01-30 00:31:53 +0300
committerDouwe Maan <douwe@selenight.nl>2017-02-07 01:12:24 +0300
commit8f85a11d9fcc1f4ccde7c46652f0be00edf46a78 (patch)
tree0b6034594c5994259b7ecfd16e294f767eec2043 /lib/gitlab/route_map.rb
parent5bf22606efa37f88a0f440205ff013d20227bd5e (diff)
Validate route map
Diffstat (limited to 'lib/gitlab/route_map.rb')
-rw-r--r--lib/gitlab/route_map.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/gitlab/route_map.rb b/lib/gitlab/route_map.rb
new file mode 100644
index 00000000000..89985d90c10
--- /dev/null
+++ b/lib/gitlab/route_map.rb
@@ -0,0 +1,52 @@
+module Gitlab
+ class RouteMap
+ class FormatError < StandardError; end
+
+ def initialize(data)
+ begin
+ entries = YAML.safe_load(data)
+ rescue
+ raise FormatError, 'Route map needs to be valid YAML'
+ end
+
+ raise FormatError, 'Route map needs to be an array' unless entries.is_a?(Array)
+
+ @map = entries.map { |entry| parse_entry(entry) }
+ end
+
+ def public_path_for_source_path(path)
+ mapping = @map.find { |mapping| path =~ mapping[:source] }
+ return unless mapping
+
+ path.sub(mapping[:source], mapping[:public])
+ end
+
+ private
+
+ def parse_entry(entry)
+ raise FormatError, 'Route map entry needs to be a hash' unless entry.is_a?(Hash)
+ raise FormatError, 'Route map entry requires a source key' unless entry.has_key?('source')
+ raise FormatError, 'Route map entry requires a public key' unless entry.has_key?('public')
+
+ source_regexp = entry['source']
+ public_path = entry['public']
+
+ unless source_regexp.start_with?('/') && source_regexp.end_with?('/')
+ raise FormatError, 'Route map entry source needs to start and end in a slash (/)'
+ end
+
+ source_regexp = source_regexp[1...-1].gsub('\/', '/')
+
+ begin
+ source_regexp = Regexp.new("^#{source_regexp}$")
+ rescue RegexpError => e
+ raise FormatError, "Route map entry source needs to be a valid regular expression: #{e}"
+ end
+
+ {
+ source: source_regexp,
+ public: public_path
+ }
+ end
+ end
+end