author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 21 Oct 2015 16:58:30 +0200 | |
changeset 10865 | 2537df9fdd27 |
parent 10852 | e35d23686d1f |
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 |
10851
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
36 |
from cubicweb.web import NotFound, Redirect |
8298
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 |
# |
10852
e35d23686d1f
[web] set proper Cache-Control header for static files
Julien Cristau <julien.cristau@logilab.fr>
parents:
10851
diff
changeset
|
69 |
# the HTTP RFC recommends not going further than 1 year ahead |
e35d23686d1f
[web] set proper Cache-Control header for static files
Julien Cristau <julien.cristau@logilab.fr>
parents:
10851
diff
changeset
|
70 |
expires = datetime.now() + timedelta(seconds=self.max_age(path)) |
8298
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()))) |
10852
e35d23686d1f
[web] set proper Cache-Control header for static files
Julien Cristau <julien.cristau@logilab.fr>
parents:
10851
diff
changeset
|
72 |
self._cw.set_header('Cache-Control', 'max-age=%s' % self.max_age(path)) |
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
|
73 |
|
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 |
# 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
|
75 |
# 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
|
76 |
# |
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 |
# 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
|
78 |
# 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
|
79 |
# |
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 |
# 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
|
81 |
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
|
82 |
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
|
83 |
return '' |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
84 |
# 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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
with open(path, 'rb') as resource: |
7f356ba4181c
[web/views/staticcontrollers] restore windows compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8328
diff
changeset
|
90 |
return resource.read() |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
91 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
92 |
@property |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
93 |
def relpath(self): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
94 |
"""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
|
95 |
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
|
96 |
if path is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
97 |
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
|
98 |
return path |
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
101 |
class ConcatFilesHandler(object): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
102 |
"""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
|
103 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
104 |
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
|
105 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
106 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
107 |
def __init__(self, config): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
108 |
self._resources = {} |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
109 |
self.config = config |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
113 |
def _resource(self, path): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
114 |
"""get the resouce""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
115 |
try: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
116 |
return self._resources[path] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
117 |
except KeyError: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
118 |
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
|
119 |
return self._resources[path] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
120 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
121 |
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
|
122 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
123 |
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
|
124 |
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
|
125 |
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
|
126 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
127 |
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
|
128 |
return False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
129 |
if self.config.debugmode: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
130 |
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
|
131 |
for path in paths: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
132 |
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
|
133 |
if rid is None: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
134 |
raise NotFound(path) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
135 |
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
|
136 |
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
|
137 |
return False |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
138 |
return True |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
139 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
140 |
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
|
141 |
"""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
|
142 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
143 |
_, ext = osp.splitext(paths[0]) |
10671
e08102f45237
[py3k] bytes required for hashlib.{md5,sha1}()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10522
diff
changeset
|
144 |
fname = 'cache_concat_' + hashlib.md5((';'.join(paths)).encode('ascii')).hexdigest() + ext |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
145 |
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
|
146 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
with self.lock: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
151 |
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
|
152 |
# 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
|
153 |
# updating the file |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
154 |
return filepath |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
155 |
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
|
156 |
try: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
157 |
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
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
# 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
|
162 |
# 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
|
163 |
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
|
164 |
'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
|
165 |
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
|
166 |
raise NotFound(path) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
167 |
else: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
168 |
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
|
169 |
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
|
170 |
f.write(line) |
10694
7ece2df9cc5c
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10671
diff
changeset
|
171 |
f.write(b'\n') |
9497
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
172 |
f.close() |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
173 |
except: |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
174 |
os.remove(tmpfile) |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
175 |
raise |
7310738fafe5
[web/staticcontrollers] create a unique temporary file for concat handling
Julien Cristau <julien.cristau@logilab.fr>
parents:
9234
diff
changeset
|
176 |
else: |
9159
7a6ea3468765
[web/views/staticcontrollers] Make ConcatFilesHandler write to a tempfile
Julien Cristau <julien.cristau@logilab.fr>
parents:
8694
diff
changeset
|
177 |
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
|
178 |
return filepath |
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
181 |
class DataController(StaticFileController): |
10451
cffe653f93a9
[web/staticcontrollers] fix docstring spelling
Julien Cristau <julien.cristau@logilab.fr>
parents:
10398
diff
changeset
|
182 |
"""Controller in charge of serving static files in /data/ |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
183 |
|
10451
cffe653f93a9
[web/staticcontrollers] fix docstring spelling
Julien Cristau <julien.cristau@logilab.fr>
parents:
10398
diff
changeset
|
184 |
Handles mod_concat-like URLs. |
8298
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 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
187 |
__regid__ = 'data' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
188 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
189 |
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
|
190 |
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
|
191 |
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
|
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: |
10851
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
204 |
if not relpath.startswith(self.base_datapath): |
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
205 |
# /data/foo, redirect to /data/{hash}/foo |
9233
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
206 |
prefix = 'data/' |
10851
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
207 |
relpath = relpath[len(prefix):] |
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
208 |
raise Redirect(self._cw.data_url(relpath), 302) |
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
209 |
# skip leading '/data/{hash}/' and url params |
70b21cf8b45c
[web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents:
10694
diff
changeset
|
210 |
prefix = self.base_datapath |
9233
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
211 |
relpath = relpath[len(prefix):] |
7d3316bfa4ff
[web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents:
9229
diff
changeset
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
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
|
216 |
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
|
217 |
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
|
218 |
|
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 |
class FCKEditorController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
221 |
"""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
|
222 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
223 |
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
|
224 |
""" |
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 |
__regid__ = 'fckeditor' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
227 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
228 |
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
|
229 |
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
|
230 |
if self._cw.https: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
231 |
uiprops = config.https_uiprops |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
232 |
else: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
233 |
uiprops = config.uiprops |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
240 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
241 |
class StaticDirectoryController(StaticFileController): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
242 |
"""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
|
243 |
""" |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
244 |
__regid__ = 'static' |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
245 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
246 |
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
|
247 |
staticdir = self._cw.vreg.config.static_directory |
10398
bd00b58b00d9
[web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
9497
diff
changeset
|
248 |
relpath = self.relpath[len(self.__regid__) + 1:] |
8298
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
249 |
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
|
250 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
251 |
STATIC_CONTROLLERS = [DataController, FCKEditorController, |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
252 |
StaticDirectoryController] |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
253 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
254 |
class StaticControlerRewriter(URLRewriter): |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
255 |
"""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
|
256 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
257 |
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
|
258 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
259 |
__regid__ = 'static' |
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 |
priority = 10 |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
262 |
|
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
break |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
267 |
else: |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
268 |
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
|
269 |
raise KeyError(uri) |
2a4bc6f75e9c
[twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
270 |
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
|
271 |
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
|
272 |
return ctrl.__regid__, None |