author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 09 May 2012 17:15:44 +0200 | |
changeset 8394 | b8b23af60019 |
parent 8389 | 94f26e3b09bf |
child 8427 | 63c4c65e4db1 |
permissions | -rw-r--r-- |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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 |
""" |
8328
c53bbeea7870
Add missing with_statement import for python2.5 compat
Julien Cristau <julien.cristau@logilab.fr>
parents:
8325
diff
changeset
|
25 |
from __future__ import with_statement |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
26 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
27 |
import os |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
28 |
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
|
29 |
import hashlib |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
30 |
import mimetypes |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
31 |
from time import mktime |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
32 |
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
|
33 |
from logging import getLogger |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
34 |
|
8325
7f2337d7937f
[static-file] return Unauthorized on blocked directory listing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8323
diff
changeset
|
35 |
from cubicweb import Unauthorized |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
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
|
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
43 |
class StaticFileController(Controller): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
44 |
"""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
|
45 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
46 |
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
|
47 |
__abstract__ = True |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
48 |
directory_listing_allowed = False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
49 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
50 |
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
|
51 |
"""max cache TTL""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
52 |
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
|
53 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
54 |
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
|
55 |
"""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
|
56 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
57 |
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
|
58 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
59 |
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
|
60 |
if osp.isdir(path): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
61 |
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
|
62 |
return u'' |
8325
7f2337d7937f
[static-file] return Unauthorized on blocked directory listing
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8323
diff
changeset
|
63 |
raise Unauthorized(path) |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
64 |
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
|
65 |
raise NotFound() |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
66 |
if not debugmode: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
67 |
# 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
|
68 |
# |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
69 |
# 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
|
70 |
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
|
71 |
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
|
72 |
|
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 |
# 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
|
74 |
# 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
|
75 |
# |
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 |
# 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
|
77 |
# 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
|
78 |
# |
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 |
# 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
|
80 |
self._cw.set_header('last-modified', generateDateTime(os.stat(path).st_mtime)) |
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
|
81 |
self._cw.validate_cache() |
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) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
84 |
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
|
85 |
with open(path, 'rb') as resource: |
7f356ba4181c
[web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8328
diff
changeset
|
86 |
return resource.read() |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
87 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
88 |
@property |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
89 |
def relpath(self): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
90 |
"""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
|
91 |
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
|
92 |
if path is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
93 |
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
|
94 |
return path |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
95 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
96 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
97 |
class ConcatFilesHandler(object): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
98 |
"""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
|
99 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
100 |
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
|
101 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
102 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
103 |
def __init__(self, config): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
104 |
self._resources = {} |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
105 |
self.config = config |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
106 |
self.logger = getLogger('cubicweb.web') |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
107 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
108 |
def _resource(self, path): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
109 |
"""get the resouce""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
110 |
try: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
111 |
return self._resources[path] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
112 |
except KeyError: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
113 |
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
|
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
116 |
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
|
117 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
122 |
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
|
123 |
return False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
124 |
if self.config.debugmode: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
125 |
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
|
126 |
for path in paths: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
127 |
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
|
128 |
if rid is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
129 |
raise NotFound(path) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
130 |
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
|
131 |
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
|
132 |
return False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
133 |
return True |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
134 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
135 |
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
|
136 |
"""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
|
137 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
138 |
_, 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
|
139 |
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
|
140 |
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
|
141 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
142 |
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
|
143 |
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
|
144 |
if not self._up_to_date(filepath, paths): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
145 |
with open(filepath, 'wb') as f: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
146 |
for path in paths: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
147 |
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
|
148 |
if rid is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
149 |
# In production mode log an error, do not return a 404 |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
150 |
# XXX the erroneous content is cached anyway |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
151 |
self.logger.error('concatenated data url error: %r file ' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
152 |
'does not exist', path) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
153 |
if self.config.debugmode: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
154 |
raise NotFound(path) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
155 |
else: |
8361
7f356ba4181c
[web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8328
diff
changeset
|
156 |
with open(osp.join(dirpath, rid), 'rb') as source: |
7f356ba4181c
[web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8328
diff
changeset
|
157 |
for line in source: |
7f356ba4181c
[web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8328
diff
changeset
|
158 |
f.write(line) |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
159 |
f.write('\n') |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
160 |
return filepath |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
161 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
162 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
163 |
class DataController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
164 |
"""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
|
165 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
166 |
Handle modeconcat like url. |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
167 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
168 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
169 |
__regid__ = 'data' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
170 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
179 |
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
|
180 |
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
|
181 |
# 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
|
182 |
relpath = self.relpath |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
# skip leading '/data/' and url params |
94f26e3b09bf
fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents:
8369
diff
changeset
|
188 |
relpath = relpath[len(self.base_datapath):].split('?', 1)[0] |
94f26e3b09bf
fix static file serving when url has parameters (like cache busters do)
Florent Cayré <florent.cayre@logilab.fr>
parents:
8369
diff
changeset
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
195 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
196 |
class FCKEditorController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
197 |
"""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
|
198 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
199 |
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
|
200 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
201 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
202 |
__regid__ = 'fckeditor' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
203 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
204 |
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
|
205 |
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
|
206 |
if self._cw.https: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
207 |
uiprops = config.https_uiprops |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
208 |
else: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
209 |
uiprops = config.uiprops |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
216 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
217 |
class StaticDirectoryController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
218 |
"""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
|
219 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
220 |
__regid__ = 'static' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
221 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
222 |
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
|
223 |
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
|
224 |
relpath = self.relpath |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
225 |
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
|
226 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
227 |
STATIC_CONTROLLERS = [DataController, FCKEditorController, |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
228 |
StaticDirectoryController] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
229 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
230 |
class StaticControlerRewriter(URLRewriter): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
231 |
"""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
|
232 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
233 |
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
|
234 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
235 |
__regid__ = 'static' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
236 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
237 |
priority = 10 |
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 |
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
|
240 |
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
|
241 |
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
|
242 |
break |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
243 |
else: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
244 |
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
|
245 |
raise KeyError(uri) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
246 |
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
|
247 |
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
|
248 |
return ctrl.__regid__, None |