Class: Net::DAV::NetHttpHandler
- Inherits:
-
Object
- Object
- Net::DAV::NetHttpHandler
- Defined in:
- lib/net/dav.rb
Direct Known Subclasses
Constant Summary
- CNONCE =
Digest::MD5.hexdigest("%x" % (Time.now.to_i + rand(65535))).slice(0, 8)
Instance Attribute Summary (collapse)
-
- (Object) disable_basic_auth
Returns the value of attribute disable_basic_auth.
-
- (Object) pass
writeonly
Sets the attribute pass.
-
- (Object) user
writeonly
Sets the attribute user.
Instance Method Summary (collapse)
- - (Object) clone_req(path, req, headers)
- - (Object) digest_auth(request, user, password, response)
- - (Object) handle_request(req, headers, limit = MAX_REDIRECTS, &block)
-
- (NetHttpHandler) initialize(uri)
constructor
A new instance of NetHttpHandler.
- - (Object) open_timeout
- - (Object) open_timeout=(sec)
- - (Object) read_timeout
- - (Object) read_timeout=(sec)
- - (Object) request(verb, path, body, headers)
- - (Object) request_returning_body(verb, path, headers, &block)
- - (Object) request_sending_body(verb, path, body, headers)
- - (Object) request_sending_stream(verb, path, stream, length, headers)
- - (Object) start(&block)
- - (Object) verify_callback=(callback)
- - (Object) verify_server=(value)
Constructor Details
- (NetHttpHandler) initialize(uri)
A new instance of NetHttpHandler
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/net/dav.rb', line 29 def initialize(uri) @disable_basic_auth = false @uri = uri case @uri.scheme when "http" @http = Net::HTTP.new(@uri.host, @uri.port) when "https" @http = Net::HTTP.new(@uri.host, @uri.port) @http.use_ssl = true self.verify_server = true else raise "unknown uri scheme" end end |
Instance Attribute Details
- (Object) disable_basic_auth
Returns the value of attribute disable_basic_auth
19 20 21 |
# File 'lib/net/dav.rb', line 19 def disable_basic_auth @disable_basic_auth end |
- (Object) pass=(value) (writeonly)
Sets the attribute pass
17 18 19 |
# File 'lib/net/dav.rb', line 17 def pass=(value) @pass = value end |
- (Object) user=(value) (writeonly)
Sets the attribute user
17 18 19 |
# File 'lib/net/dav.rb', line 17 def user=(value) @user = value end |
Instance Method Details
- (Object) clone_req(path, req, headers)
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/net/dav.rb', line 189 def clone_req(path, req, headers) new_req = req.class.new(path) new_req.body = req.body if req.body if (req.body_stream) req.body_stream.rewind new_req.body_stream = req.body_stream end new_req.content_length = req.content_length if req.content_length headers.each_pair { |key, value| new_req[key] = value } if headers return new_req end |
- (Object) digest_auth(request, user, password, response)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/net/dav.rb', line 203 def digest_auth(request, user, password, response) # based on http://segment7.net/projects/ruby/snippets/digest_auth.rb @nonce_count = 0 if @nonce_count.nil? @nonce_count += 1 raise "bad www-authenticate header" unless (response['www-authenticate'] =~ /^(\w+) (.*)/) params = {} $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 } a_1 = "#{user}:#{params['realm']}:#{password}" a_2 = "#{request.method}:#{request.path}" request_digest = '' request_digest << Digest::MD5.hexdigest(a_1) request_digest << ':' << params['nonce'] request_digest << ':' << ('%08x' % @nonce_count) request_digest << ':' << CNONCE request_digest << ':' << params['qop'] request_digest << ':' << Digest::MD5.hexdigest(a_2) header = [] header << "Digest username=\"#{user}\"" header << "realm=\"#{params['realm']}\"" header << "nonce=\"#{params['nonce']}\"" header << "uri=\"#{request.path}\"" header << "cnonce=\"#{CNONCE}\"" header << "nc=#{'%08x' % @nonce_count}" header << "qop=#{params['qop']}" header << "response=\"#{Digest::MD5.hexdigest(request_digest)}\"" header << "algorithm=\"MD5\"" header = header.join(', ') request['Authorization'] = header end |
- (Object) handle_request(req, headers, limit = MAX_REDIRECTS, &block)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/net/dav.rb', line 145 def handle_request(req, headers, limit = MAX_REDIRECTS, &block) # You should choose better exception. raise ArgumentError, 'HTTP redirect too deep' if limit == 0 response = nil if block @http.request(req) {|res| # Only start returning a body if we will not retry res.read_body nil, &block if !res.is_a?(Net::HTTPUnauthorized) && !res.is_a?(Net::HTTPRedirection) response = res } else response = @http.request(req) end case response when Net::HTTPSuccess then return response when Net::HTTPUnauthorized then response.error! unless @user response.error! if req['authorization'] new_req = clone_req(req.path, req, headers) if response['www-authenticate'] =~ /^Basic/ if disable_basic_auth raise "server requested basic auth, but that is disabled" end new_req.basic_auth @user, @pass else digest_auth(new_req, @user, @pass, response) end return handle_request(new_req, headers, limit - 1, &block) when Net::HTTPRedirection then location = URI.parse(response['location']) if (@uri.scheme != location.scheme || @uri.host != location.host || @uri.port != location.port) raise ArgumentError, "cannot redirect to a different host #{@uri} => #{location}" end new_req = clone_req(location.path, req, headers) return handle_request(new_req, headers, limit - 1, &block) else response.error! end end |
- (Object) open_timeout
56 57 58 |
# File 'lib/net/dav.rb', line 56 def open_timeout @http.read_timeout end |
- (Object) open_timeout=(sec)
60 61 62 |
# File 'lib/net/dav.rb', line 60 def open_timeout=(sec) @http.read_timeout = sec end |
- (Object) read_timeout
48 49 50 |
# File 'lib/net/dav.rb', line 48 def read_timeout @http.read_timeout end |
- (Object) read_timeout=(sec)
52 53 54 |
# File 'lib/net/dav.rb', line 52 def read_timeout=(sec) @http.read_timeout = sec end |
- (Object) request(verb, path, body, headers)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/net/dav.rb', line 114 def request(verb, path, body, headers) headers ||= {} headers = {"User-Agent" => "Ruby"}.merge(headers) req = case verb when :propfind Net::HTTP::Propfind.new(path) when :mkcol Net::HTTP::Mkcol.new(path) when :delete Net::HTTP::Delete.new(path) when :move Net::HTTP::Move.new(path) when :copy Net::HTTP::Copy.new(path) when :proppatch Net::HTTP::Proppatch.new(path) when :lock Net::HTTP::Lock.new(path) when :unlock Net::HTTP::Unlock.new(path) else raise "unkown verb #{verb}" end req.body = body headers.each_pair { |key, value| req[key] = value } if headers req.content_type = 'text/xml; charset="utf-8"' res = handle_request(req, headers) res end |
- (Object) request_returning_body(verb, path, headers, &block)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/net/dav.rb', line 99 def request_returning_body(verb, path, headers, &block) headers ||= {} headers = {"User-Agent" => "Ruby"}.merge(headers) req = case verb when :get Net::HTTP::Get.new(path) else raise "unkown returning_body verb #{verb}" end headers.each_pair { |key, value| req[key] = value } if headers res = handle_request(req, headers, MAX_REDIRECTS, &block) res.body end |
- (Object) request_sending_body(verb, path, body, headers)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/net/dav.rb', line 82 def request_sending_body(verb, path, body, headers) headers ||= {} headers = {"User-Agent" => "Ruby"}.merge(headers) req = case verb when :put Net::HTTP::Put.new(path) else raise "unkown sending_body verb #{verb}" end req.body = body headers.each_pair { |key, value| req[key] = value } if headers req.content_type = 'text/xml; charset="utf-8"' res = handle_request(req, headers) res end |
- (Object) request_sending_stream(verb, path, stream, length, headers)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/net/dav.rb', line 64 def request_sending_stream(verb, path, stream, length, headers) headers ||= {} headers = {"User-Agent" => "Ruby"}.merge(headers) req = case verb when :put Net::HTTP::Put.new(path) else raise "unkown sending_stream verb #{verb}" end req.body_stream = stream req.content_length = length headers.each_pair { |key, value| req[key] = value } if headers req.content_type = 'text/xml; charset="utf-8"' res = handle_request(req, headers) res end |
- (Object) start(&block)
44 45 46 |
# File 'lib/net/dav.rb', line 44 def start(&block) @http.start(&block) end |
- (Object) verify_callback=(callback)
21 22 23 |
# File 'lib/net/dav.rb', line 21 def verify_callback=(callback) @http.verify_callback = callback end |
- (Object) verify_server=(value)
25 26 27 |
# File 'lib/net/dav.rb', line 25 def verify_server=(value) @http.verify_mode = value ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE end |