1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """Twisted request handler for CubicWeb""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 |
|
22 |
|
23 from cubicweb.web.request import CubicWebRequestBase |
|
24 |
|
25 |
|
26 class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
27 """ from twisted .req to cubicweb .form |
|
28 req.files are put into .form[<filefield>] |
|
29 """ |
|
30 def __init__(self, req, vreg, https): |
|
31 self._twreq = req |
|
32 super(CubicWebTwistedRequestAdapter, self).__init__( |
|
33 vreg, https, req.args, headers=req.received_headers) |
|
34 for key, name_stream_list in req.files.items(): |
|
35 for name, stream in name_stream_list: |
|
36 if name is not None: |
|
37 name = unicode(name, self.encoding) |
|
38 self.form.setdefault(key, []).append((name, stream)) |
|
39 # 3.16.4 backward compat |
|
40 if len(self.form[key]) == 1: |
|
41 self.form[key] = self.form[key][0] |
|
42 self.content = self._twreq.content # stream |
|
43 |
|
44 def http_method(self): |
|
45 """returns 'POST', 'GET', 'HEAD', etc.""" |
|
46 return self._twreq.method |
|
47 |
|
48 def relative_path(self, includeparams=True): |
|
49 """return the normalized path of the request (ie at least relative to |
|
50 the instance's root, but some other normalization may be needed so that |
|
51 the returned path may be used to compare to generated urls |
|
52 |
|
53 :param includeparams: |
|
54 boolean indicating if GET form parameters should be kept in the path |
|
55 """ |
|
56 path = self._twreq.uri[1:] # remove the root '/' |
|
57 if not includeparams: |
|
58 path = path.split('?', 1)[0] |
|
59 return path |
|