Class: WEBrick::HTTPServlet::FileHandler

Inherits:
AbstractServlet show all
Defined in:
lib/webrick/httpservlet/filehandler.rb

Overview

Serves a directory including fancy indexing and a variety of other options.

Example:

server.mount '/assets', WEBrick::FileHandler, '/path/to/assets'

Constant Summary collapse

HandlerTable =

:nodoc:

Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractServlet

#do_HEAD, get_instance

Constructor Details

#initialize(server, root, options = {}, default = Config::FileHandler) ⇒ FileHandler

Creates a FileHandler servlet on server that serves files starting at directory root

options may be a Hash containing keys from WEBrick::Config::FileHandler or true or false.

If options is true or false then :FancyIndexing is enabled or disabled respectively.



184
185
186
187
188
189
190
191
192
# File 'lib/webrick/httpservlet/filehandler.rb', line 184

def initialize(server, root, options={}, default=Config::FileHandler)
  @config = server.config
  @logger = @config[:Logger]
  @root = File.expand_path(root)
  if options == true || options == false
    options = { :FancyIndexing => options }
  end
  @options = default.dup.update(options)
end

Class Method Details

.add_handler(suffix, handler) ⇒ Object

Allow custom handling of requests for files with suffix by class handler



163
164
165
# File 'lib/webrick/httpservlet/filehandler.rb', line 163

def self.add_handler(suffix, handler)
  HandlerTable[suffix] = handler
end

.remove_handler(suffix) ⇒ Object

Remove custom handling of requests for files with suffix



170
171
172
# File 'lib/webrick/httpservlet/filehandler.rb', line 170

def self.remove_handler(suffix)
  HandlerTable.delete(suffix)
end

Instance Method Details

#do_GET(req, res) ⇒ Object



217
218
219
220
221
# File 'lib/webrick/httpservlet/filehandler.rb', line 217

def do_GET(req, res)
  unless exec_handler(req, res)
    set_dir_list(req, res)
  end
end

#do_OPTIONS(req, res) ⇒ Object



229
230
231
232
233
# File 'lib/webrick/httpservlet/filehandler.rb', line 229

def do_OPTIONS(req, res)
  unless exec_handler(req, res)
    super(req, res)
  end
end

#do_POST(req, res) ⇒ Object



223
224
225
226
227
# File 'lib/webrick/httpservlet/filehandler.rb', line 223

def do_POST(req, res)
  unless exec_handler(req, res)
    raise HTTPStatus::NotFound, "`#{req.path}' not found."
  end
end

#service(req, res) ⇒ Object

:stopdoc:



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/webrick/httpservlet/filehandler.rb', line 196

def service(req, res)
  # if this class is mounted on "/" and /~username is requested.
  # we're going to override path informations before invoking service.
  if defined?(Etc) && @options[:UserDir] && req.script_name.empty?
    if %r|^(/~([^/]+))| =~ req.path_info
      script_name, user = $1, $2
      path_info = $'
      begin
        passwd = Etc::getpwnam(user)
        @root = File::join(passwd.dir, @options[:UserDir])
        req.script_name = script_name
        req.path_info = path_info
      rescue
        @logger.debug "#{self.class}#do_GET: getpwnam(#{user}) failed"
      end
    end
  end
  prevent_directory_traversal(req, res)
  super(req, res)
end