web/views/staticcontrollers.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 12 Dec 2013 14:25:24 +0100
changeset 9360 eda5071e30a1
parent 9234 ce504d23edf7
child 9497 7310738fafe5
permissions -rw-r--r--
[migration] fix handling of default value for boolean attributes We can't assert that the old value is 'True' or 'False', because False used to be stored as an empty string in pre-3.18 versions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9159
diff changeset
     1
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
#
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
#
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
# any later version.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
#
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
# details.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
#
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""Set of static resources controllers for :
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
- /data/...
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
- /static/...
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
- /fckeditor/...
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
"""
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
import os
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
import os.path as osp
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
import hashlib
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
import mimetypes
9159
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
    29
import threading
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
from time import mktime
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
from datetime import datetime, timedelta
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
from logging import getLogger
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
9234
ce504d23edf7 [staticcontrollers] Raise Forbidden, not Unauthorized
Julien Cristau <julien.cristau@logilab.fr>
parents: 9233
diff changeset
    34
from cubicweb import Forbidden
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
from cubicweb.web import NotFound
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
from cubicweb.web.http_headers import generateDateTime
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
from cubicweb.web.controller import Controller
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
from cubicweb.web.views.urlrewrite import URLRewriter
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
class StaticFileController(Controller):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    """an abtract class to serve static file
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    Make sure to add your subclass to the STATIC_CONTROLLERS list"""
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    __abstract__ = True
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    directory_listing_allowed = False
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    def max_age(self, path):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        """max cache TTL"""
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        return 60*60*24*7
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    def static_file(self, path):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        """Return full content of a static file.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        XXX iterable content would be better
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        debugmode = self._cw.vreg.config.debugmode
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        if osp.isdir(path):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
            if self.directory_listing_allowed:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
                return u''
