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::HTTPServlet::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.



203
204
205
206
207
208
209
210
211
# File 'lib/webrick/httpservlet/filehandler.rb', line 203

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



182
183
184
# File 'lib/webrick/httpservlet/filehandler.rb', line 182

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

.remove_handler(suffix) ⇒ Object

Remove custom handling of requests for files with suffix



189
190
191
# File 'lib/webrick/httpservlet/filehandler.rb', line 189

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

Instance Method Details

#do_GET(req, res) ⇒ Object



245
246
247
248
249
# File 'lib/webrick/httpservlet/filehandler.rb', line 245

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

#do_OPTIONS(req, res) ⇒ Object



257
258
259
260
261
# File 'lib/webrick/httpservlet/filehandler.rb', line 257

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

#do_POST(req, res) ⇒ Object



251
252
253
254
255
# File 'lib/webrick/httpservlet/filehandler.rb', line 251

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

#service(req, res) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/webrick/httpservlet/filehandler.rb', line 224

def service(req, res)
  # if this class is mounted on "/" and /~username is requested.
  # we're going to override path information 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

#set_filesystem_encoding(str) ⇒ Object

:stopdoc:



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

def set_filesystem_encoding(str)
  enc = Encoding.find('filesystem')
  if enc == Encoding::US_ASCII
    str.b
  else
    str.dup.force_encoding(enc)
  end
end