diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index f4b0b220..f7a73f80 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -6,6 +6,7 @@ module Travis::Web autoload :Api, 'travis/web/app/api' autoload :Config, 'travis/web/app/config' autoload :Files, 'travis/web/app/files' + autoload :Helpers, 'travis/web/app/helpers' autoload :Filter, 'travis/web/app/filter' autoload :Terminal, 'travis/web/app/terminal' autoload :Version, 'travis/web/app/version' @@ -26,15 +27,7 @@ module Travis::Web use Rack::Protection::PathTraversal use Rack::Deflater if config.deflate? - # TODO this doesn't work, how can i extract this to a separate file/class - # use Travis::Web::App::Api, config if config.run_api? - if config.run_api? - require 'travis/api/app' - map config.api_endpoint do - run Travis::Api::App.new - end - end - + use Travis::Web::App::Api, config if config.run_api? use Travis::Web::App::Version, config use Travis::Web::App::Filter, config run Travis::Web::App::Files.new diff --git a/lib/travis/web/app/api.rb b/lib/travis/web/app/api.rb index eec7efb1..88199d57 100644 --- a/lib/travis/web/app/api.rb +++ b/lib/travis/web/app/api.rb @@ -2,6 +2,8 @@ require 'travis/api/app' class Travis::Web::App class Api + include Helpers + attr_reader :app, :api, :config def initialize(app, config) @@ -11,22 +13,17 @@ class Travis::Web::App end def call(env) - path = env['PATH_INFO'] - if matches?(path) - api.call(env.merge('PATH_INFO' => api_path(path))) + if matches?(env['PATH_INFO']) + api.call(map_env(env, config.api_endpoint)) else app.call(env) end end - def matches?(path) - # TODO there's a redirect through /auth/post_message which doesn't have the /api - # prefix. is that safe_redirect in travis-api? not sure how to solve this - path.starts_with?(config.api_endpoint) || path.starts_with?('/auth') - end + private - def api_path(path) - path.sub(/^#{config.api_endpoint}/, '') - end + def matches?(path) + path.starts_with?(config.api_endpoint) + end end end diff --git a/lib/travis/web/app/filter/endpoint.rb b/lib/travis/web/app/filter/endpoint.rb index ce3b540f..72e23bcc 100644 --- a/lib/travis/web/app/filter/endpoint.rb +++ b/lib/travis/web/app/filter/endpoint.rb @@ -9,7 +9,11 @@ class Travis::Web::App::Filter end def apply(string) - string.gsub(DEFAULT_ENDPOINT, config.api_endpoint) + apply? ? string.gsub(DEFAULT_ENDPOINT, config.api_endpoint) : string + end + + def apply? + config.api_endpoint != DEFAULT_ENDPOINT end end end diff --git a/lib/travis/web/app/filter/version.rb b/lib/travis/web/app/filter/version.rb index a0146332..31111fa5 100644 --- a/lib/travis/web/app/filter/version.rb +++ b/lib/travis/web/app/filter/version.rb @@ -13,4 +13,3 @@ class Travis::Web::App::Filter end end end - diff --git a/lib/travis/web/app/helpers.rb b/lib/travis/web/app/helpers.rb new file mode 100644 index 00000000..d148aacf --- /dev/null +++ b/lib/travis/web/app/helpers.rb @@ -0,0 +1,11 @@ +class Travis::Web::App + module Helpers + # TODO what's a better name? + def map_env(env, segment) + segment = "/#{segment}" unless segment[0] == '/' + env['PATH_INFO'] = env['PATH_INFO'].gsub(%r(^#{segment}(?:/)), '') + env['SCRIPT_NAME'] = "#{env['SCRIPT_NAME']}#{segment}" + env + end + end +end diff --git a/lib/travis/web/app/version.rb b/lib/travis/web/app/version.rb index fbedbd62..74205bc6 100644 --- a/lib/travis/web/app/version.rb +++ b/lib/travis/web/app/version.rb @@ -1,5 +1,7 @@ class Travis::Web::App class Version + include Helpers + attr_reader :app, :config def initialize(app, config) @@ -12,9 +14,9 @@ class Travis::Web::App if pass?(path) app.call(env) elsif versioned?(path) - app.call(env.merge('PATH_INFO' => strip_version(path))) + app.call(map_env(env, config.version)) else - [404, { 'Content-Type' => 'text/html', 'Content-Length' => '9' }, ['not found']] + not_found end end @@ -28,8 +30,8 @@ class Travis::Web::App path.starts_with?("/#{config.version}/") end - def strip_version(path) - path.sub(%r(/#{config.version}/), '') + def not_found + [404, { 'Content-Type' => 'text/html', 'Content-Length' => '9', 'X-Cascade' => 'pass' }, ['not found']] end end end