change SCRIPT_NAME when changing PATH_INFO, add X-CASCADE header

This commit is contained in:
Sven Fuchs 2012-09-30 18:35:44 +02:00
parent 73fe04185d
commit cdd6f0047a
6 changed files with 32 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -13,4 +13,3 @@ class Travis::Web::App::Filter
end
end
end

View File

@ -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

View File

@ -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