Ejemplo n.º 1
0
def account_listing_content_type(req):
    """
    Figure out the content type of an account-listing response.

    Returns a 2-tuple: (content_type, error). Only one of them will be set;
    the other will be None.
    """
    query_format = get_param(req, 'format')
    if query_format:
        req.accept = FORMAT2CONTENT_TYPE.get(query_format.lower(),
                                             FORMAT2CONTENT_TYPE['plain'])
    content_type = req.accept.best_match(
        ['text/plain', 'application/json', 'application/xml', 'text/xml'])
    if not content_type:
        return (None, HTTPNotAcceptable(request=req))
    else:
        return (content_type, None)
Ejemplo n.º 2
0
def get_listing_content_type(req):
    """
    Determine the content type to use for an account or container listing
    response.

    :param req: request object
    :returns: content type as a string (e.g. text/plain, application/json)
    :raises: HTTPNotAcceptable if the requested content type is not acceptable
    :raises: HTTPBadRequest if the 'format' query param is provided and
             not valid UTF-8
    """
    query_format = get_param(req, "format")
    if query_format:
        req.accept = FORMAT2CONTENT_TYPE.get(query_format.lower(), FORMAT2CONTENT_TYPE["plain"])
    out_content_type = req.accept.best_match(["text/plain", "application/json", "application/xml", "text/xml"])
    if not out_content_type:
        raise HTTPNotAcceptable(request=req)
    return out_content_type
Ejemplo n.º 3
0
def get_listing_content_type(req):
    """
    Determine the content type to use for an account or container listing
    response.

    :param req: request object
    :returns: content type as a string (e.g. text/plain, application/json)
    :raises: HTTPNotAcceptable if the requested content type is not acceptable
    :raises: HTTPBadRequest if the 'format' query param is provided and
             not valid UTF-8
    """
    query_format = get_param(req, 'format')
    if query_format:
        req.accept = FORMAT2CONTENT_TYPE.get(
            query_format.lower(), FORMAT2CONTENT_TYPE['plain'])
    out_content_type = req.accept.best_match(
        ['text/plain', 'application/json', 'application/xml', 'text/xml'])
    if not out_content_type:
        raise HTTPNotAcceptable(request=req)
    return out_content_type
Ejemplo n.º 4
0
     if given_limit and given_limit.isdigit():
         limit = int(given_limit)
         if limit > ACCOUNT_LISTING_LIMIT:
             self.logger.increment('GET.errors')
             return HTTPPreconditionFailed(request=req,
                                           body='Maximum limit is %d' %
                                           ACCOUNT_LISTING_LIMIT)
     marker = get_param(req, 'marker', '')
     end_marker = get_param(req, 'end_marker')
     query_format = get_param(req, 'format')
 except UnicodeDecodeError, err:
     self.logger.increment('GET.errors')
     return HTTPBadRequest(body='parameters not utf8',
                           content_type='text/plain', request=req)
 if query_format:
     req.accept = FORMAT2CONTENT_TYPE.get(query_format.lower(),
                                          FORMAT2CONTENT_TYPE['plain'])
 try:
     out_content_type = req.accept.best_match(
         ['text/plain', 'application/json', 'application/xml',
          'text/xml'],
         default_match='text/plain')
 except AssertionError, err:
     self.logger.increment('GET.errors')
     return HTTPBadRequest(body='bad accept header: %s' % req.accept,
                           content_type='text/plain', request=req)
 account_list = broker.list_containers_iter(limit, marker, end_marker,
                                            prefix, delimiter)
 if out_content_type == 'application/json':
     data = []
     for (name, object_count, bytes_used, is_subdir) in account_list:
         if is_subdir:
Ejemplo n.º 5
0
        if broker.is_deleted():
            return HTTPNotFound(request=req)
        info = broker.get_info()
        headers = {
            "X-Container-Object-Count": info["object_count"],
            "X-Container-Bytes-Used": info["bytes_used"],
            "X-Timestamp": info["created_at"],
            "X-PUT-Timestamp": info["put_timestamp"],
        }
        headers.update(
            (key, value)
            for key, (value, timestamp) in broker.metadata.iteritems()
            if value != "" and (key.lower() in self.save_headers or key.lower().startswith("x-container-meta-"))
        )
        if get_param(req, "format"):
            req.accept = FORMAT2CONTENT_TYPE.get(get_param(req, "format").lower(), FORMAT2CONTENT_TYPE["plain"])
        try:
            headers["Content-Type"] = req.accept.best_match(
                ["text/plain", "application/json", "application/xml", "text/xml"], default_match="text/plain"
            )
        except AssertionError, err:
            return HTTPBadRequest(body="bad accept header: %s" % req.accept, content_type="text/plain", request=req)
        return HTTPNoContent(request=req, headers=headers, charset="utf-8")

    @public
    @timing_stats
    def GET(self, req):
        """Handle HTTP GET request."""
        try:
            drive, part, account, container, obj = split_path(unquote(req.path), 4, 5, True)
            validate_device_partition(drive, part)
Ejemplo n.º 6
0
Archivo: server.py Proyecto: UshF/swift
        if broker.is_deleted():
            return HTTPNotFound(request=req)
        info = broker.get_info()
        headers = {
            'X-Container-Object-Count': info['object_count'],
            'X-Container-Bytes-Used': info['bytes_used'],
            'X-Timestamp': info['created_at'],
            'X-PUT-Timestamp': info['put_timestamp'],
        }
        headers.update(
            (key, value)
            for key, (value, timestamp) in broker.metadata.iteritems()
            if value != '' and (key.lower() in self.save_headers or
                                key.lower().startswith('x-container-meta-')))
        if get_param(req, 'format'):
            req.accept = FORMAT2CONTENT_TYPE.get(
                get_param(req, 'format').lower(), FORMAT2CONTENT_TYPE['plain'])
        headers['Content-Type'] = req.accept.best_match(
            ['text/plain', 'application/json', 'application/xml', 'text/xml'])
        if not headers['Content-Type']:
            return HTTPNotAcceptable(request=req)
        return HTTPNoContent(request=req, headers=headers, charset='utf-8')

    @public
    @timing_stats
    def GET(self, req):
        """Handle HTTP GET request."""
        try:
            drive, part, account, container, obj = split_path(
                unquote(req.path), 4, 5, True)
            validate_device_partition(drive, part)
        except ValueError, err:
Ejemplo n.º 7
0
        if broker.is_deleted():
            return HTTPNotFound(request=req)
        info = broker.get_info()
        headers = {
            'X-Container-Object-Count': info['object_count'],
            'X-Container-Bytes-Used': info['bytes_used'],
            'X-Timestamp': info['created_at'],
            'X-PUT-Timestamp': info['put_timestamp'],
        }
        headers.update(
            (key, value)
            for key, (value, timestamp) in broker.metadata.iteritems()
            if value != '' and (key.lower() in self.save_headers or
                                key.lower().startswith('x-container-meta-')))
        if get_param(req, 'format'):
            req.accept = FORMAT2CONTENT_TYPE.get(
                get_param(req, 'format').lower(), FORMAT2CONTENT_TYPE['plain'])
        try:
            headers['Content-Type'] = req.accept.best_match(
                [
                    'text/plain', 'application/json', 'application/xml',
                    'text/xml'
                ],
                default_match='text/plain')
        except AssertionError, err:
            return HTTPBadRequest(body='bad accept header: %s' % req.accept,
                                  content_type='text/plain',
                                  request=req)
        return HTTPNoContent(request=req, headers=headers, charset='utf-8')

    @public
    @timing_stats