[2005-05-04 01 sugi@nemui.org**20050503205147] { hunk ./dot-htaccess 10 -#RewriteRule ^(.*)$ /~sugi/sarada/nph-sarada.cgi/$1 [PT,L] +#RewriteRule ^(.*)$ /~sugi/nph-sarada.cgi/$1 [PT,L] hunk ./nph-sarada.cgi.in 30 -class WebDAVCGI < WEBrick::CGI - def initialize(root = Dir.pwd, *rest_args) +class SaradaCGI < WEBrick::CGI + def initialize(useropt={}) hunk ./nph-sarada.cgi.in 33 - @root = root + @options = { + :Root => Dir.pwd, + :NondisclosureName => + %w(.ht* sarada.conf nph-sarada.*), + } + @options.update(useropt) + @root = @options[:Root] + @options.delete :Root hunk ./nph-sarada.cgi.in 42 - @logger.level = WEBrick::Log::DEBUG - @rest_args = rest_args + @logger.level = WEBrick::Log::DEBUG if $DEBUG + @logger.debug "#{self.class} initialized. root=#{@root}" hunk ./nph-sarada.cgi.in 46 - attr_reader :config, :logger, :status + attr_reader :config, :logger hunk ./nph-sarada.cgi.in 51 - def get_target_handler - WEBrick::HTTPServlet::WebDAVHandler.get_instance(self, @root, *@rest_args) + def start(env=ENV, stdin=$stdin, stdout=$stdout) + @env = env + super + end + + def self_uri(req) + (@env['HTTPS'] ? 'https':'http') + "://" + + @env['SERVER_NAME'] + + ((!@env['HTTPS'] && @env['SERVER_PORT'] != '80' || + @env['HTTPS'] && @env['SERVER_PORT'] != 443) ? + ":#{@env['SERVER_PORT']}" : '') + + req.path + end + + def dav_handler + WEBrick::HTTPServlet::WebDAVHandler.get_instance(self, @root, @options) + end + + def dav_redir(req, res) + # FIXME: restruct this method all! for i18n, valid-HTML, user-custom and etc.. + case req["User-Agent"] + when /Konqueror/ + davuri = self_uri(req).sub(/^https?/,'webdav') + res.body << " +
+ + open as folder + + " + when /MSIE/ + res.body << " + + +Meybe, you need to add following URL as'network place' + via 'My Network'. +
#{self_uri(req)}/+ + " + else + res.body << " + +
Specify following URL on your WebDAV client.
+#{self_uri(req)}/+ + " + end hunk ./nph-sarada.cgi.in 103 - handle = get_target_handler - if req.path_info.nil? + if req.path_info.nil? || req.path_info.empty? hunk ./nph-sarada.cgi.in 105 - elsif %r|^/~([^/]+)| =~ req.path_info - user = $1 - path_info = $'.empty? ? "/" : $' - req.instance_variable_set :@path, req.path_info - req.path_info = path_info + #elsif %r|^/~([^/]+)| =~ req.path_info + # user = $1 + # path_info = $'.empty? ? "/" : $' + # req.instance_variable_set :@path, req.path_info + # req.path_info = path_info hunk ./nph-sarada.cgi.in 111 - @config[:HTTPVersion] == "1.1" and res.chunked = true + hunk ./nph-sarada.cgi.in 114 + @config[:HTTPVersion] == "1.1" and res.chunked = true hunk ./nph-sarada.cgi.in 116 - # apache will unescape headers, but we do not need that... - !req["Destination"].nil? and - req["Destination"] = HTTPUtils.escape(req["Destination"]) - handle.service(req, res) + + if req.path_info[-1..-1] == "/" && req.request_method == "GET" + dav_redir(req, res) + else + dav_handler.service(req, res) + end hunk ./nph-sarada.cgi.in 126 + ## pre-process for apache hunk ./nph-sarada.cgi.in 128 - if env.has_key?("REDIRECT_URL") && !env["REDIRECT_URL"].empty? - env["PATH_INFO"] = env["REDIRECT_URL"] + #if env.has_key?("REDIRECT_URL") && !env["REDIRECT_URL"].empty? + # env["PATH_INFO"] = env["REDIRECT_URL"] + #end + # apache will unescape headers?? maybe mod_encoding's bug??? + #env.has_key?("HTTP_DESTINATION") and + # env["HTTP_DESTINATION"] = WEBrick::HTTPUtils.escape(env["HTTP_DESTINATION"]) + + if File.exists? "./saradaconf.yaml" + begin + require 'yaml' + opts = YAML.load(File.read("./saradaconf.yaml")) + Hash === opts or raise TypeError.new("non Hash element from yaml") + rescue => ex + opts = nil + $stderr.puts "Error when loading config: #{ex}" + end hunk ./nph-sarada.cgi.in 145 - begin - load "./sarada.conf" - rescue LoadError - # just ignore + if $DEBUG + $stderr.puts "== headers ==", env.sort.map{|k,v|"\t#{k}= #{v}"} hunk ./nph-sarada.cgi.in 148 - WebDAVCGI.new().start(env) + SaradaCGI.new(*[opts].compact).start(env) hunk ./standalone.rb 2 - hunk ./standalone.rb 3 + hunk ./webdavhandler.rb 1 +# +# webdavhandler.rb - WEBrick WebDAV handler +# +# Author: Tatsuki Sugiura