File: b2http.py

package info (click to toggle)
python-b2sdk 2.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,228 kB
  • sloc: python: 32,094; sh: 13; makefile: 8
file content (634 lines) | stat: -rw-r--r-- 22,258 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
######################################################################
#
# File: b2sdk/_internal/b2http.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

import datetime
import io
import json
import locale
import logging
import socket
import threading
import time
from contextlib import contextmanager
from random import random
from typing import Any, Callable

try:
    from typing_extensions import Literal
except ImportError:
    from typing import Literal

import requests
from requests.adapters import HTTPAdapter

from b2sdk.version import USER_AGENT

from .api_config import DEFAULT_HTTP_API_CONFIG, B2HttpApiConfig
from .exception import (
    B2ConnectionError,
    B2Error,
    B2RequestTimeout,
    B2RequestTimeoutDuringUpload,
    BadDateFormat,
    BrokenPipe,
    ClockSkew,
    ConnectionReset,
    PotentialS3EndpointPassedAsRealm,
    UnknownError,
    UnknownHost,
    interpret_b2_error,
)
from .requests import NotDecompressingResponse
from .utils.typing import JSON

LOCALE_LOCK = threading.Lock()
logger = logging.getLogger(__name__)


def _print_exception(e, indent=''):
    """
    Used for debugging to print out nested exception structures.

    :param str indent: message prefix
    """
    print(indent + 'EXCEPTION', repr(e))
    print(indent + 'CLASS', type(e))
    for i, a in enumerate(e.args):
        print(indent + 'ARG %d: %s' % (i, repr(a)))
        if isinstance(a, Exception):
            _print_exception(a, indent + '        ')


@contextmanager
def setlocale(name):
    with LOCALE_LOCK:
        saved = locale.setlocale(locale.LC_ALL)
        try:
            yield locale.setlocale(locale.LC_ALL, name)
        finally:
            locale.setlocale(locale.LC_ALL, saved)


class ResponseContextManager:
    """
    A context manager that closes a requests.Response when done.
    """

    def __init__(self, response):
        self.response = response

    def __enter__(self):
        return self.response

    def __exit__(self, exc_type, exc_val, exc_tb):
        return None


class HttpCallback:
    """
    A callback object that does nothing.  Overrides pre_request
    and/or post_request as desired.
    """

    def pre_request(self, method, url, headers):
        """
        Called before processing an HTTP request.

        Raises an exception if this request should not be processed.
        The exception raised must inherit from B2HttpCallbackPreRequestException.

        :param str method: str, one of: 'POST', 'GET', etc.
        :param str url: the URL that will be used
        :param dict headers: the header sent with the request

        """

    def post_request(self, method, url, headers, response):
        """
        Called after processing an HTTP request.
        Should not raise an exception.

        Raises an exception if this request should be treated as failing.
        The exception raised must inherit from B2HttpCallbackPostRequestException.

        :param str method: one of: 'POST', 'GET', etc.
        :param str url: the URL that will be used
        :param dict headers: the header sent with the request
        :param response: a response object from the requests library
        """


class ClockSkewHook(HttpCallback):
    def post_request(self, method, url, headers, response):
        """
        Raise an exception if the clock in the server is too different from the
        clock on the local host.

        The Date header contains a string that looks like: "Fri, 16 Dec 2016 20:52:30 GMT".

        :param str method: one of: 'POST', 'GET', etc.
        :param str url: the URL that will be used
        :param dict headers: the header sent with the request
        :param response: a response object from the requests library
        """
        # Make a string that uses month numbers instead of month names
        server_date_str = response.headers['Date']

        # Convert the server time to a datetime object
        try:
            with setlocale('C'):
                # "%Z" always creates naive datetimes, even though the timezone
                # is specified. https://github.com/python/cpython/issues/76678
                # Anyway, thankfully, HTTP/1.1 spec requires the string
                # to always say "GMT", and provide UTC time.
                # https://datatracker.ietf.org/doc/html/rfc2616#section-3.3.1
                server_time = datetime.datetime.strptime(
                    server_date_str, '%a, %d %b %Y %H:%M:%S GMT'
                ).replace(tzinfo=datetime.timezone.utc)
        except ValueError:
            logger.exception('server returned date in an inappropriate format')
            raise BadDateFormat(server_date_str)

        # Get the local time
        local_time = datetime.datetime.now(datetime.timezone.utc)

        # Check the difference.
        max_allowed = 10 * 60  # ten minutes, in seconds
        skew = local_time - server_time
        skew_seconds = skew.total_seconds()
        if max_allowed < abs(skew_seconds):
            raise ClockSkew(skew_seconds)


