

def check_metadata(req, target_type):
    '\n    Check metadata sent in the request headers.  This should only check\n    that the metadata in the request given is valid.  Checks against\n    account/container overall metadata should be forwarded on to its\n    respective server to be checked.\n\n    :param req: request object\n    :param target_type: str: one of: object, container, or account: indicates\n                        which type the target storage for the metadata is\n    :returns: HTTPBadRequest with bad metadata otherwise None\n    '
    target_type = target_type.lower()
    prefix = ('x-%s-meta-' % target_type)
    meta_count = 0
    meta_size = 0
    for (key, value) in req.headers.items():
        if (isinstance(value, six.string_types) and (len(value) > MAX_HEADER_SIZE)):
            return HTTPBadRequest(body=('Header value too long: %s' % key[:MAX_META_NAME_LENGTH]), request=req, content_type='text/plain')
        if (not key.lower().startswith(prefix)):
            continue
        key = key[len(prefix):]
        if (not key):
            return HTTPBadRequest(body='Metadata name cannot be empty', request=req, content_type='text/plain')
        bad_key = (not check_utf8(key))
        bad_value = (value and (not check_utf8(value)))
        if ((target_type in ('account', 'container')) and (bad_key or bad_value)):
            return HTTPBadRequest(body='Metadata must be valid UTF-8', request=req, content_type='text/plain')
        meta_count += 1
        meta_size += (len(key) + len(value))
        if (len(key) > MAX_META_NAME_LENGTH):
            return HTTPBadRequest(body=('Metadata name too long: %s%s' % (prefix, key)), request=req, content_type='text/plain')
        elif (len(value) > MAX_META_VALUE_LENGTH):
            return HTTPBadRequest(body=('Metadata value longer than %d: %s%s' % (MAX_META_VALUE_LENGTH, prefix, key)), request=req, content_type='text/plain')
        elif (meta_count > MAX_META_COUNT):
            return HTTPBadRequest(body=('Too many metadata items; max %d' % MAX_META_COUNT), request=req, content_type='text/plain')
        elif (meta_size > MAX_META_OVERALL_SIZE):
            return HTTPBadRequest(body=('Total metadata too large; max %d' % MAX_META_OVERALL_SIZE), request=req, content_type='text/plain')
    return None

dataset/ETHPy150Open openstack/swift/swift/common/constraints.py check_metadata/original