httpupload.py 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim.  If not, see <http://www.gnu.org/licenses/>.

Philipp Hörist's avatar
Philipp Hörist committed
15
16
17
# XEP-0363: HTTP File Upload


18
19
import os
import io
20
from urllib.parse import urlparse
21
import mimetypes
22
import hashlib
23

24
25
from nbxmpp import NS_HTTPUPLOAD_0
from nbxmpp.util import is_error_result
26
from gi.repository import GLib
27
from gi.repository import Soup
28
29

from gajim.common import app
30
from gajim.common.i18n import _
31
from gajim.common.helpers import get_tls_error_phrase
32
from gajim.common.helpers import get_user_proxy
Philipp Hörist's avatar
Philipp Hörist committed
33
from gajim.common.const import FTState
Philipp Hörist's avatar
Philipp Hörist committed
34
from gajim.common.filetransfer import FileTransfer
Philipp Hörist's avatar
Philipp Hörist committed
35
from gajim.common.modules.base import BaseModule
Philipp Hörist's avatar
Philipp Hörist committed
36
from gajim.common.structs import OutgoingMessage
37
38
from gajim.common.connection_handlers_events import InformationEvent

Philipp Hörist's avatar
Philipp Hörist committed
39

Philipp Hörist's avatar
Philipp Hörist committed
40
class HTTPUpload(BaseModule):
41
42
43

    _nbxmpp_extends = 'HTTPUpload'

Philipp Hörist's avatar
Philipp Hörist committed
44
    def __init__(self, con):
Philipp Hörist's avatar
Philipp Hörist committed
45
        BaseModule.__init__(self, con)
Philipp Hörist's avatar
Philipp Hörist committed
46
47

        self.available = False
48
        self.component = None
Philipp Hörist's avatar
Philipp Hörist committed
49
        self.httpupload_namespace = None
50
        self.max_file_size = None  # maximum file size in bytes
51

52
        self._proxy_resolver = None
53
54
55
56
        self._queued_messages = {}
        self._session = Soup.Session()
        self._session.props.ssl_strict = False
        self._session.props.user_agent = 'Gajim %s' % app.version
57

58
59
60
61
62
63
64
65
66
    def _set_proxy_if_available(self):
        proxy = get_user_proxy(self._account)
        if proxy is None:
            self._proxy_resolver = None
            self._session.props.proxy_resolver = None
        else:
            self._proxy_resolver = proxy.get_resolver()
            self._session.props.proxy_resolver = self._proxy_resolver

67
    def pass_disco(self, info):
Philipp Hörist's avatar
Philipp Hörist committed
68
        if not info.has_httpupload:
69
70
            return

Philipp Hörist's avatar
Philipp Hörist committed
71
72
        self.available = True
        self.httpupload_namespace = NS_HTTPUPLOAD_0
73
        self.component = info.jid
Philipp Hörist's avatar
Philipp Hörist committed
74
        self.max_file_size = info.httpupload_max_file_size
75

Philipp Hörist's avatar
Philipp Hörist committed
76
        self._log.info('Discovered component: %s', info.jid)
77
78

        if self.max_file_size is None:
Philipp Hörist's avatar
Philipp Hörist committed
79
            self._log.warning('Component does not provide maximum file size')
80
        else:
Philipp Hörist's avatar
Philipp Hörist committed
81
82
83
            size = GLib.format_size_full(self.max_file_size,
                                         GLib.FormatSizeFlags.IEC_UNITS)
            self._log.info('Component has a maximum file size of: %s', size)
84

Philipp Hörist's avatar
Philipp Hörist committed
85
        for ctrl in app.interface.msg_win_mgr.get_controls(acct=self._account):
86
87
            ctrl.update_actions()

