author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 29 Jan 2014 16:03:42 +0100 | |
changeset 9501 | 2904e2ba81a0 |
parent 9497 | 7310738fafe5 |
child 10398 | bd00b58b00d9 |
permissions | -rw-r--r-- |
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 |
9497
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
30 |
import tempfile |
8298
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 |
|
9234
ce504d23edf7
[staticcontrollers] Raise Forbidden, not Unauthorized
Julien Cristau <julien.cristau@logilab.fr>
parents:
9233
diff
changeset
|
35 |
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
|
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'' |
9234
ce504d23edf7
[staticcontrollers] Raise Forbidden, not Unauthorized
Julien Cristau <julien.cristau@logilab.fr>
parents:
9233
diff
changeset
|
63 |
raise Forbidden(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)) |
9229
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9159
diff
changeset
|
81 |
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
|
82 |
return '' |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
83 |
# 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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
with open(path, 'rb') as resource: |
7f356ba4181c
[web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8328
diff
changeset
|
89 |
return resource.read() |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
90 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
91 |
@property |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
92 |
def relpath(self): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
93 |
"""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
|
94 |
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
|
95 |
if path is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
96 |
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
|
97 |
return path |
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
100 |
class ConcatFilesHandler(object): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
101 |
"""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
|
102 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
103 |
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
|
104 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
105 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
106 |
def __init__(self, config): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
107 |
self._resources = {} |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
108 |
self.config = config |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
109 |
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
|
110 |
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
|
111 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
112 |
def _resource(self, path): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
113 |
"""get the resouce""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
114 |
try: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
115 |
return self._resources[path] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
116 |
except KeyError: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
117 |
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
|
118 |
return self._resources[path] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
119 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
120 |
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
|
121 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
126 |
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
|
127 |
return False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
128 |
if self.config.debugmode: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
129 |
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
|
130 |
for path in paths: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
131 |
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
|
132 |
if rid is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
133 |
raise NotFound(path) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
134 |
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
|
135 |
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
|
136 |
return False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
137 |
return True |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
138 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
139 |
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
|
140 |
"""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
|
141 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
142 |
_, 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
|
143 |
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
|
144 |
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
|
145 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
146 |
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
|
147 |
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
|
148 |
if not self._up_to_date(filepath, paths): |
9497
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
149 |
with self.lock: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
150 |
if self._up_to_date(filepath, paths): |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
151 |
# first check could have raced with some other thread |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
152 |
# updating the file |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
153 |
return filepath |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
154 |
fd, tmpfile = tempfile.mkstemp(dir=os.path.dirname(filepath)) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
155 |
try: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
156 |
f = os.fdopen(fd, 'wb') |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
157 |
for path in paths: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
158 |
dirpath, rid = self._resource(path) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
159 |
if rid is None: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
160 |
# In production mode log an error, do not return a 404 |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
161 |
# XXX the erroneous content is cached anyway |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
162 |
self.logger.error('concatenated data url error: %r file ' |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
163 |
'does not exist', path) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
164 |
if self.config.debugmode: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
165 |
raise NotFound(path) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
166 |
else: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
167 |
with open(osp.join(dirpath, rid), 'rb') as source: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
168 |
for line in source: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
169 |
f.write(line) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
170 |
f.write('\n') |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
171 |
f.close() |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
172 |
except: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
173 |
os.remove(tmpfile) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
174 |
raise |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
175 |
else: |
9159
7a6ea3468765
[web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents:
8694
diff
changeset
|
176 |
os.rename(tmpfile, filepath) |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
177 |
return filepath |
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
180 |
class DataController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
181 |
"""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
|
182 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
183 |
Handle modeconcat like url. |
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
186 |
__regid__ = 'data' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
187 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
196 |
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
|
197 |
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
|
198 |
# 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
|
199 |
relpath = self.relpath |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
# 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
|
205 |
if relpath.startswith(self.base_datapath): |
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
206 |
prefix = self.base_datapath |
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
207 |
else: |
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
208 |
prefix = 'data/' |
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
209 |
relpath = relpath[len(prefix):] |
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
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
|
216 |
|
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 |
class FCKEditorController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
219 |
"""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
|
220 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
221 |
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
|
222 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
223 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
224 |
__regid__ = 'fckeditor' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
225 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
226 |
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
|
227 |
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
|
228 |
if self._cw.https: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
229 |
uiprops = config.https_uiprops |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
230 |
else: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
231 |
uiprops = config.uiprops |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
|
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 |
class StaticDirectoryController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
240 |
"""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
|
241 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
242 |
__regid__ = 'static' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
243 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
244 |
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
|
245 |
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
|
246 |
relpath = self.relpath |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
247 |
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
|
248 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
249 |
STATIC_CONTROLLERS = [DataController, FCKEditorController, |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
250 |
StaticDirectoryController] |
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 |
class StaticControlerRewriter(URLRewriter): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
253 |
"""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
|
254 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
255 |
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
|
256 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
257 |
__regid__ = 'static' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
258 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
259 |
priority = 10 |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
260 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
break |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
265 |
else: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
266 |
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
|
267 |
raise KeyError(uri) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
268 |
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
|
269 |
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
|
270 |
return ctrl.__regid__, None |