Class: Mongrel::RedirectHandler

Class | Mongrel::RedirectHandler
In: | lib/mongrel/handlers.rb lib/mongrel/handlers.rb
Parent: | Mongrel::HttpHandler

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| ... }

### Methods

new new process process

### Public Class methods

new(pattern, replacement = nil, &block)

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

[Source]

         # File lib/mongrel/handlers.rb, line 447
    447:     def initialize(pattern, replacement = nil, &block)
    448:       unless replacement or block
    449:         @pattern, @replacement = nil, pattern
    450:       else
    451:         @pattern, @replacement, @block = pattern, replacement, block
    452:       end
    453:     end

new(pattern, replacement = nil, &block)

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

[Source]

         # File lib/mongrel/handlers.rb, line 447
    447:     def initialize(pattern, replacement = nil, &block)
    448:       unless replacement or block
    449:         @pattern, @replacement = nil, pattern
    450:       else
    451:         @pattern, @replacement, @block = pattern, replacement, block
    452:       end
    453:     end

### Public Instance methods

process(request, response)

Process the request and return a redirect response

[Source]

         # File lib/mongrel/handlers.rb, line 456
    456:     def process(request, response)
    457:       unless @pattern
    458:         response.socket.write(Mongrel::Const::REDIRECT % @replacement)
    459:       else
    460:         if @block
    461:           new_path = request.params['REQUEST_URI'].gsub(@pattern, &@block)
    462:         else
    463:           new_path = request.params['REQUEST_URI'].gsub(@pattern, @replacement)
    464:         end
    465:         response.socket.write(Mongrel::Const::REDIRECT % new_path)
    466:       end
    467:     end

process(request, response)

Process the request and return a redirect response

[Source]

         # File lib/mongrel/handlers.rb, line 456
    456:     def process(request, response)
    457:       unless @pattern
    458:         response.socket.write(Mongrel::Const::REDIRECT % @replacement)
    459:       else
    460:         if @block
    461:           new_path = request.params['REQUEST_URI'].gsub(@pattern, &@block)
    462:         else
    463:           new_path = request.params['REQUEST_URI'].gsub(@pattern, @replacement)
    464:         end
    465:         response.socket.write(Mongrel::Const::REDIRECT % new_path)
    466:       end
    467:     end

[Validate]