|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2018-2021 Greenbone Networks GmbH |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
|
5
|
|
|
# |
|
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
# it under the terms of the GNU General Public License as published by |
|
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
# (at your option) any later version. |
|
10
|
|
|
# |
|
11
|
|
|
# This program is distributed in the hope that it will be useful, |
|
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
# GNU General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the GNU General Public License |
|
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
""" |
|
19
|
|
|
Module for transforming responses |
|
20
|
|
|
""" |
|
21
|
|
|
from lxml import etree |
|
22
|
|
|
|
|
23
|
|
|
from .errors import GvmError, GvmServerError, GvmResponseError |
|
24
|
|
|
from .xml import create_parser |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class EtreeTransform: |
|
28
|
|
|
""" |
|
29
|
|
|
Transform a response into a lxml.etree root element |
|
30
|
|
|
""" |
|
31
|
|
|
|
|
32
|
|
|
def __init__(self): |
|
33
|
|
|
self._parser = create_parser() |
|
34
|
|
|
|
|
35
|
|
|
def _convert_response(self, response: str) -> etree.Element: |
|
36
|
|
|
return etree.XML(response, parser=self._parser) |
|
37
|
|
|
|
|
38
|
|
|
def __call__(self, response: str) -> etree.Element: |
|
39
|
|
|
return self._convert_response(response) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def check_command_status(root: etree.Element): |
|
43
|
|
|
status = root.get("status") |
|
44
|
|
|
|
|
45
|
|
|
if status is None: |
|
46
|
|
|
raise GvmServerError("No status in response.", root) |
|
47
|
|
|
|
|
48
|
|
|
if status[0] == "4": |
|
49
|
|
|
raise GvmResponseError(status=status, message=root.get("status_text")) |
|
50
|
|
|
elif status[0] == "5": |
|
51
|
|
|
raise GvmServerError(status=status, message=root.get("status_text")) |
|
52
|
|
|
elif status[0] != "2": |
|
53
|
|
|
raise GvmError( |
|
54
|
|
|
"Error in response. {0}".format(root.get("status_text")), root |
|
55
|
|
|
) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
class CheckCommandTransform(EtreeTransform): |
|
59
|
|
|
""" |
|
60
|
|
|
Check the response code of a response and raise GmpError if |
|
61
|
|
|
response was an error response |
|
62
|
|
|
""" |
|
63
|
|
|
|
|
64
|
|
|
def __call__(self, response: str) -> str: |
|
65
|
|
|
root = self._convert_response(response) |
|
66
|
|
|
|
|
67
|
|
|
check_command_status(root) |
|
68
|
|
|
|
|
69
|
|
|
return response |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
class EtreeCheckCommandTransform(EtreeTransform): |
|
73
|
|
|
""" |
|
74
|
|
|
Transform a response into a lxml.etree root element and raise GmpError if |
|
75
|
|
|
response was an error response |
|
76
|
|
|
""" |
|
77
|
|
|
|
|
78
|
|
|
def __call__(self, response: str) -> etree.Element: |
|
79
|
|
|
root = self._convert_response(response) |
|
80
|
|
|
|
|
81
|
|
|
check_command_status(root) |
|
82
|
|
|
|
|
83
|
|
|
return root |
|
84
|
|
|
|