88
    def check_file_before_transfer(self, path, encryption, contact,
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                                   groupchat=False):
        if not path or not os.path.exists(path):
            return

        invalid_file = False
        stat = os.stat(path)

        if os.path.isfile(path):
            if stat[6] == 0:
                invalid_file = True
                msg = _('File is empty')
        else:
            invalid_file = True
            msg = _('File does not exist')

        if self.max_file_size is not None and \
                stat.st_size > self.max_file_size:
            invalid_file = True
            size = GLib.format_size_full(self.max_file_size,
                                         GLib.FormatSizeFlags.IEC_UNITS)
            msg = _('File is too large, '
                    'maximum allowed file size is: %s') % size

        if invalid_file:
113
            self._raise_information_event('open-file-error2', msg)
114
115
116
117
118
            return

        mime = mimetypes.MimeTypes().guess_type(path)[0]
        if not mime:
            mime = 'application/octet-stream'  # fallback mime type
Philipp Hörist's avatar
Philipp Hörist committed
119
        self._log.info("Detected MIME type of file: %s", mime)
120
121

        try:
Philipp Hörist's avatar
Philipp Hörist committed
122
123
124
            transfer = HTTPFileTransfer(self._account,
                                        self._cancel_upload,
                                        path,
Philipp Hörist's avatar
Philipp Hörist committed
125
126
127
128
129
                                        contact,
                                        mime,
                                        encryption,
                                        groupchat)
            app.interface.show_httpupload_progress(transfer)
130
        except Exception as error:
Philipp Hörist's avatar
Philipp Hörist committed
131
            self._log.exception('Error while loading file')
132
            self._raise_information_event('open-file-error2', str(error))
133
134
135
            return

        if encryption is not None:
Philipp Hörist's avatar
Philipp Hörist committed
136
137
138
            app.interface.encrypt_file(transfer,
                                       self._account,
                                       self._request_slot)
139
        else:
Philipp Hörist's avatar
Philipp Hörist committed
140
            self._request_slot(transfer)
141

Philipp Hörist's avatar
Philipp Hörist committed
142
    def _cancel_upload(self, transfer):
Philipp Hörist's avatar
Philipp Hörist committed
143
        message = self._queued_messages.get(id(transfer))
144
145
146
147
        if message is None:
            return
        self._session.cancel_message(message, Soup.Status.CANCELLED)

148
    @staticmethod
149
    def _raise_information_event(dialog_name, args=None):
150
151
152
        app.nec.push_incoming_event(InformationEvent(
            None, dialog_name=dialog_name, args=args))

Philipp Hörist's avatar
Philipp Hörist committed
153
154
    def _request_slot(self, transfer):
        transfer.set_preparing()
155
156
157
        self._log.info('Sending request for slot')
        self._nbxmpp('HTTPUpload').request_slot(
            jid=self.component,
Philipp Hörist's avatar
Philipp Hörist committed
158
            filename=transfer.filename,
159
160
161
162
163
164
165
166
            size=transfer.size,
            content_type=transfer.mime,
            callback=self._received_slot,
            user_data=transfer)

    def _received_slot(self, result, transfer):
        if is_error_result(result):
            transfer.set_error()
167

168
169
170
171
            if result.app_condition == 'file-too-large':
                size_text = GLib.format_size_full(
                    result.get_max_file_size(),
                    GLib.FormatSizeFlags.IEC_UNITS)
172

173
174
175
176
177
                error_text = _('File is too large, '
                               'maximum allowed file size is: %s' % size_text)
            else:
                error_text = str(result)
                self._log.warning(result)
178

179
            self._raise_information_event('request-upload-slot-error',
180
                                          error_text)
181
182
            return

183
        transfer.process_result(result)
184

Philipp Hörist's avatar
Philipp Hörist committed
185
186
187
        if (urlparse(transfer.put_uri).scheme != 'https' or
                urlparse(transfer.get_uri).scheme != 'https'):
            transfer.set_error()
188
            self._raise_information_event('unsecure-error')
189
190
            return

Philipp Hörist's avatar
Philipp Hörist committed
191
192
        self._log.info('Uploading file to %s', transfer.put_uri)
        self._log.info('Please download from %s', transfer.get_uri)
193

Philipp Hörist's avatar
Philipp Hörist committed
194
        self._upload_file(transfer)
195

Philipp Hörist's avatar
Philipp Hörist committed
196
197
    def _upload_file(self, transfer):
        transfer.set_started()
