Class: Rack::Legacy::Cgi

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/legacy/cgi.rb

Direct Known Subclasses

Php

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, public_dir = FileUtils.pwd) ⇒ Cgi

Will setup a new instance of the Cgi middleware executing programs located in the given public_dir

use Rack::Legacy::Cgi, 'cgi-bin'


14
15
16
17
# File 'lib/rack/legacy/cgi.rb', line 14

def initialize(app, public_dir=FileUtils.pwd)
  @app = app
  @public_dir = public_dir
end

Instance Attribute Details

#public_dirObject (readonly)

Returns the value of attribute public_dir



8
9
10
# File 'lib/rack/legacy/cgi.rb', line 8

def public_dir
  @public_dir
end

Instance Method Details

#call(env) ⇒ Object

Middleware, so if it looks like we can run it then do so. Otherwise send it on for someone else to handle.



21
22
23
24
25
26
27
# File 'lib/rack/legacy/cgi.rb', line 21

def call(env)
  if valid? env['PATH_INFO']
    run env, full_path(env['PATH_INFO'])
  else
    @app.call env
  end
end

#valid?(path) ⇒ Boolean

Check to ensure the path exists and it is a child of the public directory.

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/rack/legacy/cgi.rb', line 31

def valid?(path)
  fp = full_path path
  fp.start_with?(::File.expand_path public_dir) &&
  ::File.file?(fp) && ::File.executable?(fp)
end