Skip to content
clyfe edited this page Nov 1, 2010 · 19 revisions

Rails Integration

Minimal integration application at: http://github.com/clyfe/rails3-dav4rack-example
NOTE. People reported that for maximum compatibility (with all kinds of webdav clients) webdav is to be mounted in the root ‘/’ and run on port 80, so a subdomain is perfect in that regard. Also by using a subdomain we don’t need a DAV4Rack::Interceptor.

This is a step by step tutorial based on the sample application

Install

Add dav4rack to your Gemfile gem 'dav4rack'
Run bundle install to install it.

Fix Rails

Monkey-patch Rails’s HTPP method validation to allow WebDAV HTTP extension verbs.

# config/application.rb just below "Bundler.require"
class ActionDispatch::Request
  HTTP_METHODS = %w(get head put post delete options lock unlock propfind proppatch mkcol delete put copy move)
  HTTP_METHOD_LOOKUP = HTTP_METHODS.inject({}) { |h, m| h[m] = h[m.upcase] = m.to_sym; h }
end

Load your resources

Create an webdav in initializer at config/initializers/webdav.rb.
In this file you will load your custom resources (A resource is a class that implements DAV4Rack::Resource).
For the time being we will just use the bundled FileResource to serve files from our Rails.root directory.

# config/initializers/webdav.rb
require 'dav4rack/file_resource'

Mount your handlers

Mount your dav4rack handlers in routes.rb. In this example we mount our hadler on the webdav.domain.com subdomain.
Note that mount is a special case of match, it takes the parameters backwards and is not anchored by default (matches all paths that start with the given path, see http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/).

# config/routes.rb
HelloDav::Application.routes.draw do
  mount DAV4Rack::Handler.new( :root => Rails.root.to_s, :root_uri_path => '/',
    :resource_class => ::DAV4Rack::FileResource
  ), :at => '/', :constraints => {:subdomain => "webdav"}
end

You can also mount a handler on a path segment on the main domain, say ‘/webdav’:

# config/routes.rb
HelloDav::Application.routes.draw do
  mount DAV4Rack::Handler.new( :root => Rails.root.to_s, :root_uri_path => '/webdav',
    :resource_class => ::DAV4Rack::FileResource
  ), :at => '/webdav/'
end

Understand that mounting the handler on a path segment will limit the number of WebDAV clients that can talk to your handler successfully. You find a some hints on striving for compatibility at Milton’s site: Server implementation requirements.

We could also use match:

# config/routes.rb
HelloDav::Application.routes.draw do
  match '/webdav' => DAV4Rack::Handler.new( :root => Rails.root.to_s, :root_uri_path => '/webdav',
    :resource_class => ::DAV4Rack::FileResource
  ), :anchor => false
end
:TBD Enable upstream paths with Interceptors