Class: DandelionS1

Inherits:
RackRscript
  • Object
show all
Defined in:
lib/dandelion_s1.rb

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ DandelionS1

Returns a new instance of DandelionS1.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dandelion_s1.rb', line 10

def initialize(h={})

  raw_opts = {root: 'www', access: {}, static: []}.merge h
  @app_root = Dir.pwd

  @static = raw_opts[:static]
  @root = raw_opts[:root]

  #@access_list = {'/do/r/hello3' => 'user'}
  @access_list = raw_opts[:access]

  super(logfile: h[:logfile],  pkg_src: h[:pkg_src], rsc_host: h[:rsc_host], 
        rsc_package_src: h[:rsc_package_src])
end

Instance Method Details

#call(e) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dandelion_s1.rb', line 25

def call(e)

  private_user = @access_list[e['REQUEST_PATH']]
  
  
  if private_user.nil? then 
    super(e)
  elsif private_user.is_a? String and private_user == e['REMOTE_USER']
    super(e)
  elsif private_user.is_a? Array and private_user.any? {|x| x == e['REMOTE_USER']}
    super(e)
  else
    request = '/unauthorised/'
    content, content_type, status_code = run_route(request)        
    content_type ||= 'text/html'
    [status_code=401, {"Content-Type" => content_type}, [content]]
  end

end

#default_routes(env, params) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dandelion_s1.rb', line 45

def default_routes(env, params)

  super(env, params)

  get /^(\/(?:#{@static.join('|')}).*)/ do |path|

    filepath = File.join(@app_root, @root, path)
    log("root: %s path: %s" % [@root, path])

    if path.length < 1 or path[-1] == '/' then
      path += 'index.html' 
      File.read filepath
    elsif File.directory? filepath then
      Redirect.new (path + '/') 
    elsif File.exists? filepath then
      h = {xml: 'application/xml', html: 'text/html', png: 'image/png', 
           jpg: 'image/jpeg', txt: 'text/plain', css: 'text/css',
           xsl: 'application/xml'}
      content_type = h[filepath[/\w+$/].to_sym]
      [File.read(filepath), content_type || 'text/plain']
    else
      'oops, file ' + filepath + ' not found'
    end

  end

  get /^\/$/ do

    file = File.join(@root, 'index.html')
    File.read file
  end

  get '/unauthorised/' do
    'unauthorised user'
  end
 
end