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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dandelion_s1.rb', line 11

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]
      
  h2 = SimpleConfig.new(access_list).to_h
  conf_access = h2[:body] || h2
  @access_list = conf_access.inject({}) \
                              {|r,x| k,v = x; r.merge(k.to_s => v.split)}
  
  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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dandelion_s1.rb', line 31

def call(e)

  request = e['REQUEST_PATH']
  r = @access_list.detect {|k,v| request =~ Regexp.new(k)}
  private_user = r ? r.last : nil
  
  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



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
82
83
84
85
86
87
88
# File 'lib/dandelion_s1.rb', line 52

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