class Mhc::WebDav::Client
WebDAV protocol: RFC4918 see tools.ietf.org/html/rfc4918
Attributes
top_directory[R]
Public Class Methods
new(base_url, proxy_host = nil, proxy_port = nil)
click to toggle source
# File lib/mhc/webdav.rb, line 17 def initialize(base_url, proxy_host = nil, proxy_port = nil) uri = URI.parse(base_url) @top_directory = uri.path @http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port) @http.use_ssl = true if uri.scheme == "https" @http.verify_mode = OpenSSL::SSL::VERIFY_PEER end
Public Instance Methods
copy(src_path, dest_path)
click to toggle source
8.8 COPY
# File lib/mhc/webdav.rb, line 124 def copy(src_path, dest_path) req = setup_request(Net::HTTP::Copy, src_path) req['Destination'] = dest_path res = @http.request(req) check_status_code(res, 204) # No Content return res end
delete(path, ifmatch = nil)
click to toggle source
8.6 DELETE
# File lib/mhc/webdav.rb, line 94 def delete(path, ifmatch = nil) req = setup_request(Net::HTTP::Delete, path) req['If-Match'] = ifmatch if ifmatch res = @http.request(req) check_status_code(res, 204) return res end
get(path)
click to toggle source
8.4 GET
# File lib/mhc/webdav.rb, line 67 def get(path) req = setup_request(Net::HTTP::Get, path) res = @http.request(req) check_status_code(res, 200) # OK return res end
head(path)
click to toggle source
8.4 HEAD
# File lib/mhc/webdav.rb, line 75 def head(path) req = setup_request(Net::HTTP::Head, path) res = @http.request(req) check_status_code(res, 200) # OK return res end
lock(path)
click to toggle source
8.10 LOCK
# File lib/mhc/webdav.rb, line 144 def lock(path) raise NotImplementedError end
mkcol(path)
click to toggle source
8.3 MKCOL
# File lib/mhc/webdav.rb, line 59 def mkcol(path) req = setup_request(Net::HTTP::Mkcol, path) res = @http.request(req) check_status_code(res, 201) # Created return res end
move(src_path, dest_path)
click to toggle source
8.9 MOVE
# File lib/mhc/webdav.rb, line 134 def move(src_path, dest_path) req = setup_request(Net::HTTP::Move, src_path) req['Destination'] = dest_path res = @http.request(req) check_status_code(res, 204) # No Content return res end
post(content, dest_path)
click to toggle source
8.5 POST
# File lib/mhc/webdav.rb, line 83 def post(content, dest_path) req = setup_request(Net::HTTP::Post, dest_path) req.content_length = content.size req.body = content res = @http.request(req) check_status_code(res, [201, 204]) # Created or No content return res end
propfind(path = @top_directory, depth = 1, xml_body = nil)
click to toggle source
8.1 PROPFIND
# File lib/mhc/webdav.rb, line 31 def propfind(path = @top_directory, depth = 1, xml_body = nil) req = setup_request(Net::HTTP::Propfind, path) req['Depth'] = depth if xml_body req.content_type = 'application/xml; charset="utf-8"' req.content_length = xml_body.size req.body = xml_body end res = @http.request(req) if $MHC_DEBUG STDERR.print "\n* PROPFIND RESPONSE:\n" STDERR.print dump_response(res, true) end check_status_code(res, 207) # Multi-Status return res end
proppatch()
click to toggle source
8.2 PROPPATCH
# File lib/mhc/webdav.rb, line 54 def proppatch raise NotImplementedError end
put(content, dest_path, ifmatch = nil)
click to toggle source
8.7 PUT
# File lib/mhc/webdav.rb, line 104 def put(content, dest_path, ifmatch = nil) req = setup_request(Net::HTTP::Put, dest_path) req.content_length = content.size req['If-Match'] = ifmatch if ifmatch req.content_type = "text/calendar; charset=utf-8" # xxx req.body = content res = @http.request(req) if $MHC_DEBUG STDERR.print "\n* PUT RESPONSE:\n" STDERR.print dump_response(res) STDERR.print "* HEAD RESPONSE:\n" STDERR.print dump_response(head(dest_path)) end check_status_code(res, [201, 204]) # Created or No content return res end
set_basic_auth(user, password)
click to toggle source
# File lib/mhc/webdav.rb, line 25 def set_basic_auth(user, password) @auth_user, @auth_password = user, password return self end
unlock(path)
click to toggle source
8.11 UNLOCK
# File lib/mhc/webdav.rb, line 149 def unlock(path) raise NotImplementedError end
Private Instance Methods
check_status_code(res, required_status)
click to toggle source
# File lib/mhc/webdav.rb, line 156 def check_status_code(res, required_status) unless ([required_status].flatten.map{|c| c.to_s}).member?(res.code) header = "Invalid HttpResponse" raise Exception.new("#{res.code} #{header} #{res.message} #{res.body}") end end
dump_response(res, include_body = false)
click to toggle source
# File lib/mhc/webdav.rb, line 188 def dump_response(res, include_body = false) string = "" res.each do |name, value| string += " #{name}: #{value}\n" end string += res.body + "\n" if include_body return string end
fetch(uri_str, limit = 10)
click to toggle source
# File lib/mhc/webdav.rb, line 174 def fetch(uri_str, limit = 10) raise StandardError, 'HTTP redirect too deep' if limit == 0 response = Net::HTTP.get_response(URI.parse(uri_str)) case response when Net::HTTPSuccess response when Net::HTTPRedirection fetch(response['location'], limit - 1) else response.value end end
setup_request(request, *args)
click to toggle source
# File lib/mhc/webdav.rb, line 163 def setup_request(request, *args) req = request.new(*args) req.basic_auth @auth_user, @auth_password # XXX: should implement re-connection mechanism for Keep-Alive: # http://d.hatena.ne.jp/daftbeats/20080321/1206092975 req["Connection"] = "Keep-Alive" return req end