class B2Http:
    """
    A wrapper for the requests module.  Provides the operations
    needed to access B2, and handles retrying when the returned
    status is 503 Service Unavailable, 429 Too Many Requests, etc.

    The operations supported are:

    - post_json_return_json
    - post_content_return_json
    - get_content

    The methods that return JSON either return a Python dict or
    raise a subclass of B2Error.  They can be used like this:

    .. code-block:: python

       try:
           response_dict = b2_http.post_json_return_json(url, headers, params)
           ...
       except B2Error as e:
           ...

    Please note that the timeout/retry system, including class-level variables,
    is not a part of the interface and is subject to change.
    """

    CONNECTION_TIMEOUT = 3 + 6 + 12 + 24 + 1  # 4 standard tcp retransmissions + 1s latency
    TIMEOUT = 128
    TIMEOUT_FOR_COPY = 1200  # 20 minutes as server-side copy can take time
    TIMEOUT_FOR_UPLOAD = 128
    TRY_COUNT_DATA = 20
    TRY_COUNT_DOWNLOAD = 20
    TRY_COUNT_HEAD = 5
    TRY_COUNT_OTHER = 5

    def __init__(self, api_config: B2HttpApiConfig = DEFAULT_HTTP_API_CONFIG):
        """
        Initialize with a reference to the requests module, which makes
        it easy to mock for testing.
        """
        self.user_agent = self._get_user_agent(api_config.user_agent_append)
        self.session = api_config.http_session_factory()
        if not api_config.decode_content:
            self.session.adapters.clear()
            self.session.mount('', NotDecompressingHTTPAdapter())
        self.callbacks = []
        if api_config.install_clock_skew_hook:
            self.add_callback(ClockSkewHook())

    def add_callback(self, callback):
        """
        Add a callback that inherits from HttpCallback.

        :param callback: a callback to be added to a chain
        :type callback: callable
        """
        self.callbacks.append(callback)

    def request(
        self,
        method: Literal['POST', 'GET', 'HEAD'],
        url: str,
        headers: dict[str, str],
        data: io.BytesIO | bytes | None = None,
        try_count: int = TRY_COUNT_DATA,
        params: dict[str, str] | None = None,
        *,
        stream: bool = False,
        _timeout: int | None = None,
    ) -> requests.Response:
        """
        Use like this:

        .. code-block:: python

           try:
               response_dict = b2_http.request('POST', url, headers, data)
               ...
           except B2Error as e:
               ...

        :param method: uppercase HTTP method name
        :param url: a URL to call
        :param headers: headers to send.
        :param data: raw bytes or a file-like object to send
        :param try_count: a number of retries
        :param params: a dict that will be converted to query string for GET requests or additional metadata for POST requests
        :param stream: if True, the response will be streamed
        :param _timeout: a timeout for the request in seconds if not default
        :return: final response
        :raises: B2Error if the request fails
        """
        method = method.upper()
        request_headers = {**headers, 'User-Agent': self.user_agent}

        def do_request():
            # This may retry, so each time we need to rewind the data back to the beginning.
            if data is not None and not isinstance(data, bytes):
                data.seek(0)
            self._run_pre_request_hooks(method, url, request_headers)
            response = self.session.request(
                method,
                url,
                headers=request_headers,
                data=data,
                params=params if method == 'GET' else None,
                timeout=(self.CONNECTION_TIMEOUT, _timeout or self.TIMEOUT_FOR_UPLOAD),
                stream=stream,
            )
            self._run_post_request_hooks(method, url, request_headers, response)
            return response

        return self._translate_and_retry(do_request, try_count, params)

    def request_content_return_json(
        self,
        method: Literal['POST', 'GET', 'HEAD'],
        url: str,
        headers: dict[str, str],
        data: io.BytesIO | bytes | None = None,
        try_count: int = TRY_COUNT_DATA,
        params: dict[str, str] | None = None,
        *,
        _timeout: int | None = None,
    ) -> JSON:
        """
        Use like this:

        .. code-block:: python

           try:
               response_dict = b2_http.request_content_return_json('POST', url, headers, data)
               ...
           except B2Error as e:
               ...

        :param method: uppercase HTTP method name
        :param url: a URL to call
        :param headers: headers to send.
        :param data: raw bytes or a file-like object to send
        :return: decoded JSON
        """
        response = self.request(
            method,
            url,
            headers={**headers, 'Accept': 'application/json'},
            data=data,
            try_count=try_count,
            params=params,
            _timeout=_timeout,
        )

        # Decode the JSON that came back.  If we've gotten this far,
        # we know we have a status of 200 OK.  In this case, the body
        # of the response is always JSON, so we don't need to handle
        # it being something else.
        try:
            return json.loads(response.content.decode('utf-8'))
        finally:
            response.close()

    def post_content_return_json(
        self,
        url: str,
        headers: dict[str, str],
        data: bytes | io.IOBase,
        try_count: int = TRY_COUNT_DATA,
        post_params: dict[str, str] | None = None,
        _timeout: int | None = None,
    ) -> JSON:
        """
        Use like this:

        .. code-block:: python

           try:
               response_dict = b2_http.post_content_return_json(url, headers, data)
               ...
           except B2Error as e:
               ...

        :param str url: a URL to call
        :param dict headers: headers to send.
        :param data: a file-like object to send
        :return: a dict that is the decoded JSON
        """
        try:
            return self.request_content_return_json(
                'POST', url, headers, data, try_count, post_params, _timeout=_timeout
            )
        except B2RequestTimeout:
            # this forces a token refresh, which is necessary if request is still alive
            # on the server but has terminated for some reason on the client. See #79
            raise B2RequestTimeoutDuringUpload()

    def post_json_return_json(self, url, headers, params, try_count: int = TRY_COUNT_OTHER):
        """
        Use like this:

        .. code-block:: python

           try:
               response_dict = b2_http.post_json_return_json(url, headers, params)
               ...
           except B2Error as e:
               ...

        :param str url: a URL to call
        :param dict headers: headers to send.
        :param dict params: a dict that will be converted to JSON
        :return: the decoded JSON document
        :rtype: dict
        """

        # This is not just b2_copy_file or b2_copy_part, but it would not
        # be good to find out by analyzing the url.
        # In the future a more generic system between raw_api and b2http
        # to indicate the timeouts should be designed.
        timeout = self.TIMEOUT_FOR_COPY

        data = json.dumps(params).encode()
        return self.post_content_return_json(
            url,
            {
                **headers,
                'Content-Type': 'application/json',
            },
            data,
            try_count,
            params,
            _timeout=timeout,
        )

    def get_content(self, url, headers, try_count: int = TRY_COUNT_DOWNLOAD):
        """
        Fetches content from a URL.

        Use like this:

        .. code-block:: python

           try:
               with b2_http.get_content(url, headers) as response:
                   for byte_data in response.iter_content(chunk_size=1024):
                       ...
           except B2Error as e:
               ...

        The response object is only guarantee to have:
            - headers
            - iter_content()

        :param str url: a URL to call
        :param dict headers: headers to send
        :param int try_count: a number or retries
        :return: Context manager that returns an object that supports iter_content()
        """
        response = self.request(
            'GET', url, headers=headers, try_count=try_count, stream=True, _timeout=self.TIMEOUT
        )
        return ResponseContextManager(response)

    def head_content(
        self,
        url: str,
        headers: dict[str, Any],
        try_count: int = TRY_COUNT_HEAD,
    ) -> requests.Response:
        """
        Does a HEAD instead of a GET for the URL.
        The response's content is limited to the headers.

        Use like this:

        .. code-block:: python

           try:
               response_dict = b2_http.head_content(url, headers)
               ...
           except B2Error as e:
               ...

        The response object is only guaranteed to have:
            - headers

        :param str url: a URL to call
        :param dict headers: headers to send
        :param int try_count: a number or retries
        :return: HTTP response
        """
        return self.request('HEAD', url, headers=headers, try_count=try_count)

    @classmethod
    def _get_user_agent(cls, user_agent_append):
        if user_agent_append:
            return f'{USER_AGENT} {user_agent_append}'
        return USER_AGENT

    def _run_pre_request_hooks(self, method, url, headers):
        for callback in self.callbacks:
            callback.pre_request(method, url, headers)

    def _run_post_request_hooks(self, method, url, headers, response):
        for callback in self.callbacks:
            callback.post_request(method, url, headers, response)

    @classmethod
    def _translate_errors(cls, fcn, post_params=None):
        """
        Call the given function, turning any exception raised into the right
        kind of B2Error.

        :param dict post_params: request parameters
        """
        response = None
        try:
            response = fcn()
            if response.status_code not in (200, 206):
                # Decode the error object returned by the service
                try:
                    error = json.loads(response.content.decode('utf-8')) if response.content else {}
                    if not isinstance(error, dict):
                        raise ValueError('json error value is not a dict')
                except (json.JSONDecodeError, UnicodeDecodeError, ValueError):
                    logger.error('failed to decode error response: %r', response.content)
                    # When the user points to an S3 endpoint, he won't receive the JSON error
                    # he expects. In that case, we can provide at least a hint of "what happened".
                    # s3 url has the form of e.g. https://s3.us-west-000.backblazeb2.com
                    if '://s3.' in response.url:
                        raise PotentialS3EndpointPassedAsRealm(response.content)
                    error = {
                        'message': response.content.decode('utf-8', errors='replace'),
                        'code': 'non_json_response',
                    }
                extra_error_keys = error.keys() - ('code', 'status', 'message')
                if extra_error_keys:
                    logger.debug(
                        'received error has extra (unsupported) keys: %s', extra_error_keys
                    )

                try:
                    status = int(error.get('status', response.status_code))
                    if status != response.status_code:
                        raise ValueError('status code is not equal to the one in the response')
                except (TypeError, ValueError) as exc:
                    logger.warning(
                        'Inconsistent status codes returned by the server %r != %r; parsing exception: %r',
                        error.get('status'),
                        response.status_code,
                        exc,
                    )
                    status = response.status_code

                raise interpret_b2_error(
                    status,
                    str(error['code']) if 'code' in error else None,
                    str(error['message']) if 'message' in error else None,
                    response.headers,
                    post_params,
                )
            return response

        except B2Error:
            raise  # pass through exceptions from just above

        except requests.ConnectionError as e0:
            e1 = e0.args[0]
            if isinstance(e1, requests.packages.urllib3.exceptions.MaxRetryError):
                msg = e1.args[0]
                if 'nodename nor servname provided, or not known' in msg:
                    # Unknown host, or DNS failing.  In the context of calling
                    # B2, this means that something is down between here and
                    # Backblaze, so we treat it like 503 Service Unavailable.
                    raise UnknownHost()
            elif isinstance(e1, requests.packages.urllib3.exceptions.ProtocolError):
                e2 = e1.args[1]

                if isinstance(e2, TimeoutError):
                    raise B2RequestTimeout(str(e0))

                if isinstance(e2, socket.error):
                    if len(e2.args) >= 2 and e2.args[1] == 'Broken pipe':
                        # Broken pipes are usually caused by the service rejecting
                        # an upload request for cause, so we use a 400 Bad Request
                        # code.
                        raise BrokenPipe()
            raise B2ConnectionError(str(e0))

        except requests.Timeout as e:
            raise B2RequestTimeout(str(e))

        except Exception as e:
            text = repr(e)

            # This is a special case to handle when urllib3 doesn't translate
            # ECONNRESET into something that requests can turn into something
            # we understand.  The SysCallError is from the optional library
            # pyOpenSsl, which we don't require, so we can't import it and
            # catch it explicitly.
            #
            # The text from one such error looks like this: SysCallError(104, 'ECONNRESET')
            if text.startswith('SysCallError'):
                if 'ECONNRESET' in text:
                    raise ConnectionReset()

            logger.exception('_translate_errors has intercepted an unexpected exception')
            raise UnknownError(text)

    @classmethod
    def _translate_and_retry(
        cls, fcn: Callable, try_count: int, post_params: dict[str, Any] | None = None
    ):
        """
        Try calling fcn try_count times, retrying only if
        the exception is a retryable B2Error.

        :param fcn: request function to call
        :param try_count: a number of retries
        :param post_params: request parameters
        """
        # For all but the last try, catch the exception.
        wait_time = 1.0
        max_wait_time = 64
        for _ in range(try_count - 1):
            try:
                return cls._translate_errors(fcn, post_params)
            except B2Error as e:
                if not e.should_retry_http():
                    raise
                logger.debug(str(e), exc_info=True)
                if e.retry_after_seconds is not None:
                    sleep_duration = e.retry_after_seconds
                    sleep_reason = 'server asked us to'
                else:
                    sleep_duration = wait_time
                    sleep_reason = 'that is what the default exponential backoff is'

                logger.info(
                    'Pausing thread for %i seconds because %s',
                    sleep_duration,
                    sleep_reason,
                )
                time.sleep(sleep_duration)

                # Set up wait time for the next iteration
                wait_time *= 1.5
                if wait_time > max_wait_time:
                    # avoid clients synchronizing and causing a wave
                    # of requests when connectivity is restored
                    wait_time = max_wait_time + random()

        # If the last try gets an exception, it will be raised.
        return cls._translate_errors(fcn, post_params)


class NotDecompressingHTTPAdapter(HTTPAdapter):
    """
    HTTP adapter that uses :class:`b2sdk._internal.requests.NotDecompressingResponse` instead of the default
    :code:`requests.Response` class.
    """

    def build_response(self, req, resp):
        return NotDecompressingResponse.from_builtin_response(super().build_response(req, resp))