From 941d1391bbb18ee31409fdcfa50d4fee1a31f93e Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 19:05:49 +0100 Subject: [PATCH 01/10] move AltVersions to its own file, try setting domain and secure on the cookie --- lib/travis/web/app.rb | 47 +++--------------------------- lib/travis/web/app/alt_versions.rb | 33 +++++++++++++++++++++ spec/app_spec.rb | 4 +-- 3 files changed, 39 insertions(+), 45 deletions(-) create mode 100644 lib/travis/web/app/alt_versions.rb diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index 6ec4f3df..6812a731 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -6,6 +6,7 @@ require 'delegate' require 'time' class Travis::Web::App + autoload :AltVersions, 'travis/web/app/alt_versions' autoload :MobileRedirect, 'travis/web/app/mobile_redirect' S3_URL = 'https://s3.amazonaws.com/travis-web-production/assets' @@ -24,42 +25,6 @@ class Travis::Web::App end end - class AltVersions - attr_reader :app - - def initialize(app) - @app = app - end - - def call(env) - alt = alt_from(env) - env['travis.alt'] = alt if alt - status, headers, body = app.call(env) - set_cookies(headers, env['travis.alt']) if env.key?('travis.alt') - [status, headers, body] - end - - def set_cookies(headers, alt) - headers['Set-Cookie'] = "alt=#{alt}; Max-Age=#{alt == 'default' ? 0 : 86400}" - end - - def alt_from(env) - alt_from_params(env) || alt_from_cookie(env) - end - - def alt_from_params(env) - alt_from_string env['QUERY_STRING'] - end - - def alt_from_cookie(env) - alt_from_string env['HTTP_COOKIE'] - end - - def alt_from_string(string) - $1 if string =~ /alt=([^&]*)/ - end - end - class << self def new(options = {}) return super unless options[:environment] == 'development' @@ -68,7 +33,7 @@ class Travis::Web::App def build(options = {}) builder = Rack::Builder.new - if options.fetch(:environment) == 'production' + if options[:environment] == 'production' builder.use Rack::SSL builder.use Rack::Cache end @@ -84,11 +49,10 @@ class Travis::Web::App end end - attr_reader :routers, :environment, :version, :last_modified, :age, :options, :root + attr_reader :routers, :version, :last_modified, :age, :options, :root def initialize(options = {}) @options = options - @environment = options.fetch(:environment) @root = options.fetch(:root) @version = File.read File.expand_path('version', root) @last_modified = Time.now @@ -118,7 +82,6 @@ class Travis::Web::App def response_for(file, options = {}) content = File.read(file) set_config(content, options) if config_needed?(file) - headers = { 'Content-Length' => content.bytesize.to_s, 'Content-Location' => path_for(file), @@ -130,13 +93,11 @@ class Travis::Web::App 'Expires' => (last_modified + age).httpdate, 'Vary' => vary_for(file) } - [ 200, headers, [content] ] end def each_file - pattern = File.join(root, '**/*') - Dir.glob(pattern) { |f| yield f if File.file? f } + Dir.glob(File.join(root, '**/*')) { |file| yield file if File.file?(file) } end def prefix?(file) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb new file mode 100644 index 00000000..61619f41 --- /dev/null +++ b/lib/travis/web/app/alt_versions.rb @@ -0,0 +1,33 @@ +class Travis::Web::App::AltVersions + attr_reader :app + + def initialize(app) + @app = app + end + + def call(env) + alt = alt_from_params(env) || alt_from_cookie(env) + env['travis.alt'] = alt if alt + status, headers, body = app.call(env) + headers['Set-Cookie'] = cookie(alt) if env.key?('travis.alt') + [status, headers, body] + end + + private + + def cookie(alt) + "alt=#{alt}; Domain=travis-ci.org; Secure; Max-Age=#{alt == 'default' ? 0 : 86400}" + end + + def alt_from_params(env) + alt_from_string env['QUERY_STRING'] + end + + def alt_from_cookie(env) + alt_from_string env['HTTP_COOKIE'] + end + + def alt_from_string(string) + $1 if string =~ /alt=([^&]*)/ + end +end diff --git a/spec/app_spec.rb b/spec/app_spec.rb index b50f687a..acef4049 100644 --- a/spec/app_spec.rb +++ b/spec/app_spec.rb @@ -42,12 +42,12 @@ describe Travis::Web::App do example { last_response.should be_ok } example { last_response.body.should include('/assets/foo/styles/app.css') } example { last_response.body.should include('/assets/foo/scripts/app.js') } - example { headers['Set-Cookie'].should == 'alt=foo; Max-Age=86400' } + example { headers['Set-Cookie'].should == 'alt=foo; Domain=travis-ci.org; Secure; Max-Age=86400' } end context 'passing default as an alt param' do before { get('/?alt=default') } - example { headers['Set-Cookie'].should == 'alt=default; Max-Age=0' } + example { headers['Set-Cookie'].should == 'alt=default; Domain=travis-ci.org; Secure; Max-Age=0' } end end end From d686b741bf8a5ce0cc926b3f360b77fd5abc8867 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 19:12:37 +0100 Subject: [PATCH 02/10] try using the staging domain --- lib/travis/web/app/alt_versions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb index 61619f41..a3ad2b87 100644 --- a/lib/travis/web/app/alt_versions.rb +++ b/lib/travis/web/app/alt_versions.rb @@ -16,7 +16,7 @@ class Travis::Web::App::AltVersions private def cookie(alt) - "alt=#{alt}; Domain=travis-ci.org; Secure; Max-Age=#{alt == 'default' ? 0 : 86400}" + "alt=#{alt}; Domain=staging.travis-ci.org; Secure; Max-Age=#{alt == 'default' ? 0 : 86400}" end def alt_from_params(env) From 5c18bd3406084c43aa6fa36ad82803093394a8bf Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 20:09:47 +0100 Subject: [PATCH 03/10] try using path for the cookie --- lib/travis/web/app/alt_versions.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb index a3ad2b87..14cdcd9b 100644 --- a/lib/travis/web/app/alt_versions.rb +++ b/lib/travis/web/app/alt_versions.rb @@ -9,16 +9,12 @@ class Travis::Web::App::AltVersions alt = alt_from_params(env) || alt_from_cookie(env) env['travis.alt'] = alt if alt status, headers, body = app.call(env) - headers['Set-Cookie'] = cookie(alt) if env.key?('travis.alt') + headers['Set-Cookie'] = "alt=#{alt}; path=/; Secure" if alt [status, headers, body] end private - def cookie(alt) - "alt=#{alt}; Domain=staging.travis-ci.org; Secure; Max-Age=#{alt == 'default' ? 0 : 86400}" - end - def alt_from_params(env) alt_from_string env['QUERY_STRING'] end From c5aaa8806a16314eaac9970a6d58d562b2c27cee Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 22:55:00 +0100 Subject: [PATCH 04/10] uppercase Path --- lib/travis/web/app/alt_versions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb index 14cdcd9b..119d14fb 100644 --- a/lib/travis/web/app/alt_versions.rb +++ b/lib/travis/web/app/alt_versions.rb @@ -9,7 +9,7 @@ class Travis::Web::App::AltVersions alt = alt_from_params(env) || alt_from_cookie(env) env['travis.alt'] = alt if alt status, headers, body = app.call(env) - headers['Set-Cookie'] = "alt=#{alt}; path=/; Secure" if alt + headers['Set-Cookie'] = "alt=#{alt}; Path=/; Secure" if alt [status, headers, body] end From b314670530ee040d02c706824faf8b3a1925a2ef Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 23:00:17 +0100 Subject: [PATCH 05/10] debug output --- lib/travis/web/app/alt_versions.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb index 119d14fb..5cb934b8 100644 --- a/lib/travis/web/app/alt_versions.rb +++ b/lib/travis/web/app/alt_versions.rb @@ -9,12 +9,18 @@ class Travis::Web::App::AltVersions alt = alt_from_params(env) || alt_from_cookie(env) env['travis.alt'] = alt if alt status, headers, body = app.call(env) - headers['Set-Cookie'] = "alt=#{alt}; Path=/; Secure" if alt + set_cookie(headers, alt) if alt [status, headers, body] end private + def set_cookie(headers, alt) + cookie = "alt=#{alt}; Path=/; Secure" + puts "setting cookie #{cookie}" + headers['Set-Cookie'] = cookie + end + def alt_from_params(env) alt_from_string env['QUERY_STRING'] end From 9bda77e757d6688c4977e310eb0171c5a2e64a0c Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 23:07:09 +0100 Subject: [PATCH 06/10] try removing Secure from the cookie --- lib/travis/web/app/alt_versions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb index 5cb934b8..6d16a23c 100644 --- a/lib/travis/web/app/alt_versions.rb +++ b/lib/travis/web/app/alt_versions.rb @@ -16,7 +16,7 @@ class Travis::Web::App::AltVersions private def set_cookie(headers, alt) - cookie = "alt=#{alt}; Path=/; Secure" + cookie = "alt=#{alt}; path=/" puts "setting cookie #{cookie}" headers['Set-Cookie'] = cookie end From 47765ba8c03c827685137e77ff72857cdcbfc335 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 23:11:40 +0100 Subject: [PATCH 07/10] try disabling rack/cache --- lib/travis/web/app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index 6812a731..d459eee3 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -35,7 +35,7 @@ class Travis::Web::App builder = Rack::Builder.new if options[:environment] == 'production' builder.use Rack::SSL - builder.use Rack::Cache + # builder.use Rack::Cache end builder.use Rack::Deflater builder.use Rack::Head From 5ae74ddcdc5cac09ea55a1eb8d8a7070dc5fdb70 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 23:39:03 +0100 Subject: [PATCH 08/10] defer calling things until the document is ready --- public/index.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/public/index.html b/public/index.html index fb4970ef..afa54710 100644 --- a/public/index.html +++ b/public/index.html @@ -15,8 +15,10 @@ From b93ce53a7ff701389471ad8f75129cf578863439 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 26 Feb 2013 23:55:49 +0100 Subject: [PATCH 09/10] restore cookie max_age --- lib/travis/web/app/alt_versions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/web/app/alt_versions.rb b/lib/travis/web/app/alt_versions.rb index 6d16a23c..f59e3c53 100644 --- a/lib/travis/web/app/alt_versions.rb +++ b/lib/travis/web/app/alt_versions.rb @@ -16,7 +16,7 @@ class Travis::Web::App::AltVersions private def set_cookie(headers, alt) - cookie = "alt=#{alt}; path=/" + cookie = "alt=#{alt}; path=/; max-age=#{alt == 'default' ? 0 : 86400}" puts "setting cookie #{cookie}" headers['Set-Cookie'] = cookie end From 24a38d03e0eb345b27632e9c4fee675478fabbbd Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Wed, 27 Feb 2013 00:04:09 +0100 Subject: [PATCH 10/10] Revert "defer calling things until the document is ready" This reverts commit 5ae74ddcdc5cac09ea55a1eb8d8a7070dc5fdb70. --- public/index.html | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/public/index.html b/public/index.html index afa54710..fb4970ef 100644 --- a/public/index.html +++ b/public/index.html @@ -15,10 +15,8 @@