--- a/etwist/server.py Thu Mar 01 17:22:56 2012 +0100
+++ b/etwist/server.py Mon Feb 27 17:02:31 2012 +0100
@@ -69,166 +69,6 @@
return baseurl
-class ForbiddenDirectoryLister(resource.Resource):
- def render(self, request):
- return HTTPResponse(twisted_request=request,
- code=http.FORBIDDEN,
- stream='Access forbidden')
-
-
-class NoListingFile(static.File):
- def __init__(self, config, path=None):
- if path is None:
- path = config.static_directory
- static.File.__init__(self, path)
- self.config = config
-
- def set_expires(self, request):
- if not self.config.debugmode:
- # XXX: Don't provide additional resource information to error responses
- #
- # the HTTP RFC recommands not going further than 1 year ahead
- expires = date.today() + timedelta(days=6*30)
- request.setHeader('Expires', generateDateTime(mktime(expires.timetuple())))
-
- def directoryListing(self):
- return ForbiddenDirectoryLister()
-
-
-class DataLookupDirectory(NoListingFile):
- def __init__(self, config, path):
- self.md5_version = config.instance_md5_version()
- NoListingFile.__init__(self, config, path)
- self.here = path
- self._defineChildResources()
- if self.config.debugmode:
- self.data_modconcat_basepath = '/data/??'
- else:
- self.data_modconcat_basepath = '/data/%s/??' % self.md5_version
-
- def _defineChildResources(self):
- self.putChild(self.md5_version, self)
-
- def getChild(self, path, request):
- if not path:
- uri = request.uri
- if uri.startswith('/https/'):
- uri = uri[6:]
- if uri.startswith(self.data_modconcat_basepath):
- resource_relpath = uri[len(self.data_modconcat_basepath):]
- if resource_relpath:
- paths = resource_relpath.split(',')
- try:
- self.set_expires(request)
- return ConcatFiles(self.config, paths)
- except ConcatFileNotFoundError:
- return self.childNotFound
- return self.directoryListing()
- childpath = join(self.here, path)
- dirpath, rid = self.config.locate_resource(childpath)
- if dirpath is None:
- # resource not found
- return self.childNotFound
- filepath = os.path.join(dirpath, rid)
- if os.path.isdir(filepath):
- resource = DataLookupDirectory(self.config, childpath)
- # cache resource for this segment path to avoid recomputing
- # directory lookup
- self.putChild(path, resource)
- return resource
- else:
- self.set_expires(request)
- return NoListingFile(self.config, filepath)
-
-
-class FCKEditorResource(NoListingFile):
-
- def getChild(self, path, request):
- pre_path = request.path.split('/')[1:]
- if pre_path[0] == 'https':
- pre_path.pop(0)
- uiprops = self.config.https_uiprops
- else:
- uiprops = self.config.uiprops
- return static.File(osp.join(uiprops['FCKEDITOR_PATH'], path))
-
-
-class LongTimeExpiringFile(DataLookupDirectory):
- """overrides static.File and sets a far future ``Expires`` date
- on the resouce.
-
- versions handling is done by serving static files by different
- URLs for each version. For instance::
-
- http://localhost:8080/data-2.48.2/cubicweb.css
- http://localhost:8080/data-2.49.0/cubicweb.css
- etc.
-
- """
- def _defineChildResources(self):
- pass
-
-
-class ConcatFileNotFoundError(CubicWebException):
- pass
-
-
-class ConcatFiles(LongTimeExpiringFile):
- def __init__(self, config, paths):
- _, ext = osp.splitext(paths[0])
- self._resources = {}
- # create a unique / predictable filename. We don't consider cubes
- # version since uicache is cleared at server startup, and file's dates
- # are checked in debug mode
- fname = 'cache_concat_' + md5(';'.join(paths)).hexdigest() + ext
- filepath = osp.join(config.appdatahome, 'uicache', fname)
- LongTimeExpiringFile.__init__(self, config, filepath)
- self._concat_cached_filepath(filepath, paths)
-
- def _resource(self, path):
- try:
- return self._resources[path]
- except KeyError:
- self._resources[path] = self.config.locate_resource(path)
- return self._resources[path]
-
- def _concat_cached_filepath(self, filepath, paths):
- if not self._up_to_date(filepath, paths):
- with open(filepath, 'wb') as f:
- for path in paths:
- dirpath, rid = self._resource(path)
- if rid is None:
- # In production mode log an error, do not return a 404
- # XXX the erroneous content is cached anyway
- LOGGER.error('concatenated data url error: %r file '
- 'does not exist', path)
- if self.config.debugmode:
- raise ConcatFileNotFoundError(path)
- else:
- for line in open(osp.join(dirpath, rid)):
- f.write(line)
- f.write('\n')
-
- def _up_to_date(self, filepath, paths):
- """
- The concat-file is considered up-to-date if it exists.
- In debug mode, an additional check is performed to make sure that
- concat-file is more recent than all concatenated files
- """
- if not osp.isfile(filepath):
- return False
- if self.config.debugmode:
- concat_lastmod = os.stat(filepath).st_mtime
- for path in paths:
- dirpath, rid = self._resource(path)
- if rid is None:
- raise ConcatFileNotFoundError(path)
- path = osp.join(dirpath, rid)
- if os.stat(path).st_mtime > concat_lastmod:
- return False
- return True
-
-
class CubicWebRootResource(resource.Resource):
def __init__(self, config, vreg=None):
resource.Resource.__init__(self)
@@ -240,9 +80,6 @@
self.https_url = config['https-url']
global MAX_POST_LENGTH
MAX_POST_LENGTH = config['max-post-length']
- self.putChild('static', NoListingFile(config))
- self.putChild('fckeditor', FCKEditorResource(self.config, ''))
- self.putChild('data', DataLookupDirectory(self.config, ''))
def init_publisher(self):
config = self.config