author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 06 Jul 2016 20:14:55 +0200 | |
branch | 3.22 |
changeset 11440 | 8388b37720e1 |
parent 11339 | 67373614e125 |
permissions | -rw-r--r-- |
10215
96085e379066
[views,js] remove user_callback feature
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10094
diff
changeset
|
1 |
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5423
e15abfdcce38
backport default into stable: stable is now cw 3.8
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
18 |
"""abstract class for http request""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
import time |
|
23 |
import random |
|
24 |
import base64 |
|
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7872
diff
changeset
|
25 |
from hashlib import sha1 # pylint: disable=E0611 |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
26 |
from calendar import timegm |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
27 |
from datetime import date, datetime |
6078
c08e74b0609e
fix missing import in web.request
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6064
diff
changeset
|
28 |
from warnings import warn |
10618 | 29 |
from io import BytesIO |
0 | 30 |
|
10733
9f66ac5eec1e
[py3k] http headers should be ascii-only unicode objects
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10717
diff
changeset
|
31 |
from six import PY2, binary_type, text_type, string_types |
10604
d4bf85db41f2
[py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10603
diff
changeset
|
32 |
from six.moves import http_client |
10603
65ad6980976e
[py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10355
diff
changeset
|
33 |
from six.moves.urllib.parse import urlsplit, quote as urlquote |
10605
61521bee2b68
[py3k] import SimpleCookie using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10604
diff
changeset
|
34 |
from six.moves.http_cookies import SimpleCookie |
10603
65ad6980976e
[py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10355
diff
changeset
|
35 |
|
0 | 36 |
from rql.utils import rqlvar_maker |
37 |
||
38 |
from logilab.common.decorators import cached |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2559
diff
changeset
|
39 |
from logilab.common.deprecation import deprecated |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2258
diff
changeset
|
40 |
from logilab.mtconverter import xml_escape |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
41 |
|
10331
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
42 |
from cubicweb import AuthenticationError |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
43 |
from cubicweb.req import RequestSessionBase |
6063
17a53f6dea42
[request] js is defined in uilib, not utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6062
diff
changeset
|
44 |
from cubicweb.uilib import remove_html_tags, js |
10932
cb217b2b3463
[views] remove breadcrumbs usage to retrieve last visited page
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
10903
diff
changeset
|
45 |
from cubicweb.utils import HTMLHead, make_uid |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
46 |
from cubicweb.view import TRANSITIONAL_DOCTYPE_NOEXT |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
47 |
from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit, |
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5890
diff
changeset
|
48 |
RequestError, StatusResponse) |
10305
937deb71a681
[web] kill GMTOFFSET (closes #2154655)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10304
diff
changeset
|
49 |
from cubicweb.web.httpcache import get_validators |
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
50 |
from cubicweb.web.http_headers import Headers, Cookie, parseDateTime |
0 | 51 |
|
662
6f867ab70e3d
move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents:
610
diff
changeset
|
52 |
_MARKER = object() |
6f867ab70e3d
move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents:
610
diff
changeset
|
53 |
|
6564
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
54 |
def build_cb_uid(seed): |
10671
e08102f45237
[py3k] bytes required for hashlib.{md5,sha1}()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
55 |
sha = sha1(('%s%s%s' % (time.time(), seed, random.random())).encode('ascii')) |
6564
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
56 |
return 'cb_%s' % (sha.hexdigest()) |
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
57 |
|
0 | 58 |
|
59 |
def list_form_param(form, param, pop=False): |
|
60 |
"""get param from form parameters and return its value as a list, |
|
61 |
skipping internal markers if any |
|
62 |
||
63 |
* if the parameter isn't defined, return an empty list |
|
64 |
* if the parameter is a single (unicode) value, return a list |
|
65 |
containing that value |
|
66 |
* if the parameter is already a list or tuple, just skip internal |
|
67 |
markers |
|
68 |
||
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8177
diff
changeset
|
69 |
if pop is True, the parameter is removed from the form dictionary |
0 | 70 |
""" |
71 |
if pop: |
|
72 |
try: |
|
73 |
value = form.pop(param) |
|
74 |
except KeyError: |
|
75 |
return [] |
|
76 |
else: |
|
77 |
value = form.get(param, ()) |
|
78 |
if value is None: |
|
79 |
value = () |
|
80 |
elif not isinstance(value, (list, tuple)): |
|
81 |
value = [value] |
|
82 |
return [v for v in value if v != INTERNAL_FIELD_VALUE] |
|
83 |
||
84 |
||
9884
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
85 |
class Counter(object): |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
86 |
"""A picklable counter object, usable for e.g. page tab index count""" |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
87 |
__slots__ = ('value',) |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
88 |
|
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
89 |
def __init__(self, initialvalue=0): |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
90 |
self.value = initialvalue |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
91 |
|
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
92 |
def __call__(self): |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
93 |
value = self.value |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
94 |
self.value += 1 |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
95 |
return value |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
96 |
|
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
97 |
def __getstate__(self): |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
98 |
return {'value': self.value} |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
99 |
|
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
100 |
def __setstate__(self, state): |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
101 |
self.value = state['value'] |
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
102 |
|
0 | 103 |
|
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
104 |
class _CubicWebRequestBase(RequestSessionBase): |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
105 |
"""abstract HTTP request, should be extended according to the HTTP backend |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
106 |
Immutable attributes that describe the received query and generic configuration |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
107 |
""" |
8128
0a927fe4541b
[controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8082
diff
changeset
|
108 |
ajax_request = False # to be set to True by ajax controllers |
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
109 |
|
9549
a1f94c2d2a39
[web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents:
9469
diff
changeset
|
110 |
def __init__(self, vreg, https=False, form=None, headers=None): |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
111 |
""" |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
112 |
:vreg: Vregistry, |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
113 |
:https: boolean, s this a https request |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
114 |
:form: Forms value |
9549
a1f94c2d2a39
[web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents:
9469
diff
changeset
|
115 |
:headers: dict, request header |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
116 |
""" |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
117 |
super(_CubicWebRequestBase, self).__init__(vreg) |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
118 |
#: (Boolean) Is this an https request. |
0 | 119 |
self.https = https |
8480
086cff6a306a
[book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8316
diff
changeset
|
120 |
#: User interface property (vary with https) (see :ref:`uiprops`) |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
121 |
self.uiprops = None |
8480
086cff6a306a
[book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8316
diff
changeset
|
122 |
#: url for serving datadir (vary with https) (see :ref:`resources`) |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
123 |
self.datadir_url = None |
8869
a46197b1696f
[webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8732
diff
changeset
|
124 |
if https and vreg.config.https_uiprops is not None: |
5467
57372dbfd114
[https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5444
diff
changeset
|
125 |
self.uiprops = vreg.config.https_uiprops |
8869
a46197b1696f
[webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8732
diff
changeset
|
126 |
else: |
a46197b1696f
[webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8732
diff
changeset
|
127 |
self.uiprops = vreg.config.uiprops |
a46197b1696f
[webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8732
diff
changeset
|
128 |
if https and vreg.config.https_datadir_url is not None: |
5467
57372dbfd114
[https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5444
diff
changeset
|
129 |
self.datadir_url = vreg.config.https_datadir_url |
57372dbfd114
[https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5444
diff
changeset
|
130 |
else: |
57372dbfd114
[https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5444
diff
changeset
|
131 |
self.datadir_url = vreg.config.datadir_url |
10090
0aebb1c0f849
[utils] Add a '_cwtracehtml' GET parameter to trace self._cw.w() calls (closes #4601327)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10080
diff
changeset
|
132 |
#: enable UStringIO's write tracing |
0aebb1c0f849
[utils] Add a '_cwtracehtml' GET parameter to trace self._cw.w() calls (closes #4601327)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10080
diff
changeset
|
133 |
self.tracehtml = False |
0aebb1c0f849
[utils] Add a '_cwtracehtml' GET parameter to trace self._cw.w() calls (closes #4601327)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10080
diff
changeset
|
134 |
if vreg.config.debugmode: |
0aebb1c0f849
[utils] Add a '_cwtracehtml' GET parameter to trace self._cw.w() calls (closes #4601327)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10080
diff
changeset
|
135 |
self.tracehtml = bool(form.pop('_cwtracehtml', False)) |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
136 |
#: raw html headers that can be added from any view |
10090
0aebb1c0f849
[utils] Add a '_cwtracehtml' GET parameter to trace self._cw.w() calls (closes #4601327)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10080
diff
changeset
|
137 |
self.html_headers = HTMLHead(self, tracewrites=self.tracehtml) |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
138 |
#: received headers |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
139 |
self._headers_in = Headers() |
9549
a1f94c2d2a39
[web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents:
9469
diff
changeset
|
140 |
if headers is not None: |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10618
diff
changeset
|
141 |
for k, v in headers.items(): |
9549
a1f94c2d2a39
[web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents:
9469
diff
changeset
|
142 |
self._headers_in.addRawHeader(k, v) |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
143 |
#: form parameters |
0 | 144 |
self.setup_params(form) |
8752
e19f4bba89cd
Add CubicWebRequestBase.content (closes #2742453)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8732
diff
changeset
|
145 |
#: received body |
10618 | 146 |
self.content = BytesIO() |
10003
6bcb460826cc
[web] set Vary response header to "Accept-Language" when using content negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
10001
diff
changeset
|
147 |
# prepare output header |
6bcb460826cc
[web] set Vary response header to "Accept-Language" when using content negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
10001
diff
changeset
|
148 |
#: Header used for the final response |
6bcb460826cc
[web] set Vary response header to "Accept-Language" when using content negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
10001
diff
changeset
|
149 |
self.headers_out = Headers() |
6bcb460826cc
[web] set Vary response header to "Accept-Language" when using content negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
10001
diff
changeset
|
150 |
#: HTTP status use by the final response |
6bcb460826cc
[web] set Vary response header to "Accept-Language" when using content negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
10001
diff
changeset
|
151 |
self.status_out = 200 |
9601
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
152 |
# set up language based on request headers or site default (we don't |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
153 |
# have a user yet, and might not get one) |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
154 |
self.set_user_language(None) |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
155 |
#: dictionary that may be used to store request data that has to be |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
156 |
#: shared among various components used to publish the request (views, |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
157 |
#: controller, application...) |
0 | 158 |
self.data = {} |
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
159 |
self._search_state = None |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
160 |
#: page id, set by htmlheader template |
0 | 161 |
self.pageid = None |
3816
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
162 |
self._set_pageid() |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
163 |
|
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
164 |
def _set_pageid(self): |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
165 |
"""initialize self.pageid |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
166 |
if req.form provides a specific pageid, use it, otherwise build a |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
167 |
new one. |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
168 |
""" |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
169 |
pid = self.form.get('pageid') |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
170 |
if pid is None: |
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
171 |
pid = make_uid(id(self)) |
7374
3c9850d929e5
[web request] don't define pageid js variable when one is found in form parameters (eg, page ajax call)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7268
diff
changeset
|
172 |
self.html_headers.define_var('pageid', pid, override=False) |
3816
37b376bb4088
[web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3275
diff
changeset
|
173 |
self.pageid = pid |
0 | 174 |
|
8155
c075950cc929
[bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8128
diff
changeset
|
175 |
def _get_json_request(self): |
8128
0a927fe4541b
[controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8082
diff
changeset
|
176 |
warn('[3.15] self._cw.json_request is deprecated, use self._cw.ajax_request instead', |
0a927fe4541b
[controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8082
diff
changeset
|
177 |
DeprecationWarning, stacklevel=2) |
0a927fe4541b
[controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8082
diff
changeset
|
178 |
return self.ajax_request |
8155
c075950cc929
[bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8128
diff
changeset
|
179 |
def _set_json_request(self, value): |
c075950cc929
[bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8128
diff
changeset
|
180 |
warn('[3.15] self._cw.json_request is deprecated, use self._cw.ajax_request instead', |
c075950cc929
[bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8128
diff
changeset
|
181 |
DeprecationWarning, stacklevel=2) |
c075950cc929
[bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8128
diff
changeset
|
182 |
self.ajax_request = value |
c075950cc929
[bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8128
diff
changeset
|
183 |
json_request = property(_get_json_request, _set_json_request) |
8128
0a927fe4541b
[controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8082
diff
changeset
|
184 |
|
9818
e3d2012adcd0
[request] Ensure base_url() always has a trailing '/' (closes #3955093)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9736
diff
changeset
|
185 |
def _base_url(self, secure=None): |
8309
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
186 |
"""return the root url of the instance |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
187 |
|
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
188 |
secure = False -> base-url |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
189 |
secure = None -> https-url if req.https |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
190 |
secure = True -> https if it exist |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
191 |
""" |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
192 |
if secure is None: |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
193 |
secure = self.https |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
194 |
base_url = None |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
195 |
if secure: |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
196 |
base_url = self.vreg.config.get('https-url') |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
197 |
if base_url is None: |
9818
e3d2012adcd0
[request] Ensure base_url() always has a trailing '/' (closes #3955093)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9736
diff
changeset
|
198 |
base_url = super(_CubicWebRequestBase, self)._base_url() |
8309
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
199 |
return base_url |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8271
diff
changeset
|
200 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
201 |
@property |
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6564
diff
changeset
|
202 |
def authmode(self): |
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
203 |
"""Authentification mode of the instance |
8480
086cff6a306a
[book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8316
diff
changeset
|
204 |
(see :ref:`WebServerConfig`)""" |
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6564
diff
changeset
|
205 |
return self.vreg.config['auth-mode'] |
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6564
diff
changeset
|
206 |
|
8271
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
207 |
# Various variable generator. |
3bd9b317b1d8
[req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8239
diff
changeset
|
208 |
|
6581
4a3b264589dc
[dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6564
diff
changeset
|
209 |
@property |
2801
7ef4c1c9266b
fix syntax error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
210 |
def varmaker(self): |
4366
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
211 |
"""the rql varmaker is exposed both as a property and as the |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
212 |
set_varmaker function since we've two use cases: |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
213 |
|
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
214 |
* accessing the req.varmaker property to get a new variable name |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
215 |
|
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
216 |
* calling req.set_varmaker() to ensure a varmaker is set for later ajax |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
217 |
calls sharing our .pageid |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
218 |
""" |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
219 |
return self.set_varmaker() |
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
220 |
|
7266
f625097d1e99
[request] don't init tabindex unnecessarily, since it's added to page data which will makes an ajax call to unload_page_data necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7070
diff
changeset
|
221 |
def next_tabindex(self): |
6259
c3d98e4bee12
[request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6108
diff
changeset
|
222 |
nextfunc = self.get_page_data('nexttabfunc') |
c3d98e4bee12
[request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6108
diff
changeset
|
223 |
if nextfunc is None: |
9884
5ad1c3a9c4a1
[web/request] use a picklable Counter object for tab index counters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
224 |
nextfunc = Counter(1) |
6259
c3d98e4bee12
[request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6108
diff
changeset
|
225 |
self.set_page_data('nexttabfunc', nextfunc) |
7266
f625097d1e99
[request] don't init tabindex unnecessarily, since it's added to page data which will makes an ajax call to unload_page_data necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7070
diff
changeset
|
226 |
return nextfunc() |
6259
c3d98e4bee12
[request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6108
diff
changeset
|
227 |
|
4366
d51f28ba9399
fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4277
diff
changeset
|
228 |
def set_varmaker(self): |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
229 |
varmaker = self.get_page_data('rql_varmaker') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
230 |
if varmaker is None: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
231 |
varmaker = rqlvar_maker() |
11440
8388b37720e1
[session] Ensure access to rql varmaker always mark the session as dirty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11339
diff
changeset
|
232 |
self.set_page_data('rql_varmaker', varmaker) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
233 |
return varmaker |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
234 |
|
0 | 235 |
# input form parameters management ######################################## |
1426 | 236 |
|
0 | 237 |
# common form parameters which should be protected against html values |
238 |
# XXX can't add 'eid' for instance since it may be multivalued |
|
239 |
# dont put rql as well, if query contains < and > it will be corrupted! |
|
1426 | 240 |
no_script_form_params = set(('vid', |
241 |
'etype', |
|
0 | 242 |
'vtitle', 'title', |
243 |
'__redirectvid', '__redirectrql')) |
|
1426 | 244 |
|
0 | 245 |
def setup_params(self, params): |
10903
da30851f9706
spelling: *aly → *ally
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10850
diff
changeset
|
246 |
"""WARNING: we're intentionally leaving INTERNAL_FIELD_VALUE here |
0 | 247 |
|
1426 | 248 |
subclasses should overrides to |
0 | 249 |
""" |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
250 |
self.form = {} |
0 | 251 |
if params is None: |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
252 |
return |
0 | 253 |
encoding = self.encoding |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10618
diff
changeset
|
254 |
for param, val in params.items(): |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
255 |
if isinstance(val, (tuple, list)): |
10693
be0bd5aa21b4
[web/request] don't convert request parameters if py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10690
diff
changeset
|
256 |
if PY2: |
be0bd5aa21b4
[web/request] don't convert request parameters if py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10690
diff
changeset
|
257 |
val = [unicode(x, encoding) for x in val] |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
258 |
if len(val) == 1: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
259 |
val = val[0] |
10693
be0bd5aa21b4
[web/request] don't convert request parameters if py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10690
diff
changeset
|
260 |
elif PY2 and isinstance(val, str): |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
261 |
val = unicode(val, encoding) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
262 |
if param in self.no_script_form_params and val: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
263 |
val = self.no_script_form_param(param, val) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
264 |
if param == '_cwmsgid': |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
265 |
self.set_message_id(val) |
0 | 266 |
else: |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
267 |
self.form[param] = val |
1426 | 268 |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
269 |
def no_script_form_param(self, param, value): |
0 | 270 |
"""ensure there is no script in a user form param |
271 |
||
272 |
by default return a cleaned string instead of raising a security |
|
273 |
exception |
|
274 |
||
275 |
this method should be called on every user input (form at least) fields |
|
276 |
that are at some point inserted in a generated html page to protect |
|
277 |
against script kiddies |
|
278 |
""" |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
279 |
# safety belt for strange urls like http://...?vtitle=yo&vtitle=yo |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
280 |
if isinstance(value, (list, tuple)): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
281 |
self.error('no_script_form_param got a list (%s). Who generated the URL ?', |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
282 |
repr(value)) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
283 |
value = value[0] |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
284 |
return remove_html_tags(value) |
1426 | 285 |
|
0 | 286 |
def list_form_param(self, param, form=None, pop=False): |
287 |
"""get param from form parameters and return its value as a list, |
|
288 |
skipping internal markers if any |
|
1426 | 289 |
|
0 | 290 |
* if the parameter isn't defined, return an empty list |
291 |
* if the parameter is a single (unicode) value, return a list |
|
292 |
containing that value |
|
293 |
* if the parameter is already a list or tuple, just skip internal |
|
294 |
markers |
|
295 |
||
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8177
diff
changeset
|
296 |
if pop is True, the parameter is removed from the form dictionary |
0 | 297 |
""" |
298 |
if form is None: |
|
299 |
form = self.form |
|
1426 | 300 |
return list_form_param(form, param, pop) |
301 |
||
0 | 302 |
def reset_headers(self): |
303 |
"""used by AutomaticWebTest to clear html headers between tests on |
|
304 |
the same resultset |
|
305 |
""" |
|
7762
a3f9ba4d44eb
[web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7660
diff
changeset
|
306 |
self.html_headers = HTMLHead(self) |
0 | 307 |
return self |
308 |
||
309 |
# web state helpers ####################################################### |
|
1426 | 310 |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
311 |
@property |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
312 |
def message(self): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
313 |
try: |
7432
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
314 |
return self.session.data.pop(self._msgid, u'') |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
315 |
except AttributeError: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
316 |
try: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
317 |
return self._msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
318 |
except AttributeError: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
319 |
return None |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
320 |
|
0 | 321 |
def set_message(self, msg): |
10717
46f934bebd85
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10693
diff
changeset
|
322 |
assert isinstance(msg, text_type) |
9561
3bdf85279c67
[request] Make sure set_message() actually displays its given message (closes #3003425)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9549
diff
changeset
|
323 |
self.reset_message() |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
324 |
self._msg = msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
325 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
326 |
def set_message_id(self, msgid): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
327 |
self._msgid = msgid |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
328 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
329 |
@cached |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
330 |
def redirect_message_id(self): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
331 |
return make_uid() |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
332 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
333 |
def set_redirect_message(self, msg): |
7432
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
334 |
# TODO - this should probably be merged with append_to_redirect_message |
10717
46f934bebd85
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10693
diff
changeset
|
335 |
assert isinstance(msg, text_type) |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
336 |
msgid = self.redirect_message_id() |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
337 |
self.session.data[msgid] = msg |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
338 |
return msgid |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
339 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
340 |
def append_to_redirect_message(self, msg): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
341 |
msgid = self.redirect_message_id() |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
342 |
currentmsg = self.session.data.get(msgid) |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
343 |
if currentmsg is not None: |
7432
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
344 |
currentmsg = u'%s %s' % (currentmsg, msg) |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
345 |
else: |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
346 |
currentmsg = msg |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
347 |
self.session.data[msgid] = currentmsg |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
348 |
return msgid |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
349 |
|
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
350 |
def reset_message(self): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
351 |
if hasattr(self, '_msg'): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
352 |
del self._msg |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
353 |
if hasattr(self, '_msgid'): |
9561
3bdf85279c67
[request] Make sure set_message() actually displays its given message (closes #3003425)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9549
diff
changeset
|
354 |
self.session.data.pop(self._msgid, u'') |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4851
diff
changeset
|
355 |
del self._msgid |
1426 | 356 |
|
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
357 |
def _load_search_state(self, searchstate): |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
358 |
if searchstate is None or searchstate == 'normal': |
10275
d6131c5045be
[web/request] simplify search_state loading, fix comment
Julien Cristau <julien.cristau@logilab.fr>
parents:
10274
diff
changeset
|
359 |
self._search_state = ('normal',) |
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
360 |
else: |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
361 |
self._search_state = ('linksearch', searchstate.split(':')) |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
362 |
assert len(self._search_state[-1]) == 4, 'invalid searchstate' |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
363 |
|
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
364 |
@property |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
365 |
def search_state(self): |
10275
d6131c5045be
[web/request] simplify search_state loading, fix comment
Julien Cristau <julien.cristau@logilab.fr>
parents:
10274
diff
changeset
|
366 |
"""search state: 'normal' or 'linksearch' (i.e. searching for an object |
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
367 |
to create a relation with another)""" |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
368 |
if self._search_state is None: |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
369 |
searchstate = self.session.data.get('search_state', 'normal') |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
370 |
self._load_search_state(searchstate) |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
371 |
return self._search_state |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
372 |
|
10276
ffb269e60348
[web/request] Restore attribute setting for search_state
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10275
diff
changeset
|
373 |
@search_state.setter |
ffb269e60348
[web/request] Restore attribute setting for search_state
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10275
diff
changeset
|
374 |
def search_state(self, searchstate): |
ffb269e60348
[web/request] Restore attribute setting for search_state
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10275
diff
changeset
|
375 |
self._search_state = searchstate |
ffb269e60348
[web/request] Restore attribute setting for search_state
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10275
diff
changeset
|
376 |
|
0 | 377 |
def update_search_state(self): |
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
378 |
"""update the current search state if needed""" |
0 | 379 |
searchstate = self.form.get('__mode') |
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
380 |
if searchstate: |
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
381 |
self.set_search_state(searchstate) |
0 | 382 |
|
383 |
def set_search_state(self, searchstate): |
|
384 |
"""set a new search state""" |
|
8541
5b6bc27ece6e
[web] search state doesn't need cnx, remove useless test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8538
diff
changeset
|
385 |
self.session.data['search_state'] = searchstate |
10274
1f84295bfe95
[webrequest] Lazy-load search_state
Christophe de Vienne <christophe@unlish.com>
parents:
10215
diff
changeset
|
386 |
self._load_search_state(searchstate) |
0 | 387 |
|
1173
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
388 |
def match_search_state(self, rset): |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
389 |
"""when searching an entity to create a relation, return True if entities in |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
390 |
the given rset may be used as relation end |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
391 |
""" |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
392 |
try: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
393 |
searchedtype = self.search_state[1][-1] |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
394 |
except IndexError: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
395 |
return False # no searching for association |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
396 |
for etype in rset.column_types(0): |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
397 |
if etype != searchedtype: |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
398 |
return False |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
399 |
return True |
8f123fd081f4
forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
400 |
|
0 | 401 |
# web edition helpers ##################################################### |
1426 | 402 |
|
0 | 403 |
@cached # so it's writed only once |
404 |
def fckeditor_config(self): |
|
7277
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7273
diff
changeset
|
405 |
fckeditor_url = self.build_url('fckeditor/fckeditor.js') |
acd7f0e9f276
[etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents:
7273
diff
changeset
|
406 |
self.add_js(fckeditor_url, localfile=False) |
0 | 407 |
self.html_headers.define_var('fcklang', self.lang) |
408 |
self.html_headers.define_var('fckconfigpath', |
|
7070
5f8e52d722c5
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6581
diff
changeset
|
409 |
self.data_url('cubicweb.fckcwconfig.js')) |
1013
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
940
diff
changeset
|
410 |
def use_fckeditor(self): |
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
940
diff
changeset
|
411 |
return self.vreg.config.fckeditor_installed() and self.property_value('ui.fckeditor') |
0 | 412 |
|
413 |
def edited_eids(self, withtype=False): |
|
414 |
"""return a list of edited eids""" |
|
415 |
yielded = False |
|
416 |
# warning: use .keys since the caller may change `form` |
|
417 |
form = self.form |
|
418 |
try: |
|
419 |
eids = form['eid'] |
|
420 |
except KeyError: |
|
4155
80cc9c6ddcf0
NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4154
diff
changeset
|
421 |
raise NothingToEdit(self._('no selected entities')) |
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10605
diff
changeset
|
422 |
if isinstance(eids, string_types): |
0 | 423 |
eids = (eids,) |
424 |
for peid in eids: |
|
425 |
if withtype: |
|
426 |
typekey = '__type:%s' % peid |
|
427 |
assert typekey in form, 'no entity type specified' |
|
428 |
yield peid, form[typekey] |
|
429 |
else: |
|
430 |
yield peid |
|
431 |
yielded = True |
|
432 |
if not yielded: |
|
4155
80cc9c6ddcf0
NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4154
diff
changeset
|
433 |
raise NothingToEdit(self._('no selected entities')) |
0 | 434 |
|
435 |
# minparams=3 by default: at least eid, __type, and some params to change |
|
436 |
def extract_entity_params(self, eid, minparams=3): |
|
437 |
"""extract form parameters relative to the given eid""" |
|
438 |
params = {} |
|
439 |
eid = str(eid) |
|
440 |
form = self.form |
|
441 |
for param in form: |
|
442 |
try: |
|
443 |
name, peid = param.split(':', 1) |
|
444 |
except ValueError: |
|
7639
5c9e94a09e97
[web form] avoid mis-formated warning for _cw_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
445 |
if not param.startswith('__') and param not in ('eid', '_cw_fields'): |
6079 | 446 |
self.warning('param %s mis-formatted', param) |
0 | 447 |
continue |
448 |
if peid == eid: |
|
449 |
value = form[param] |
|
450 |
if value == INTERNAL_FIELD_VALUE: |
|
451 |
value = None |
|
452 |
params[name] = value |
|
453 |
params['eid'] = eid |
|
454 |
if len(params) < minparams: |
|
455 |
raise RequestError(self._('missing parameters for entity %s') % eid) |
|
456 |
return params |
|
1426 | 457 |
|
4277
35cd057339b2
turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
458 |
# XXX this should go to the GenericRelationsField. missing edition cancel protocol. |
0 | 459 |
|
460 |
def remove_pending_operations(self): |
|
461 |
"""shortcut to clear req's pending_{delete,insert} entries |
|
462 |
||
463 |
This is needed when the edition is completed (whether it's validated |
|
464 |
or cancelled) |
|
465 |
""" |
|
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
466 |
self.session.data.pop('pending_insert', None) |
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
467 |
self.session.data.pop('pending_delete', None) |
0 | 468 |
|
469 |
def cancel_edition(self, errorurl): |
|
470 |
"""remove pending operations and `errorurl`'s specific stored data |
|
471 |
""" |
|
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
472 |
self.session.data.pop(errorurl, None) |
0 | 473 |
self.remove_pending_operations() |
1426 | 474 |
|
0 | 475 |
# high level methods for HTTP headers management ########################## |
476 |
||
477 |
# must be cached since login/password are popped from the form dictionary |
|
478 |
# and this method may be called multiple times during authentication |
|
479 |
@cached |
|
480 |
def get_authorization(self): |
|
481 |
"""Parse and return the Authorization header""" |
|
482 |
if self.authmode == "cookie": |
|
483 |
try: |
|
484 |
user = self.form.pop("__login") |
|
485 |
passwd = self.form.pop("__password", '') |
|
486 |
return user, passwd.encode('UTF8') |
|
487 |
except KeyError: |
|
488 |
self.debug('no login/password in form params') |
|
489 |
return None, None |
|
490 |
else: |
|
491 |
return self.header_authorization() |
|
1426 | 492 |
|
0 | 493 |
def get_cookie(self): |
494 |
"""retrieve request cookies, returns an empty cookie if not found""" |
|
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
495 |
# XXX use http_headers implementation |
0 | 496 |
try: |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
497 |
return SimpleCookie(self.get_header('Cookie')) |
0 | 498 |
except KeyError: |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
499 |
return SimpleCookie() |
0 | 500 |
|
10001
1245357b3b3e
[web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents:
9897
diff
changeset
|
501 |
def set_cookie(self, name, value, maxage=300, expires=None, secure=False, httponly=False): |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
502 |
"""set / update a cookie |
0 | 503 |
|
504 |
by default, cookie will be available for the next 5 minutes. |
|
505 |
Give maxage = None to have a "session" cookie expiring when the |
|
506 |
client close its browser |
|
507 |
""" |
|
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
508 |
if isinstance(name, SimpleCookie): |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
509 |
warn('[3.13] set_cookie now takes name and value as two first ' |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
510 |
'argument, not anymore cookie object and name', |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
511 |
DeprecationWarning, stacklevel=2) |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
512 |
secure = name[value]['secure'] |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
513 |
name, value = value, name[value].value |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
514 |
if maxage: # don't check is None, 0 may be specified |
7858
f91255a3b2c2
[web request] fix stupid error that gone through the review
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7855
diff
changeset
|
515 |
assert expires is None, 'both max age and expires cant be specified' |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
516 |
expires = maxage + time.time() |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
517 |
elif expires: |
8176
eff4fe02ec64
[req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8081
diff
changeset
|
518 |
# we don't want to handle times before the EPOCH (cause bug on |
eff4fe02ec64
[req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8081
diff
changeset
|
519 |
# windows). Also use > and not >= else expires == 0 and Cookie think |
eff4fe02ec64
[req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8081
diff
changeset
|
520 |
# that means no expire... |
10305
937deb71a681
[web] kill GMTOFFSET (closes #2154655)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10304
diff
changeset
|
521 |
assert expires > date(1970, 1, 1) |
937deb71a681
[web] kill GMTOFFSET (closes #2154655)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10304
diff
changeset
|
522 |
expires = timegm(expires.timetuple()) |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
523 |
else: |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
524 |
expires = None |
0 | 525 |
# make sure cookie is set on the correct path |
7870
1a1e04163cde
[test] unicode name/value in cookie makes tests failing (SimpleCookie then suppose it's a dict...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7858
diff
changeset
|
526 |
cookie = Cookie(str(name), str(value), self.base_url_path(), |
10001
1245357b3b3e
[web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents:
9897
diff
changeset
|
527 |
expires=expires, secure=secure, httponly=httponly) |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
528 |
self.headers_out.addHeader('Set-cookie', cookie) |
0 | 529 |
|
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
530 |
def remove_cookie(self, name, bwcompat=None): |
0 | 531 |
"""remove a cookie by expiring it""" |
7855
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
532 |
if bwcompat is not None: |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
533 |
warn('[3.13] remove_cookie now take only a name as argument', |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
534 |
DeprecationWarning, stacklevel=2) |
54283a5b7afc
[web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7793
diff
changeset
|
535 |
name = bwcompat |
8176
eff4fe02ec64
[req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8081
diff
changeset
|
536 |
self.set_cookie(name, '', maxage=0, expires=date(2000, 1, 1)) |
0 | 537 |
|
8611
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
538 |
def set_content_type(self, content_type, filename=None, encoding=None, |
8612
3bb96935a6b9
[web] use `inline` `Content-Disposition` by default (closes #2535734)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8611
diff
changeset
|
539 |
disposition='inline'): |
0 | 540 |
"""set output content type for this request. An optional filename |
8611
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
541 |
may be given. |
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
542 |
|
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
543 |
The disposition argument may be `attachement` or `inline` as specified |
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
544 |
for the Content-disposition HTTP header. The disposition parameter have |
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
545 |
no effect if no filename are specified. |
0 | 546 |
""" |
6353
b622602f8e9d
don't add another encoding if already present in content-type
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6259
diff
changeset
|
547 |
if content_type.startswith('text/') and ';charset=' not in content_type: |
0 | 548 |
content_type += ';charset=' + (encoding or self.encoding) |
549 |
self.set_header('content-type', content_type) |
|
550 |
if filename: |
|
8611
51068fe1e39a
[web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8610
diff
changeset
|
551 |
header = [disposition] |
8610
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
552 |
unicode_filename = None |
8600
d74addac92bb
[downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8480
diff
changeset
|
553 |
try: |
10733
9f66ac5eec1e
[py3k] http headers should be ascii-only unicode objects
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10717
diff
changeset
|
554 |
ascii_filename = filename.encode('ascii').decode('ascii') |
8600
d74addac92bb
[downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8480
diff
changeset
|
555 |
except UnicodeEncodeError: |
d74addac92bb
[downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8480
diff
changeset
|
556 |
# fallback filename for very old browser |
8610
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
557 |
unicode_filename = filename |
10733
9f66ac5eec1e
[py3k] http headers should be ascii-only unicode objects
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10717
diff
changeset
|
558 |
ascii_filename = filename.encode('ascii', 'ignore').decode('ascii') |
8610
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
559 |
# escape " and \ |
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
560 |
# see http://greenbytes.de/tech/tc2231/#attwithfilenameandextparamescaped |
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
561 |
ascii_filename = ascii_filename.replace('\x5c', r'\\').replace('"', r'\"') |
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
562 |
header.append('filename="%s"' % ascii_filename) |
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
563 |
if unicode_filename is not None: |
8600
d74addac92bb
[downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8480
diff
changeset
|
564 |
# encoded filename according RFC5987 |
10603
65ad6980976e
[py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10355
diff
changeset
|
565 |
urlquoted_filename = urlquote(unicode_filename.encode('utf-8'), '') |
8610
b1145ad53999
[downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8600
diff
changeset
|
566 |
header.append("filename*=utf-8''" + urlquoted_filename) |
8600
d74addac92bb
[downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8480
diff
changeset
|
567 |
self.set_header('content-disposition', ';'.join(header)) |
0 | 568 |
|
569 |
# high level methods for HTML headers management ########################## |
|
570 |
||
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2255
diff
changeset
|
571 |
def add_onload(self, jscode): |
4851
e55bdd10421e
remove deprecation warning introduced by add_onload api change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
572 |
self.html_headers.add_onload(jscode) |
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2255
diff
changeset
|
573 |
|
0 | 574 |
def add_js(self, jsfiles, localfile=True): |
8032
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
575 |
"""specify a list of JS files to include in the HTML headers. |
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
576 |
|
0 | 577 |
:param jsfiles: a JS filename or a list of JS filenames |
578 |
:param localfile: if True, the default data dir prefix is added to the |
|
579 |
JS filename |
|
580 |
""" |
|
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10605
diff
changeset
|
581 |
if isinstance(jsfiles, string_types): |
0 | 582 |
jsfiles = (jsfiles,) |
583 |
for jsfile in jsfiles: |
|
584 |
if localfile: |
|
7070
5f8e52d722c5
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6581
diff
changeset
|
585 |
jsfile = self.data_url(jsfile) |
0 | 586 |
self.html_headers.add_js(jsfile) |
587 |
||
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
588 |
def add_css(self, cssfiles, media=u'all', localfile=True, ieonly=False, |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
589 |
iespec=u'[if lt IE 8]'): |
0 | 590 |
"""specify a CSS file to include in the HTML headers |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
591 |
|
8032
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
592 |
:param cssfiles: a CSS filename or a list of CSS filenames. |
0 | 593 |
:param media: the CSS's media if necessary |
594 |
:param localfile: if True, the default data dir prefix is added to the |
|
595 |
CSS filename |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
596 |
:param ieonly: True if this css is specific to IE |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
597 |
:param iespec: conditional expression that will be used around |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
598 |
the css inclusion. cf: |
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
599 |
http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx |
0 | 600 |
""" |
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10605
diff
changeset
|
601 |
if isinstance(cssfiles, string_types): |
0 | 602 |
cssfiles = (cssfiles,) |
603 |
if ieonly: |
|
604 |
if self.ie_browser(): |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
605 |
extraargs = [iespec] |
0 | 606 |
add_css = self.html_headers.add_ie_css |
607 |
else: |
|
608 |
return # no need to do anything on non IE browsers |
|
609 |
else: |
|
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
610 |
extraargs = [] |
0 | 611 |
add_css = self.html_headers.add_css |
612 |
for cssfile in cssfiles: |
|
613 |
if localfile: |
|
7070
5f8e52d722c5
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6581
diff
changeset
|
614 |
cssfile = self.data_url(cssfile) |
4860
cedb6afdb7da
[web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4719
diff
changeset
|
615 |
add_css(cssfile, media, *extraargs) |
1426 | 616 |
|
5713
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
617 |
def ajax_replace_url(self, nodeid, replacemode='replace', **extraparams): |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
618 |
"""builds an ajax url that will replace nodeid's content |
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
619 |
|
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
620 |
:param nodeid: the dom id of the node to replace |
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
621 |
:param replacemode: defines how the replacement should be done. |
5290
15846058378b
[doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4984
diff
changeset
|
622 |
|
5713
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
623 |
Possible values are : |
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
624 |
- 'replace' to replace the node's content with the generated HTML |
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
625 |
- 'swap' to replace the node itself with the generated HTML |
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
626 |
- 'append' to append the generated HTML to the node's content |
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
627 |
|
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
628 |
Arbitrary extra named arguments may be given, they will be included as |
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
629 |
parameters of the generated url. |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
630 |
""" |
6564
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
631 |
# define a function in headers and use it in the link to avoid url |
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
632 |
# unescaping pb: browsers give the js expression to the interpreter |
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
633 |
# after having url unescaping the content. This may make appear some |
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
634 |
# quote or other special characters that will break the js expression. |
5713
605f571198eb
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5685
diff
changeset
|
635 |
extraparams.setdefault('fname', 'view') |
8730
d4d9f33fd01b
[web/request] Prune extraneous 'pageid' from generated ajax URL parameters (closes #2758130)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8728
diff
changeset
|
636 |
# remove pageid from the generated URL as it's forced as a parameter |
d4d9f33fd01b
[web/request] Prune extraneous 'pageid' from generated ajax URL parameters (closes #2758130)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8728
diff
changeset
|
637 |
# to the loadxhtml call below. |
d4d9f33fd01b
[web/request] Prune extraneous 'pageid' from generated ajax URL parameters (closes #2758130)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8728
diff
changeset
|
638 |
extraparams.pop('pageid', None) |
8728
75be9de9d68e
[web] Use the new '/ajax' URL path to access the AjaxController (closes #2758254)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8612
diff
changeset
|
639 |
url = self.build_url('ajax', **extraparams) |
6564
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
640 |
cbname = build_cb_uid(url[:50]) |
7515
e1ba23fdcf2d
closes #1484510: ajax_replace_url should propagate page id (until an underlying api does it automatically
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7411
diff
changeset
|
641 |
# think to propagate pageid. XXX see https://www.cubicweb.org/ticket/1753121 |
7660
a1506b5306cc
closes #1826543: error on some ajax_replace_url call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7639
diff
changeset
|
642 |
jscode = u'function %s() { $("#%s").%s; }' % ( |
7515
e1ba23fdcf2d
closes #1484510: ajax_replace_url should propagate page id (until an underlying api does it automatically
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7411
diff
changeset
|
643 |
cbname, nodeid, js.loadxhtml(url, {'pageid': self.pageid}, |
e1ba23fdcf2d
closes #1484510: ajax_replace_url should propagate page id (until an underlying api does it automatically
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7411
diff
changeset
|
644 |
'get', replacemode)) |
6564
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
645 |
self.html_headers.add_post_inline_script(jscode) |
ff9f7c566464
[request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6543
diff
changeset
|
646 |
return "javascript: %s()" % cbname |
1801
672acc730ce5
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1718
diff
changeset
|
647 |
|
0 | 648 |
# urls/path management #################################################### |
1426 | 649 |
|
7432
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
650 |
def build_url(self, *args, **kwargs): |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
651 |
"""return an absolute URL using params dictionary key/values as URL |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
652 |
parameters. Values are automatically URL quoted, and the |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
653 |
publishing method to use may be specified or will be guessed. |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
654 |
""" |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
655 |
if '__message' in kwargs: |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
656 |
msg = kwargs.pop('__message') |
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
657 |
kwargs['_cwmsgid'] = self.set_redirect_message(msg) |
9022
a1cee6915ea3
[req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents:
8974
diff
changeset
|
658 |
if not args: |
a1cee6915ea3
[req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents:
8974
diff
changeset
|
659 |
method = 'view' |
a1cee6915ea3
[req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents:
8974
diff
changeset
|
660 |
if (self.from_controller() == 'view' |
a1cee6915ea3
[req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents:
8974
diff
changeset
|
661 |
and not '_restpath' in kwargs): |
a1cee6915ea3
[req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents:
8974
diff
changeset
|
662 |
method = self.relative_path(includeparams=False) or 'view' |
a1cee6915ea3
[req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents:
8974
diff
changeset
|
663 |
args = (method,) |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
664 |
return super(_CubicWebRequestBase, self).build_url(*args, **kwargs) |
7432
cab99ccdb774
[ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
7412
diff
changeset
|
665 |
|
0 | 666 |
def url(self, includeparams=True): |
667 |
"""return currently accessed url""" |
|
668 |
return self.base_url() + self.relative_path(includeparams) |
|
669 |
||
670 |
def selected(self, url): |
|
671 |
"""return True if the url is equivalent to currently accessed url""" |
|
672 |
reqpath = self.relative_path().lower() |
|
673 |
baselen = len(self.base_url()) |
|
674 |
return (reqpath == url[baselen:].lower()) |
|
675 |
||
676 |
def base_url_prepend_host(self, hostname): |
|
677 |
protocol, roothost = urlsplit(self.base_url())[:2] |
|
678 |
if roothost.startswith('www.'): |
|
679 |
roothost = roothost[4:] |
|
680 |
return '%s://%s.%s' % (protocol, hostname, roothost) |
|
681 |
||
682 |
def base_url_path(self): |
|
683 |
"""returns the absolute path of the base url""" |
|
684 |
return urlsplit(self.base_url())[2] |
|
1426 | 685 |
|
7070
5f8e52d722c5
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6581
diff
changeset
|
686 |
def data_url(self, relpath): |
10850
928c7ec525e0
[web] fix typo in doc string
Julien Cristau <julien.cristau@logilab.fr>
parents:
10849
diff
changeset
|
687 |
"""returns the absolute path for a data resource""" |
7070
5f8e52d722c5
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6581
diff
changeset
|
688 |
return self.datadir_url + relpath |
5f8e52d722c5
[web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6581
diff
changeset
|
689 |
|
0 | 690 |
@cached |
691 |
def from_controller(self): |
|
692 |
"""return the id (string) of the controller issuing the request""" |
|
693 |
controller = self.relative_path(False).split('/', 1)[0] |
|
8696
0bb18407c053
[toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
694 |
if controller in self.vreg['controllers']: |
0 | 695 |
return controller |
696 |
return 'view' |
|
1426 | 697 |
|
9229
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
698 |
def is_client_cache_valid(self): |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
699 |
"""check if a client cached page exists (as specified in request |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
700 |
headers) and is still usable. |
0 | 701 |
|
9229
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
702 |
Return False if the page has to be calculated, else True. |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
703 |
|
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
704 |
Some response cache headers may be set by this method. |
0 | 705 |
""" |
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
706 |
modified = True |
10848
e905c95ccdfb
[web] stop looking at a request's Cache-Control header
Julien Cristau <julien.cristau@logilab.fr>
parents:
10779
diff
changeset
|
707 |
# Here, we search for any invalid 'not modified' condition |
e905c95ccdfb
[web] stop looking at a request's Cache-Control header
Julien Cristau <julien.cristau@logilab.fr>
parents:
10779
diff
changeset
|
708 |
# see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3 |
e905c95ccdfb
[web] stop looking at a request's Cache-Control header
Julien Cristau <julien.cristau@logilab.fr>
parents:
10779
diff
changeset
|
709 |
validators = get_validators(self._headers_in) |
e905c95ccdfb
[web] stop looking at a request's Cache-Control header
Julien Cristau <julien.cristau@logilab.fr>
parents:
10779
diff
changeset
|
710 |
if validators: # if we have no |
e905c95ccdfb
[web] stop looking at a request's Cache-Control header
Julien Cristau <julien.cristau@logilab.fr>
parents:
10779
diff
changeset
|
711 |
modified = any(func(val, self.headers_out) for func, val in validators) |
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
712 |
# Forge expected response |
10849
79066409fdcf
[web] stop adding an Expires header with all responses
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10848
diff
changeset
|
713 |
if not modified: |
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
714 |
# overwrite headers_out to forge a brand new not-modified response |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
715 |
self.headers_out = self._forge_cached_headers() |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
716 |
if self.http_method() in ('HEAD', 'GET'): |
10604
d4bf85db41f2
[py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10603
diff
changeset
|
717 |
self.status_out = http_client.NOT_MODIFIED |
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
718 |
else: |
10604
d4bf85db41f2
[py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10603
diff
changeset
|
719 |
self.status_out = http_client.PRECONDITION_FAILED |
9229
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
720 |
# XXX replace by True once validate_cache bw compat method is dropped |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
721 |
return self.status_out |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
722 |
# XXX replace by False once validate_cache bw compat method is dropped |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
723 |
return None |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
724 |
|
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
725 |
@deprecated('[3.18] use .is_client_cache_valid() method instead') |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
726 |
def validate_cache(self): |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
727 |
"""raise a `StatusResponse` exception if a cached page along the way |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
728 |
exists and is still usable. |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
729 |
""" |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
730 |
status_code = self.is_client_cache_valid() |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
731 |
if status_code is not None: |
739ae5366bed
[web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9138
diff
changeset
|
732 |
raise StatusResponse(status_code) |
1426 | 733 |
|
0 | 734 |
# abstract methods to override according to the web front-end ############# |
1426 | 735 |
|
0 | 736 |
def http_method(self): |
737 |
"""returns 'POST', 'GET', 'HEAD', etc.""" |
|
738 |
raise NotImplementedError() |
|
739 |
||
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
740 |
def _forge_cached_headers(self): |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
741 |
# overwrite headers_out to forge a brand new not-modified response |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
742 |
headers = Headers() |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
743 |
for header in ( |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
744 |
# Required from sec 10.3.5: |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
745 |
'date', 'etag', 'content-location', 'expires', |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
746 |
'cache-control', 'vary', |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
747 |
# Others: |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
748 |
'server', 'proxy-authenticate', 'www-authenticate', 'warning'): |
11172
5a19524e59e1
[web] fix headers on 304 responses (closes #11466875)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10997
diff
changeset
|
749 |
value = self.headers_out.getRawHeaders(header) |
8316
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
750 |
if value is not None: |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
751 |
headers.setRawHeaders(header, value) |
d5b1b75805dd
[cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8314
diff
changeset
|
752 |
return headers |
1426 | 753 |
|
0 | 754 |
def relative_path(self, includeparams=True): |
755 |
"""return the normalized path of the request (ie at least relative |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2315
diff
changeset
|
756 |
to the instance's root, but some other normalization may be needed |
0 | 757 |
so that the returned path may be used to compare to generated urls |
758 |
||
759 |
:param includeparams: |
|
760 |
boolean indicating if GET form parameters should be kept in the path |
|
761 |
""" |
|
762 |
raise NotImplementedError() |
|
763 |
||
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
764 |
# http headers ############################################################ |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
765 |
|
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
766 |
### incoming headers |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
767 |
|
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
768 |
def get_header(self, header, default=None, raw=True): |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
769 |
"""return the value associated with the given input header, raise |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
770 |
KeyError if the header is not set |
0 | 771 |
""" |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
772 |
if raw: |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
773 |
return self._headers_in.getRawHeaders(header, [default])[0] |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
774 |
return self._headers_in.getHeader(header, default) |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
775 |
|
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
776 |
def header_accept_language(self): |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
777 |
"""returns an ordered list of preferred languages""" |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
778 |
acceptedlangs = self.get_header('Accept-Language', raw=False) or {} |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10618
diff
changeset
|
779 |
for lang, _ in sorted(acceptedlangs.items(), key=lambda x: x[1], |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
780 |
reverse=True): |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
781 |
lang = lang.split('-')[0] |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
782 |
yield lang |
0 | 783 |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
784 |
def header_if_modified_since(self): |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
785 |
"""If the HTTP header If-modified-since is set, return the equivalent |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
786 |
date time value (GMT), else return None |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
787 |
""" |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
788 |
mtime = self.get_header('If-modified-since', raw=False) |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
789 |
if mtime: |
10304
b6c55274135a
[web/request] don't play tricks with utc offset
Julien Cristau <julien.cristau@logilab.fr>
parents:
10276
diff
changeset
|
790 |
return datetime.utcfromtimestamp(mtime) |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
791 |
return None |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
792 |
|
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
793 |
### outcoming headers |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
794 |
def set_header(self, header, value, raw=True): |
0 | 795 |
"""set an output HTTP header""" |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
796 |
if raw: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
797 |
# adding encoded header is important, else page content |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
798 |
# will be reconverted back to unicode and apart unefficiency, this |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
799 |
# may cause decoding problem (e.g. when downloading a file) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
800 |
self.headers_out.setRawHeaders(header, [str(value)]) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
801 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
802 |
self.headers_out.setHeader(header, value) |
0 | 803 |
|
804 |
def add_header(self, header, value): |
|
805 |
"""add an output HTTP header""" |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
806 |
# adding encoded header is important, else page content |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
807 |
# will be reconverted back to unicode and apart unefficiency, this |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
808 |
# may cause decoding problem (e.g. when downloading a file) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
809 |
self.headers_out.addRawHeader(header, str(value)) |
1426 | 810 |
|
0 | 811 |
def remove_header(self, header): |
812 |
"""remove an output HTTP header""" |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
4984
diff
changeset
|
813 |
self.headers_out.removeHeader(header) |
1426 | 814 |
|
0 | 815 |
def header_authorization(self): |
816 |
"""returns a couple (auth-type, auth-value)""" |
|
817 |
auth = self.get_header("Authorization", None) |
|
818 |
if auth: |
|
819 |
scheme, rest = auth.split(' ', 1) |
|
820 |
scheme = scheme.lower() |
|
821 |
try: |
|
822 |
assert scheme == "basic" |
|
10779
6cef304aca4c
[web] base64 works on bytes not str (py3k)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10733
diff
changeset
|
823 |
user, passwd = base64.decodestring(rest.encode('ascii')).split(b":", 1) |
0 | 824 |
# XXX HTTP header encoding: use email.Header? |
825 |
return user.decode('UTF8'), passwd |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8645
diff
changeset
|
826 |
except Exception as ex: |
0 | 827 |
self.debug('bad authorization %s (%s: %s)', |
828 |
auth, ex.__class__.__name__, ex) |
|
829 |
return None, None |
|
830 |
||
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
831 |
def parse_accept_header(self, header): |
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
832 |
"""returns an ordered list of accepted values""" |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
833 |
try: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
834 |
value_parser, value_sort_key = ACCEPT_HEADER_PARSER[header.lower()] |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
835 |
except KeyError: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
836 |
value_parser = value_sort_key = None |
1716
b12d9e22bac3
basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
837 |
accepteds = self.get_header(header, '') |
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
838 |
values = _parse_accept_header(accepteds, value_parser, value_sort_key) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
839 |
return (raw_value for (raw_value, parsed_value, score) in values) |
0 | 840 |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
841 |
@deprecated('[3.17] demote_to_html is deprecated as we always serve html') |
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
842 |
def demote_to_html(self): |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
843 |
"""helper method to dynamically set request content type to text/html |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
844 |
|
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
845 |
The global doctype and xmldec must also be changed otherwise the browser |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
846 |
will display '<[' at the beginning of the page |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
847 |
""" |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
848 |
pass |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
849 |
|
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7164
diff
changeset
|
850 |
|
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
851 |
# xml doctype ############################################################# |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
852 |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
853 |
def set_doctype(self, doctype, reset_xmldecl=None): |
7187
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7164
diff
changeset
|
854 |
"""helper method to dynamically change page doctype |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7164
diff
changeset
|
855 |
|
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7164
diff
changeset
|
856 |
:param doctype: the new doctype, e.g. '<!DOCTYPE html>' |
496f51b92154
[views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7164
diff
changeset
|
857 |
""" |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
858 |
if reset_xmldecl is not None: |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
859 |
warn('[3.17] reset_xmldecl is deprecated as we only serve html', |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
860 |
DeprecationWarning, stacklevel=2) |
9138
bc6e25dbfd04
[web doctype] don't give through reset_xmldecl to avoid double deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8974
diff
changeset
|
861 |
self.main_stream.set_doctype(doctype) |
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
862 |
|
0 | 863 |
# page data management #################################################### |
864 |
||
865 |
def get_page_data(self, key, default=None): |
|
5683
ca2badf54858
[request] fix typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5505
diff
changeset
|
866 |
"""return value associated to `key` in current page data""" |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
867 |
page_data = self.session.data.get(self.pageid) |
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
868 |
if page_data is None: |
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
869 |
return default |
0 | 870 |
return page_data.get(key, default) |
1426 | 871 |
|
0 | 872 |
def set_page_data(self, key, value): |
873 |
"""set value associated to `key` in current page data""" |
|
874 |
self.html_headers.add_unload_pagedata() |
|
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
875 |
page_data = self.session.data.setdefault(self.pageid, {}) |
0 | 876 |
page_data[key] = value |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
877 |
self.session.data[self.pageid] = page_data |
1426 | 878 |
|
0 | 879 |
def del_page_data(self, key=None): |
880 |
"""remove value associated to `key` in current page data |
|
881 |
if `key` is None, all page data will be cleared |
|
882 |
""" |
|
883 |
if key is None: |
|
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
884 |
self.session.data.pop(self.pageid, None) |
0 | 885 |
else: |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
886 |
try: |
11339
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
887 |
page_data = self.session.data[self.pageid] |
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
888 |
del page_data[key] |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
889 |
except KeyError: |
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
890 |
pass |
11339
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
891 |
else: |
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
892 |
# make sure we write the session data value in the |
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
893 |
# self.session.data dict-like object so any session |
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
894 |
# handler can "detect" and manage the persistency |
67373614e125
[web/session] make sure not to modify an inner value of session.data (closes #11217958)
David Douard <david.douard@logilab.fr>
parents:
11172
diff
changeset
|
895 |
self.session.data[self.pageid] = page_data |
0 | 896 |
|
897 |
# user-agent detection #################################################### |
|
898 |
||
899 |
@cached |
|
900 |
def useragent(self): |
|
901 |
return self.get_header('User-Agent', None) |
|
902 |
||
903 |
def ie_browser(self): |
|
904 |
useragent = self.useragent() |
|
905 |
return useragent and 'MSIE' in useragent |
|
1426 | 906 |
|
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
907 |
@deprecated('[3.17] xhtml_browser is deprecated (xhtml is no longer served)') |
0 | 908 |
def xhtml_browser(self): |
2558
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
909 |
"""return True if the browser is considered as xhtml compatible. |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
910 |
|
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
911 |
If the instance is configured to always return text/html and not |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
912 |
application/xhtml+xml, this method will always return False, even though |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
913 |
this is semantically different |
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2556
diff
changeset
|
914 |
""" |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8889
diff
changeset
|
915 |
return False |
0 | 916 |
|
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
917 |
def html_content_type(self): |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
918 |
return 'text/html' |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1173
diff
changeset
|
919 |
|
9601
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
920 |
def set_user_language(self, user): |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
921 |
vreg = self.vreg |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
922 |
if user is not None: |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
923 |
try: |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
924 |
# 1. user-specified language |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
925 |
lang = vreg.typed_value('ui.language', user.properties['ui.language']) |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
926 |
self.set_language(lang) |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
927 |
return |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
928 |
except KeyError: |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
929 |
pass |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
930 |
if vreg.config.get('language-negociation', False): |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
931 |
# 2. http accept-language |
10003
6bcb460826cc
[web] set Vary response header to "Accept-Language" when using content negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
10001
diff
changeset
|
932 |
self.headers_out.addHeader('Vary', 'Accept-Language') |
9601
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
933 |
for lang in self.header_accept_language(): |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
934 |
if lang in self.translations: |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
935 |
self.set_language(lang) |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
936 |
return |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
937 |
# 3. site's default language |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
938 |
self.set_default_language(vreg) |
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
939 |
|
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
940 |
|
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
941 |
def _cnx_func(name): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
942 |
def proxy(req, *args, **kwargs): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
943 |
return getattr(req.cnx, name)(*args, **kwargs) |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
944 |
return proxy |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
945 |
|
10331
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
946 |
class _NeedAuthAccessMock(object): |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
947 |
|
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
948 |
def __getattribute__(self, attr): |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
949 |
raise AuthenticationError() |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
950 |
|
10690
c6290d727c0c
[py3k] __nonzero__ → __bool__
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10672
diff
changeset
|
951 |
def __bool__(self): |
10331
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
952 |
return False |
10690
c6290d727c0c
[py3k] __nonzero__ → __bool__
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10672
diff
changeset
|
953 |
|
c6290d727c0c
[py3k] __nonzero__ → __bool__
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10672
diff
changeset
|
954 |
__nonzero__ = __bool__ |
10331
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
955 |
|
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
956 |
class _MockAnonymousSession(object): |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
957 |
sessionid = 'thisisnotarealsession' |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
958 |
|
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
959 |
@property |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
960 |
def data(self): |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
961 |
return {} |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
962 |
|
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
963 |
@property |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
964 |
def anonymous_session(self): |
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
965 |
return True |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
966 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
967 |
class ConnectionCubicWebRequestBase(_CubicWebRequestBase): |
10355
60b8204fcca3
[session] all cnx._session become cnx.session
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
968 |
cnx = None |
60b8204fcca3
[session] all cnx._session become cnx.session
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
969 |
session = None |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
970 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
971 |
def __init__(self, vreg, https=False, form=None, headers={}): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
972 |
"""""" |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
973 |
self.vreg = vreg |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
974 |
try: |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
975 |
# no vreg or config which doesn't handle translations |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
976 |
self.translations = vreg.config.translations |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
977 |
except AttributeError: |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
978 |
self.translations = {} |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
979 |
super(ConnectionCubicWebRequestBase, self).__init__(vreg, https=https, |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
980 |
form=form, headers=headers) |
10331
6f25c7e4f19b
[dbapi] remove the dbapi module and its immediate remaining users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10305
diff
changeset
|
981 |
self.session = _MockAnonymousSession() |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
982 |
self.cnx = self.user = _NeedAuthAccessMock() |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
983 |
|
9774
b7b71be569cf
deprecate get/set_shared_data API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9601
diff
changeset
|
984 |
@property |
b7b71be569cf
deprecate get/set_shared_data API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9601
diff
changeset
|
985 |
def transaction_data(self): |
b7b71be569cf
deprecate get/set_shared_data API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9601
diff
changeset
|
986 |
return self.cnx.transaction_data |
b7b71be569cf
deprecate get/set_shared_data API
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9601
diff
changeset
|
987 |
|
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
988 |
def set_cnx(self, cnx): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
989 |
self.cnx = cnx |
10355
60b8204fcca3
[session] all cnx._session become cnx.session
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
990 |
self.session = cnx.session |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
991 |
self._set_user(cnx.user) |
9601
e5a80bd337e8
[web] fix language negotiation
Julien Cristau <julien.cristau@logilab.fr>
parents:
9582
diff
changeset
|
992 |
self.set_user_language(cnx.user) |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
993 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
994 |
def execute(self, *args, **kwargs): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
995 |
rset = self.cnx.execute(*args, **kwargs) |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
996 |
rset.req = self |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
997 |
return rset |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
998 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
999 |
def set_default_language(self, vreg): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1000 |
try: |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1001 |
lang = vreg.property_value('ui.language') |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1002 |
except Exception: # property may not be registered |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1003 |
lang = 'en' |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1004 |
try: |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1005 |
self.set_language(lang) |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1006 |
except KeyError: |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1007 |
# this occurs usually during test execution |
10672
f6f425a54a8d
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10671
diff
changeset
|
1008 |
self._ = self.__ = text_type |
f6f425a54a8d
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10671
diff
changeset
|
1009 |
self.pgettext = lambda x, y: text_type(y) |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1010 |
|
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9402
diff
changeset
|
1011 |
entity_metas = _cnx_func('entity_metas') |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1012 |
source_defs = _cnx_func('source_defs') |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1013 |
get_shared_data = _cnx_func('get_shared_data') |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1014 |
set_shared_data = _cnx_func('set_shared_data') |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9402
diff
changeset
|
1015 |
describe = _cnx_func('describe') # deprecated XXX |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1016 |
|
10079
962324edd6de
[web/request] add security_enabled method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10006
diff
changeset
|
1017 |
# security ################################################################# |
962324edd6de
[web/request] add security_enabled method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10006
diff
changeset
|
1018 |
|
962324edd6de
[web/request] add security_enabled method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10006
diff
changeset
|
1019 |
security_enabled = _cnx_func('security_enabled') |
962324edd6de
[web/request] add security_enabled method
Julien Cristau <julien.cristau@logilab.fr>
parents:
10006
diff
changeset
|
1020 |
|
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1021 |
# server-side service call ################################################# |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1022 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1023 |
def call_service(self, regid, **kwargs): |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1024 |
return self.cnx.call_service(regid, **kwargs) |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1025 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1026 |
# entities cache management ############################################### |
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1027 |
|
10997
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1028 |
def entity_cache(self, eid): |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1029 |
return self.transaction_data['req_ecache'][eid] |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1030 |
|
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1031 |
def set_entity_cache(self, entity): |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1032 |
ecache = self.transaction_data.setdefault('req_ecache', {}) |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1033 |
ecache.setdefault(entity.eid, entity) |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1034 |
|
10997
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1035 |
def cached_entities(self): |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1036 |
return self.transaction_data.get('req_ecache', {}).values() |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1037 |
|
10997
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1038 |
def drop_entity_cache(self, eid=None): |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1039 |
if eid is None: |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1040 |
self.transaction_data.pop('req_ecache', None) |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1041 |
else: |
da712d3f0601
Bring back the separate web-side entity cache
Julien Cristau <julien.cristau@logilab.fr>
parents:
10932
diff
changeset
|
1042 |
del self.transaction_data['req_ecache'][eid] |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1043 |
|
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1044 |
|
9071
46885bfa4150
Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9063
diff
changeset
|
1045 |
CubicWebRequestBase = ConnectionCubicWebRequestBase |
5444
f7fdb5dd82f6
[webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
1046 |
|
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1047 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1048 |
## HTTP-accept parsers / utilies ############################################## |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1049 |
def _mimetype_sort_key(accept_info): |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1050 |
"""accepted mimetypes must be sorted by : |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1051 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1052 |
1/ highest score first |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1053 |
2/ most specific mimetype first, e.g. : |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1054 |
- 'text/html level=1' is more specific 'text/html' |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1055 |
- 'text/html' is more specific than 'text/*' |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1056 |
- 'text/*' itself more specific than '*/*' |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1057 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1058 |
""" |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1059 |
raw_value, (media_type, media_subtype, media_type_params), score = accept_info |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1060 |
# FIXME: handle '+' in media_subtype ? (should xhtml+xml have a |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1061 |
# higher precedence than xml ?) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1062 |
if media_subtype == '*': |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1063 |
score -= 0.0001 |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1064 |
if media_type == '*': |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1065 |
score -= 0.0001 |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1066 |
return 1./score, media_type, media_subtype, 1./(1+len(media_type_params)) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1067 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1068 |
def _charset_sort_key(accept_info): |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1069 |
"""accepted mimetypes must be sorted by : |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1070 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1071 |
1/ highest score first |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1072 |
2/ most specific charset first, e.g. : |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1073 |
- 'utf-8' is more specific than '*' |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1074 |
""" |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1075 |
raw_value, value, score = accept_info |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1076 |
if value == '*': |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1077 |
score -= 0.0001 |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1078 |
return 1./score, value |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1079 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1080 |
def _parse_accept_header(raw_header, value_parser=None, value_sort_key=None): |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1081 |
"""returns an ordered list accepted types |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1082 |
|
7273
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1083 |
:param value_parser: a function to parse a raw accept chunk. If None |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1084 |
is provided, the function defaults to identity. If a function is provided, |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1085 |
it must accept 2 parameters ``value`` and ``other_params``. ``value`` is |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1086 |
the value found before the first ';', `other_params` is a dictionary |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1087 |
built from all other chunks after this first ';' |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1088 |
|
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1089 |
:param value_sort_key: a key function to sort values found in the accept |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1090 |
header. This function will be passed a 3-tuple |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1091 |
(raw_value, parsed_value, score). If None is provided, the default |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1092 |
sort_key is 1./score |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1093 |
|
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1094 |
:return: a list of 3-tuple (raw_value, parsed_value, score), |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1095 |
ordered by score. ``parsed_value`` will be the return value of |
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1096 |
``value_parser(raw_value)`` |
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1097 |
""" |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1098 |
if value_sort_key is None: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1099 |
value_sort_key = lambda infos: 1./infos[-1] |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1100 |
values = [] |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1101 |
for info in raw_header.split(','): |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1102 |
score = 1.0 |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1103 |
other_params = {} |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1104 |
try: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1105 |
value, infodef = info.split(';', 1) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1106 |
except ValueError: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1107 |
value = info |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1108 |
else: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1109 |
for info in infodef.split(';'): |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1110 |
try: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1111 |
infokey, infoval = info.split('=') |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1112 |
if infokey == 'q': # XXX 'level' |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1113 |
score = float(infoval) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1114 |
continue |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1115 |
except ValueError: |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1116 |
continue |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1117 |
other_params[infokey] = infoval |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1118 |
parsed_value = value_parser(value, other_params) if value_parser else value |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1119 |
values.append( (value.strip(), parsed_value, score) ) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1120 |
values.sort(key=value_sort_key) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1121 |
return values |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1122 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1123 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1124 |
def _mimetype_parser(value, other_params): |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1125 |
"""return a 3-tuple |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1126 |
(type, subtype, type_params) corresponding to the mimetype definition |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1127 |
e.g. : for 'text/*', `mimetypeinfo` will be ('text', '*', {}), for |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1128 |
'text/html;level=1', `mimetypeinfo` will be ('text', '*', {'level': '1'}) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1129 |
""" |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1130 |
try: |
7273
a949fc438029
[etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7187
diff
changeset
|
1131 |
media_type, media_subtype = value.strip().split('/', 1) |
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1132 |
except ValueError: # safety belt : '/' should always be present |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1133 |
media_type = value.strip() |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1134 |
media_subtype = '*' |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1135 |
return (media_type, media_subtype, other_params) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1136 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1137 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1138 |
ACCEPT_HEADER_PARSER = { |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1139 |
'accept': (_mimetype_parser, _mimetype_sort_key), |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1140 |
'accept-charset': (None, _charset_sort_key), |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1141 |
} |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7070
diff
changeset
|
1142 |
|
0 | 1143 |
from cubicweb import set_log_methods |
9063
383d45cf61fa
[webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9041
diff
changeset
|
1144 |
set_log_methods(_CubicWebRequestBase, LOGGER) |