198

Philipp Hörist's avatar
Philipp Hörist committed
199
        message = Soup.Message.new('PUT', transfer.put_uri)
200
201
202
203
        message.connect('starting', self._check_certificate)

        # Set CAN_REBUILD so chunks get discarded after they are beeing
        # written to the network
204
205
206
        message.set_flags(Soup.MessageFlags.CAN_REBUILD |
                          Soup.MessageFlags.NO_REDIRECT)

207
208
        message.props.request_body.set_accumulate(False)

Philipp Hörist's avatar
Philipp Hörist committed
209
210
211
        message.props.request_headers.set_content_type(transfer.mime, None)
        message.props.request_headers.set_content_length(transfer.size)
        for name, value in transfer.headers:
212
213
            message.props.request_headers.append(name, value)

Philipp Hörist's avatar
Philipp Hörist committed
214
215
        message.connect('wrote-headers', self._on_wrote_headers, transfer)
        message.connect('wrote-chunk', self._on_wrote_chunk, transfer)
216

Philipp Hörist's avatar
Philipp Hörist committed
217
        self._queued_messages[id(transfer)] = message
218
        self._set_proxy_if_available()
Philipp Hörist's avatar
Philipp Hörist committed
219
        self._session.queue_message(message, self._on_finish, transfer)
220
221
222
223
224
225

    def _check_certificate(self, message):
        https_used, _tls_certificate, tls_errors = message.get_https_status()
        if not https_used:
            self._log.warning('HTTPS was not used for upload')
            self._session.cancel_message(message, Soup.Status.CANCELLED)
226
227
            return

228
229
230
231
        if not app.config.get_per('accounts',
                                  self._account,
                                  'httpupload_verify'):
            return
232

233
234
        if tls_errors:
            phrase = get_tls_error_phrase(tls_errors)
235
            self._log.warning('TLS verification failed: %s', phrase)
236
237
238
            self._session.cancel_message(message, Soup.Status.CANCELLED)
            self._raise_information_event('httpupload-error', phrase)
            return
239

Philipp Hörist's avatar
Philipp Hörist committed
240
241
242
    def _on_finish(self, _session, message, transfer):
        self._queued_messages.pop(id(transfer), None)
        transfer.set_finished()
243
244
245

        if message.props.status_code == Soup.Status.CANCELLED:
            self._log.info('Upload cancelled')
246
247
            return

248
249
        if message.props.status_code in (Soup.Status.OK, Soup.Status.CREATED):
            self._log.info('Upload completed successfully')
Philipp Hörist's avatar
Philipp Hörist committed
250
            uri = transfer.get_transformed_uri()
251

Philipp Hörist's avatar
Philipp Hörist committed
252
            type_ = 'chat'
Philipp Hörist's avatar
Philipp Hörist committed
253
            if transfer.is_groupchat:
Philipp Hörist's avatar
Philipp Hörist committed
254
255
256
                type_ = 'groupchat'

            message = OutgoingMessage(account=self._account,
257
                                      contact=transfer.contact,
Philipp Hörist's avatar
Philipp Hörist committed
258
259
                                      message=uri,
                                      type_=type_,
260
                                      oob_url=uri)
Philipp Hörist's avatar
Philipp Hörist committed
261
262

            self._con.send_message(message)
263
264

        else:
265
            phrase = Soup.Status.get_phrase(message.props.status_code)
Philipp Hörist's avatar
Philipp Hörist committed
266
            self._log.error('Got unexpected http upload response code: %s',
267
268
                            phrase)
            self._raise_information_event('httpupload-response-error', phrase)
269

Philipp Hörist's avatar
Philipp Hörist committed
270
271
272
    def _on_wrote_chunk(self, message, transfer):
        transfer.update_progress()
        if transfer.is_complete:
273
274
275
            message.props.request_body.complete()
            return

Philipp Hörist's avatar
Philipp Hörist committed
276
        bytes_ = transfer.get_chunk()
