Mercurial > libervia-web
annotate libervia/pages/_bridge/page_meta.py @ 1422:027f5059e1e2
pages (lists/view_item): browser part was not in the repository
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 01 May 2021 19:05:05 +0200 |
parents | b1215347b5c3 |
children | 7472d5a88006 |
rev | line source |
---|---|
1288 | 1 #!/usr/bin/env python3 |
2 | |
3 import tempfile | |
4 import os | |
5 import os.path | |
6 import json | |
7 from twisted.internet import defer | |
8 from twisted.web import static | |
9 from sat.core.i18n import _ | |
10 from sat.core.log import getLogger | |
1296
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
11 from sat_frontends.bridge.bridge_frontend import BridgeException |
1288 | 12 from libervia.server.constants import Const as C |
13 from libervia.server.utils import ProgressHandler | |
14 | |
15 | |
16 log = getLogger(__name__) | |
17 """access to restricted bridge""" | |
18 | |
19 name = "bridge" | |
20 on_data_post = "continue" | |
21 | |
22 | |
23 def parse_url(self, request): | |
24 self.getPathArgs(request, ["method_name"], min_args=1) | |
25 | |
26 | |
27 async def render(self, request): | |
28 if request.method != b'POST': | |
29 log.warning(f"Bad method used with _bridge endpoint: {request.method.decode()}") | |
30 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
31 data = self.getRData(request) | |
32 profile = self.getProfile(request) | |
33 if profile is None: | |
34 log.warning("_bridge endpoint accessed without authorisation") | |
35 return self.pageError(request, C.HTTP_UNAUTHORIZED) | |
36 self.checkCSRF(request) | |
37 method_name = data["method_name"] | |
38 method_data = json.load(request.content) | |
39 try: | |
40 bridge_method = getattr(self.host.restricted_bridge, method_name) | |
41 except AttributeError: | |
42 log.warning(_( | |
43 "{profile!r} is trying to access a bridge method not implemented in " | |
44 "RestrictedBridge: {method_name}").format( | |
45 profile=profile, method_name=method_name)) | |
46 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
47 | |
48 try: | |
49 args, kwargs = method_data['args'], method_data['kwargs'] | |
50 except KeyError: | |
51 log.warning(_( | |
52 "{profile!r} has sent a badly formatted method call: {method_data}" | |
53 ).format(profile=profile, method_data=method_data)) | |
54 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
55 | |
1296
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
56 if "profile" in kwargs or "profile_key" in kwargs: |
1288 | 57 log.warning(_( |
58 '"profile" key should not be in method kwargs, hack attempt? ' | |
59 "profile={profile}, method_data={method_data}" | |
60 ).format(profile=profile, method_data=method_data)) | |
61 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
62 | |
1296
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
63 try: |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
64 ret = await bridge_method(*args, **kwargs, profile=profile) |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
65 except BridgeException as e: |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
66 request.setResponseCode(C.HTTP_PROXY_ERROR) |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
67 ret = { |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
68 "fullname": e.fullname, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
69 "message": e.message, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
70 "condition": e.condition, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
71 "module": e.module, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
72 "classname": e.classname, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
73 } |
1288 | 74 return json.dumps(ret) |