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.



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

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



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

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

.remove_handler(suffix) ⇒ Object

Remove custom handling of requests for files with suffix



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

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

Instance Method Details

#do_GET(req, res) ⇒ Object



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

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

#do_OPTIONS(req, res) ⇒ Object



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

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

#do_POST(req, res) ⇒ Object



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

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

#service(req, res) ⇒ Object

:stopdoc:



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

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