Class: WEBrick::HTTPServlet::FileHandler

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

Constant Summary collapse

HandlerTable =
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

Returns a new instance of FileHandler.



139
140
141
142
143
144
145
146
147
# File 'lib/webrick/httpservlet/filehandler.rb', line 139

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



131
132
133
# File 'lib/webrick/httpservlet/filehandler.rb', line 131

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

.remove_handler(suffix) ⇒ Object



135
136
137
# File 'lib/webrick/httpservlet/filehandler.rb', line 135

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

Instance Method Details

#do_GET(req, res) ⇒ Object



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

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

#do_OPTIONS(req, res) ⇒ Object



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

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

#do_POST(req, res) ⇒ Object



176
177
178
179
180
# File 'lib/webrick/httpservlet/filehandler.rb', line 176

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

#service(req, res) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/webrick/httpservlet/filehandler.rb', line 149

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