<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>test/proxy_test.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,6 +1,7 @@
 2009-06-04 - Sean Huber (shuber@huberry.com)
   * Update test related rake tasks
   * Remove huberry namespace
+  * Add support for multiple domains
 
 2009-05-20 - Sean Huber (shuber@huberry.com)
   * Update date in gemspec so github rebuilds gem</diff>
      <filename>CHANGELOG</filename>
    </modified>
    <modified>
      <diff>@@ -1,25 +1,24 @@
-Proxy
-=====
+# Proxy
 
-A gem/plugin that allows rails applications to dynamically respond to proxied requests by detecting forwarded host/uri headers and setting the session domain, default host, and relative url root. The plugin adds this functionality to calls to url\_for, named route helpers, and view url helpers while still allowing you to specifically set the :host and :only\_path options to override this behavior.
+A gem/plugin that allows rails applications to dynamically respond to multiple domains and proxied requests by detecting forwarded host/uri headers and setting the session domain, default host, and relative url root. The plugin adds this functionality to calls to url\_for, named route helpers, and view url helpers while still allowing you to specifically set the :host and :only\_path options to override this behavior.
 
 The original session domain, default host, and relative url root will be restored after each request.
 
 Requires actionpack version &gt;= 2.0.0
 
 
-Installation
-------------
+## Installation
 
 	script/plugin install git://github.com/shuber/proxy.git
 	OR
 	gem install shuber-proxy --source http://gems.github.com
 
 
-Usage
------
+## Usage
 
-Lets say you have a suite of hosted applications all running on the same domain but mounted on different paths. One of them is an order/invoicing application located at:
+### Proxied Requests
+
+Let's say you have a suite of hosted applications all running on the same domain but mounted on different paths. One of them is an order/invoicing application located at:
 
 	http://client.example.com/orders
 
@@ -29,6 +28,9 @@ Imagine you sold an account to a client but the client wants the application to
 
 This plugin will automatically detect this forwarded host and set the session domain and default host (for url generation) accordingly.
 
+
+### Proxied Requests with Custom URIs
+
 Now imagine the client had an existing ordering system already running at /orders, and he wants to slowly migrate his data into your application, so he'll need both applications running for awhile. He wants to keep his original ordering application running at /orders and he wants your application running at:
 
 	http://clientdomain.com/neworders
@@ -42,8 +44,7 @@ Note: this plugin looks for a request header called 'HTTP\_X\_FORWARDED\_URI' to
 You can add that line in environment.rb or an initializer.
 
 
-Relative Url Root Proxy Setup
------------------------------
+#### Relative Url Root Proxy Setup
 
 The client's proxy must forward the request uri header in order for this plugin to automatically set the relative url root correctly. Here is how the client would setup a proxy in apache for the example above:
 
@@ -51,7 +52,42 @@ The client's proxy must forward the request uri header in order for this plugin
 	RequestHeader append X_FORWARDED_URI %{originalUri}e e=originalUri
 
 
-Contact
--------
+### Multiple Domains
+
+Imagine you have a CMS that hosts multiple client sites. You want your users to manage their sites on your root domain `http://yourcmsapp.com` and you display a site's public content when it's accessed by its subdomain (e.g. `http://cool-site.yourcmsapp.com`). You'll probably be using [subdomain-fu](http://github.com/mbleigh/subdomain-fu) so you can route based on subdomains like:
+
+	ActionController::Routing::Routes.draw do |map|
+	  # this routing controller has a before_filter callback that looks up a site by subdomain
+	  map.public_page '*path', :controller =&gt; 'routing', :conditions =&gt; { :subdomain =&gt; /^[^\.]+$/ }
+	
+	  map.with_options :conditions =&gt; { :subdomain =&gt; nil } do |admin|
+	    admin.resource :account, :controller =&gt; 'account'
+	    admin.resources :sites
+	    ...
+	  end
+	end
+
+Now, it gets tricky if you want `http://cool-site.com` to render `cool-site`'s public content because you can't tell if this request has a subdomain or not. In order for your routes to work, you must have all requests coming in from your domain `yourcmsapp.com`. You can accomplish this by calling the `Proxy.replace_host_with(&amp;block)` method like so:
+
+	class ApplicationController &lt; ActionController::Base
+	  
+	  # you can put this in an initializer or something instead if you'd like
+	  Proxy.replace_host_with do |request|
+	    &quot;#{Site.find_by_domain(request.host).try(:subdomain) || '-INVALID-'}.yourcmsapp.com&quot; unless request.host =~ /(.*\.|^)yourcmsapp.com$/i
+	  end
+	  
+	end
+
+Let's examine what this block is doing:
+
+* First, it checks if the current request's host is already on your domain. If it is, we don't need to do anything, otherwise...
+* It checks if a site exists with a domain that matches the current request's host.
+* If a site does exist, a new host is returned using the site's `subdomain` with your app domain and everything renders fine, otherwise...
+* A fake host is returned (-INVALID-.yourcmsapp.com), and the request 404s once it gets to your `routing` controller and a site can't be found with the subdomain `-INVALID-`
+
+If `nil, false, or an empty string` is returned when you call the `Proxy.replace_host_with` method, the current request's host is not modified. Otherwise, the `HTTP_X_FORWARDED_HOST` request header is set to an array: `[the_original_host, the_new_host]`. This allows your routes to use your domain when evaluating routing conditions and also allows all of the application's url generators to use the original host.
+
+
+## Contact
 
 Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com)
\ No newline at end of file</diff>
      <filename>README.markdown</filename>
    </modified>
    <modified>
      <diff>@@ -4,6 +4,27 @@ require 'proxy/action_controller/named_route_collection'
 require 'proxy/action_controller/url_rewriter'
 require 'proxy/action_view/url_helper'
 
