Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Use encodeXMLName()
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfredo Sanchez committed Jul 20, 2012
1 parent 88185b0 commit e9d8c08
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 83 deletions.
7 changes: 4 additions & 3 deletions twext/web2/dav/method/report.py
Expand Up @@ -44,6 +44,7 @@
from twext.web2.dav.util import davXMLFromStream from twext.web2.dav.util import davXMLFromStream
from txdav.xml import element as davxml from txdav.xml import element as davxml
from txdav.xml.element import lookupElement from txdav.xml.element import lookupElement
from txdav.xml.base import encodeXMLName


log = Logger() log = Logger()


Expand Down Expand Up @@ -115,7 +116,7 @@ def to_method(s):
if namespace == davxml.dav_namespace: if namespace == davxml.dav_namespace:
request.submethod = "DAV:" + name request.submethod = "DAV:" + name
else: else:
request.submethod = "{%s}%s" % (namespace, name) request.submethod = encodeXMLName(namespace, name)
else: else:
method_name = to_method(name) method_name = to_method(name)


Expand All @@ -136,8 +137,8 @@ def to_method(s):
# #
# Requested report is not supported. # Requested report is not supported.
# #
log.err("Unsupported REPORT {%s}%s for resource %s (no method %s)" log.err("Unsupported REPORT %s for resource %s (no method %s)"
% (namespace, name, self, method_name)) % (encodeXMLName(namespace, name), self, method_name))


