Class: Rackful::RelativeLocation

Inherits:
Object
  • Object
show all
Defined in:
gems/rackful-0.1.1/lib/rackful/relative_location.rb,
gems/rackful-0.1.1.orig/lib/rackful/relative_location.rb

Overview

Rack middleware, inspired by Rack::RelativeRedirect.

This middleware allows you to return a relative or absolute path your Location: response header. This was common practice in HTTP/1.0, but HTTP/1.1 requires a full URI in the Location: header. This middleware automatically translates your path to a full URI.

Differences with Rack::RelativeRedirect:

  • uses Rack::Request::base_url for creating absolute URIs.

  • the `Location:` header, if present, is always rectified, independent of the HTTP status code.

Since:

Instance Method Summary (collapse)

Constructor Details

- (RelativeLocation) initialize(app)

A new instance of RelativeLocation

Since:

  • 0.0.1



36
37
38
# File 'gems/rackful-0.1.1/lib/rackful/relative_location.rb', line 36

def initialize(app)
  @app = app
end

Instance Method Details

- (void) call(env)

Since:

  • 0.0.1



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'gems/rackful-0.1.1/lib/rackful/relative_location.rb', line 58

def call(env)
  res = @app.call(env)
  request = nil
  ['Location', 'Content-Location'].each do
    |header|
    if  ( location = res[1][header] ) and
        '/' == location[0,1]
      request ||= Rack::Request.new env
      res[1][header] = request.base_url + location
    end
  end
  res
end

- (void) call_old(env)

Since:

  • 0.0.1



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'gems/rackful-0.1.1/lib/rackful/relative_location.rb', line 40

def call_old(env)
  res = @app.call(env)
  if ( location = res[1]['Location'] ) and
     ! %r{\A[a-z]+://}.match(location)
    request = Rack::Request.new env
    unless '/' == location[0, 1]
      path = ( res[1]['Content-Location'] || request.path ).dup
      path[ %r{[^/]*\z} ] = ''
      location = File.expand_path( location, path )
    end
    if '/' == location[0, 1]
      location = request.base_url + location
    end
    res[1]['Location'] = location
  end
  res
end