about summary refs log tree commit homepage
path: root/lib/mongrel/handlers.rb
diff options
context:
space:
mode:
authorzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-06-18 04:57:26 +0000
committerzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-06-18 04:57:26 +0000
commit364270615b8e6f1bd5cc8611267137f1a83b8124 (patch)
treeaff4bf91f421237f8fb39e666c9e7f4666fbe356 /lib/mongrel/handlers.rb
parente5c6b9ad70d844f4d656efa2306c2f7973cb2fee (diff)
downloadunicorn-364270615b8e6f1bd5cc8611267137f1a83b8124.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@245 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib/mongrel/handlers.rb')
-rw-r--r--lib/mongrel/handlers.rb59
1 files changed, 57 insertions, 2 deletions
diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb
index d20ca17..509d3e9 100644
--- a/lib/mongrel/handlers.rb
+++ b/lib/mongrel/handlers.rb
@@ -31,11 +31,19 @@ module Mongrel
   # should be implemented using the HttpHandlerPlugin mixin.
   #
   class HttpHandler
-    attr_reader :header_only
+    attr_reader :request_notify
     attr_accessor :listener
 
+    # This will be called by Mongrel on the *first* (index 0) handler *if* it has
+    # HttpHandler.request_notify set to *true*.  You only get the parameters
+    # for the request, with the idea that you'd "bound" the beginning of the
+    # request processing and the first call to process.
+    def request_begins(params)
+    end
+
     def process(request, response)
     end
+
   end
 
 
@@ -45,9 +53,12 @@ module Mongrel
   # the process method later.
   module HttpHandlerPlugin
     attr_reader :options
-    attr_reader :header_only
+    attr_reader :request_notify
     attr_accessor :listener
 
+    def request_begins(params)
+    end
+
     def initialize(options={})
       @options = options
       @header_only = false
@@ -398,4 +409,48 @@ module Mongrel
       end
     end
   end
+
+  # This handler allows you to redirect one url to another.
+  # You can use it like String#gsub, where the string is the REQUEST_URI.
+  # REQUEST_URI is the full path with GET parameters.
+  #
+  # Eg. /test/something?help=true&disclaimer=false
+  #
+  # == Examples
+  #
+  #   h = Mongrel::HttpServer.new('0.0.0.0')
+  #   h.register '/test', Mongrel::RedirectHandler.new('/to/there') # simple
+  #   h.register '/to',   Mongrel::RedirectHandler.new(/t/, 'w') # regexp
+  #   # and with a block
+  #   h.register '/hey',  Mongrel::RedirectHandler.new(/(\w+)/) { |match| ... }
+  #
+  class RedirectHandler < Mongrel::HttpHandler
+    # You set the rewrite rules when building the object.
+    #
+    # pattern            => What to look for or replacement if used alone
+    #
+    # replacement, block => One of them is used to replace the found text
+
+    def initialize(pattern, replacement = nil, &block)
+      unless replacement or block
+        @replacement = pattern
+      else
+        @pattern, @replacement, @block = pattern, replacement, block
+      end
+    end
+
+    # Process the request and return a redirect response
+    def process(request, response)
+      unless @pattern
+        response.socket.write(Mongrel::Const::REDIRECT % @replacement)
+      else
+        if @block
+          new_path = request.params['REQUEST_URI'].gsub(@pattern, &@block)
+        else
+          new_path = request.params['REQUEST_URI'].gsub(@pattern, @replacement)
+        end
+        response.socket.write(Mongrel::Const::REDIRECT % new_path)
+      end
+    end
+  end
 end