+module Proxy
+  mattr_accessor :replace_host_with_proc
+  self.replace_host_with_proc = proc { |request| }
+  
+  def self.replace_host_with(&amp;block)
+    self.replace_host_with_proc = block
+  end
+  
+  private
+  
+    def self.before_dispatch(dispatcher)
+      request = dispatcher.instance_variable_get('@request')
+      new_host = replace_host_with_proc.call(request)
+      request.env['HTTP_X_FORWARDED_HOST'] = [request.host, new_host].join(', ') unless new_host.blank?
+    end
+end
+
+ActionController::Dispatcher.before_dispatch do |dispatcher|
+  Proxy.send :before_dispatch, dispatcher
+end
+
 ActionController::AbstractRequest = ActionController::Request if defined?(ActionController::Request)
 ActionController::AbstractRequest.send :include, Proxy::ActionController::AbstractRequest
 ActionController::Base.send :include, Proxy::ActionController::Base</diff>
      <filename>lib/proxy.rb</filename>
    </modified>
    <modified>
      <diff>@@ -13,7 +13,7 @@ module Proxy
           class &lt;&lt; self; delegate :relative_url_root, :relative_url_root=, :to =&gt; ::ActionController::AbstractRequest unless ::ActionController::Base.respond_to?(:relative_url_root); end
         end
       end
-
+      
       protected
     
         # Calculates the &lt;tt&gt;relative_url_root&lt;/tt&gt; by parsing the request path out of the</diff>
      <filename>lib/proxy/action_controller/base.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,10 +1,10 @@
 Gem::Specification.new do |s|
   s.name    = 'proxy'
-  s.version = '1.3.0'
-  s.date    = '2009-05-20'
+  s.version = '1.3.1'
+  s.date    = '2009-06-04'
   
-  s.summary     = 'A gem/plugin that allows rails applications to dynamically respond to proxied requests'
-  s.description = 'A gem/plugin that allows rails applications to dynamically respond to proxied requests by detecting forwarded host/uri headers and setting the session domain, default host, and relative url root'
+  s.summary     = 'A gem/plugin that allows rails applications to respond to multiple domains and proxied requests'
+  s.description = 'A gem/plugin that allows rails applications to respond to multiple domains and proxied requests'
   
   s.author   = 'Sean Huber'
   s.email    = 'shuber@huberry.com'
@@ -18,11 +18,11 @@ Gem::Specification.new do |s|
   s.files = %w(
     CHANGELOG
     init.rb
-    lib/huberry/proxy/action_controller/abstract_request.rb
-    lib/huberry/proxy/action_controller/base.rb
-    lib/huberry/proxy/action_controller/named_route_collection.rb
-    lib/huberry/proxy/action_controller/url_rewriter.rb
-    lib/huberry/proxy/action_view/url_helper.rb
+    lib/proxy/action_controller/abstract_request.rb
+    lib/proxy/action_controller/base.rb
+    lib/proxy/action_controller/named_route_collection.rb
+    lib/proxy/action_controller/url_rewriter.rb
+    lib/proxy/action_view/url_helper.rb
     lib/proxy.rb
     MIT-LICENSE
     Rakefile
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
     test/abstract_request_test.rb
     test/base_test.rb
     test/named_route_collection_test.rb
+    test/proxy_test.rb
     test/url_helper_test.rb
     test/url_rewriter_test.rb
   )</diff>
      <filename>proxy.gemspec</filename>
    </modified>
    <modified>
      <diff>@@ -12,6 +12,7 @@ args &lt;&lt; ENV['ACTION_PACK_VERSION'] if ENV['ACTION_PACK_VERSION']
 gem *args
 require 'action_pack'
 require 'action_controller'
+require 'action_controller/dispatcher'
 require 'action_controller/routing'
 require 'action_controller/session_management'
 require 'action_controller/url_rewriter'
@@ -38,6 +39,8 @@ class ActionController::Routing::RouteSet
 	end
 end
 
+ActionController::Base.session_options.merge! :key =&gt; '_proxy_session', :secret =&gt; '60447f093cd3f5d57686dd5ca1ced83216c846047db0ec97480a5e85411d9bd41ec5587cc4aafac27a4f0e649f0c628b0955b315856b78787a96168671dc96d4'
+
 # Require the main proxy.rb file
 #
 require File.join(File.dirname(File.dirname(__FILE__)), 'lib', 'proxy')
@@ -45,6 +48,7 @@ require File.join(File.dirname(File.dirname(__FILE__)), 'lib', 'proxy')
 # Test controller
 #
 class TestController &lt; ActionController::Base
+  
   def asset_action
     render :inline =&gt; '&lt;%= image_tag &quot;test.gif&quot; %&gt;, &lt;%= javascript_include_tag &quot;test&quot; %&gt;, &lt;%= stylesheet_link_tag &quot;test&quot; %&gt;'
   end</diff>
      <filename>test/init.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>dba9f3a23bab1e7aaad5cea3f7acdfd260531b59</id>
    </parent>
  </parents>
  <author>
    <name>Sean Huber</name>
    <email>shuber@huberry.com</email>
  </author>
  <url>http://github.com/shuber/proxy/commit/db194ab4c006e9e0e69bd0b924fd140debf6fce6</url>
  <id>db194ab4c006e9e0e69bd0b924fd140debf6fce6</id>
  <committed-date>2009-06-04T14:46:56-07:00</committed-date>
  <authored-date>2009-06-04T14:46:56-07:00</authored-date>
  <message>Add support for multiple domains</message>
  <tree>4dcec23b847201aca4ffc15f7669a0489c00c4f6</tree>
  <committer>
    <name>Sean Huber</name>
    <email>shuber@huberry.com</email>
  </committer>
</commit>