9234
ce504d23edf7 [staticcontrollers] Raise Forbidden, not Unauthorized
Julien Cristau <julien.cristau@logilab.fr>
parents: 9233
diff changeset
    62
            raise Forbidden(path)
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        if not osp.isfile(path):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
            raise NotFound()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        if not debugmode:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
            # XXX: Don't provide additional resource information to error responses
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
            #
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
            # the HTTP RFC recommands not going further than 1 year ahead
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
            expires = datetime.now() + timedelta(days=6*30)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
            self._cw.set_header('Expires', generateDateTime(mktime(expires.timetuple())))
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    71
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    72
        # XXX system call to os.stats could be cached once and for all in
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    73
        # production mode (where static files are not expected to change)
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    74
        #
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    75
        # Note that: we do a osp.isdir + osp.isfile before and a potential
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    76
        # os.read after. Improving this specific call will not help
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    77
        #
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    78
        # Real production environment should use dedicated static file serving.
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8298
diff changeset
    79
        self._cw.set_header('last-modified', generateDateTime(os.stat(path).st_mtime))
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9159
diff changeset
    80
        if self._cw.is_client_cache_valid():
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9159
diff changeset
    81
            return ''
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        # XXX elif uri.startswith('/https/'): uri = uri[6:]
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        mimetype, encoding = mimetypes.guess_type(path)
8427
63c4c65e4db1 [web/views/staticcontroller] safety belt for unguessed mimetypes (closes #2380304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8389
diff changeset
    84
        if mimetype is None:
63c4c65e4db1 [web/views/staticcontroller] safety belt for unguessed mimetypes (closes #2380304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8389
diff changeset
    85
            mimetype = 'application/octet-stream'
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
        self._cw.set_content_type(mimetype, osp.basename(path), encoding)
8361
7f356ba4181c [web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8328
diff changeset
    87
        with open(path, 'rb') as resource:
7f356ba4181c [web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8328
diff changeset
    88
            return resource.read()
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    @property
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    def relpath(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        """path of a requested file relative to the controller"""
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        path = self._cw.form.get('static_relative_path')
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        if path is None:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
            path = self._cw.relative_path(includeparams=True)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        return path
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
class ConcatFilesHandler(object):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    """Emulating the behavior of modconcat
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    this serve multiple file as a single one.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    def __init__(self, config):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        self._resources = {}
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        self.config = config
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        self.logger = getLogger('cubicweb.web')
9159
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   109
        self.lock = threading.Lock()
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    def _resource(self, path):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        """get the resouce"""
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        try:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
            return self._resources[path]
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        except KeyError:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
            self._resources[path] = self.config.locate_resource(path)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
            return self._resources[path]
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
    def _up_to_date(self, filepath, paths):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        The concat-file is considered up-to-date if it exists.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        In debug mode, an additional check is performed to make sure that
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        concat-file is more recent than all concatenated files
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        if not osp.isfile(filepath):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            return False
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
        if self.config.debugmode:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
            concat_lastmod = os.stat(filepath).st_mtime
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
            for path in paths:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
                dirpath, rid = self._resource(path)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
                if rid is None:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
                    raise NotFound(path)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
                path = osp.join(dirpath, rid)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
                if os.stat(path).st_mtime > concat_lastmod:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
                    return False
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        return True
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
    def build_filepath(self, paths):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        """return the filepath that will be used to cache concatenation of `paths`
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        _, ext = osp.splitext(paths[0])
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        fname = 'cache_concat_' + hashlib.md5(';'.join(paths)).hexdigest() + ext
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        return osp.join(self.config.appdatahome, 'uicache', fname)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    def concat_cached_filepath(self, paths):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        filepath = self.build_filepath(paths)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        if not self._up_to_date(filepath, paths):
9159
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   148
            tmpfile = filepath + '.tmp'
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   149
            try:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   150
                with self.lock:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   151
                    if self._up_to_date(filepath, paths):
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   152
                        # first check could have raced with some other thread
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   153
                        # updating the file
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   154
                        return filepath
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   155
                    with open(tmpfile, 'wb') as f:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   156
                        for path in paths:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   157
                            dirpath, rid = self._resource(path)
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   158
                            if rid is None:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   159
                                # In production mode log an error, do not return a 404
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   160
                                # XXX the erroneous content is cached anyway
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   161
                                self.logger.error('concatenated data url error: %r file '
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   162
                                                  'does not exist', path)
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   163
                                if self.config.debugmode:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   164
                                    raise NotFound(path)
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   165
                            else:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   166
                                with open(osp.join(dirpath, rid), 'rb') as source:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   167
                                    for line in source:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   168
                                        f.write(line)
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   169
                                f.write('\n')
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   170
                    os.rename(tmpfile, filepath)
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   171
            except:
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   172
                os.remove(tmpfile)
7a6ea3468765 [web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents: 8694
diff changeset
   173
                raise
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
        return filepath
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
class DataController(StaticFileController):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
    """Controller in charge of serving static file in /data/
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
    Handle modeconcat like url.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
    """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
    __regid__ = 'data'
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
    def __init__(self, *args, **kwargs):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
        super(DataController, self).__init__(*args, **kwargs)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
        config = self._cw.vreg.config
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
        md5_version = config.instance_md5_version()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
        self.base_datapath = config.data_relpath()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        self.data_modconcat_basepath = '%s??' % self.base_datapath
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
        self.concat_files_registry = ConcatFilesHandler(config)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
    def publish(self, rset=None):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
        config = self._cw.vreg.config
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        # includeparams=True for modconcat-like urls
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        relpath = self.relpath
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
        if relpath.startswith(self.data_modconcat_basepath):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
            paths = relpath[len(self.data_modconcat_basepath):].split(',')
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
            filepath = self.concat_files_registry.concat_cached_filepath(paths)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
        else:
8389
94f26e3b09bf fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8369
diff changeset
   201
            # skip leading '/data/' and url params
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 9229
diff changeset
   202
            if relpath.startswith(self.base_datapath):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 9229
diff changeset
   203
                prefix = self.base_datapath
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 9229
diff changeset
   204
            else:
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 9229
diff changeset
   205
                prefix = 'data/'
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 9229
diff changeset
   206
            relpath = relpath[len(prefix):]
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 9229
diff changeset
   207
            relpath = relpath.split('?', 1)[0]
8389
94f26e3b09bf fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8369
diff changeset
   208
            dirpath, rid = config.locate_resource(relpath)
94f26e3b09bf fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8369
diff changeset
   209
            if dirpath is None:
94f26e3b09bf fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8369
diff changeset
   210
                raise NotFound()
94f26e3b09bf fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8369
diff changeset
   211
            filepath = osp.join(dirpath, rid)
94f26e3b09bf fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8369
diff changeset
   212
        return self.static_file(filepath)
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
class FCKEditorController(StaticFileController):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
    """Controller in charge of serving FCKEditor related file
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
    The motivational for a dedicated controller have been lost.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
    """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
    __regid__ = 'fckeditor'
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    def publish(self, rset=None):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        config = self._cw.vreg.config
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
        if self._cw.https:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
            uiprops = config.https_uiprops
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
        else:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
            uiprops = config.uiprops
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
        relpath = self.relpath
8369
e538ad6f9b99 [static web] fix fckeditor static files delivery: remove leading subpath and url parameters from computed file path
Florent Cayré <florent.cayre@logilab.fr>
parents: 8361
diff changeset
   230
        if relpath.startswith('fckeditor/'):
e538ad6f9b99 [static web] fix fckeditor static files delivery: remove leading subpath and url parameters from computed file path
Florent Cayré <florent.cayre@logilab.fr>
parents: 8361
diff changeset
   231
            relpath = relpath[len('fckeditor/'):]
e538ad6f9b99 [static web] fix fckeditor static files delivery: remove leading subpath and url parameters from computed file path
Florent Cayré <florent.cayre@logilab.fr>
parents: 8361
diff changeset
   232
        relpath = relpath.split('?', 1)[0]
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
        return self.static_file(osp.join(uiprops['FCKEDITOR_PATH'], relpath))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
class StaticDirectoryController(StaticFileController):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
    """Controller in charge of serving static file in /static/
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
    """
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
    __regid__ = 'static'
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
    def publish(self, rset=None):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
        staticdir = self._cw.vreg.config.static_directory
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
        relpath = self.relpath
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
        return self.static_file(osp.join(staticdir, relpath))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
STATIC_CONTROLLERS = [DataController, FCKEditorController,
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
                      StaticDirectoryController]
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
class StaticControlerRewriter(URLRewriter):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
    """a quick and dirty rewritter in charge of server static file.
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
    This is a work around the flatness of url handling in cubicweb."""
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
    __regid__ = 'static'
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    priority = 10
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
    def rewrite(self, req, uri):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
        for ctrl in STATIC_CONTROLLERS:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
            if uri.startswith('/%s/' % ctrl.__regid__):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
                break
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
        else:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
            self.debug("not a static file uri: %s", uri)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
            raise KeyError(uri)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
        relpath = self._cw.relative_path(includeparams=False)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
        self._cw.form['static_relative_path'] = self._cw.relative_path(includeparams=True)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
        return ctrl.__regid__, None