1
# -*- test-case-name: twisted.web2.dav.test.test_prop.PROP.test_PROPPATCH -*-
3
# Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
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:
12
# The above copyright notice and this permission notice shall be included in all
13
# copies or substantial portions of the Software.
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
23
# DRI: Wilfredo Sanchez, wsanchez@apple.com
27
WebDAV-aware static resources.
30
__all__ = ["http_PROPPATCH"]
32
from twisted.python import log
33
from twisted.python.failure import Failure
34
from twisted.internet.defer import deferredGenerator, waitForDeferred
35
from twisted.web2 import responsecode
36
from twisted.web2.http import HTTPError, StatusResponse
37
from twisted.web2.dav import davxml
38
from twisted.web2.dav.http import MultiStatusResponse, PropertyStatusResponseQueue
39
from twisted.web2.dav.util import davXMLFromStream
41
def http_PROPPATCH(self, request):
43
Respond to a PROPPATCH request. (RFC 2518, section 8.2)
45
if not self.fp.exists():
46
log.err("File not found: %s" % (self.fp.path,))
47
raise HTTPError(responsecode.NOT_FOUND)
53
doc = waitForDeferred(davXMLFromStream(request.stream))
57
log.err("Error while handling PROPPATCH body: %s" % (e,))
58
raise HTTPError(StatusResponse(responsecode.BAD_REQUEST, str(e)))
61
error = "Request XML body is required."
63
raise HTTPError(StatusResponse(responsecode.BAD_REQUEST, error))
68
update = doc.root_element
69
if not isinstance(update, davxml.PropertyUpdate):
70
error = ("Request XML body must be a propertyupdate element."
71
% (davxml.PropertyUpdate.sname(),))
73
raise HTTPError(StatusResponse(responsecode.BAD_REQUEST, error))
75
responses = PropertyStatusResponseQueue("PROPPATCH", request.uri, responsecode.NO_CONTENT)
83
for setOrRemove in update.children:
84
assert len(setOrRemove.children) == 1
86
container = setOrRemove.children[0]
88
assert isinstance(container, davxml.PropertyContainer)
90
properties = container.children
92
def do(action, property):
94
Perform action(property, request) while maintaining an
97
has = waitForDeferred(self.hasProperty(property, request))
102
oldProperty = waitForDeferred(self.readProperty(property, request))
104
oldProperty.getResult()
107
return self.writeProperty(oldProperty, request)
110
return self.removeProperty(property, request)
113
x = waitForDeferred(action(property, request))
116
except ValueError, e:
117
# Convert ValueError exception into HTTPError
119
Failure(exc_value=HTTPError(StatusResponse(responsecode.FORBIDDEN, str(e)))),
125
responses.add(Failure(), property)
129
responses.add(responsecode.OK, property)
131
# Only add undo action for those that succeed because those that fail will not have changed
132
undoActions.append(undo)
137
do = deferredGenerator(do)
139
if isinstance(setOrRemove, davxml.Set):
140
for property in properties:
141
ok = waitForDeferred(do(self.writeProperty, property))
146
elif isinstance(setOrRemove, davxml.Remove):
147
for property in properties:
148
ok = waitForDeferred(do(self.removeProperty, property))
154
raise AssertionError("Unknown child of PropertyUpdate: %s" % (setOrRemove,))
157
# If there is an error, we have to back out whatever we have
158
# operations we have done because PROPPATCH is an
159
# all-or-nothing request.
160
# We handle the first one here, and then re-raise to handle the
161
# rest in the containing scope.
163
for action in undoActions:
164
x = waitForDeferred(action())
170
# If we had an error we need to undo any changes that did succeed and change status of
171
# those to 424 Failed Dependency.
174
for action in undoActions:
175
x = waitForDeferred(action())
183
yield MultiStatusResponse([responses.response()])
185
http_PROPPATCH = deferredGenerator(http_PROPPATCH)