2010-10-14 07:43:56 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2011-04-15 04:26:20 -05:00
|
|
|
import os, re, httplib, sys, stat, urlparse, time, socket, xml.sax
|
2010-10-22 09:06:27 -05:00
|
|
|
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
|
2012-01-12 14:49:23 -06:00
|
|
|
from appy.shared.utils import copyData, 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-12-17 07:46:55 -06:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class ResourceError(Exception): pass
|
|
|
|
|
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
|
2013-07-11 09:41:45 -05:00
|
|
|
# self.data is here a Python object. Wrap it in a SOAP Body.
|
2010-11-08 04:40:41 -06:00
|
|
|
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.'''
|
2013-07-11 16:40:01 -05:00
|
|
|
def __init__(self, response, body, duration=None):
|
|
|
|
self.code = response.status # The return code, ie 404, 200, 500...
|
|
|
|
self.text = response.reason # Textual description of the code
|
|
|
|
self.headers = response.msg # A dict-like object containing the headers
|
2010-10-22 09:06:27 -05:00
|
|
|
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)
|
|
|
|
|
2011-04-15 04:26:20 -05:00
|
|
|
def extractContentType(self, contentType):
|
|
|
|
'''Extract the content type from the HTTP header, potentially removing
|
|
|
|
encoding-related data.'''
|
|
|
|
i = contentType.find(';')
|
|
|
|
if i != -1: return contentType[:i]
|
|
|
|
return contentType
|
|
|
|
|
|
|
|
xmlHeaders = ('text/xml', 'application/xml', 'application/soap+xml')
|
2010-10-22 09:06:27 -05:00
|
|
|
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-12-17 07:46:55 -06:00
|
|
|
elif self.headers.has_key('content-type'):
|
2011-04-15 04:26:20 -05:00
|
|
|
contentType = self.extractContentType(self.headers['content-type'])
|
2010-12-17 07:46:55 -06:00
|
|
|
for xmlHeader in self.xmlHeaders:
|
|
|
|
if contentType.startswith(xmlHeader):
|
|
|
|
# Return an unmarshalled version of the XML content, for
|
|
|
|
# easy use in Python.
|
2011-04-15 04:26:20 -05:00
|
|
|
try:
|
|
|
|
return XmlUnmarshaller().parse(self.body)
|
|
|
|
except xml.sax.SAXParseException, se:
|
|
|
|
raise ResourceError('Invalid XML response (%s)'%str(se))
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2012-01-09 10:00:47 -06:00
|
|
|
urlRex = re.compile(r'http[s]?://([^:/]+)(:[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 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 '/'
|
2012-01-09 10:00:47 -06:00
|
|
|
else: raise Exception('Wrong URL: %s' % str(url))
|
2010-12-17 07:46:55 -06: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 = {'Host': self.host}
|
2010-10-14 07:43:56 -05:00
|
|
|
|
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.'''
|
2013-07-11 16:40:01 -05:00
|
|
|
conn = httplib.HTTPConnection(self.host, self.port)
|
2010-12-17 07:46:55 -06:00
|
|
|
try:
|
2013-07-11 16:40:01 -05:00
|
|
|
conn.connect()
|
2010-12-17 07:46:55 -06:00
|
|
|
except socket.gaierror, sge:
|
|
|
|
raise ResourceError('Check your Internet connection (%s)'% str(sge))
|
|
|
|
except socket.error, se:
|
2013-07-11 16:40:01 -05:00
|
|
|
raise ResourceError('Connection error (%s)' % str(se))
|
2010-10-22 09:06:27 -05:00
|
|
|
# Tell what kind of HTTP request it will be.
|
2013-07-11 16:40:01 -05:00
|
|
|
conn.putrequest(method, uri, skip_host=True)
|
2010-10-14 07:43:56 -05:00
|
|
|
# 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()
|
2013-07-11 16:40:01 -05:00
|
|
|
response = conn.getresponse()
|
2010-10-22 09:06:27 -05:00
|
|
|
if self.measure: endTime = time.time()
|
2013-07-11 16:40:01 -05:00
|
|
|
body = response.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
|
2013-07-11 16:40:01 -05:00
|
|
|
return HttpResponse(response, 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)
|
2010-12-17 07:46:55 -06:00
|
|
|
rss = get
|
2010-11-08 04:40:41 -06:00
|
|
|
|
|
|
|
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
|
|
|
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)
|
|
|
|
|
2013-07-11 09:41:45 -05:00
|
|
|
def soap(self, data, uri=None, headers={}, namespace=None, soapAction=None):
|
2010-11-08 04:40:41 -06:00
|
|
|
'''Sends a SOAP message to this resource. p_namespace is the URL of the
|
2013-07-11 09:41:45 -05:00
|
|
|
server-specific namespace. If header value "SOAPAction" is different
|
|
|
|
from self.url, specify it in p_soapAction.'''
|
2010-11-08 04:40:41 -06:00
|
|
|
if not uri: uri = self.uri
|
|
|
|
# Prepare the data to send
|
|
|
|
data = SoapDataEncoder(data, namespace).encode()
|
2013-07-11 09:41:45 -05:00
|
|
|
headers['SOAPAction'] = soapAction or self.url
|
2010-11-08 04:40:41 -06:00
|
|
|
headers['Content-Type'] = 'text/xml'
|
|
|
|
res = self.post(data, uri, headers=headers, encode=None)
|
|
|
|
# Unwrap content from the SOAP envelope
|
2013-07-11 09:41:45 -05:00
|
|
|
if hasattr(res.data, 'Body'):
|
|
|
|
res.data = res.data.Body
|
2010-11-08 04:40:41 -06:00
|
|
|
return res
|
2010-10-14 07:43:56 -05:00
|
|
|
# ------------------------------------------------------------------------------
|