2010-10-14 07:43:56 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2010-10-22 09:06:27 -05:00
|
|
|
import os, re, httplib, sys, stat, urlparse, time
|
|
|
|
from urllib import quote
|
2010-10-14 07:43:56 -05:00
|
|
|
from StringIO import StringIO
|
|
|
|
from mimetypes import guess_type
|
|
|
|
from base64 import encodestring
|
2010-11-08 04:40:41 -06:00
|
|
|
from appy import Object
|
2010-10-15 08:14:28 -05:00
|
|
|
from appy.shared.utils import copyData
|
2010-10-22 09:06:27 -05:00
|
|
|
from appy.gen.utils import sequenceTypes
|
2010-11-08 04:40:41 -06:00
|
|
|
from appy.shared.xml_parser import XmlUnmarshaller, XmlMarshaller
|
2010-10-22 09:06:27 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2010-11-08 04:40:41 -06:00
|
|
|
class FormDataEncoder:
|
2010-10-22 09:06:27 -05:00
|
|
|
'''Allows to encode form data for sending it through a HTTP request.'''
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data # The data to encode, as a dict
|
|
|
|
|
|
|
|
def marshalValue(self, name, value):
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
return '%s=%s' % (name, quote(str(value)))
|
|
|
|
elif isinstance(value, float):
|
|
|
|
return '%s:float=%s' % (name, value)
|
|
|
|
elif isinstance(value, int):
|
|
|
|
return '%s:int=%s' % (name, value)
|
|
|
|
elif isinstance(value, long):
|
|
|
|
res = '%s:long=%s' % (name, value)
|
|
|
|
if res[-1] == 'L':
|
|
|
|
res = res[:-1]
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
raise 'Cannot encode value %s' % str(value)
|
|
|
|
|
|
|
|
def encode(self):
|
|
|
|
res = []
|
|
|
|
for name, value in self.data.iteritems():
|
|
|
|
res.append(self.marshalValue(name, value))
|
|
|
|
return '&'.join(res)
|
|
|
|
|
2010-11-08 04:40:41 -06:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class SoapDataEncoder:
|
|
|
|
'''Allows to encode SOAP data for sending it through a HTTP request.'''
|
|
|
|
namespaces = {'SOAP-ENV': 'http://schemas.xmlsoap.org/soap/envelope/',
|
|
|
|
'xsd' : 'http://www.w3.org/2001/XMLSchema',
|
|
|
|
'xsi' : 'http://www.w3.org/2001/XMLSchema-instance'}
|
|
|
|
namespacedTags = {'Envelope': 'SOAP-ENV', 'Body': 'SOAP-ENV', '*': 'py'}
|
|
|
|
|
|
|
|
def __init__(self, data, namespace='http://appyframework.org'):
|
|
|
|
self.data = data
|
|
|
|
# p_data can be:
|
|
|
|
# - a string already containing a complete SOAP message
|
|
|
|
# - a Python object, that we will convert to a SOAP message
|
|
|
|
# Define the namespaces for this request
|
|
|
|
self.ns = self.namespaces.copy()
|
|
|
|
self.ns['py'] = namespace
|
|
|
|
|
|
|
|
def encode(self):
|
|
|
|
# Do nothing if we have a SOAP message already
|
|
|
|
if isinstance(self.data, basestring): return self.data
|
|
|
|
# self.data is here a Python object. Wrap it a SOAP Body.
|
|
|
|
soap = Object(Body=self.data)
|
|
|
|
# Marshall it.
|
|
|
|
marshaller = XmlMarshaller(rootTag='Envelope', namespaces=self.ns,
|
|
|
|
namespacedTags=self.namespacedTags)
|
|
|
|
return marshaller.marshall(soap)
|
|
|
|
|
2010-10-22 09:06:27 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class HttpResponse:
|
|
|
|
'''Stores information about a HTTP response.'''
|
|
|
|
def __init__(self, code, text, headers, body, duration=None):
|
|
|
|
self.code = code # The return code, ie 404, 200, ...
|
|
|
|
self.text = text # Textual description of the code
|
|
|
|
self.headers = headers # A dict-like object containing the headers
|
|
|
|
self.body = body # The body of the HTTP response
|
2010-11-08 04:40:41 -06:00
|
|
|
# p_duration, if given, is the time, in seconds, we have waited, before
|
|
|
|
# getting this response after having sent the request.
|
|
|
|
self.duration = duration
|
2010-10-22 09:06:27 -05:00
|
|
|
# The following attribute may contain specific data extracted from
|
|
|
|
# the previous fields. For example, when response if 302 (Redirect),
|
|
|
|
# self.data contains the URI where we must redirect the user to.
|
|
|
|
self.data = self.extractData()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
duration = ''
|
|
|
|
if self.duration: duration = ', got in %.4f seconds' % self.duration
|
|
|
|
return '<HttpResponse %s (%s)%s>' % (self.code, self.text, duration)
|
|
|
|
|
|
|
|
def extractData(self):
|
|
|
|
'''This method extracts, from the various parts of the HTTP response,
|
|
|
|
some useful information. For example, it will find the URI where to
|
2010-11-08 04:40:41 -06:00
|
|
|
redirect the user to if self.code is 302, or will unmarshall XML
|
|
|
|
data into Python objects.'''
|
2010-10-22 09:06:27 -05:00
|
|
|
if self.code == 302:
|
|
|
|
return urlparse.urlparse(self.headers['location'])[2]
|
2010-11-08 04:40:41 -06:00
|
|
|
elif self.headers.has_key('content-type') and \
|
|
|
|
self.headers['content-type'].startswith('text/xml'):
|
|
|
|
# Return an unmarshalled version of the XML content, for easy use
|
|
|
|
# in Python.
|
|
|
|
return XmlUnmarshaller().parse(self.body)
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
urlRex = re.compile(r'http://([^:/]+)(:[0-9]+)?(/.+)?', re.I)
|
2010-10-15 08:14:28 -05:00
|
|
|
binaryRex = re.compile(r'[\000-\006\177-\277]')
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
class Resource:
|
|
|
|
'''Every instance of this class represents some web resource accessible
|
2010-10-27 05:06:21 -05:00
|
|
|
through HTTP.'''
|
2010-10-14 07:43:56 -05:00
|
|
|
|
2010-10-22 09:06:27 -05:00
|
|
|
def __init__(self, url, username=None, password=None, measure=False):
|
2010-10-14 07:43:56 -05:00
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
self.url = url
|
2010-10-22 09:06:27 -05:00
|
|
|
# If some headers must be sent with any request sent through this
|
|
|
|
# resource (like a cookie), you can store them in the following dict.
|
|
|
|
self.headers = {}
|
|
|
|
# If p_measure is True, we will measure, for every request sent, the
|
|
|
|
# time we wait until we receive the response.
|
|
|
|
self.measure = measure
|
|
|
|
# If measure is True, we will store hereafter, the total time (in
|
|
|
|
# seconds) spent waiting for the server for all requests sent through
|
|
|
|
# this resource object.
|
|
|
|
self.serverTime = 0
|
2010-10-14 07:43:56 -05:00
|
|
|
# Split the URL into its components
|
|
|
|
res = urlRex.match(url)
|
|
|
|
if res:
|
|
|
|
host, port, uri = res.group(1,2,3)
|
|
|
|
self.host = host
|
|
|
|
self.port = port and int(port[1:]) or 80
|
|
|
|
self.uri = uri or '/'
|
|
|
|
else: raise 'Wrong URL: %s' % str(url)
|
|
|
|
|
2010-10-15 08:14:28 -05:00
|
|
|
def __repr__(self):
|
2010-10-27 05:06:21 -05:00
|
|
|
return '<Dav resource at %s>' % self.url
|
2010-10-15 08:14:28 -05:00
|
|
|
|
2010-10-14 07:43:56 -05:00
|
|
|
def updateHeaders(self, headers):
|
|
|
|
# Add credentials if present
|
|
|
|
if not (self.username and self.password): return
|
|
|
|
if headers.has_key('Authorization'): return
|
|
|
|
credentials = '%s:%s' % (self.username,self.password)
|
|
|
|
credentials = credentials.replace('\012','')
|
|
|
|
headers['Authorization'] = "Basic %s" % encodestring(credentials)
|
|
|
|
headers['User-Agent'] = 'WebDAV.client'
|
|
|
|
headers['Host'] = self.host
|
|
|
|
headers['Connection'] = 'close'
|
|
|
|
headers['Accept'] = '*/*'
|
|
|
|
return headers
|
|
|
|
|
2010-11-08 04:40:41 -06:00
|
|
|
def send(self, method, uri, body=None, headers={}, bodyType=None):
|
2010-10-14 07:43:56 -05:00
|
|
|
'''Sends a HTTP request with p_method, for p_uri.'''
|
|
|
|
conn = httplib.HTTP()
|
|
|
|
conn.connect(self.host, self.port)
|
2010-10-22 09:06:27 -05:00
|
|
|
# Tell what kind of HTTP request it will be.
|
2010-10-14 07:43:56 -05:00
|
|
|
conn.putrequest(method, uri)
|
|
|
|
# Add HTTP headers
|
|
|
|
self.updateHeaders(headers)
|
2010-10-22 09:06:27 -05:00
|
|
|
if self.headers: headers.update(self.headers)
|
2010-10-14 07:43:56 -05:00
|
|
|
for n, v in headers.items(): conn.putheader(n, v)
|
|
|
|
conn.endheaders()
|
2010-10-22 09:06:27 -05:00
|
|
|
# Add HTTP body
|
|
|
|
if body:
|
|
|
|
if not bodyType: bodyType = 'string'
|
|
|
|
copyData(body, conn, 'send', type=bodyType)
|
|
|
|
# Send the request, get the reply
|
|
|
|
if self.measure: startTime = time.time()
|
|
|
|
code, text, headers = conn.getreply()
|
|
|
|
if self.measure: endTime = time.time()
|
|
|
|
body = conn.getfile().read()
|
2010-10-14 07:43:56 -05:00
|
|
|
conn.close()
|
2010-10-22 09:06:27 -05:00
|
|
|
# Return a smart object containing the various parts of the response
|
|
|
|
duration = None
|
|
|
|
if self.measure:
|
|
|
|
duration = endTime - startTime
|
|
|
|
self.serverTime += duration
|
|
|
|
return HttpResponse(code, text, headers, body, duration=duration)
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
def mkdir(self, name):
|
|
|
|
'''Creates a folder named p_name in this resource.'''
|
|
|
|
folderUri = self.uri + '/' + name
|
|
|
|
#body = '<d:propertyupdate xmlns:d="DAV:"><d:set><d:prop>' \
|
|
|
|
# '<d:displayname>%s</d:displayname></d:prop></d:set>' \
|
|
|
|
# '</d:propertyupdate>' % name
|
2010-11-08 04:40:41 -06:00
|
|
|
return self.send('MKCOL', folderUri)
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
def delete(self, name):
|
|
|
|
'''Deletes a file or a folder (and all contained files if any) named
|
|
|
|
p_name within this resource.'''
|
|
|
|
toDeleteUri = self.uri + '/' + name
|
2010-11-08 04:40:41 -06:00
|
|
|
return self.send('DELETE', toDeleteUri)
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
def add(self, content, type='fileName', name=''):
|
|
|
|
'''Adds a file in this resource. p_type can be:
|
|
|
|
- "fileName" In this case, p_content is the path to a file on disk
|
|
|
|
and p_name is ignored;
|
|
|
|
- "zope" In this case, p_content is an instance of
|
|
|
|
OFS.Image.File and the name of the file is given in
|
|
|
|
p_name.
|
|
|
|
'''
|
|
|
|
if type == 'fileName':
|
|
|
|
# p_content is the name of a file on disk
|
2010-10-15 08:14:28 -05:00
|
|
|
size = os.stat(content)[stat.ST_SIZE]
|
|
|
|
body = file(content, 'rb')
|
|
|
|
name = os.path.basename(content)
|
|
|
|
fileType, encoding = guess_type(content)
|
|
|
|
bodyType = 'file'
|
2010-10-14 07:43:56 -05:00
|
|
|
elif type == 'zope':
|
|
|
|
# p_content is a "Zope" file, ie a OFS.Image.File instance
|
2010-10-15 08:14:28 -05:00
|
|
|
# p_name is given
|
2010-10-14 07:43:56 -05:00
|
|
|
fileType = content.content_type
|
|
|
|
encoding = None
|
2010-10-15 08:14:28 -05:00
|
|
|
size = content.size
|
|
|
|
body = content
|
|
|
|
bodyType = 'zope'
|
|
|
|
fileUri = self.uri + '/' + name
|
|
|
|
headers = {'Content-Length': str(size)}
|
2010-10-14 07:43:56 -05:00
|
|
|
if fileType: headers['Content-Type'] = fileType
|
|
|
|
if encoding: headers['Content-Encoding'] = encoding
|
2010-11-08 04:40:41 -06:00
|
|
|
res = self.send('PUT', fileUri, body, headers, bodyType=bodyType)
|
2010-10-15 08:14:28 -05:00
|
|
|
# Close the file when relevant
|
|
|
|
if type =='fileName': body.close()
|
2010-10-22 09:06:27 -05:00
|
|
|
return res
|
|
|
|
|
|
|
|
def get(self, uri=None, headers={}):
|
|
|
|
'''Perform a HTTP GET on the server.'''
|
|
|
|
if not uri: uri = self.uri
|
2010-11-08 04:40:41 -06:00
|
|
|
return self.send('GET', uri, headers=headers)
|
|
|
|
|
|
|
|
def post(self, data=None, uri=None, headers={}, encode='form'):
|
|
|
|
'''Perform a HTTP POST on the server. If p_encode is "form", p_data is
|
|
|
|
considered to be a dict representing form data that will be
|
|
|
|
form-encoded. Else, p_data will be considered as the ready-to-send
|
|
|
|
body of the HTTP request.'''
|
2010-10-22 09:06:27 -05:00
|
|
|
if not uri: uri = self.uri
|
2010-10-27 05:06:21 -05:00
|
|
|
# Prepare the data to send
|
2010-11-08 04:40:41 -06:00
|
|
|
headers['Host'] = self.host
|
|
|
|
if encode == 'form':
|
2010-10-27 05:06:21 -05:00
|
|
|
# Format the form data and prepare headers
|
2010-11-08 04:40:41 -06:00
|
|
|
body = FormDataEncoder(data).encode()
|
2010-10-27 05:06:21 -05:00
|
|
|
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
2010-11-08 04:40:41 -06:00
|
|
|
else:
|
2010-10-27 05:06:21 -05:00
|
|
|
body = data
|
2010-10-22 09:06:27 -05:00
|
|
|
headers['Content-Length'] = str(len(body))
|
2010-11-08 04:40:41 -06:00
|
|
|
return self.send('POST', uri, headers=headers, body=body)
|
|
|
|
|
|
|
|
def soap(self, data, uri=None, headers={}, namespace=None):
|
|
|
|
'''Sends a SOAP message to this resource. p_namespace is the URL of the
|
|
|
|
server-specific namespace.'''
|
|
|
|
if not uri: uri = self.uri
|
|
|
|
# Prepare the data to send
|
|
|
|
data = SoapDataEncoder(data, namespace).encode()
|
|
|
|
headers['SOAPAction'] = self.url
|
|
|
|
headers['Content-Type'] = 'text/xml'
|
|
|
|
res = self.post(data, uri, headers=headers, encode=None)
|
|
|
|
# Unwrap content from the SOAP envelope
|
|
|
|
res.data = res.data.Body
|
|
|
|
return res
|
2010-10-14 07:43:56 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2010-11-08 04:40:41 -06:00
|
|
|
|