Module | WebDAV |
In: |
lib/davclient.rb
lib/davclient.rb |
Main WebDAV client. Current working URL is stored in a tempfile named /tmp/cwurl.pid, where pid is the parent process. Username an password is stored in the ~/.netrc file. This means there is no need to set up any sessions or connections.
If a change directory command is executed with a relative path, like for instance WebDAV.cd("../collection"), it will raise an exception if current working url has not been set. First a change directory command is executed it should use an absolute URL, like for instance WebDAV.cd("www.webdav.org/collection/").
If using an URL with a server name not found in the ~/.netrc file, instructions for adding the necessary servername, username and password will be printed to stdout.
CWURL | = | url |
CWURL | = | url |
Returns current working url.
# File lib/davclient.rb, line 41 41: def self.CWURL 42: return $CWURL if($CWURL) 43: cwurl = nil 44: filename = DavClient.cwurl_filename 45: if(File.exists?(filename)) 46: File.open(filename, 'r') {|f| cwurl = f.read() } 47: end 48: return cwurl 49: end
Returns current working url.
# File lib/davclient.rb, line 41 41: def self.CWURL 42: return $CWURL if($CWURL) 43: cwurl = nil 44: filename = DavClient.cwurl_filename 45: if(File.exists?(filename)) 46: File.open(filename, 'r') {|f| cwurl = f.read() } 47: end 48: return cwurl 49: end
Sets current working url.
Current working url is stored in aa tempfile with parent process pid as part of the filename.
# File lib/davclient.rb, line 113 113: def self.CWURL=(url) 114: $CWURL = url 115: File.open(DavClient.cwurl_filename, 'w') {|f| f.write(url) } 116: # puts "DEBUG: writing " + DavClient.cwurl_filename 117: end
Sets current working url.
Current working url is stored in aa tempfile with parent process pid as part of the filename.
# File lib/davclient.rb, line 113 113: def self.CWURL=(url) 114: $CWURL = url 115: File.open(DavClient.cwurl_filename, 'w') {|f| f.write(url) } 116: # puts "DEBUG: writing " + DavClient.cwurl_filename 117: end
Make relative url absolute. Returns error if no current working url has been set.
Example:
WebDAV.cd("https://example.org/subfolder") print WebDAV.absoluteUrl("..") => "https://example.org/"
# File lib/davclient.rb, line 58 58: def self.absoluteUrl(url) 59: if(not(url =~ /^http.?:\/\//))then 60: cwurl = Pathname.new(self.CWURL) 61: cwurl = cwurl + url 62: url = cwurl.to_s 63: # url = url + "/" if(not(url =~ /\/$/)) 64: 65: if(not(url =~ /^http.?:\/\//))then 66: raise "illegal url: " + url 67: end 68: end 69: return url 70: end
Make relative url absolute. Returns error if no current working url has been set.
Example:
WebDAV.cd("https://example.org/subfolder") print WebDAV.absoluteUrl("..") => "https://example.org/"
# File lib/davclient.rb, line 58 58: def self.absoluteUrl(url) 59: if(not(url =~ /^http.?:\/\//))then 60: cwurl = Pathname.new(self.CWURL) 61: cwurl = cwurl + url 62: url = cwurl.to_s 63: # url = url + "/" if(not(url =~ /\/$/)) 64: 65: if(not(url =~ /^http.?:\/\//))then 66: raise "illegal url: " + url 67: end 68: end 69: return url 70: end
Change current working url. Takes relative pathnames. First time this method is called, an absolute URL must be used. Raises exception if servername is not found in ~/.netrc file.
Examples:
WebDAV.cd("http://www.example.org") WebDAV.cd("../folder")
# File lib/davclient.rb, line 98 98: def self.cd(url) 99: url = absoluteUrl(url) 100: url = url + "/" if(not(url =~ /\/$/)) 101: resource = WebDAV.propfind(url) 102: if(resource and resource.isCollection?)then 103: WebDAV.CWURL = url 104: else 105: raise Exception, "#{$0} cd: #{url}: No such collection on remote server." 106: end 107: end
Change current working url. Takes relative pathnames. First time this method is called, an absolute URL must be used. Raises exception if servername is not found in ~/.netrc file.
Examples:
WebDAV.cd("http://www.example.org") WebDAV.cd("../folder")
# File lib/davclient.rb, line 98 98: def self.cd(url) 99: url = absoluteUrl(url) 100: url = url + "/" if(not(url =~ /\/$/)) 101: resource = WebDAV.propfind(url) 102: if(resource and resource.isCollection?)then 103: WebDAV.CWURL = url 104: else 105: raise Exception, "#{$0} cd: #{url}: No such collection on remote server." 106: end 107: end
Copy resources
Examples:
WebDAV.cp("src.html","https://example.org/destination/destination.html"
# File lib/davclient.rb, line 345 345: def self.cp(src,dest) 346: srcUrl = absoluteUrl(src) 347: destUrl = absoluteUrl(dest) 348: 349: # puts "DEBUG: " + srcUrl + " => " + destUrl 350: curl_command = CURL_COPY.sub("<!--destination-->", destUrl) + " " + srcUrl 351: response = DavClient.exec_curl(curl_command) 352: 353: if(response == "")then 354: return destUrl 355: end 356: return false 357: end
Copy resources
Examples:
WebDAV.cp("src.html","https://example.org/destination/destination.html"
# File lib/davclient.rb, line 345 345: def self.cp(src,dest) 346: srcUrl = absoluteUrl(src) 347: destUrl = absoluteUrl(dest) 348: 349: # puts "DEBUG: " + srcUrl + " => " + destUrl 350: curl_command = CURL_COPY.sub("<!--destination-->", destUrl) + " " + srcUrl 351: response = DavClient.exec_curl(curl_command) 352: 353: if(response == "")then 354: return destUrl 355: end 356: return false 357: end
Delete resource
Examples:
WebDAV.cd("https://example.org/folder") WebDAV.mkcol("subfolder") WebDAV.delete("subfolder")
# File lib/davclient.rb, line 387 387: def self.delete(url) 388: 389: url = absoluteUrl(url) 390: 391: curl_command = CURL_DELETE + url 392: response = DavClient.exec_curl(curl_command) 393: 394: if(response == "")then 395: return url 396: end 397: if(not(response =~ /200 OK/)) then 398: puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 399: return false 400: end 401: return url 402: end
Delete resource
Examples:
WebDAV.cd("https://example.org/folder") WebDAV.mkcol("subfolder") WebDAV.delete("subfolder")
# File lib/davclient.rb, line 387 387: def self.delete(url) 388: 389: url = absoluteUrl(url) 390: 391: curl_command = CURL_DELETE + url 392: response = DavClient.exec_curl(curl_command) 393: 394: if(response == "")then 395: return url 396: end 397: if(not(response =~ /200 OK/)) then 398: puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 399: return false 400: end 401: return url 402: end
Returns true if resource exists
# File lib/davclient.rb, line 330 330: def self.exists?(url) 331: url = absoluteUrl(url) 332: props = WebDAV.propfind(url) 333: if(props.to_s.size == 0)then 334: return false 335: else 336: return true 337: end 338: end
Returns true if resource exists
# File lib/davclient.rb, line 330 330: def self.exists?(url) 331: url = absoluteUrl(url) 332: props = WebDAV.propfind(url) 333: if(props.to_s.size == 0)then 334: return false 335: else 336: return true 337: end 338: end
Find files and folders.
Examples:
result = find( url [:type => "collection"|"resource"] [:recursive => true]) result = find( url, :type => "collection" ,:recursive => true)
You can also pass a block of code:
find( url, :type => "collection" ,:recursive => true) do |folder| puts folder.href end
If no url is specified, current working url is used.
cd("https://webdav.org") find() do |item| print item.title end
# File lib/davclient.rb, line 211 211: def self.find(*args, &block) 212: 213: if(args.size == 0) 214: href = self.CWURL 215: elsif(args.size == 1) 216: if(args[0].class == String) 217: href = args[0] 218: else 219: options = args[0] 220: end 221: elsif(args.size == 2) 222: href = args[0] 223: options = args[1] 224: else 225: raise "Illegal number of arguments." 226: end 227: 228: if(href == nil ) 229: if(WebDAV.CWURL == nil) 230: raise "no current working url set" 231: else 232: href = WebDAV.CWURL 233: end 234: end 235: 236: type = nil 237: recursive = false 238: if(options)then 239: 240: if(options[:type])then 241: type = options[:type] 242: end 243: if(options[:recursive])then 244: recursive = options[:recursive] 245: end 246: end 247: begin 248: dav_xml_output = propfind(href, :xml => true) 249: rescue Exception => exception 250: $stderr.puts "Warning: " + href + " : " + exception 251: # raise "er 252: return nil 253: end 254: if(not(dav_xml_output))then 255: return nil 256: end 257: 258: doc = Hpricot( dav_xml_output ) 259: items_filtered = Array.new() 260: items = doc.search("//d:response").reverse 261: 262: # filter items 263: items.each do |item| 264: 265: # Ignore info about root item (file or folder) 266: if(item.href != href) then 267: 268: if(type == nil)then 269: # No filters 270: items_filtered.push(item) 271: if(block) then 272: yield item 273: end 274: 275: else 276: # Filter result set 277: if((type == "collection" or type == "folder" or type == "directory") and item.isCollection? )then 278: items_filtered.push(item) 279: if(block) then 280: yield item 281: end 282: end 283: 284: # TODO BUG: Item is not yielded if :type => "resource" is given... 285: # puts "DEBUG: " + type + " " + item.isCollection?.to_s 286: if( (type == "file" or type == "resource") and item.isCollection? == false )then 287: items_filtered.push(item) 288: if(block) then 289: yield item 290: end 291: end 292: end 293: 294: end 295: end 296: 297: if(recursive)then 298: items_filtered.each do |item| 299: if(item.collection && item.href != args[0])then 300: result = find(item.href, args[1], &block) 301: if(result != nil) 302: items_filtered.concat( result) 303: end 304: end 305: end 306: end 307: 308: return items_filtered 309: end
Find files and folders.
Examples:
result = find( url [:type => "collection"|"resource"] [:recursive => true]) result = find( url, :type => "collection" ,:recursive => true)
You can also pass a block of code:
find( url, :type => "collection" ,:recursive => true) do |folder| puts folder.href end
If no url is specified, current working url is used.
cd("https://webdav.org") find() do |item| print item.title end
# File lib/davclient.rb, line 211 211: def self.find(*args, &block) 212: 213: if(args.size == 0) 214: href = self.CWURL 215: elsif(args.size == 1) 216: if(args[0].class == String) 217: href = args[0] 218: else 219: options = args[0] 220: end 221: elsif(args.size == 2) 222: href = args[0] 223: options = args[1] 224: else 225: raise "Illegal number of arguments." 226: end 227: 228: if(href == nil ) 229: if(WebDAV.CWURL == nil) 230: raise "no current working url set" 231: else 232: href = WebDAV.CWURL 233: end 234: end 235: 236: type = nil 237: recursive = false 238: if(options)then 239: 240: if(options[:type])then 241: type = options[:type] 242: end 243: if(options[:recursive])then 244: recursive = options[:recursive] 245: end 246: end 247: begin 248: dav_xml_output = propfind(href, :xml => true) 249: rescue Exception => exception 250: $stderr.puts "Warning: " + href + " : " + exception 251: # raise "er 252: return nil 253: end 254: if(not(dav_xml_output))then 255: return nil 256: end 257: 258: doc = Hpricot( dav_xml_output ) 259: items_filtered = Array.new() 260: items = doc.search("//d:response").reverse 261: 262: # filter items 263: items.each do |item| 264: 265: # Ignore info about root item (file or folder) 266: if(item.href != href) then 267: 268: if(type == nil)then 269: # No filters 270: items_filtered.push(item) 271: if(block) then 272: yield item 273: end 274: 275: else 276: # Filter result set 277: if((type == "collection" or type == "folder" or type == "directory") and item.isCollection? )then 278: items_filtered.push(item) 279: if(block) then 280: yield item 281: end 282: end 283: 284: # TODO BUG: Item is not yielded if :type => "resource" is given... 285: # puts "DEBUG: " + type + " " + item.isCollection?.to_s 286: if( (type == "file" or type == "resource") and item.isCollection? == false )then 287: items_filtered.push(item) 288: if(block) then 289: yield item 290: end 291: end 292: end 293: 294: end 295: end 296: 297: if(recursive)then 298: items_filtered.each do |item| 299: if(item.collection && item.href != args[0])then 300: result = find(item.href, args[1], &block) 301: if(result != nil) 302: items_filtered.concat( result) 303: end 304: end 305: end 306: end 307: 308: return items_filtered 309: end
Get content of resource as string
Example:
html = WebDAV.get(url) html = WebDAV.get("file_in_current_working_folder.html")
# File lib/davclient.rb, line 127 127: def self.get(url) 128: url = absoluteUrl(url) 129: 130: curl_command = url 131: return DavClient.exec_curl(curl_command) 132: end
Get content of resource as string
Example:
html = WebDAV.get(url) html = WebDAV.get("file_in_current_working_folder.html")
# File lib/davclient.rb, line 127 127: def self.get(url) 128: url = absoluteUrl(url) 129: 130: curl_command = url 131: return DavClient.exec_curl(curl_command) 132: end
Boolean function that returns true if url is a collection
Example:
WebDAV.isCollection("../test.html") => false
# File lib/davclient.rb, line 77 77: def self.isCollection?(url) 78: url = absoluteUrl(url) 79: url = url + "/" if(not(url =~ /\/$/)) 80: resource = WebDAV.propfind(url) 81: if(resource == nil) 82: return false 83: else 84: return resource.isCollection? 85: end 86: end
Boolean function that returns true if url is a collection
Example:
WebDAV.isCollection("../test.html") => false
# File lib/davclient.rb, line 77 77: def self.isCollection?(url) 78: url = absoluteUrl(url) 79: url = url + "/" if(not(url =~ /\/$/)) 80: resource = WebDAV.propfind(url) 81: if(resource == nil) 82: return false 83: else 84: return resource.isCollection? 85: end 86: end
Make collection Accepts relative url‘s
# File lib/davclient.rb, line 313 313: def self.mkcol(*args) # url, props) 314: url = args[0] 315: props = args[3] 316: url = absoluteUrl(url) 317: curl_command = CURL_MKCOL + " " + url 318: response = DavClient.exec_curl(curl_command) 319: 320: if(props)then 321: proppatch(url,props) 322: end 323: if(response =~ />Created</)then 324: return true 325: end 326: return response 327: end
Make collection Accepts relative url‘s
# File lib/davclient.rb, line 313 313: def self.mkcol(*args) # url, props) 314: url = args[0] 315: props = args[3] 316: url = absoluteUrl(url) 317: curl_command = CURL_MKCOL + " " + url 318: response = DavClient.exec_curl(curl_command) 319: 320: if(props)then 321: proppatch(url,props) 322: end 323: if(response =~ />Created</)then 324: return true 325: end 326: return response 327: end
Move resources
Examples:
WebDAV.mv("src.html","https://example.org/destination/destination.html"
# File lib/davclient.rb, line 365 365: def self.mv(src,dest) 366: srcUrl = absoluteUrl(src) 367: destUrl = absoluteUrl(dest) 368: 369: # puts "DEBUG: " + srcUrl + " => " + destUrl 370: curl_command = CURL_MOVE.sub("<!--destination-->", destUrl) + " " + srcUrl 371: response = DavClient.exec_curl(curl_command) 372: 373: if(response == "")then 374: return destUrl 375: end 376: return false 377: end
Move resources
Examples:
WebDAV.mv("src.html","https://example.org/destination/destination.html"
# File lib/davclient.rb, line 365 365: def self.mv(src,dest) 366: srcUrl = absoluteUrl(src) 367: destUrl = absoluteUrl(dest) 368: 369: # puts "DEBUG: " + srcUrl + " => " + destUrl 370: curl_command = CURL_MOVE.sub("<!--destination-->", destUrl) + " " + srcUrl 371: response = DavClient.exec_curl(curl_command) 372: 373: if(response == "")then 374: return destUrl 375: end 376: return false 377: end
Get WebDAV properties
Examples:
item = propfind(url) - Returns an Hpricot::Elem object xml = propfind(url, :xml => true) - Returns xml for debugging.
# File lib/davclient.rb, line 157 157: def self.propfind(*args) 158: url = args[0] 159: url = absoluteUrl(url) 160: options = args[1] 161: 162: curl_command = CURL_PROPFIND + " \"" + url + "\"" 163: response = DavClient.exec_curl(curl_command) 164: 165: if(response == "")then 166: return nil 167: end 168: 169: if(not(response =~ /200 OK/)) then 170: # puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 171: # exit(0) 172: raise "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 173: end 174: 175: if(options and options[:xml])then 176: return response 177: end 178: doc = Hpricot( response ) 179: items_filtered = Array.new() 180: items = doc.search("//d:response").reverse 181: items.each do |item| 182: 183: # Only return root item if folder 184: if(item.href == url or item.href == url + "/" ) then 185: return item 186: end 187: end 188: return nil 189: end
Get WebDAV properties
Examples:
item = propfind(url) - Returns an Hpricot::Elem object xml = propfind(url, :xml => true) - Returns xml for debugging.
# File lib/davclient.rb, line 157 157: def self.propfind(*args) 158: url = args[0] 159: url = absoluteUrl(url) 160: options = args[1] 161: 162: curl_command = CURL_PROPFIND + " \"" + url + "\"" 163: response = DavClient.exec_curl(curl_command) 164: 165: if(response == "")then 166: return nil 167: end 168: 169: if(not(response =~ /200 OK/)) then 170: # puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 171: # exit(0) 172: raise "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 173: end 174: 175: if(options and options[:xml])then 176: return response 177: end 178: doc = Hpricot( response ) 179: items_filtered = Array.new() 180: items = doc.search("//d:response").reverse 181: items.each do |item| 182: 183: # Only return root item if folder 184: if(item.href == url or item.href == url + "/" ) then 185: return item 186: end 187: end 188: return nil 189: end
Set WebDAV properties for url as xml.
Example:
WebDAV.proppatch("https://dav.webdav.org/folder","<contentLastModified>2007-12-12 12:00:00 GMT</contentLastModified>
# File lib/davclient.rb, line 139 139: def self.proppatch(url, property) 140: url = absoluteUrl(url) 141: curl_command = CURL_PROPPATCH + " \""+url+"\"" 142: curl_command = curl_command.gsub("<!--property-and-value-->",property) 143: response = DavClient.exec_curl(curl_command) 144: if(not(response =~ /200 OK/)) then 145: puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 146: exit(0) 147: end 148: end
Set WebDAV properties for url as xml.
Example:
WebDAV.proppatch("https://dav.webdav.org/folder","<contentLastModified>2007-12-12 12:00:00 GMT</contentLastModified>
# File lib/davclient.rb, line 139 139: def self.proppatch(url, property) 140: url = absoluteUrl(url) 141: curl_command = CURL_PROPPATCH + " \""+url+"\"" 142: curl_command = curl_command.gsub("<!--property-and-value-->",property) 143: response = DavClient.exec_curl(curl_command) 144: if(not(response =~ /200 OK/)) then 145: puts "Error:\nRequest:\n" + curl_command + "\n\nResponse: " + response 146: exit(0) 147: end 148: end
Upload local file to webserver
Example:
WebDAV.put("https://example.org/myfile.html", "myfile.html")
# File lib/davclient.rb, line 441 441: def self.put(url, file_name) 442: url = absoluteUrl(url) 443: 444: curl_command = CURL_UPLOAD + " " + file_name + " " + url 445: response = DavClient.exec_curl(curl_command) 446: if(response != "" and not(response =~ /200 OK/)) then 447: raise "Error:\n WebDAV.put: WebDAV Request:\n" + curl_command + "\n\nResponse: " + response 448: end 449: end
Upload local file to webserver
Example:
WebDAV.put("https://example.org/myfile.html", "myfile.html")
# File lib/davclient.rb, line 441 441: def self.put(url, file_name) 442: url = absoluteUrl(url) 443: 444: curl_command = CURL_UPLOAD + " " + file_name + " " + url 445: response = DavClient.exec_curl(curl_command) 446: if(response != "" and not(response =~ /200 OK/)) then 447: raise "Error:\n WebDAV.put: WebDAV Request:\n" + curl_command + "\n\nResponse: " + response 448: end 449: end
Puts content of string to file on server with url
Example:
WebDAV.put("https://dav.webdav.org/file.html", "<html><h1>Test</h1></html>"
# File lib/davclient.rb, line 424 424: def self.put_string(url,str) 425: url = absoluteUrl(url) 426: 427: if(url =~ /\/$/)then 428: raise "Error: WebDAV.put_html: url can not be a collection (folder)." 429: end 430: 431: filename = DavClient.string2tempfile(str) 432: put(url,filename) 433: end
Puts content of string to file on server with url
Example:
WebDAV.put("https://dav.webdav.org/file.html", "<html><h1>Test</h1></html>"
# File lib/davclient.rb, line 424 424: def self.put_string(url,str) 425: url = absoluteUrl(url) 426: 427: if(url =~ /\/$/)then 428: raise "Error: WebDAV.put_html: url can not be a collection (folder)." 429: end 430: 431: filename = DavClient.string2tempfile(str) 432: put(url,filename) 433: end