Class: WEBrick::HTTPServlet::FileHandler
- Inherits:
-
AbstractServlet
- Object
- AbstractServlet
- WEBrick::HTTPServlet::FileHandler
- Defined in:
- lib/webrick/httpservlet/filehandler.rb
Overview
Serves files from a directory
Constant Summary collapse
- HandlerTable =
Hash.new
Class Method Summary collapse
-
.add_handler(suffix, handler) ⇒ Object
Allow custom handling of requests for files with
suffix
by classhandler
. -
.remove_handler(suffix) ⇒ Object
Remove custom handling of requests for files with
suffix
.
Instance Method Summary collapse
- #do_GET(req, res) ⇒ Object
- #do_OPTIONS(req, res) ⇒ Object
- #do_POST(req, res) ⇒ Object
-
#initialize(server, root, options = {}, default = Config::FileHandler) ⇒ FileHandler
constructor
Creates a FileHandler servlet on
server
that serves files starting at directoryroot
. - #service(req, res) ⇒ Object
Methods inherited from AbstractServlet
Constructor Details
#initialize(server, root, options = {}, default = Config::FileHandler) ⇒ FileHandler
Creates a FileHandler servlet on server
that serves files starting at directory root
If options
is a Hash the following keys are allowed:
- :AcceptableLanguages
-
Array of languages allowed for accept-language
- :DirectoryCallback
-
Allows preprocessing of directory requests
- :FancyIndexing
-
If true, show an index for directories
- :FileCallback
-
Allows preprocessing of file requests
- :HandlerCallback
-
Allows preprocessing of requests
- :HandlerTable
-
Maps file suffixes to file handlers. DefaultFileHandler is used by default but any servlet can be used.
- :NondisclosureName
-
Do not show files matching this array of globs
- :UserDir
-
Directory inside ~user to serve content from for /~user requests. Only works if mounted on /
If options
is true or false then :FancyIndexing
is enabled or disabled respectively.
170 171 172 173 174 175 176 177 178 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 170 def initialize(server, root, ={}, default=Config::FileHandler) @config = server.config @logger = @config[:Logger] @root = File.(root) if == true || == false = { :FancyIndexing => } end @options = default.dup.update() end |
Class Method Details
.add_handler(suffix, handler) ⇒ Object
Allow custom handling of requests for files with suffix
by class handler
138 139 140 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 138 def self.add_handler(suffix, handler) HandlerTable[suffix] = handler end |
.remove_handler(suffix) ⇒ Object
Remove custom handling of requests for files with suffix
145 146 147 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 145 def self.remove_handler(suffix) HandlerTable.delete(suffix) end |
Instance Method Details
#do_GET(req, res) ⇒ Object
201 202 203 204 205 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 201 def do_GET(req, res) unless exec_handler(req, res) set_dir_list(req, res) end end |
#do_OPTIONS(req, res) ⇒ Object
213 214 215 216 217 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 213 def do_OPTIONS(req, res) unless exec_handler(req, res) super(req, res) end end |
#do_POST(req, res) ⇒ Object
207 208 209 210 211 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 207 def do_POST(req, res) unless exec_handler(req, res) raise HTTPStatus::NotFound, "`#{req.path}' not found." end end |
#service(req, res) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/webrick/httpservlet/filehandler.rb', line 180 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 |