1 # copyright 2003-2016 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 from six import text_type |
|
21 |
|
22 from cubicweb.web.request import CubicWebRequestBase |
|
23 |
|
24 |
|
25 class CubicWebTwistedRequestAdapter(CubicWebRequestBase): |
|
26 """ from twisted .req to cubicweb .form |
|
27 req.files are put into .form[<filefield>] |
|
28 """ |
|
29 def __init__(self, req, vreg): |
|
30 self._twreq = req |
|
31 super(CubicWebTwistedRequestAdapter, self).__init__( |
|
32 vreg, req.args, headers=req.received_headers) |
|
33 for key, name_stream_list in req.files.items(): |
|
34 for name, stream in name_stream_list: |
|
35 if name is not None: |
|
36 name = text_type(name, self.encoding) |
|
37 self.form.setdefault(key, []).append((name, stream)) |
|
38 # 3.16.4 backward compat |
|
39 if len(self.form[key]) == 1: |
|
40 self.form[key] = self.form[key][0] |
|
41 self.content = self._twreq.content # stream |
|
42 |
|
43 def http_method(self): |
|
44 """returns 'POST', 'GET', 'HEAD', etc.""" |
|
45 return self._twreq.method |
|
46 |
|
47 def relative_path(self, includeparams=True): |
|
48 """return the normalized path of the request (ie at least relative to |
|
49 the instance's root, but some other normalization may be needed so that |
|
50 the returned path may be used to compare to generated urls |
|
51 |
|
52 :param includeparams: |
|
53 boolean indicating if GET form parameters should be kept in the path |
|
54 """ |
|
55 path = self._twreq.uri[1:] # remove the root '/' |
|
56 if not includeparams: |
|
57 path = path.split('?', 1)[0] |
|
58 return path |
|