File: xso.py

package info (click to toggle)
python-aioxmpp 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,152 kB
  • sloc: python: 96,969; xml: 215; makefile: 155; sh: 72
file content (194 lines) | stat: -rw-r--r-- 5,053 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
########################################################################
# File name: xso.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import aioxmpp.stanza
import aioxmpp.xso

from aioxmpp.utils import namespaces

namespaces.xep0363_http_upload = "urn:xmpp:http:upload:0"


@aioxmpp.IQ.as_payload_class
class Request(aioxmpp.xso.XSO):
    """
    XSO to request an upload slot from the server.

    The parameters initialise the attributes below.

    .. attribute:: filename
        :annotation: : str

        The file name (without path, but possibly with "extension") of the
        file to upload. The server MAY use this in the URL.


    .. attribute:: size
        :annotation: : int

        The size of the file in bytes. This must be accurate and MUST also
        be used as ``Content-Length`` header in the PUT request.

    .. attribute:: content_type
        :annotation: : str

        The MIME type of the file. This MUST be set in the PUT request as
        ``Content-Type`` header.
    """

    TAG = namespaces.xep0363_http_upload, "request"

    filename = aioxmpp.xso.Attr("filename")

    size = aioxmpp.xso.Attr(
        "size",
        type_=aioxmpp.xso.Integer(),
    )

    content_type = aioxmpp.xso.Attr("content-type")

    def __init__(self, filename, size, content_type):
        super().__init__()
        self.filename = filename
        self.size = size
        self.content_type = content_type


class Header(aioxmpp.xso.XSO):
    TAG = namespaces.xep0363_http_upload, "header"

    name = aioxmpp.xso.Attr("name")

    value = aioxmpp.xso.Text()


class HeaderType(aioxmpp.xso.AbstractElementType):
    @staticmethod
    def get_xso_types():
        return (Header,)

    @classmethod
    def unpack(self, header_xso):
        return header_xso.name, header_xso.value

    @classmethod
    def pack(self, t):
        header_xso = Header()
        header_xso.name, header_xso.value = t
        return header_xso


class Put(aioxmpp.xso.XSO):
    """
    .. attribute:: url
        :annotation: : str

        The URL against which the PUT request must be made.

    .. attribute:: headers
        :annotation: : multidict.MultiDict

        The headers which MUST be used in the PUT request as
        :class:`multidict.MultiDict`, in addition to the ``Content-Type``
        and ``Content-Length`` headers.

        The headers are already sanitised according to :xep:`363` (see also
        :attr:`HEADER_WHITELIST`).

    .. attribute:: HEADER_WHITELIST

        This *class attribute* holds the list of headers which are allowed to
        be used by the server. This defaults to the list specified in
        :xep:`363`.

        .. warning::

            Changing the list of allowed headers may have unintended security
            implications.
    """

    HEADER_WHITELIST = (
        "Authorization",
        "Expires",
        "Cookie",
    )

    TAG = namespaces.xep0363_http_upload, "put"

    url = aioxmpp.xso.Attr("url")

    headers = aioxmpp.xso.ChildValueMultiMap(
        HeaderType
    )

    def xso_after_load(self):
        whitelist = self.HEADER_WHITELIST
        headers = list(self.headers.items())
        self.headers.clear()

        for key, value in headers:
            if key not in whitelist:
                continue
            value = value.replace("\n", "")
            self.headers.add(key, value)


class Get(aioxmpp.xso.XSO):
    """
    .. attribute:: url
        :annotation: : str

        The URL at which the file can be retrieved after uploading.
    """

    TAG = namespaces.xep0363_http_upload, "get"

    url = aioxmpp.xso.Attr("url")


@aioxmpp.IQ.as_payload_class
class Slot(aioxmpp.xso.XSO):
    """
    XSO representing the an upload slot provided by the server.

    .. attribute:: get

        Information about the GET request for the slot as :class:`.Get` XSO.

    .. attribute:: put

        Information about the PUT request for the slot as :class:`.Put` XSO.
    """

    TAG = namespaces.xep0363_http_upload, "slot"

    put = aioxmpp.xso.Child([Put])

    get = aioxmpp.xso.Child([Get])

    def validate(self):
        super().validate()

        if self.put is None:
            raise ValueError("missing PUT information")

        if self.get is None:
            raise ValueError("missing GET information")