change SCRIPT_NAME when changing PATH_INFO, add X-CASCADE header
This commit is contained in:
parent
73fe04185d
commit
cdd6f0047a
|
@ -6,6 +6,7 @@ module Travis::Web
|
||||||
autoload :Api, 'travis/web/app/api'
|
autoload :Api, 'travis/web/app/api'
|
||||||
autoload :Config, 'travis/web/app/config'
|
autoload :Config, 'travis/web/app/config'
|
||||||
autoload :Files, 'travis/web/app/files'
|
autoload :Files, 'travis/web/app/files'
|
||||||
|
autoload :Helpers, 'travis/web/app/helpers'
|
||||||
autoload :Filter, 'travis/web/app/filter'
|
autoload :Filter, 'travis/web/app/filter'
|
||||||
autoload :Terminal, 'travis/web/app/terminal'
|
autoload :Terminal, 'travis/web/app/terminal'
|
||||||
autoload :Version, 'travis/web/app/version'
|
autoload :Version, 'travis/web/app/version'
|
||||||
|
@ -26,15 +27,7 @@ module Travis::Web
|
||||||
use Rack::Protection::PathTraversal
|
use Rack::Protection::PathTraversal
|
||||||
use Rack::Deflater if config.deflate?
|
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?
|
||||||
# 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::Version, config
|
use Travis::Web::App::Version, config
|
||||||
use Travis::Web::App::Filter, config
|
use Travis::Web::App::Filter, config
|
||||||
run Travis::Web::App::Files.new
|
run Travis::Web::App::Files.new
|
||||||
|
|
|
@ -2,6 +2,8 @@ require 'travis/api/app'
|
||||||
|
|
||||||
class Travis::Web::App
|
class Travis::Web::App
|
||||||
class Api
|
class Api
|
||||||
|
include Helpers
|
||||||
|
|
||||||
attr_reader :app, :api, :config
|
attr_reader :app, :api, :config
|
||||||
|
|
||||||
def initialize(app, config)
|
def initialize(app, config)
|
||||||
|
@ -11,22 +13,17 @@ class Travis::Web::App
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
path = env['PATH_INFO']
|
if matches?(env['PATH_INFO'])
|
||||||
if matches?(path)
|
api.call(map_env(env, config.api_endpoint))
|
||||||
api.call(env.merge('PATH_INFO' => api_path(path)))
|
|
||||||
else
|
else
|
||||||
app.call(env)
|
app.call(env)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(path)
|
private
|
||||||
# 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
|
|
||||||
|
|
||||||
def api_path(path)
|
def matches?(path)
|
||||||
path.sub(/^#{config.api_endpoint}/, '')
|
path.starts_with?(config.api_endpoint)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,11 @@ class Travis::Web::App::Filter
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply(string)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,4 +13,3 @@ class Travis::Web::App::Filter
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
11
lib/travis/web/app/helpers.rb
Normal file
11
lib/travis/web/app/helpers.rb
Normal 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
|
|
@ -1,5 +1,7 @@
|
||||||
class Travis::Web::App
|
class Travis::Web::App
|
||||||
class Version
|
class Version
|
||||||
|
include Helpers
|
||||||
|
|
||||||
attr_reader :app, :config
|
attr_reader :app, :config
|
||||||
|
|
||||||
def initialize(app, config)
|
def initialize(app, config)
|
||||||
|
@ -12,9 +14,9 @@ class Travis::Web::App
|
||||||
if pass?(path)
|
if pass?(path)
|
||||||
app.call(env)
|
app.call(env)
|
||||||
elsif versioned?(path)
|
elsif versioned?(path)
|
||||||
app.call(env.merge('PATH_INFO' => strip_version(path)))
|
app.call(map_env(env, config.version))
|
||||||
else
|
else
|
||||||
[404, { 'Content-Type' => 'text/html', 'Content-Length' => '9' }, ['not found']]
|
not_found
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,8 +30,8 @@ class Travis::Web::App
|
||||||
path.starts_with?("/#{config.version}/")
|
path.starts_with?("/#{config.version}/")
|
||||||
end
|
end
|
||||||
|
|
||||||
def strip_version(path)
|
def not_found
|
||||||
path.sub(%r(/#{config.version}/), '')
|
[404, { 'Content-Type' => 'text/html', 'Content-Length' => '9', 'X-Cascade' => 'pass' }, ['not found']]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user