Beispiel #1
0
def _send_http_request(conn, http_method, uri, headers, body, send_buf_size):
    conn.putrequest(http_method,
                    uri,
                    skip_host=True,
                    skip_accept_encoding=True)

    for k, v in headers.items():
        k = utils.convert_to_standard_string(k)
        v = utils.convert_to_standard_string(v)
        conn.putheader(k, v)
    conn.endheaders()

    if body:
        if isinstance(body, str):
            conn.send(body)
        else:
            total = int(headers[http_headers.CONTENT_LENGTH])
            sent = 0
            while sent < total:
                size = total - sent
                if size > send_buf_size:
                    size = send_buf_size
                buf = body[:size]
                if not buf:
                    raise BceClientError(
                        'Insufficient data, only %d bytes available while %s is %d'
                        % (sent, http_headers.CONTENT_LENGTH, total))
                conn.send(buf)
                sent += len(buf)

    return conn.getresponse()
Beispiel #2
0
def _send_http_request(conn, http_method, uri, headers, body, send_buf_size):
    # putrequest() need that http_method and uri is Ascii on Py2 and unicode \
    # on Py3
    http_method = compat.convert_to_string(http_method)
    uri = compat.convert_to_string(uri)
    conn.putrequest(http_method,
                    uri,
                    skip_host=True,
                    skip_accept_encoding=True)

    for k, v in iteritems(headers):
        k = utils.convert_to_standard_string(k)
        v = utils.convert_to_standard_string(v)
        conn.putheader(k, v)
    conn.endheaders()

    if body:
        if isinstance(body, (bytes, str)):
            conn.send(body)
        else:
            total = int(headers[http_headers.CONTENT_LENGTH])
            sent = 0
            while sent < total:
                size = total - sent
                if size > send_buf_size:
                    size = send_buf_size
                buf = body.read(size)
                if not buf:
                    raise BceClientError(
                        'Insufficient data, only %d bytes available while %s is %d'
                        % (sent, http_headers.CONTENT_LENGTH, total))
                conn.send(buf)
                sent += len(buf)

    return conn.getresponse()
def _send_http_request(conn, http_method, uri, headers, body, send_buf_size):
    conn.putrequest(http_method, uri, skip_host=True, skip_accept_encoding=True)

    for k, v in headers.items():
        k = utils.convert_to_standard_string(k)
        v = utils.convert_to_standard_string(v)
        conn.putheader(k, v)
    conn.endheaders()

    if body:
        if isinstance(body, (str, unicode)):
            conn.send(body)
        else:
            total = int(headers[http_headers.CONTENT_LENGTH])
            sent = 0
            while sent < total:
                size = total - sent
                if size > send_buf_size:
                    size = send_buf_size
                buf = body.read(size)
                if not buf:
                    raise BceClientError(
                        'Insufficient data, only %d bytes available while %s is %d' % (
                            sent, http_headers.CONTENT_LENGTH, total))
                conn.send(buf)
                sent += len(buf)

    return conn.getresponse()
    def _prepare_object_headers(content_length=None,
                                content_md5=None,
                                content_type=None,
                                content_sha256=None,
                                etag=None,
                                user_metadata=None,
                                storage_class=None):
        headers = {}

        if content_length is not None:
            if content_length and content_length < 0:
                raise ValueError('content_length should not be negative.')
            headers[http_headers.CONTENT_LENGTH] = str(content_length)

        if content_md5 is not None:
            headers[
                http_headers.CONTENT_MD5] = utils.convert_to_standard_string(
                    content_md5)

        if content_type is not None:
            headers[
                http_headers.CONTENT_TYPE] = utils.convert_to_standard_string(
                    content_type)
        else:
            headers[
                http_headers.CONTENT_TYPE] = http_content_types.OCTET_STREAM

        if content_sha256 is not None:
            headers[http_headers.BCE_CONTENT_SHA256] = content_sha256

        if etag is not None:
            headers[http_headers.
                    ETAG] = '"%s"' % utils.convert_to_standard_string(etag)

        if user_metadata is not None:
            meta_size = 0
            if not isinstance(user_metadata, dict):
                raise TypeError('user_metadata should be of type dict.')
            for k, v in user_metadata.items():
                k = utils.convert_to_standard_string(k)
                v = utils.convert_to_standard_string(v)
                normalized_key = http_headers.BCE_USER_METADATA_PREFIX + k
                headers[normalized_key] = v
                meta_size += len(normalized_key)
                meta_size += len(v)
            if meta_size > bos.MAX_USER_METADATA_SIZE:
                raise ValueError(
                    'Metadata size should not be greater than %d.' %
                    bos.MAX_USER_METADATA_SIZE)

        if storage_class is not None:
            headers[http_headers.BOS_STORAGE_CLASS] = storage_class

        return headers
