Module: ServeWebdav::InstanceMethods

Defined in:
lib/serve_webdav/serve_webdav.rb

Overview

SingletonMethods

Instance Method Summary collapse

Instance Method Details

#indexObject



100
101
102
# File 'lib/serve_webdav/serve_webdav.rb', line 100

def index
  webdav
end

#webdavObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/serve_webdav/serve_webdav.rb', line 104

def webdav
  #we can get the method (as webdav has a lot of new methods) from the request
  webdav_method = request.method.to_s
  @path_info = convert_path_info(@request, params[:path_info].to_s) 
 
  begin
   #going to call the method for this webdav method 
   if respond_to?("webdav_#{webdav_method}", true)
     __send__("webdav_#{webdav_method}")
   else
     raise WebDavErrors::UnknownWebDavMethodError
   end
   
  rescue WebDavErrors::BaseError => webdav_error
    logger.debug("ERROR : #{webdav_error.to_s}")
    logger.debug("STATUS : #{webdav_error.http_status}")
    render :nothing => true, :status => webdav_error.http_status and return
  end
end

#webdav_copyObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/serve_webdav/serve_webdav.rb', line 229

def webdav_copy()
  @depth = get_depth

  #Check the destination URI
  begin
    dest_uri = get_destination_uri
    dest_path = convert_path_info(@request, get_dest_path)
    # Bad Gateway it if the servers arnt the same
    raise WebDavErrors::BadGatewayError unless request.host_with_port == "#{dest_uri.host}:#{dest_uri.port}"
  rescue URI::InvalidURIError
    raise WebDavErrors::BadGatewayError
  end

  resource = get_resource_for_path(@path_info)

  # Not found if the source doesnt exist
  raise WebDavErrors::NotFoundError if resource.nil?

  file_exist = file_exist?(dest_path)
  
  raise WebDavErrors::PreconditionFailsError if file_exist && !get_overwrite
  
  copy_to_path(resource, dest_path, @depth)

  file_exist ? render(:nothing => true, :status => 204) : render(:nothing => true, :status => 201)
end

#webdav_deleteObject



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/serve_webdav/serve_webdav.rb', line 210

def webdav_delete()
  resource = get_resource_for_path(@path_info)
  unless resource.nil?
    logger.debug('RESOURCE IS' + resource.inspect)
    resource.delete!
  else
    # Delete on a non-existant resource
    raise WebDavErrors::NotFoundError
  end

  render :nothing => true, :status => 204
end

#webdav_getObject



284
285
286
287
# File 'lib/serve_webdav/serve_webdav.rb', line 284

def webdav_get
  resource = get_resource_for_path(@path_info)
  send_data resource.data, :filename => resource.get_displayname
end

#webdav_headObject



289
290
291
292
293
294
# File 'lib/serve_webdav/serve_webdav.rb', line 289

def webdav_head
  resource = get_resource_for_path(@path_info)
  raise WebDavErrors::NotFoundError if resource.blank?
  @response.headers["Last-Modified"] = resource.getlastmodified
  render(:nothing => true, :status => 200) and return  
end

#webdav_lockObject



131
132
133
134
# File 'lib/serve_webdav/serve_webdav.rb', line 131

def webdav_lock()
  #TODO implementation for now return a 200 OK
  render :nothing => true, :status => 200 and return
end

#webdav_mkcolObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/serve_webdav/serve_webdav.rb', line 192

def webdav_mkcol
    # need to check the content-type header to not allow invalid content types
    mkcol_ct = request.env['CONTENT_TYPE']

    logger.info(request.inspect)

    if (!mkcol_ct.blank? && (mkcol_ct != "httpd/unix-directory")) || !request.raw_post.nil?
      raise WebDavErrors::UnSupportedTypeError
    end

    
    raise WebDavErrors::UnknownWebDavMethodError if file_exist?(@path_info)
    
    mkcol_for_path(@path_info)
    
    render :nothing => true, :status => 201
end

#webdav_moveObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/serve_webdav/serve_webdav.rb', line 256

def webdav_move()
  @depth = get_depth

  #Check the destination URI
  begin
    dest_uri = get_destination_uri
    dest_path = convert_path_info(@request, get_dest_path)
    #Bad Gateway it if the servers arnt the same
    raise WebDavErrors::BadGatewayError unless request.host_with_port == "#{dest_uri.host}:#{dest_uri.port}"
  rescue URI::InvalidURIError => e
    logger.debug("INVALID URI: " + e.to_s)
    logger.debug(request.env['HTTP_DESTINATION'])
    raise WebDavErrors::BadGatewayError
  end

  resource = get_resource_for_path(@path_info)

  #Not found if the source doesnt exist
  raise WebDavErrors::NotFoundError if resource.nil?

  file_exist = file_exist?(dest_path)
  
  raise WebDavErrors::PreconditionFailsError if file_exist && !get_overwrite

  move_to_path(resource, dest_path, @depth)
  file_exist ? render(:nothing => true, :status => 204) : render(:nothing => true, :status => 201)
end

#webdav_optionsObject



124
125
126
127
128
129
# File 'lib/serve_webdav/serve_webdav.rb', line 124

def webdav_options()
  @response.headers['DAV'] = "1,2"
  @response.headers['MS-Author-Via'] = "DAV"
  @response.headers["Allow"] = "COPY,DELETE,GET,HEAD,MKCOL,MOVE,OPTIONS,POST,PROPFIND,PROPPATCH,PUT"
  render  :nothing => true, :status => 200 and return
end

#webdav_propfindObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/serve_webdav/serve_webdav.rb', line 141

def webdav_propfind() 
  logger.info("PATH INFO: " + @path_info)
  @depth = get_depth
  
  #default a big depth if infinity
  @depth = 500 if @depth.nil?
  
  
  unless request.raw_post.blank?
    begin
      req_doc = REXML::Document.new @request.raw_post
    rescue REXML::ParseException
      raise WebDavErrors::BadRequestBodyError 
    end
  end
  
  @resources = get_dav_resource_props(@path_info)
  
  raise WebDavErrors::NotFoundError if @resources.blank?
  
  response.headers["Content-Type"] = 'text/xml; charset="utf-8"'
  #render the Multistatus XML
  render :inline => self.class.get_propfind_xml, :type => :rxml, :status => 207  and return
end

#webdav_proppatchObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/serve_webdav/serve_webdav.rb', line 166

def webdav_proppatch()
  
  @resource = get_resource_for_path(@path_info)
  
  ns = {""=>"DAV:"}
  begin
    req_doc = REXML::Document.new request.raw_post
  rescue REXML::ParseException
    raise WebDavErrors::BadRequestBodyError
  end
  
  @remove_properties = []
  @set_properties = []
  REXML::XPath.each(req_doc, "/propertyupdate/remove/prop/*", ns){|e|
    @remove_properties << e
  }
  REXML::XPath.each(req_doc, "/propertyupdate/set/prop/*", ns){|e|
    @set_properties << e
  }
  
   response.headers["Content-Type"] = 'text/xml; charset="utf-8"'
   
   #render the Multistatus XML
   render :inline => self.class.get_proppatch_xml, :type => :rxml, :status => 207 # and return
end

#webdav_putObject



223
224
225
226
227
# File 'lib/serve_webdav/serve_webdav.rb', line 223

def webdav_put()
  write_content_to_path(@path_info, request.raw_post)

  render :nothing => true, :status => 201 and return
end

#webdav_unlockObject



136
137
138
139
# File 'lib/serve_webdav/serve_webdav.rb', line 136

def webdav_unlock()
  #TODO implementation for now return a 200 OK
  render :nothing => true, :status => 200 and return
end