lib/net/dav.rb in net_dav-0.4.1 vs lib/net/dav.rb in net_dav-0.5.0

- old
+ new

@@ -360,13 +360,39 @@ def credentials(user, pass) @handler.user = user @handler.pass = pass end - def propfind(path) #:nodoc: + # Perform a PROPFIND request + # + # Example: + # + # Basic propfind: + # + # properties = propfind('/path/') + # + # Get ACL for resource: + # + # properties = propfind('/path/', :acl) + # + # Custom propfind: + # + # properties = propfind('/path/', '<?xml version="1.0" encoding="utf-8"?>...') + # + # See http://webdav.org/specs/rfc3744.html#rfc.section.5.9 for more on + # how to retrieve access control properties. + def propfind(path,*options) headers = {'Depth' => '1'} - body = '<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>' + if(options[0] == :acl) + body = '<?xml version="1.0" encoding="utf-8" ?><D:propfind xmlns:D="DAV:"><D:prop><D:owner/>' + + '<D:supported-privilege-set/><D:current-user-privilege-set/><D:acl/></D:prop></D:propfind>' + else + body = options[0] + end + if(!body) + body = '<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>' + end res = @handler.request(:propfind, path, body, headers) Nokogiri::XML.parse(res.body) end # Find files and directories, yields Net::DAV::Item @@ -397,18 +423,18 @@ def find(path, options = {}) path = @uri.merge(path).path namespaces = {'x' => "DAV:"} begin doc = propfind(path) - rescue Net::HTTPServerException => e + rescue Net::ProtocolError => e msg = e.to_s + ": " + path.to_s if(options[:suppress_errors])then # Ignore dir if propfind returns an error warn("Warning: " + msg) return nil else - raise Net::HTTPServerException.new(msg, nil) + raise e.class.new(msg, nil) end end path.sub!(/\/$/, '') doc./('.//x:response', namespaces).each do |item| uri = @uri.merge(item.xpath("x:href", namespaces).inner_text) @@ -544,9 +570,26 @@ '</d:prop>' + '</d:set>' + '</d:propertyupdate>' res = @handler.request(:proppatch, path, body, headers) Nokogiri::XML.parse(res.body) + end + + # Returns true if resource exists on server. + # + # Example: + # dav.exists?('https://www.example.com/collection/') => true + # dav.exists?('/collection/') => true + def exists?(path) + path = @uri.merge(path).path + headers = {'Depth' => '1'} + body = '<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>' + begin + res = @handler.request(:propfind, path, body, headers) + rescue + return false + end + return (res.is_a? Net::HTTPSuccess) end # Makes a new directory (collection) def mkdir(path) path = @uri.merge(path).path