Class: Mentawai::Handler::MentaHandler

Inherits:
Rack::Handler::Mongrel
  • Object
show all
Defined in:
lib/mentawai/handler/menta_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, app) ⇒ MentaHandler

Returns a new instance of MentaHandler.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mentawai/handler/menta_handler.rb', line 9

def initialize(server, app)
  
  @server = server
  
  listing_allowed = false
  
  # Check if any application has listing turned on...

  @server.apps.each do |k,v|
    if v.listing_allowed
      listing_allowed = true
      break
    end
  end
  
  @dir_handler = Mongrel::DirHandler.new('.', listing_allowed)
  
  super app
end

Class Method Details

.add_mime_type(extension, type) ⇒ Object



32
33
34
# File 'lib/mentawai/handler/menta_handler.rb', line 32

def self.add_mime_type(extension, type)
  mime_types[extension] = type
end

.mime_typesObject



28
29
30
# File 'lib/mentawai/handler/menta_handler.rb', line 28

def self.mime_types
  Mongrel::DirHandler.const_get(:MIME_TYPES)
end

.run(menta_server, app, options = {}) {|server| ... } ⇒ Object

Yields:

  • (server)


36
37
38
39
40
41
# File 'lib/mentawai/handler/menta_handler.rb', line 36

def self.run(menta_server, app, options={})
  server = ::Mongrel::HttpServer.new(options[:Host] || '0.0.0.0', options[:Port] || 8080)
  server.register('/', MentaHandler.new(menta_server, app))
  yield server  if block_given?
  server.run.join
end

Instance Method Details

#process(request, response) ⇒ Object



43
44
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mentawai/handler/menta_handler.rb', line 43

def process(request, response)
  
  env = {}.replace(request.params)
  env["SCRIPT_NAME"] = ""  if env["SCRIPT_NAME"] == "/"
  env["QUERY_STRING"] ||= ""
  env.delete "PATH_INFO"  if env["PATH_INFO"] == ""
  
  req = Rack::Request.new(env)

  url = req.fullpath
  
  context_path = @server.find_context_path(url)
  
  app = @server.apps[context_path]
  
  context_path_dir = app.contextPathDir

  ru = RequestURL.new(context_path, context_path_dir, url, app.default_page)
  
  # Block forbidden directory...

  if ru.forbidden? then
    response.start(403) do |head,out|
      out << "You are not allowed to go inside here !!!"
    end
    return
  end

  # Block if no listing is allowed and this is a directory...

  if ru.is_dir? and not app.listing_allowed
    response.start(403) do |head,out|
      out << "Listing is not unabled for this application!"
    end
    return
  end
  
  # Block if listing is allowed but directory is not allowed...

  if ru.is_dir? and app.listing_allowed and not app.dirs_allowed_for_listing.empty?

    allowed = false
    
    app.dirs_allowed_for_listing.each do |dir|
      dir = '/' + dir if dir !~ /^\//
      if ru.url_without_context_dir =~ /^#{dir}\/?$/
        allowed = true
        break
      end
    end
    
    if not allowed
      response.start(403) do |head,out|
        out << "You cannot list this directory! (Forbidden)"
      end
      return
    end
  end
    
  if url =~ /\.#{app.extension}\??$/ or url =~ /\.#{app.extension}\?.+$/
    # We need to process this inside the Mentawai controller... (action request)

    super
  elsif (url =~ /(\.\w+)\??$/ or url =~ /(\.\w+)\?.+$/) and Mentawai::Core::Forward.is_registered?($1)
    # We need to process this inside the Mentawai controller... (ERB page request)

    super
  else
    # Change Mongrel request to reflect the context path dir...

    request.params["PATH_INFO"] = ru.url_with_context_dir

    # Let Mongrel serve this request...

    @dir_handler.process(request, response)
  end
end