Beispiel #5
0
    def _prepare_object_headers(
            content_length=None,
            content_md5=None,
            content_type=None,
            content_sha256=None,
            etag=None,
            user_metadata=None):
        headers = {}

        if content_length is not None:
            if content_length and content_length < 0:
                raise ValueError('content_length should not be negative.')
            headers[http_headers.CONTENT_LENGTH] = str(content_length)

        if content_md5 is not None:
            headers[http_headers.CONTENT_MD5] = utils.convert_to_standard_string(content_md5)

        if content_type is not None:
            headers[http_headers.CONTENT_TYPE] = utils.convert_to_standard_string(content_type)
        else:
            headers[http_headers.CONTENT_TYPE] = http_content_types.OCTET_STREAM

        if content_sha256 is not None:
            headers[http_headers.BCE_CONTENT_SHA256] = content_sha256

        if etag is not None:
            headers[http_headers.ETAG] = '"%s"' % utils.convert_to_standard_string(etag)

        if user_metadata is not None:
            meta_size = 0
            if not isinstance(user_metadata, dict):
                raise TypeError('user_metadata should be of type dict.')
            for k, v in user_metadata.items():
                k = utils.convert_to_standard_string(k)
                v = utils.convert_to_standard_string(v)
                normalized_key = http_headers.BCE_USER_METADATA_PREFIX + k
                headers[normalized_key] = v
                meta_size += len(normalized_key)
                meta_size += len(v)
            if meta_size > bos.MAX_USER_METADATA_SIZE:
                raise ValueError(
                    'Metadata size should not be greater than %d.' % bos.MAX_USER_METADATA_SIZE)

        return headers
Beispiel #6
0
    def get_tags(self, metric):
        """
        get tags

        :type metric: string
        :param metric:

        :return: {tagk1:[tagk11,tagk21,..],tagk2:[tagk21,tagk22,..]..}
        :rtype: baidubce.bce_response.BceResponse
        """
        metric = utils.convert_to_standard_string(metric)
        path = b'/v1/metric/' + metric + b'/tag'
        return self._send_request(http_methods.GET,
                                  path=path,
                                  body_parser=tsdb_handler.parse_json)
Beispiel #7
0
    def get_fields(self, metric):
        """
        get fields

        :type metric: string
        :param metric:

        :return: field dict. {field1:{type: 'Number'},field2:{type: 'String'}}
        :rtype: baidubce.bce_response.BceResponse
        """
        metric = utils.convert_to_standard_string(metric)
        path = b'/v1/metric/' + metric + b'/field'
        return self._send_request(http_methods.GET,
                                  path=path,
                                  body_parser=tsdb_handler.parse_json)
Beispiel #8
0
def _get_canonical_headers(headers, headers_to_sign=None):
    headers = headers or {}

    if headers_to_sign is None or len(headers_to_sign) == 0:
        headers_to_sign = set(
            [b"host", b"content-md5", b"content-length", b"content-type"])
    result = []
    for k in headers:
        k_lower = k.strip().lower()
        value = utils.convert_to_standard_string(headers[k]).strip()
        if k_lower.startswith(http_headers.BCE_PREFIX) \
                or k_lower in headers_to_sign:
            str_tmp = b"%s:%s" % (utils.normalize_string(k_lower),
                                  utils.normalize_string(value))
            result.append(str_tmp)
    result.sort()
    return (b'\n').join(result)
Beispiel #9
0
 def test_convert_to_standard_string(self):
     """test_convert_utf8"""
     self.assertEqual(b"aaa", utils.convert_to_standard_string(u"aaa"))
     self.assertEqual("北京".encode("utf-8"),
                      utils.convert_to_standard_string("北京"))