Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit cb2e3721d9c7394c9153079cd943678bc57cc0e4 @dhh committed Nov 27, 2008
Showing with 176 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +18 −0 README
  3. +22 −0 Rakefile
  4. +52 −0 lib/asset_hosting_with_minimum_ssl.rb
  5. +64 −0 test/asset_hosting_with_minimum_ssl_test.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 David Heinemeier Hansson
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 README
@@ -0,0 +1,18 @@
+AssetHostingWithMinimumSsl
+==========================
+
+It's slow to serve assets that doesn't need to be secure over SSL. This plugin will let you send assets over non-SSL as often as possible.
+
+
+Example
+=======
+
+In your config/production.rb
+
+config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
+ "http://assets%d.example.com", # will serve non-SSL assetts on http://assets[1-4].example.com
+ "https://assets1.example.com" # will serve SSL assets on https://assets1.example.com
+)
+
+
+Copyright (c) 2008 David Heinemeier Hansson, released under the MIT license
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the asset_hosting_with_minimum_ssl plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the asset_hosting_with_minimum_ssl plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'AssetHostingWithMinimumSsl'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,52 @@
+class AssetHostingWithMinimumSsl
+ attr_accessor :asset_host, :ssl_asset_host
+
+ def initialize(asset_host, ssl_asset_host)
+ self.asset_host, self.ssl_asset_host = asset_host, ssl_asset_host
+ end
+
+ def call(source, request)
+ if request.ssl?
+ case
+ when javascript_file?(source)
+ ssl_asset_host(source)
+ when safari?(request)
+ asset_host(source)
+ when firefox?(request) && image_file?(source)
+ asset_host(source)
+ else
+ ssl_asset_host(source)
+ end
+ else
+ asset_host(source)
+ end
+ end
+
+
+ private
+ def asset_host(source)
+ @asset_host % (source.hash % 4)
+ end
+
+ def ssl_asset_host(source)
+ @ssl_asset_host % (source.hash % 4)
+ end
+
+
+ def javascript_file?(source)
+ source =~ /\.js$/
+ end
+
+ def image_file?(source)
+ source =~ /^\/images/
+ end
+
+
+ def safari?(request)
+ request.headers["USER_AGENT"] =~ /Safari/
+ end
+
+ def firefox?(request)
+ request.headers["USER_AGENT"] =~ /Firefox/
+ end
+end
@@ -0,0 +1,64 @@
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+
+$LOAD_PATH << File.dirname(__FILE__) + "/../lib"
+require 'asset_hosting_with_minimum_ssl'
+
+
+class AssetHostingWithMinimumSslTest < Test::Unit::TestCase
+ def setup
+ @asset_host = AssetHostingWithMinimumSsl.new("http://assets%d.example.com/", "https://assets1.example.com/")
+ end
+
+ def test_ssl_requests_for_javascript_files_should_stay_ssl_regardless_of_the_browser
+ %w( Safari Firefox IE ).each do |browser|
+ assert_equal \
+ ssl_host,
+ @asset_host.call("/javascripts/prototype.js", ssl_request_from(browser))
+ end
+ end
+
+ def test_ssl_requests_for_anything_but_js_files_should_go_non_ssl_on_safari
+ assert_match \
+ non_ssl_host,
+ @asset_host.call("/images/blank.gif", ssl_request_from("Safari"))
+
+ assert_match \
+ non_ssl_host,
+ @asset_host.call("/stylesheets/application.css", ssl_request_from("Safari"))
+ end
+
+ def test_ssl_requests_for_image_files_should_go_non_ssl_on_firefox
+ assert_match \
+ non_ssl_host,
+ @asset_host.call("/images/blank.gif", ssl_request_from("Firefox"))
+ end
+
+ def test_ssl_requests_for_non_image_files_should_stay_ssl_on_firefox
+ assert_match \
+ ssl_host,
+ @asset_host.call("/stylesheets/application.css", ssl_request_from("Firefox"))
+ end
+
+ def test_ssl_requests_for_anything_should_stay_ssl_on_ie
+ assert_match \
+ ssl_host,
+ @asset_host.call("/stylesheets/application.css", ssl_request_from("IE"))
+ end
+
+
+ private
+ def non_ssl_host
+ %r|http://assets\d.example.com/|
+ end
+
+ def ssl_host
+ "https://assets1.example.com/"
+ end
+
+
+ def ssl_request_from(user_agent)
+ stub(:headers => { "USER_AGENT" => user_agent }, :ssl? => true)
+ end
+end

1 comment on commit cb2e372

bensie commented on cb2e372 Dec 3, 2008

Should request.headers[‘USER_AGENT’] be request.headers[‘HTTP_USER_AGENT’]?

Also, the regexp for /Safari/ also includes Google Chrome, which throws a non-ssl warning. You just can’t win with these dumb browsers.

Please sign in to comment.