net_dav 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/dav +3 -1
- data/lib/net/dav.rb +170 -65
- data/net_dav.gemspec +2 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/bin/dav
CHANGED
data/lib/net/dav.rb
CHANGED
@@ -2,20 +2,164 @@ require 'net/https'
|
|
2
2
|
require 'uri'
|
3
3
|
require 'nokogiri'
|
4
4
|
require 'net/dav/item'
|
5
|
+
require 'base64'
|
6
|
+
begin
|
7
|
+
require 'curb'
|
8
|
+
rescue LoadError
|
9
|
+
end
|
5
10
|
|
6
11
|
module Net #:nodoc:
|
7
12
|
# Implement a WebDAV client
|
8
13
|
class DAV
|
14
|
+
class NetHttpHandler
|
15
|
+
attr_writer :user, :pass
|
16
|
+
|
17
|
+
def initialize(uri)
|
18
|
+
@uri = uri
|
19
|
+
case @uri.scheme
|
20
|
+
when "http"
|
21
|
+
@http = Net::HTTP.new(@uri.host, @uri.port)
|
22
|
+
when "https"
|
23
|
+
@http = Net::HTTPS.new(@uri.host, @uri.port)
|
24
|
+
else
|
25
|
+
raise "unknown uri scheme"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def start(&block)
|
30
|
+
@http.start(&block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def read_timeout
|
34
|
+
@http.read_timeout
|
35
|
+
end
|
36
|
+
|
37
|
+
def read_timeout=(sec)
|
38
|
+
@http.read_timeout = sec
|
39
|
+
end
|
40
|
+
|
41
|
+
def open_timeout
|
42
|
+
@http.read_timeout
|
43
|
+
end
|
44
|
+
|
45
|
+
def open_timeout=(sec)
|
46
|
+
@http.read_timeout = sec
|
47
|
+
end
|
48
|
+
|
49
|
+
def request_sending_stream(verb, path, stream, length, headers)
|
50
|
+
req =
|
51
|
+
case verb
|
52
|
+
when :put
|
53
|
+
Net::HTTP::Put.new(path)
|
54
|
+
else
|
55
|
+
raise "unkown sending_stream verb #{verb}"
|
56
|
+
end
|
57
|
+
req.body_stream = stream
|
58
|
+
req.content_length = length
|
59
|
+
headers.each_pair { |key, value| req[key] = value } if headers
|
60
|
+
req.content_type = 'text/xml; charset="utf-8"'
|
61
|
+
if (@user)
|
62
|
+
req.basic_auth @user, @pass
|
63
|
+
end
|
64
|
+
res = @http.request(req)
|
65
|
+
res.value # raises error if not success
|
66
|
+
res
|
67
|
+
end
|
68
|
+
|
69
|
+
def request_sending_body(verb, path, body, headers)
|
70
|
+
req =
|
71
|
+
case verb
|
72
|
+
when :put
|
73
|
+
Net::HTTP::Put.new(path)
|
74
|
+
else
|
75
|
+
raise "unkown sending_body verb #{verb}"
|
76
|
+
end
|
77
|
+
req.body = body
|
78
|
+
headers.each_pair { |key, value| req[key] = value } if headers
|
79
|
+
req.content_type = 'text/xml; charset="utf-8"'
|
80
|
+
if (@user)
|
81
|
+
req.basic_auth @user, @pass
|
82
|
+
end
|
83
|
+
res = @http.request(req)
|
84
|
+
res.value # raises error if not success
|
85
|
+
res
|
86
|
+
end
|
87
|
+
|
88
|
+
def request_returning_body(verb, path, headers, &block)
|
89
|
+
req =
|
90
|
+
case verb
|
91
|
+
when :get
|
92
|
+
Net::HTTP::Get.new(path)
|
93
|
+
else
|
94
|
+
raise "unkown returning_body verb #{verb}"
|
95
|
+
end
|
96
|
+
headers.each_pair { |key, value| req[key] = value } if headers
|
97
|
+
if (@user)
|
98
|
+
req.basic_auth @user, @pass
|
99
|
+
end
|
100
|
+
res = nil
|
101
|
+
@http.request(req) {|response|
|
102
|
+
response.read_body nil, &block
|
103
|
+
res = response
|
104
|
+
}
|
105
|
+
res.value # raises error if not success
|
106
|
+
res.body
|
107
|
+
end
|
108
|
+
|
109
|
+
def request(verb, path, body, headers)
|
110
|
+
req =
|
111
|
+
case verb
|
112
|
+
when :propfind
|
113
|
+
Net::HTTP::Propfind.new(path)
|
114
|
+
when :mkcol
|
115
|
+
Net::HTTP::Mkcol.new(path)
|
116
|
+
else
|
117
|
+
raise "unkown verb #{verb}"
|
118
|
+
end
|
119
|
+
req.body = body
|
120
|
+
headers.each_pair { |key, value| req[key] = value } if headers
|
121
|
+
req.content_type = 'text/xml; charset="utf-8"'
|
122
|
+
if (@user)
|
123
|
+
req.basic_auth @user, @pass
|
124
|
+
end
|
125
|
+
res = @http.request(req)
|
126
|
+
res.value # raises error if not success
|
127
|
+
res
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class CurlHandler < NetHttpHandler
|
132
|
+
def request_returning_body(verb, path, headers)
|
133
|
+
raise "unkown returning_body verb #{verb}" unless verb == :get
|
134
|
+
url = @uri.merge(path)
|
135
|
+
curl = Curl::Easy.new(url.to_s)
|
136
|
+
headers.each_pair { |key, value| curl.headers[key] = value } if headers
|
137
|
+
if (@user)
|
138
|
+
curl.headers["Authorization"] = "Basic #{Base64.encode64("#{@user}:#{@pass}")}"
|
139
|
+
end
|
140
|
+
res = nil
|
141
|
+
if block_given?
|
142
|
+
curl.on_body do |frag|
|
143
|
+
yield frag
|
144
|
+
frag.length
|
145
|
+
end
|
146
|
+
end
|
147
|
+
curl.perform
|
148
|
+
curl.body_str
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
9
153
|
# Seconds to wait until reading one block (by one system call).
|
10
154
|
# If the DAV object cannot read a block in this many seconds,
|
11
155
|
# it raises a TimeoutError exception.
|
12
156
|
#
|
13
157
|
def read_timeout
|
14
|
-
@
|
158
|
+
@handler.read_timeout
|
15
159
|
end
|
16
160
|
|
17
161
|
def read_timeout=(sec)
|
18
|
-
@
|
162
|
+
@handler.read_timeout = sec
|
19
163
|
end
|
20
164
|
|
21
165
|
# Seconds to wait until connection is opened.
|
@@ -23,11 +167,11 @@ module Net #:nodoc:
|
|
23
167
|
# it raises a TimeoutError exception.
|
24
168
|
#
|
25
169
|
def open_timeout
|
26
|
-
@
|
170
|
+
@handler.read_timeout
|
27
171
|
end
|
28
172
|
|
29
173
|
def open_timeout=(sec)
|
30
|
-
@
|
174
|
+
@handler.read_timeout = sec
|
31
175
|
end
|
32
176
|
|
33
177
|
# Creates a new Net::DAV object and opens the connection
|
@@ -40,24 +184,23 @@ module Net #:nodoc:
|
|
40
184
|
# puts "#{item.uri} is size #{item.size}"
|
41
185
|
# end
|
42
186
|
# end
|
43
|
-
def self.start(uri, &block) # :yield: dav
|
44
|
-
new(uri).start(&block)
|
187
|
+
def self.start(uri, options = nil, &block) # :yield: dav
|
188
|
+
new(uri, options).start(&block)
|
45
189
|
end
|
46
190
|
|
47
191
|
# Creates a new Net::DAV object for the specified host
|
48
192
|
# The path part of the URI is used to handle relative URLs
|
49
193
|
# in subsequent requests.
|
50
|
-
|
194
|
+
# You can pass :curl => false if you want to disable use
|
195
|
+
# of the curb (libcurl) gem if present for acceleration
|
196
|
+
def initialize(uri, options = nil)
|
197
|
+
@have_curl = Curl rescue nil
|
198
|
+
if options && options.has_key?(:curl) && !options[:curl]
|
199
|
+
@have_curl = false
|
200
|
+
end
|
51
201
|
@uri = uri
|
52
202
|
@uri = URI.parse(@uri) if @uri.is_a? String
|
53
|
-
|
54
|
-
when "http"
|
55
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
56
|
-
when "https"
|
57
|
-
@http = Net::HTTPS.new(@uri.host, @uri.port)
|
58
|
-
else
|
59
|
-
raise "unknown uri scheme"
|
60
|
-
end
|
203
|
+
@handler = @have_curl ? CurlHandler.new(@uri) : NetHttpHandler.new(@uri)
|
61
204
|
end
|
62
205
|
|
63
206
|
# Opens the connection to the host. Yields self to the block.
|
@@ -69,28 +212,22 @@ module Net #:nodoc:
|
|
69
212
|
# puts item.inspect
|
70
213
|
# end
|
71
214
|
# end
|
72
|
-
def start # :yield: dav
|
73
|
-
@
|
215
|
+
def start(&block) # :yield: dav
|
216
|
+
@handler.start do
|
74
217
|
return yield(self)
|
75
218
|
end
|
76
219
|
end
|
77
220
|
|
78
221
|
# Set credentials for basic authentication
|
79
222
|
def credentials(user, pass)
|
80
|
-
@user = user
|
81
|
-
@pass = pass
|
223
|
+
@handler.user = user
|
224
|
+
@handler.pass = pass
|
82
225
|
end
|
83
226
|
|
84
227
|
def propfind(path) #:nodoc:
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
req.content_type = 'text/xml; charset="utf-8"'
|
89
|
-
if (@user)
|
90
|
-
req.basic_auth @user, @pass
|
91
|
-
end
|
92
|
-
res = @http.request(req)
|
93
|
-
res.value # raises error if not success
|
228
|
+
headers = {'Depth' => '1'}
|
229
|
+
body = '<?xml version="1.0" encoding="utf-8"?><DAV:propfind xmlns:DAV="DAV:"><DAV:allprop/></DAV:propfind>'
|
230
|
+
res = @handler.request(:propfind, path, body, headers)
|
94
231
|
Nokogiri::XML.parse(res.body)
|
95
232
|
end
|
96
233
|
|
@@ -146,17 +283,8 @@ module Net #:nodoc:
|
|
146
283
|
# object will *not* contain a (meaningful) body.
|
147
284
|
|
148
285
|
def get(path, &block)
|
149
|
-
|
150
|
-
|
151
|
-
if (@user)
|
152
|
-
req.basic_auth @user, @pass
|
153
|
-
end
|
154
|
-
res = nil
|
155
|
-
@http.request(req) {|response|
|
156
|
-
response.read_body nil, &block
|
157
|
-
res = response
|
158
|
-
}
|
159
|
-
res.body
|
286
|
+
@handler.request_returning_body(:get, path, nil, &block)
|
287
|
+
true
|
160
288
|
end
|
161
289
|
|
162
290
|
# Stores the content of a stream to a URL
|
@@ -166,16 +294,7 @@ module Net #:nodoc:
|
|
166
294
|
# dav.put(url.path, stream, File.size(file))
|
167
295
|
# end
|
168
296
|
def put(path, stream, length)
|
169
|
-
|
170
|
-
req.content_type = 'text/xml; charset="utf-8"'
|
171
|
-
req.content_length = length
|
172
|
-
req.body_stream = stream
|
173
|
-
#req['transfer-encoding'] = 'chunked'
|
174
|
-
if (@user)
|
175
|
-
req.basic_auth @user, @pass
|
176
|
-
end
|
177
|
-
res = @http.request(req)
|
178
|
-
res.value
|
297
|
+
res = @handler.request_sending_stream(:put, path, stream, length, nil)
|
179
298
|
res.body
|
180
299
|
end
|
181
300
|
|
@@ -185,28 +304,14 @@ module Net #:nodoc:
|
|
185
304
|
# dav.put(url.path, "hello world")
|
186
305
|
#
|
187
306
|
def put_string(path, str)
|
188
|
-
|
189
|
-
req.content_type = 'text/xml; charset="utf-8"'
|
190
|
-
req.body = str
|
191
|
-
#req['transfer-encoding'] = 'chunked'
|
192
|
-
if (@user)
|
193
|
-
req.basic_auth @user, @pass
|
194
|
-
end
|
195
|
-
res = @http.request(req)
|
196
|
-
res.value
|
307
|
+
res = @handler.request_sending_body(:put, path, str, nil)
|
197
308
|
res.body
|
198
309
|
end
|
199
310
|
|
200
311
|
|
201
312
|
# Makes a new directory (collection)
|
202
313
|
def mkdir(path)
|
203
|
-
|
204
|
-
req.content_type = 'text/xml; charset="utf-8"'
|
205
|
-
if (@user)
|
206
|
-
req.basic_auth @user, @pass
|
207
|
-
end
|
208
|
-
res = @http.request(req)
|
209
|
-
res.value
|
314
|
+
res = @handler.request(:mkcol, path, nil, nil)
|
210
315
|
res.body
|
211
316
|
end
|
212
317
|
|
data/net_dav.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{net_dav}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Miron Cuperman"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-30}
|
13
13
|
s.default_executable = %q{dav}
|
14
14
|
s.description = %q{WebDAV client library in the style of Net::HTTP}
|
15
15
|
s.email = %q{c1.github@niftybox.net}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net_dav
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miron Cuperman
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-30 00:00:00 -07:00
|
13
13
|
default_executable: dav
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|