class Sgfa::Demo::WebBinders

Demo of the web interface to {Binder}s using {BinderFs} with the first part of the path being the Binder name. The user is taken from the REMOTE_USER with no groups.

Public Class Methods

new(dir, css) click to toggle source

New web binder demo

@param dir [String] The directory @param css [String] URL for the style sheet

# File lib/sgfa/demo/web_binders.rb, line 34
def initialize(dir, css)
 @path = dir
 @css = css
 @app = Sgfa::Web::Binder.new
end

Public Instance Methods

call(env) click to toggle source

Rack call

@param env [Hash] The rack environment @return [Array] Response, Headers, Body

# File lib/sgfa/demo/web_binders.rb, line 47
def call(env)
  bnd = Sgfa::BinderFs.new
  env['sgfa.user'] = env['REMOTE_USER'].dup
  env['sgfa.groups'] = []
  env['sgfa.css'] = @css

  # get binder name
  old_path = env['PATH_INFO']
  old_script = env['SCRIPT_NAME']
  path = old_path.split('/')
  return not_found if path.empty?
  bnam = Rack::Utils.unescape(path[1])
  return not_found if bnam[0] == '.' || bnam[0] == '_'

  # Adjust SCRIPT_NAME and PATH_INFO
  env['PATH_INFO'] = '/' + path[2..-1].join('/')
  new_script = old_script + path[0,2].join('/')
  env['SCRIPT_NAME'] = new_script.dup

  # Open binder
  begin
    bnd.open(File.join(@path, bnam))
  rescue Sgfa::Error::NonExistent => exp
    return not_found
  end

  # call app
  begin
    env['sgfa.binder'] = bnd
    env['sgfa.binder.url'] = new_script.dup
    env['sgfa.binder.name'] = bnam
    ret = @app.call(env)
  ensure
    bnd.close
    env['PATH_INFO'] = old_path
    env['SCRIPT_NAME'] = old_script
  end

  return ret
    
rescue => exc
  err = []
  err.push "<html><head><title>error</title></head>\n<body>\n"
  err.push "<p>" + Rack::Utils.escape_html(exc.class.name) + "</p>\n"
  err.push "<p>" + Rack::Utils.escape_html(exc.message) + "</p>\n"
  exc.backtrace.each do |bt|
    err.push "<p>" + Rack::Utils.escape_html(bt) + "</p>\n"
  end
  err.push "</body>\n</html>\n"
  return ['404', {'Content-Type' => 'text/html'}, err]
end
not_found() click to toggle source

Generic not found error

# File lib/sgfa/demo/web_binders.rb, line 102
def not_found()
  [404, {'Content-Type' => 'text/plain'}, ['Not found.']]
end