Sha256: 1f237a61eeefc17830f18f609e03233a70c392a9bf6d1d1202d48397416074b2

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# -*- encoding: binary -*-

# Matches GET and HEAD requests in the format of ":domain.example.com/:dkey"
# where domain is read from the Host: header.
#
# If a client were to make a request to "http://foo.example.com/bar",
# this endpoint would respond with the file from the "foo" domain
# with the key "bar".
# To use as middleware:
#    require 'regurgitator'
#    db = Sequel.connect('mysql2://user@example.com/mogilefs')
#    use Regurgitator::DomainHost, :suffix => '.example.com', :db => db
#
# See the {domain_host.ru}[link:examples/domain_host.ru]
# example for a standalone app.

class Regurgitator::DomainHost
  include Regurgitator::Endpoint

  def initialize(app, opts) # :nodoc:
    case suffix = opts[:suffix]
    when String
      suffix = Regexp.quote(suffix)
      suffix = %r{\A(.*)#{suffix}\z}
    when Regexp
    else
      raise TypeError, ":suffix must be a String or Regexp #{suffix.inspect}"
    end
    @domain_regexp = suffix
    super(app, opts[:db])
  end

  def call(env) # :nodoc:
    case env['REQUEST_METHOD']
    when 'GET', 'HEAD'
      host = env['HTTP_HOST'] or return @app.call(env)
      domain = @domain_regexp =~ host ? $1 : host
      serve_file(env, domain, env['PATH_INFO'][1..-1])
    else
      @app.call(env)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regurgitator-0.0.0 lib/regurgitator/domain_host.rb