277
278
279
280
281
282
283
284
        self._session.pause_message(message)
        GLib.idle_add(self._append, message, bytes_)

    def _append(self, message, bytes_):
        if message.props.status_code == Soup.Status.CANCELLED:
            return
        self._session.unpause_message(message)
        message.props.request_body.append(bytes_)
285
286

    @staticmethod
Philipp Hörist's avatar
Philipp Hörist committed
287
288
    def _on_wrote_headers(message, transfer):
        message.props.request_body.append(transfer.get_chunk())
289
290


Philipp Hörist's avatar
Philipp Hörist committed
291
class HTTPFileTransfer(FileTransfer):
Philipp Hörist's avatar
Philipp Hörist committed
292
293
294
295
296
297
298

    _state_descriptions = {
        FTState.ENCRYPTING: _('Encrypting file…'),
        FTState.PREPARING: _('Requesting HTTP File Upload Slot…'),
        FTState.STARTED: _('Uploading via HTTP File Upload…'),
    }

299
    def __init__(self,
Philipp Hörist's avatar
Philipp Hörist committed
300
301
                 account,
                 cancel_func,
302
303
304
305
306
                 path,
                 contact,
                 mime,
                 encryption,
                 groupchat):
Philipp Hörist's avatar
Philipp Hörist committed
307
        FileTransfer.__init__(self, account, cancel_func=cancel_func)
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

        self._path = path
        self._encryption = encryption
        self._groupchat = groupchat
        self._contact = contact
        self._mime = mime

        self.size = os.stat(path).st_size
        self.put_uri = None
        self.get_uri = None
        self._uri_transform_func = None

        self._stream = None
        self._data = None
        self._headers = {}
323

324
325
326
    @property
    def mime(self):
        return self._mime
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    @property
    def contact(self):
        return self._contact

    @property
    def is_groupchat(self):
        return self._groupchat

    @property
    def encryption(self):
        return self._encryption

    @property
    def headers(self):
        return self._headers
343

Philipp Hörist's avatar
Philipp Hörist committed
344
345
346
347
    @property
    def path(self):
        return self._path

348
349
350
351
    def get_transformed_uri(self):
        if self._uri_transform_func is not None:
            return self._uri_transform_func(self.get_uri)
        return self.get_uri
352

353
354
355
    def set_uri_transform_func(self, func):
        self._uri_transform_func = func

Philipp Hörist's avatar
Philipp Hörist committed
356
357
    @property
    def filename(self):
358
        basename = os.path.basename(self._path)
Raleigh's avatar
Raleigh committed
359
        if app.config.get_per('accounts',
360
361
                              self._account,
                              'httpupload_obf_fn'):
Raleigh's avatar
Raleigh committed
362
363
            basename = hashlib.sha1(basename.encode('utf-8')).hexdigest()
        return basename
Philipp Hörist's avatar
Philipp Hörist committed
364

Philipp Hörist's avatar
Philipp Hörist committed
365
366
367
368
    def set_error(self, text=''):
        self._close()
        super().set_error(text)

369
370
    def set_finished(self):
        self._close()
Philipp Hörist's avatar
Philipp Hörist committed
371
        super().set_finished()
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

    def set_encrypted_data(self, data):
        self._data = data

    def _close(self):
        if self._stream is not None:
            self._stream.close()

    def get_chunk(self):
        if self._stream is None:
            if self._encryption is None:
                self._stream = open(self._path, 'rb')
            else:
                self._stream = io.BytesIO(self._data)

        data = self._stream.read(16384)
        if not data:
            self._close()
            return None
        self._seen += len(data)
        if self.is_complete:
            self._close()
        return data

    def get_data(self):
        with open(self._path, 'rb') as file:
            data = file.read()
        return data
Philipp Hörist's avatar
Philipp Hörist committed
400

401
402
403
404
405
    def process_result(self, result):
        self.put_uri = result.put_uri
        self.get_uri = result.get_uri
        self._headers = result.headers

Philipp Hörist's avatar
Philipp Hörist committed
406

407
408
def get_instance(*args, **kwargs):
    return HTTPUpload(*args, **kwargs), 'HTTPUpload'