Class: Async::Caldav::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/async/caldav/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(storage:) ⇒ Server

Returns a new instance of Server.



9
10
11
# File 'lib/async/caldav/server.rb', line 9

def initialize(storage:)
  @storage = storage
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/caldav/server.rb', line 13

def call(env)
  method = env['REQUEST_METHOD']
  raw_path = env['PATH_INFO'] || '/'
  path = Protocol::Caldav::Path.new(raw_path, storage_class: @storage)

  # OPTIONS doesn't require auth
  if method == 'OPTIONS'
    Handlers::Options.call(path: path, storage: @storage)
  else
    user = env['dav.user']
    if user && !user.to_s.empty?
      if raw_path.include?('..')
        [400, { 'content-type' => 'text/plain' }, ['Bad Request']]
      else
        resource_type = resource_type_for(path)
        body = read_body(env)
        headers = extract_headers(env)
        dispatch(
          method,
          path:          path,
          body:          body,
          storage:       @storage,
          user:          user,
          headers:       headers,
          resource_type: resource_type,
        )
      end
    else
      [401, { 'content-type' => 'text/plain', 'www-authenticate' => 'Basic realm="caldav"' }, ['Unauthorized']]
    end
  end
end