~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/web2/dav/method/report.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- test-case-name: twisted.web2.dav.test.test_report -*-
 
2
##
 
3
# Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
 
4
#
 
5
# Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
# of this software and associated documentation files (the "Software"), to deal
 
7
# in the Software without restriction, including without limitation the rights
 
8
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
# copies of the Software, and to permit persons to whom the Software is
 
10
# furnished to do so, subject to the following conditions:
 
11
 
12
# The above copyright notice and this permission notice shall be included in all
 
13
# copies or substantial portions of the Software.
 
14
 
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
18
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
19
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
21
# SOFTWARE.
 
22
#
 
23
# DRI: Wilfredo Sanchez, wsanchez@apple.com
 
24
##
 
25
 
 
26
"""
 
27
WebDAV REPORT method
 
28
"""
 
29
 
 
30
__all__ = ["http_REPORT"]
 
31
 
 
32
import string
 
33
 
 
34
from twisted.python import log
 
35
from twisted.internet.defer import deferredGenerator, waitForDeferred
 
36
from twisted.web2 import responsecode
 
37
from twisted.web2.http import HTTPError, StatusResponse
 
38
from twisted.web2.dav import davxml
 
39
from twisted.web2.dav.http import ErrorResponse
 
40
from twisted.web2.dav.util import davXMLFromStream
 
41
 
 
42
def http_REPORT(self, request):
 
43
    """
 
44
    Respond to a REPORT request. (RFC 3253, section 3.6)
 
45
    """
 
46
    if not self.fp.exists():
 
47
        log.err("File not found: %s" % (self.fp.path,))
 
48
        raise HTTPError(responsecode.NOT_FOUND)
 
49
 
 
50
    #
 
51
    # Read request body
 
52
    #
 
53
    try:
 
54
        doc = waitForDeferred(davXMLFromStream(request.stream))
 
55
        yield doc
 
56
        doc = doc.getResult()
 
57
    except ValueError, e:
 
58
        log.err("Error while handling REPORT body: %s" % (e,))
 
59
        raise HTTPError(StatusResponse(responsecode.BAD_REQUEST, str(e)))
 
60
 
 
61
    if doc is None:
 
62
        raise HTTPError(StatusResponse(
 
63
            responsecode.BAD_REQUEST,
 
64
            "REPORT request body may not be empty"
 
65
        ))
 
66
 
 
67
    #
 
68
    # Parse request
 
69
    #
 
70
    namespace = doc.root_element.namespace
 
71
    name = doc.root_element.name
 
72
 
 
73
    def to_method(s):
 
74
        ok = string.ascii_letters + string.digits + "_"
 
75
        out = []
 
76
        for c in s:
 
77
            if c in ok:
 
78
                out.append(c)
 
79
            else:
 
80
                out.append("_")
 
81
        return "report_" + "".join(out)
 
82
 
 
83
    if namespace:
 
84
        method_name = to_method(namespace + "_" + name)
 
85
    else:
 
86
        method_name = to_method(name)
 
87
 
 
88
    try:
 
89
        method = getattr(self, method_name)
 
90
    except AttributeError:
 
91
        #
 
92
        # Requested report is not supported.
 
93
        #
 
94
        log.err("Unsupported REPORT {%s}%s for resource %s (no method %s)"
 
95
                % (namespace, name, self, method_name))
 
96
 
 
97
        raise HTTPError(ErrorResponse(
 
98
            responsecode.FORBIDDEN,
 
99
            davxml.SupportedReport()
 
100
        ))
 
101
 
 
102
    yield method(request, doc.root_element)
 
103
 
 
104
http_REPORT = deferredGenerator(http_REPORT)