raise HTTPError(ErrorResponse( raise HTTPError(ErrorResponse(
responsecode.FORBIDDEN, responsecode.FORBIDDEN,
Expand Down
12 changes: 10 additions & 2 deletions twext/web2/dav/noneprops.py
Expand Up @@ -33,6 +33,8 @@


from twext.web2 import responsecode from twext.web2 import responsecode
from twext.web2.http import HTTPError, StatusResponse from twext.web2.http import HTTPError, StatusResponse
from txdav.xml.base import encodeXMLName



class NonePropertyStore (object): class NonePropertyStore (object):
""" """
Expand All @@ -50,10 +52,16 @@ def __init__(self, resource):
pass pass


def get(self, qname, uid=None): def get(self, qname, uid=None):
raise HTTPError(StatusResponse(responsecode.NOT_FOUND, "No such property: {%s}%s" % qname)) raise HTTPError(StatusResponse(
responsecode.NOT_FOUND,
"No such property: %s" % (encodeXMLName(*qname),)
))


def set(self, property, uid=None): def set(self, property, uid=None):
raise HTTPError(StatusResponse(responsecode.FORBIDDEN, "Permission denied for setting property: %s" % (property,))) raise HTTPError(StatusResponse(
responsecode.FORBIDDEN,
"Permission denied for setting property: %s" % (property,)
))


def delete(self, qname, uid=None): def delete(self, qname, uid=None):
# RFC 2518 Section 12.13.1 says that removal of # RFC 2518 Section 12.13.1 says that removal of
Expand Down
19 changes: 10 additions & 9 deletions twext/web2/dav/resource.py
Expand Up @@ -57,6 +57,12 @@
from twisted.internet import reactor from twisted.internet import reactor


from twext.python.log import Logger from twext.python.log import Logger
from txdav.xml import element
from txdav.xml.base import encodeXMLName
from txdav.xml.element import WebDAVElement, WebDAVEmptyElement, WebDAVTextElement
from txdav.xml.element import dav_namespace
from txdav.xml.element import twisted_dav_namespace, twisted_private_namespace
from txdav.xml.element import registerElement, lookupElement
from twext.web2 import responsecode from twext.web2 import responsecode
from twext.web2.http import HTTPError, RedirectResponse, StatusResponse from twext.web2.http import HTTPError, RedirectResponse, StatusResponse
from twext.web2.http_headers import generateContentType from twext.web2.http_headers import generateContentType
Expand All @@ -70,11 +76,6 @@
from twext.web2.dav.noneprops import NonePropertyStore from twext.web2.dav.noneprops import NonePropertyStore
from twext.web2.dav.util import unimplemented, parentForURL, joinURL from twext.web2.dav.util import unimplemented, parentForURL, joinURL
from twext.web2.dav.auth import PrincipalCredentials from twext.web2.dav.auth import PrincipalCredentials
from txdav.xml import element
from txdav.xml.element import WebDAVElement, WebDAVEmptyElement, WebDAVTextElement
from txdav.xml.element import dav_namespace
from txdav.xml.element import twisted_dav_namespace, twisted_private_namespace
from txdav.xml.element import registerElement, lookupElement




log = Logger() log = Logger()
Expand Down Expand Up @@ -217,7 +218,7 @@ def readProperty(self, property, request):
def defer(): def defer():
if type(property) is tuple: if type(property) is tuple:
qname = property qname = property
sname = "{%s}%s" % property sname = encodeXMLName(*property)
else: else:
qname = property.qname() qname = property.qname()
sname = property.sname() sname = property.sname()
Expand Down Expand Up @@ -416,7 +417,7 @@ def removeProperty(self, property, request):
def defer(): def defer():
if type(property) is tuple: if type(property) is tuple:
qname = property qname = property
sname = "{%s}%s" % property sname = encodeXMLName(*property)
else: else:
qname = property.qname() qname = property.qname()
sname = property.sname() sname = property.sname()
Expand Down Expand Up @@ -1873,9 +1874,9 @@ def gotPrincipal(principal):
return None return None


if not isinstance(principal, element.Principal): if not isinstance(principal, element.Principal):
log.err("Non-principal value in property {%s}%s " log.err("Non-principal value in property %s "
"referenced by property principal." "referenced by property principal."
% (namespace, name)) % (encodeXMLName(namespace, name),))
return None return None


if len(principal.children) != 1: if len(principal.children) != 1:
Expand Down
4 changes: 2 additions & 2 deletions twext/web2/dav/test/test_xattrprops.py
Expand Up @@ -132,7 +132,7 @@ def _setValue(self, originalDocument, value, uid=None):
attribute = ( attribute = (
self.propertyStore.deadPropertyXattrPrefix + self.propertyStore.deadPropertyXattrPrefix +
(uid if uid is not None else "") + (uid if uid is not None else "") +
"{%s}%s" % element.qname()) element.sname())
self.attrs[attribute] = value self.attrs[attribute] = value




Expand All @@ -141,7 +141,7 @@ def _getValue(self, originalDocument, uid=None):
attribute = ( attribute = (
self.propertyStore.deadPropertyXattrPrefix + self.propertyStore.deadPropertyXattrPrefix +
(uid if uid is not None else "") + (uid if uid is not None else "") +
"{%s}%s" % element.qname()) element.sname())
return self.attrs[attribute] return self.attrs[attribute]




Expand Down
3 changes: 2 additions & 1 deletion twext/web2/dav/test/util.py
Expand Up @@ -38,6 +38,7 @@
from twext.web2.dav.static import DAVFile from twext.web2.dav.static import DAVFile
from twext.web2.dav.util import joinURL from twext.web2.dav.util import joinURL
from txdav.xml import element from txdav.xml import element
from txdav.xml.base import encodeXMLName


log = Logger() log = Logger()


Expand All @@ -57,7 +58,7 @@ def get(self, qname):
except KeyError: except KeyError:
raise HTTPError(StatusResponse( raise HTTPError(StatusResponse(
responsecode.NOT_FOUND, responsecode.NOT_FOUND,
"No such property: {%s}%s" % qname "No such property: %s" % (encodeXMLName(*qname),)
)) ))


doc = element.WebDAVDocument.fromString(property) doc = element.WebDAVDocument.fromString(property)
Expand Down
48 changes: 26 additions & 22 deletions twext/web2/dav/xattrprops.py
Expand Up @@ -50,10 +50,11 @@
from twisted.python.util import untilConcludes from twisted.python.util import untilConcludes
from twisted.python.failure import Failure from twisted.python.failure import Failure
from twisted.python.log import err from twisted.python.log import err
from txdav.xml.base import encodeXMLName
from txdav.xml.parser import WebDAVDocument
from twext.web2 import responsecode from twext.web2 import responsecode
from twext.web2.http import HTTPError, StatusResponse from twext.web2.http import HTTPError, StatusResponse
from twext.web2.dav.http import statusForFailure from twext.web2.dav.http import statusForFailure
from txdav.xml.parser import WebDAVDocument


# RFC 2518 Section 12.13.1 says that removal of non-existing property # RFC 2518 Section 12.13.1 says that removal of non-existing property
# is not an error. python-xattr on Linux fails with ENODATA in this # is not an error. python-xattr on Linux fails with ENODATA in this
Expand Down Expand Up @@ -86,7 +87,7 @@ class xattrPropertyStore (object):
deadPropertyXattrPrefix = "user." deadPropertyXattrPrefix = "user."


def _encode(clazz, name, uid=None): def _encode(clazz, name, uid=None):
result = urllib.quote("{%s}%s" % name, safe='{}:') result = urllib.quote(encodeXMLName(*name), safe='{}:')
if uid: if uid:
result = uid + result result = uid + result
r = clazz.deadPropertyXattrPrefix + result r = clazz.deadPropertyXattrPrefix + result
Expand Down Expand Up @@ -138,17 +139,20 @@ def get(self, qname, uid=None):
data = self.attrs.get(self._encode(qname, uid)) data = self.attrs.get(self._encode(qname, uid))
except KeyError: except KeyError:
raise HTTPError(StatusResponse( raise HTTPError(StatusResponse(
responsecode.NOT_FOUND, responsecode.NOT_FOUND,
"No such property: {%s}%s" % qname)) "No such property: %s" % (encodeXMLName(*qname),)
))
except IOError, e: except IOError, e:
if e.errno in _ATTR_MISSING or e.errno == errno.ENOENT: if e.errno in _ATTR_MISSING or e.errno == errno.ENOENT:
raise HTTPError(StatusResponse( raise HTTPError(StatusResponse(
responsecode.NOT_FOUND, responsecode.NOT_FOUND,
"No such property: {%s}%s" % qname)) "No such property: %s" % (encodeXMLName(*qname),)
))
else: else:
raise HTTPError(StatusResponse( raise HTTPError(StatusResponse(
statusForFailure(Failure()), statusForFailure(Failure()),
"Unable to read property: {%s}%s" % qname)) "Unable to read property: %s" % (encodeXMLName(*qname),)
))


# #
# Unserialize XML data from an xattr. The storage format has changed # Unserialize XML data from an xattr. The storage format has changed
Expand All @@ -175,8 +179,8 @@ def get(self, qname, uid=None):
try: try:
doc = unpickle(data) doc = unpickle(data)
except UnpicklingError: except UnpicklingError:
format = "Invalid property value stored on server: {%s}%s %s" format = "Invalid property value stored on server: %s %s"
msg = format % (qname[0], qname[1], data) msg = format % (encodeXMLName(*qname), data)
err(None, msg) err(None, msg)
raise HTTPError( raise HTTPError(
StatusResponse(responsecode.INTERNAL_SERVER_ERROR, msg)) StatusResponse(responsecode.INTERNAL_SERVER_ERROR, msg))
Expand Down Expand Up @@ -225,10 +229,10 @@ def delete(self, qname, uid=None):
if e.errno not in _ATTR_MISSING: if e.errno not in _ATTR_MISSING:
raise raise
except: except:
raise HTTPError( raise HTTPError(StatusResponse(
StatusResponse( statusForFailure(Failure()),
statusForFailure(Failure()), "Unable to delete property: %s", (key,)
"Unable to delete property: " + key)) ))




def contains(self, qname, uid=None): def contains(self, qname, uid=None):
Expand All @@ -252,10 +256,10 @@ def contains(self, qname, uid=None):
except IOError, e: except IOError, e:
if e.errno in _ATTR_MISSING or e.errno == errno.ENOENT: if e.errno in _ATTR_MISSING or e.errno == errno.ENOENT:
return False return False
raise HTTPError( raise HTTPError(StatusResponse(
StatusResponse( statusForFailure(Failure()),
statusForFailure(Failure()), "Unable to read property: %s" % (key,)
"Unable to read property: " + key)) ))
else: else:
return True return True


Expand All @@ -277,10 +281,10 @@ def list(self, uid=None, filterByUID=True):
except IOError, e: except IOError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
return [] return []
raise HTTPError( raise HTTPError(StatusResponse(
StatusResponse( statusForFailure(Failure()),
statusForFailure(Failure()), "Unable to list properties: %s", (self.resource.fp.path,)
"Unable to list properties: " + self.resource.fp.path)) ))
else: else:
results = [ results = [
self._decode(name) self._decode(name)
Expand Down
7 changes: 4 additions & 3 deletions twistedcaldav/extensions.py
Expand Up @@ -46,6 +46,7 @@
from twext.web2.stream import FileStream from twext.web2.stream import FileStream
from twext.web2.static import MetaDataMixin, StaticRenderMixin from twext.web2.static import MetaDataMixin, StaticRenderMixin
from txdav.xml import element from txdav.xml import element
from txdav.xml.base import encodeXMLName
from txdav.xml.element import dav_namespace from txdav.xml.element import dav_namespace
from twext.web2.dav.http import MultiStatusResponse from twext.web2.dav.http import MultiStatusResponse
from twext.web2.dav.static import DAVFile as SuperDAVFile from twext.web2.dav.static import DAVFile as SuperDAVFile
Expand Down Expand Up @@ -339,8 +340,8 @@ def gotError(f, name):
whenAllProperties = gatherResults([ whenAllProperties = gatherResults([
maybeDeferred(self.resource.readProperty, qn, request) maybeDeferred(self.resource.readProperty, qn, request)
.addCallback(lambda p, iqn=qn: (p.sname(), p.toxml()) .addCallback(lambda p, iqn=qn: (p.sname(), p.toxml())
if p is not None else ("{%s}%s" % iqn, None) ) if p is not None else (encodeXMLName(*iqn), None) )
.addErrback(gotError, "{%s}%s" % qn) .addErrback(gotError, encodeXMLName(*qn))
for qn in sorted(qnames) for qn in sorted(qnames)
]) ])


Expand Down Expand Up @@ -771,7 +772,7 @@ def __init__(self, qname):
HTTPError.__init__(self, HTTPError.__init__(self,
StatusResponse( StatusResponse(
responsecode.NOT_FOUND, responsecode.NOT_FOUND,
"No such property: {%s}%s" % qname "No such property: %s" % encodeXMLName(*qname)
) )
) )


Expand Down
8 changes: 4 additions & 4 deletions twistedcaldav/memcacheprops.py
Expand Up @@ -36,6 +36,7 @@
from twext.python.memcacheclient import ClientFactory from twext.python.memcacheclient import ClientFactory
from twext.python.memcacheclient import MemcacheError, TokenMismatchError from twext.python.memcacheclient import MemcacheError, TokenMismatchError
from twext.python.filepath import CachingFilePath as FilePath from twext.python.filepath import CachingFilePath as FilePath
from txdav.xml.base import encodeXMLName
from twext.web2 import responsecode from twext.web2 import responsecode
from twext.web2.http import HTTPError, StatusResponse from twext.web2.http import HTTPError, StatusResponse


Expand Down Expand Up @@ -293,11 +294,10 @@ def setProperty(self, child, property, uid, delete=False):
else: else:
log.error("memcacheprops setProperty had too many failures") log.error("memcacheprops setProperty had too many failures")
delattr(self, "_propertyCache") delattr(self, "_propertyCache")
raise MemcacheError("Unable to %s property %s{%s}%s on %s" % ( raise MemcacheError("Unable to %s property %s%s on %s" % (
"delete" if delete else "set", "delete" if delete else "set",
uid if uid else "", uid if uid else "",
qname[0], encodeXMLName(*qname),
qname[1],
child child
)) ))


Expand Down Expand Up @@ -345,7 +345,7 @@ def get(self, qname, uid=None, cache=True):
else: else:
raise HTTPError(StatusResponse( raise HTTPError(StatusResponse(
responsecode.NOT_FOUND, responsecode.NOT_FOUND,
"No such property: %s{%s}%s" % (uid if uid else "", qname[0], qname[1],) "No such property: %s%s" % (uid if uid else "", encodeXMLName(*qname))
)) ))


self.log_debug("Read for %s%s on %s" % ( self.log_debug("Read for %s%s on %s" % (
Expand Down
7 changes: 4 additions & 3 deletions twistedcaldav/method/report.py
Expand Up @@ -33,6 +33,7 @@
from twext.web2.http import HTTPError, StatusResponse from twext.web2.http import HTTPError, StatusResponse
from twext.web2.dav.util import davXMLFromStream from twext.web2.dav.util import davXMLFromStream
from txdav.xml import element as davxml from txdav.xml import element as davxml
from txdav.xml.base import encodeXMLName
from txdav.xml.element import lookupElement from txdav.xml.element import lookupElement


from twext.python.log import Logger from twext.python.log import Logger
Expand Down Expand Up @@ -78,7 +79,7 @@ def http_REPORT(self, request):
elif namespace == caldavxml.caldav_namespace: elif namespace == caldavxml.caldav_namespace:
request.submethod = "CalDAV:" + name request.submethod = "CalDAV:" + name
else: else:
request.submethod = "{%s}%s" % (namespace, name) request.submethod = encodeXMLName(namespace, name)
else: else:
request.submethod = name request.submethod = name


Expand Down Expand Up @@ -114,8 +115,8 @@ def to_method(namespace, name):
# #
# Requested report is not supported. # Requested report is not supported.
# #
log.err("Unsupported REPORT {%s}%s for resource %s (no method %s)" log.err("Unsupported REPORT %s for resource %s (no method %s)"
% (namespace, name, self, method_name)) % (encodeXMLName(namespace, name), self, method_name))


raise HTTPError(ErrorResponse( raise HTTPError(ErrorResponse(
responsecode.FORBIDDEN, responsecode.FORBIDDEN,
Expand Down

0 comments on commit e9d8c08

Please sign in to comment.